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 
23 namespace armarx
24 {
25  SimpleLexer::SimpleLexer(std::string src, int tokenFin, int tokenError)
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 SimpleLexer::addRule(int id, const std::string& name, const std::string& regex)
35  {
36  rules.push_back(Rule(id, name, boost::regex("\\A(" + regex + ")")));
37  }
38 
40  {
41  if (pos >= (int)src.size())
42  {
43  currentTokenValue = "";
45  lastPos = pos;
46  return currentTokenId;
47  }
48 
49  for (const Rule& rule : rules)
50  {
51  boost::cmatch result;
52 
53  if (boost::regex_search(src.c_str() + pos, result, rule.regex, boost::regex_constants::match_default))
54  {
55  currentTokenValue = std::string(result[0].first, result[0].second);
56  currentTokenId = rule.id;
57  lastPos = pos;
58  pos += currentTokenValue.size();
59  return currentTokenId;
60  }
61  }
62 
63  currentTokenValue = "";
65  lastPos = pos;
66  return currentTokenId;
67  }
68 
70  {
71  this->pos = 0;
72  this->lastPos = 0;
73  currentTokenValue = "";
75  }
76 
77  std::string SimpleLexer::tokenIdToName(int id)
78  {
79  if (id == tokenError)
80  {
81  return "Error";
82  }
83 
84  if (id == tokenFin)
85  {
86  return "EndOfString";
87  }
88 
89  for (Rule rule : rules)
90  {
91  if (rule.id == id)
92  {
93  return rule.name;
94  }
95  }
96 
97  return "";
98  }
99 
100  void SimpleLexer::positionToLineAndColumn(int pos, int& line, int& column)
101  {
102  if (pos < 0 || pos > (int)src.length())
103  {
104  line = 0;
105  column = 0;
106  return;
107  }
108 
109  line = 1; // 1 based counting
110  column = 0;
111  int lineStart = 0;
112 
113  for (int i = 0; i < (int)src.length(); i++)
114  {
115  if (i == pos)
116  {
117  column = i - lineStart + 1; // 1 based counting
118  return;
119  }
120 
121  if (src[i] == '\n')
122  {
123  line++;
124  lineStart = i + 1; // line starts after \n
125  }
126  }
127 
128  column = pos - lineStart; // pos == src.length(), no +1 needed here
129 
130  }
131 
133  {
134  int line, column;
135  positionToLineAndColumn(pos, line, column);
136  return LexerInfo(pos, line, column);
137  }
138 }
armarx::SimpleLexer::Rule
Definition: SimpleLexer.h:32
armarx::SimpleLexer::rules
std::vector< Rule > rules
Definition: SimpleLexer.h:63
armarx::SimpleLexer::getPositionInfo
LexerInfo getPositionInfo(int pos)
Definition: SimpleLexer.cpp:132
armarx::SimpleLexer::pos
int pos
Definition: SimpleLexer.h:55
armarx::SimpleLexer::currentTokenId
int currentTokenId
Definition: SimpleLexer.h:59
armarx::SimpleLexer::src
std::string src
Definition: SimpleLexer.h:57
armarx::SimpleLexer::tokenError
int tokenError
Definition: SimpleLexer.h:62
armarx::SimpleLexer::reset
void reset()
Definition: SimpleLexer.cpp:69
armarx::SimpleLexer::positionToLineAndColumn
void positionToLineAndColumn(int pos, int &line, int &column)
Definition: SimpleLexer.cpp:100
armarx::SimpleLexer::currentTokenValue
std::string currentTokenValue
Definition: SimpleLexer.h:60
armarx::SimpleLexer::tokenIdToName
std::string tokenIdToName(int id)
Definition: SimpleLexer.cpp:77
armarx::SimpleLexer::SimpleLexer
SimpleLexer(std::string src, int tokenFin=-1, int tokenError=-2)
Definition: SimpleLexer.cpp:25
armarx::SimpleLexer::nextToken
int nextToken()
Definition: SimpleLexer.cpp:39
armarx::SimpleLexer::addRule
void addRule(int id, const std::string &name, const std::string &regex)
Definition: SimpleLexer.cpp:34
armarx::LexerInfo
Definition: LexerInfo.h:30
armarx::SimpleLexer::lastPos
int lastPos
Definition: SimpleLexer.h:56
armarx::SimpleLexer::tokenFin
int tokenFin
Definition: SimpleLexer.h:62
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
SimpleLexer.h