Initial commit
[yaffs-website] / node_modules / node-sass / src / libsass / src / position.cpp
1 #include "sass.hpp"
2 #include "position.hpp"
3
4 namespace Sass {
5
6
7   Offset::Offset(const char* string)
8   : line(0), column(0)
9   {
10     *this = inc(string, string + strlen(string));
11   }
12
13   Offset::Offset(const std::string& text)
14   : line(0), column(0)
15   {
16     *this = inc(text.c_str(), text.c_str() + text.size());
17   }
18
19   Offset::Offset(const size_t line, const size_t column)
20   : line(line), column(column) { }
21
22   // init/create instance from const char substring
23   Offset Offset::init(const char* beg, const char* end)
24   {
25     Offset offset(0, 0);
26     if (end == 0) {
27       end += strlen(beg);
28     }
29     offset.add(beg, end);
30     return offset;
31   }
32
33   // increase offset by given string (mostly called by lexer)
34   // increase line counter and count columns on the last line
35   // ToDo: make the col count utf8 aware
36   Offset Offset::add(const char* begin, const char* end)
37   {
38     if (end == 0) return *this;
39     while (begin < end && *begin) {
40       if (*begin == '\n') {
41         ++ line;
42         // start new line
43         column = 0;
44       } else {
45         ++ column;
46       }
47       ++begin;
48     }
49     return *this;
50   }
51
52   // increase offset by given string (mostly called by lexer)
53   // increase line counter and count columns on the last line
54   Offset Offset::inc(const char* begin, const char* end) const
55   {
56     Offset offset(line, column);
57     offset.add(begin, end);
58     return offset;
59   }
60
61   bool Offset::operator== (const Offset &pos) const
62   {
63     return line == pos.line && column == pos.column;
64   }
65
66   bool Offset::operator!= (const Offset &pos) const
67   {
68     return line != pos.line || column != pos.column;
69   }
70
71   void Offset::operator+= (const Offset &off)
72   {
73     *this = Offset(line + off.line, off.line > 0 ? off.column : column + off.column);
74   }
75
76   Offset Offset::operator+ (const Offset &off) const
77   {
78     return Offset(line + off.line, off.line > 0 ? off.column : column + off.column);
79   }
80
81   Offset Offset::operator- (const Offset &off) const
82   {
83     return Offset(line - off.line, off.line == line ? column - off.column : column);
84   }
85
86   Position::Position(const size_t file)
87   : Offset(0, 0), file(file) { }
88
89   Position::Position(const size_t file, const Offset& offset)
90   : Offset(offset), file(file) { }
91
92   Position::Position(const size_t line, const size_t column)
93   : Offset(line, column), file(-1) { }
94
95   Position::Position(const size_t file, const size_t line, const size_t column)
96   : Offset(line, column), file(file) { }
97
98
99   ParserState::ParserState(const char* path, const char* src, const size_t file)
100   : Position(file, 0, 0), path(path), src(src), offset(0, 0), token() { }
101
102   ParserState::ParserState(const char* path, const char* src, const Position& position, Offset offset)
103   : Position(position), path(path), src(src), offset(offset), token() { }
104
105   ParserState::ParserState(const char* path, const char* src, const Token& token, const Position& position, Offset offset)
106   : Position(position), path(path), src(src), offset(offset), token(token) { }
107
108   Position Position::add(const char* begin, const char* end)
109   {
110     Offset::add(begin, end);
111     return *this;
112   }
113
114   Position Position::inc(const char* begin, const char* end) const
115   {
116     Offset offset(line, column);
117     offset = offset.inc(begin, end);
118     return Position(file, offset);
119   }
120
121   bool Position::operator== (const Position &pos) const
122   {
123     return file == pos.file && line == pos.line && column == pos.column;
124   }
125
126   bool Position::operator!= (const Position &pos) const
127   {
128     return file == pos.file || line != pos.line || column != pos.column;
129   }
130
131   void Position::operator+= (const Offset &off)
132   {
133     *this = Position(file, line + off.line, off.line > 0 ? off.column : column + off.column);
134   }
135
136   const Position Position::operator+ (const Offset &off) const
137   {
138     return Position(file, line + off.line, off.line > 0 ? off.column : column + off.column);
139   }
140
141   const Offset Position::operator- (const Offset &off) const
142   {
143     return Offset(line - off.line, off.line == line ? column - off.column : column);
144   }
145
146   /* not used anymore - remove?
147   std::ostream& operator<<(std::ostream& strm, const Offset& off)
148   {
149     if (off.line == string::npos) strm << "-1:"; else strm << off.line << ":";
150     if (off.column == string::npos) strm << "-1"; else strm << off.column;
151     return strm;
152   } */
153
154   /* not used anymore - remove?
155   std::ostream& operator<<(std::ostream& strm, const Position& pos)
156   {
157     if (pos.file != string::npos) strm << pos.file << ":";
158     if (pos.line == string::npos) strm << "-1:"; else strm << pos.line << ":";
159     if (pos.column == string::npos) strm << "-1"; else strm << pos.column;
160     return strm;
161   } */
162
163 }