2 #include "position.hpp"
7 Offset::Offset(const char* string)
10 *this = inc(string, string + strlen(string));
13 Offset::Offset(const std::string& text)
16 *this = inc(text.c_str(), text.c_str() + text.size());
19 Offset::Offset(const size_t line, const size_t column)
20 : line(line), column(column) { }
22 // init/create instance from const char substring
23 Offset Offset::init(const char* beg, const char* end)
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)
38 if (end == 0) return *this;
39 while (begin < end && *begin) {
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
56 Offset offset(line, column);
57 offset.add(begin, end);
61 bool Offset::operator== (const Offset &pos) const
63 return line == pos.line && column == pos.column;
66 bool Offset::operator!= (const Offset &pos) const
68 return line != pos.line || column != pos.column;
71 void Offset::operator+= (const Offset &off)
73 *this = Offset(line + off.line, off.line > 0 ? off.column : column + off.column);
76 Offset Offset::operator+ (const Offset &off) const
78 return Offset(line + off.line, off.line > 0 ? off.column : column + off.column);
81 Offset Offset::operator- (const Offset &off) const
83 return Offset(line - off.line, off.line == line ? column - off.column : column);
86 Position::Position(const size_t file)
87 : Offset(0, 0), file(file) { }
89 Position::Position(const size_t file, const Offset& offset)
90 : Offset(offset), file(file) { }
92 Position::Position(const size_t line, const size_t column)
93 : Offset(line, column), file(-1) { }
95 Position::Position(const size_t file, const size_t line, const size_t column)
96 : Offset(line, column), file(file) { }
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() { }
102 ParserState::ParserState(const char* path, const char* src, const Position& position, Offset offset)
103 : Position(position), path(path), src(src), offset(offset), token() { }
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) { }
108 Position Position::add(const char* begin, const char* end)
110 Offset::add(begin, end);
114 Position Position::inc(const char* begin, const char* end) const
116 Offset offset(line, column);
117 offset = offset.inc(begin, end);
118 return Position(file, offset);
121 bool Position::operator== (const Position &pos) const
123 return file == pos.file && line == pos.line && column == pos.column;
126 bool Position::operator!= (const Position &pos) const
128 return file == pos.file || line != pos.line || column != pos.column;
131 void Position::operator+= (const Offset &off)
133 *this = Position(file, line + off.line, off.line > 0 ? off.column : column + off.column);
136 const Position Position::operator+ (const Offset &off) const
138 return Position(file, line + off.line, off.line > 0 ? off.column : column + off.column);
141 const Offset Position::operator- (const Offset &off) const
143 return Offset(line - off.line, off.line == line ? column - off.column : column);
146 /* not used anymore - remove?
147 std::ostream& operator<<(std::ostream& strm, const Offset& off)
149 if (off.line == string::npos) strm << "-1:"; else strm << off.line << ":";
150 if (off.column == string::npos) strm << "-1"; else strm << off.column;
154 /* not used anymore - remove?
155 std::ostream& operator<<(std::ostream& strm, const Position& pos)
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;