SimpleLexer.cpp
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License version 2 as
6* published by the Free Software Foundation.
7*
8* ArmarX is distributed in the hope that it will be useful, but
9* WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*
13* You should have received a copy of the GNU General Public License
14* along with this program. If not, see <http://www.gnu.org/licenses/>.
15*
16* @author Simon Ottenhaus (simon dot ottenhaus at kit dot edu)
17* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
18* GNU General Public License
19*/
20
21#include "SimpleLexer.h"
22
23namespace armarx
24{
26 {
27 this->pos = 0;
28 this->lastPos = 0;
29 this->src = src;
30 this->tokenFin = tokenFin;
31 this->tokenError = tokenError;
32 }
33
34 void
35 SimpleLexer::addRule(int id, const std::string& name, const std::string& regex)
36 {
37 rules.push_back(Rule(id, name, boost::regex("\\A(" + regex + ")")));
38 }
39
40 int
42 {
43 if (pos >= (int)src.size())
44 {
47 lastPos = pos;
48 return currentTokenId;
49 }
50
51 for (const Rule& rule : rules)
52 {
53 boost::cmatch result;
54
55 if (boost::regex_search(
56 src.c_str() + pos, result, rule.regex, boost::regex_constants::match_default))
57 {
58 currentTokenValue = std::string(result[0].first, result[0].second);
59 currentTokenId = rule.id;
60 lastPos = pos;
61 pos += currentTokenValue.size();
62 return currentTokenId;
63 }
64 }
65
68 lastPos = pos;
69 return currentTokenId;
70 }
71
72 void
74 {
75 this->pos = 0;
76 this->lastPos = 0;
79 }
80
81 std::string
83 {
84 if (id == tokenError)
85 {
86 return "Error";
87 }
88
89 if (id == tokenFin)
90 {
91 return "EndOfString";
92 }
93
94 for (Rule rule : rules)
95 {
96 if (rule.id == id)
97 {
98 return rule.name;
99 }
100 }
101
102 return "";
103 }
104
105 void
106 SimpleLexer::positionToLineAndColumn(int pos, int& line, int& column)
107 {
108 if (pos < 0 || pos > (int)src.length())
109 {
110 line = 0;
111 column = 0;
112 return;
113 }
114
115 line = 1; // 1 based counting
116 column = 0;
117 int lineStart = 0;
118
119 for (int i = 0; i < (int)src.length(); i++)
120 {
121 if (i == pos)
122 {
123 column = i - lineStart + 1; // 1 based counting
124 return;
125 }
126
127 if (src[i] == '\n')
128 {
129 line++;
130 lineStart = i + 1; // line starts after \n
131 }
132 }
133
134 column = pos - lineStart; // pos == src.length(), no +1 needed here
135 }
136
139 {
140 int line, column;
141 positionToLineAndColumn(pos, line, column);
142 return LexerInfo(pos, line, column);
143 }
144} // namespace armarx
LexerInfo getPositionInfo(int pos)
std::vector< Rule > rules
Definition SimpleLexer.h:67
void addRule(int id, const std::string &name, const std::string &regex)
void positionToLineAndColumn(int pos, int &line, int &column)
SimpleLexer(std::string src, int tokenFin=-1, int tokenError=-2)
std::string tokenIdToName(int id)
std::string currentTokenValue
Definition SimpleLexer.h:64
This file offers overloads of toIce() and fromIce() functions for STL container types.