Initial commit
[yaffs-website] / node_modules / node-sass / src / libsass / src / position.hpp
1 #ifndef SASS_POSITION_H
2 #define SASS_POSITION_H
3
4 #include <string>
5 #include <cstring>
6 // #include <iostream>
7
8 namespace Sass {
9
10
11   class Offset {
12
13     public: // c-tor
14       Offset(const char* string);
15       Offset(const std::string& text);
16       Offset(const size_t line, const size_t column);
17
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;
21
22       // init/create instance from const char substring
23       static Offset init(const char* beg, const char* end);
24
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;
31
32     public: // overload output stream operator
33       // friend std::ostream& operator<<(std::ostream& strm, const Offset& off);
34
35     public:
36       Offset off() { return *this; }
37
38     public:
39       size_t line;
40       size_t column;
41
42   };
43
44   class Position : public Offset {
45
46     public: // c-tor
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);
51
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;
61
62     public: // overload output stream operator
63       // friend std::ostream& operator<<(std::ostream& strm, const Position& pos);
64
65     public:
66       size_t file;
67
68   };
69
70   // Token type for representing lexed chunks of text
71   class Token {
72   public:
73     const char* prefix;
74     const char* begin;
75     const char* end;
76
77     Token()
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) { }
85
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);
93     }
94
95     operator bool()        { return begin && end && begin >= end; }
96     operator std::string() { return to_string(); }
97
98     bool operator==(Token t)  { return to_string() == t.to_string(); }
99   };
100
101   class ParserState : public Position {
102
103     public: // c-tor
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));
107
108     public: // down casts
109       Offset off() { return *this; }
110       Position pos() { return *this; }
111       ParserState pstate() { return *this; }
112
113     public:
114       const char* path;
115       const char* src;
116       Offset offset;
117       Token token;
118
119   };
120
121 }
122
123 #endif