1 #ifndef SASS_POSITION_H
2 #define SASS_POSITION_H
14 Offset(const char* string);
15 Offset(const std::string& text);
16 Offset(const size_t line, const size_t column);
18 // return new position, incremented by the given string
19 Offset add(const char* begin, const char* end);
20 Offset inc(const char* begin, const char* end) const;
22 // init/create instance from const char substring
23 static Offset init(const char* beg, const char* end);
25 public: // overload operators for position
26 void operator+= (const Offset &pos);
27 bool operator== (const Offset &pos) const;
28 bool operator!= (const Offset &pos) const;
29 Offset operator+ (const Offset &off) const;
30 Offset operator- (const Offset &off) const;
32 public: // overload output stream operator
33 // friend std::ostream& operator<<(std::ostream& strm, const Offset& off);
36 Offset off() { return *this; }
44 class Position : public Offset {
47 Position(const size_t file); // line(0), column(0)
48 Position(const size_t file, const Offset& offset);
49 Position(const size_t line, const size_t column); // file(-1)
50 Position(const size_t file, const size_t line, const size_t column);
52 public: // overload operators for position
53 void operator+= (const Offset &off);
54 bool operator== (const Position &pos) const;
55 bool operator!= (const Position &pos) const;
56 const Position operator+ (const Offset &off) const;
57 const Offset operator- (const Offset &off) const;
58 // return new position, incremented by the given string
59 Position add(const char* begin, const char* end);
60 Position inc(const char* begin, const char* end) const;
62 public: // overload output stream operator
63 // friend std::ostream& operator<<(std::ostream& strm, const Position& pos);
70 // Token type for representing lexed chunks of text
78 : prefix(0), begin(0), end(0) { }
79 Token(const char* b, const char* e)
80 : prefix(b), begin(b), end(e) { }
81 Token(const char* str)
82 : prefix(str), begin(str), end(str + strlen(str)) { }
83 Token(const char* p, const char* b, const char* e)
84 : prefix(p), begin(b), end(e) { }
86 size_t length() const { return end - begin; }
87 std::string ws_before() const { return std::string(prefix, begin); }
88 const std::string to_string() const { return std::string(begin, end); }
89 std::string time_wspace() const {
90 std::string str(to_string());
91 std::string whitespaces(" \t\f\v\n\r");
92 return str.erase(str.find_last_not_of(whitespaces)+1);
95 operator bool() { return begin && end && begin >= end; }
96 operator std::string() { return to_string(); }
98 bool operator==(Token t) { return to_string() == t.to_string(); }
101 class ParserState : public Position {
104 ParserState(const char* path, const char* src = 0, const size_t file = std::string::npos);
105 ParserState(const char* path, const char* src, const Position& position, Offset offset = Offset(0, 0));
106 ParserState(const char* path, const char* src, const Token& token, const Position& position, Offset offset = Offset(0, 0));
108 public: // down casts
109 Offset off() { return *this; }
110 Position pos() { return *this; }
111 ParserState pstate() { return *this; }