-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.ll
258 lines (195 loc) · 4.82 KB
/
scanner.ll
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* $Id: scanner.ll 44 2008-10-23 09:03:19Z tb $ -*- mode: c++ -*- */
/** \file scanner.ll Define the example Flex lexical scanner */
%{ /*** C/C++ Declarations ***/
#include <string>
#include "scanner.h"
/* import the parser's token type into a local typedef */
typedef example::Parser::token token;
typedef example::Parser::token_type token_type;
/* By default yylex returns int, we use token_type. Unfortunately yyterminate
* by default returns 0, which is not of token_type. */
#define yyterminate() return token::END
/* This disables inclusion of unistd.h, which is not available under Visual C++
* on Win32. The C++ scanner uses STL streams instead. */
#define YY_NO_UNISTD_H
%}
/*** Flex Declarations and Options ***/
/* enable c++ scanner class generation */
%option c++
/* change the name of the scanner class. results in "ExampleFlexLexer" */
%option prefix="Example"
/* the manual says "somewhat more optimized" */
%option batch
/* enable scanner to generate debug output. disable this for release
* versions. */
/* %option debug */
/* no support for include files is planned */
%option yywrap nounput
/* enables the use of start condition stacks */
%option stack
/* exclusive start conditions.
keep is exclusive because its possible values may colide with package names.
*/
%x comment ccomment keep
/* The following paragraph suffices to track locations accurately. Each time
* yylex is invoked, the begin position is moved onto the end position. */
%{
#define YY_USER_ACTION yylloc->columns(yyleng);
%}
%% /*** Regular Expressions Part ***/
/* code to place at the beginning of yylex() */
%{
// reset location
yylloc->step();
%}
/*** BEGIN EXAMPLE - Change the example lexer rules below ***/
true {
yylval->boolVal = true;
return token::BOOL;
}
false {
yylval->boolVal = false;
return token::BOOL;
}
preamble: {
return token::PREAMBLE;
}
property: {
return token::PROPERTYKW;
}
package: {
return token::PACKAGEKW;
}
version: {
return token::VERSIONKW;
}
depends: {
return token::DEPENDSKW;
}
conflicts: {
return token::CONFLICTSKW;
}
provides: {
return token::PROVIDESKW;
}
installed: {
return token::INSTALLEDKW;
}
keep: {
BEGIN(keep);
return token::KEEPKW;
}
<keep>version {
BEGIN(INITIAL);
return token::KEEPVERSION;
}
<keep>package {
BEGIN(INITIAL);
return token::KEEPPACKAGE;
}
<keep>feature {
BEGIN(INITIAL);
return token::KEEPFEATURE;
}
<keep>none {
BEGIN(INITIAL);
return token::KEEPNONE;
}
request:[^\n]* {
return token::REQUEST;
}
upgrade: {
return token::UPGRADE;
}
install: {
return token::INSTALL;
}
remove: {
return token::REMOVE;
}
>= { return token::RGE; }
> { return token::RGT; }
\< { return token::RLT; }
\<= { return token::RLE; }
!= { return token::RNEQ; }
= { return token::REQ; }
[0-9]+ {
yylval->integerVal = atoi(yytext);
return token::INTEGER;
}
[a-zA-z][A-Za-z0-9_@()~%/+\.]+":" {
/* property name is an identifier followed by a colon */
yylval->stringVal = new std::string(yytext, yyleng);
return token::PROPNAME;
}
[A-Za-z0-9_@()~%/+\.\-:]+ {
/* from now on an identifier and a package name are considered
the same thing.
*/
yylval->stringVal = new std::string(yytext, yyleng);
return token::IDENT;
}
/* gobble up white-spaces */
[ \t\r]+ {
yylloc->step();
}
/* gobble up end-of-lines */
\n {
yylloc->lines(yyleng); yylloc->step();
return token::EOL;
}
<INITIAL># {
BEGIN(comment);
}
<comment>[^\n]*
<comment>\n {
yylloc->lines(yyleng); yylloc->step();
BEGIN(INITIAL);
}
<INITIAL>"/*" { BEGIN(ccomment); }
<ccomment>"*/" { BEGIN(INITIAL); }
<ccomment>[^*\n]+
<ccomment>"*"
<ccomment>\n {
yylloc->lines(yyleng); yylloc->step();
}
/* pass all other characters up to bison */
. {
return static_cast<token_type>(*yytext);
}
/*** END EXAMPLE - Change the example lexer rules above ***/
%% /*** Additional Code ***/
namespace example {
Scanner::Scanner(std::istream* in,
std::ostream* out)
: ExampleFlexLexer(in, out)
{
}
Scanner::~Scanner()
{
}
void Scanner::set_debug(bool b)
{
yy_flex_debug = b;
}
}
/* This implementation of ExampleFlexLexer::yylex() is required to fill the
* vtable of the class ExampleFlexLexer. We define the scanner's main yylex
* function via YY_DECL to reside in the Scanner class instead. */
#ifdef yylex
#undef yylex
#endif
int ExampleFlexLexer::yylex()
{
std::cerr << "in ExampleFlexLexer::yylex() !" << std::endl;
return 0;
}
/* When the scanner receives an end-of-file indication from YY_INPUT, it then
* checks the yywrap() function. If yywrap() returns false (zero), then it is
* assumed that the function has gone ahead and set up `yyin' to point to
* another input file, and scanning continues. If it returns true (non-zero),
* then the scanner terminates, returning 0 to its caller. */
int ExampleFlexLexer::yywrap()
{
return 1;
}