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
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
41
SimpleLexer::nextToken
()
42
{
43
if
(
pos
>= (
int
)
src
.size())
44
{
45
currentTokenValue
=
""
;
46
currentTokenId
=
tokenFin
;
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
66
currentTokenValue
=
""
;
67
currentTokenId
=
tokenError
;
68
lastPos
=
pos
;
69
return
currentTokenId
;
70
}
71
72
void
73
SimpleLexer::reset
()
74
{
75
this->
pos
= 0;
76
this->
lastPos
= 0;
77
currentTokenValue
=
""
;
78
currentTokenId
=
tokenError
;
79
}
80
81
std::string
82
SimpleLexer::tokenIdToName
(
int
id
)
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
137
LexerInfo
138
SimpleLexer::getPositionInfo
(
int
pos)
139
{
140
int
line, column;
141
positionToLineAndColumn
(
pos
, line, column);
142
return
LexerInfo
(
pos
, line, column);
143
}
144
}
// namespace armarx
armarx::SimpleLexer::Rule
Definition:
SimpleLexer.h:34
armarx::SimpleLexer::rules
std::vector< Rule > rules
Definition:
SimpleLexer.h:67
armarx::SimpleLexer::getPositionInfo
LexerInfo getPositionInfo(int pos)
Definition:
SimpleLexer.cpp:138
armarx::SimpleLexer::pos
int pos
Definition:
SimpleLexer.h:59
armarx::SimpleLexer::currentTokenId
int currentTokenId
Definition:
SimpleLexer.h:63
armarx::SimpleLexer::src
std::string src
Definition:
SimpleLexer.h:61
armarx::SimpleLexer::tokenError
int tokenError
Definition:
SimpleLexer.h:66
armarx::SimpleLexer::reset
void reset()
Definition:
SimpleLexer.cpp:73
armarx::SimpleLexer::positionToLineAndColumn
void positionToLineAndColumn(int pos, int &line, int &column)
Definition:
SimpleLexer.cpp:106
armarx::SimpleLexer::currentTokenValue
std::string currentTokenValue
Definition:
SimpleLexer.h:64
armarx::SimpleLexer::tokenIdToName
std::string tokenIdToName(int id)
Definition:
SimpleLexer.cpp:82
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:41
armarx::SimpleLexer::addRule
void addRule(int id, const std::string &name, const std::string ®ex)
Definition:
SimpleLexer.cpp:35
armarx::LexerInfo
Definition:
LexerInfo.h:30
armarx::SimpleLexer::lastPos
int lastPos
Definition:
SimpleLexer.h:60
armarx::SimpleLexer::tokenFin
int tokenFin
Definition:
SimpleLexer.h:66
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition:
ArmarXTimeserver.cpp:27
SimpleLexer.h
ArmarXGui
libraries
StructuralJson
SimpleLexer.cpp
Generated on Sat Mar 29 2025 09:17:23 for armarx_documentation by
1.8.17