Debian Codegraph @

scanner/lexer

raw
1 %filenames scanner
2
3 %class-name = "Scanner"
4 %token-path = "../tokens/tokens.h"
5
6 %debug
7 // %print-tokens
8
9 %x xstring pstring pxstring string rawstring comment quote block typespec
10 %x typecomment
11
12 OCTAL [0-7]
13 OCT3 {OCTAL}{3}
14 HEX [[:xdigit:]]
15 HEX2 {HEX}{2}
16 ID1 [[:alpha:]_]
17 ID2 [[:alnum:]_]
18 IDENT {ID1}{ID2}*
19 NR [[:digit:]]+
20
21 %%
22
23 <INITIAL,block>{
24
25 "{" {
26 // open or count a nested a block
27 d_block.open(lineNr(), filename());
28 begin(StartCondition_::block);
29 }
30
31 // The whitespace-eating RegExes (REs) will normally simply consume the
32 // WS. However, if d_retWS is unequal 0 then WS is returned. This is
33 // sometimes needed (e.g., inside code blocks to be able to return the ws
34 // as part of the code-block). Comment is part of the WS returning REs
35 [ \t]+ {
36 if (d_block)
37 d_block += " ";
38 }
39
40 [\n]+ {
41 setLineNrs();
42 if (d_block)
43 d_block += "\n";
44 }
45
46 "//".* // ignore eoln comment in source blocks
47
48 // If comment is entered from `block' either a blank or a newline will be
49 // added to the block as soon as the matching end-comment is seen, and
50 // the scanner will return to its block-miniscanner state
51 "/*" {
52 d_commentChar[0] = ' ';
53 begin(StartCondition_::comment);
54 }
55 }
56
57 // Blocks start at { and end at their matching } char. They may contain
58 // comment and whitespace, but whitespace is reduced to single blanks or
59 // newlines. All STRING and QUOTE constants are kept as-is, and are
60 // registered as skip-ranges for $-checks
61 <block>{
62 R\"{IDENT}?\( rawString();
63
64 "}" {
65 if (d_block.close()) // close a block
66 {
67 begin(StartCondition_::INITIAL);
68 return Tokens::BLOCK;
69 }
70 }
71
72 "\"" {
73 begin(StartCondition_::string);
74 more();
75 }
76
77 "'" {
78 begin(StartCondition_::quote);
79 more();
80 }
81
82
83 // negative dollar indices with tag indicate a .get request.
84 // there is no reference option:
85 // "_$<"{IDENT}">"-{NR} | // refDid_n
86
87 // see: parser/substituteblock.cc
88 // AtDollar Pattern:
89 "$$"[ \t\n]*= | // assignments are refDD or refD_
90 "$"-?{NR}[ \t\n]*= assignment();
91
92 "$$(" |
93 @@ |
94 @{NR} |
95 "_$$" |
96 "$$"\.? |
97 "$$->" |
98 "_$"-?{NR} |
99 \$-?{NR} |
100 \$-?{NR}\. |
101 \$-?{NR}"->" |
102 "$<"{IDENT}">"-{NR}\.? |
103 "$<"{IDENT}">"-{NR}"->" d_block.atDollar(lineNr(), d_matched, false);
104
105 . d_block(d_matched);
106 }
107
108 %baseclass-header[ \t]+ {
109 begin(StartCondition_::pxstring);
110 return Tokens::BASECLASS_HEADER;
111 }
112 %baseclass-preinclude[ \t]+ {
113 begin(StartCondition_::pxstring);
114 return Tokens::BASECLASS_PREINCLUDE;
115 }
116 %class-header[ \t]+ {
117 begin(StartCondition_::pxstring);
118 return Tokens::CLASS_HEADER;
119 }
120 %class-name return Tokens::CLASS_NAME;
121 %constructor-checks return Tokens::CONSTRUCTOR_CHECKS;
122 %default-actions return Tokens::DEFAULT_ACTIONS;
123 %debug return Tokens::DEBUGFLAG;
124 %error-verbose return Tokens::ERROR_VERBOSE;
125 %expect return Tokens::EXPECT;
126 %filenames[ \t]+ {
127 begin(StartCondition_::pxstring);
128 return Tokens::FILENAMES;
129 }
130 "%flex" return Tokens::FLEX;
131 %implementation-header[ \t]+ {
132 begin(StartCondition_::pxstring);
133 return Tokens::IMPLEMENTATION_HEADER;
134 }
135 %include[ \t]+ {
136 begin(StartCondition_::pxstring);
137 d_include = true;
138 }
139 %left return Tokens::LEFT;
140 %locationstruct return Tokens::LOCATIONSTRUCT;
141 %lsp-needed return Tokens::LSP_NEEDED;
142 %ltype[ \t]+ {
143 begin(StartCondition_::xstring);
144 return Tokens::LTYPE;
145 }
146 %namespace return Tokens::NAMESPACE;
147 %negative-dollar-indices return Tokens::NEG_DOLLAR;
148 %no-lines return Tokens::NOLINES;
149 %nonassoc return Tokens::NONASSOC;
150 %parsefun-source[ \t]+ {
151 begin(StartCondition_::pxstring);
152 return Tokens::PARSEFUN_SOURCE;
153 }
154 %polymorphic return Tokens::POLYMORPHIC;
155 %prec return Tokens::PREC;
156 %print-tokens return Tokens::PRINT_TOKENS;
157 %prompt return Tokens::PROMPT;
158 %required-tokens return Tokens::REQUIRED;
159 %right return Tokens::RIGHT;
160 %scanner[ \t]+ {
161 begin(StartCondition_::pxstring);
162 return Tokens::SCANNER;
163 }
164 %scanner-class-name[ \t]+ {
165 begin(StartCondition_::pxstring);
166 return Tokens::SCANNER_CLASS_NAME;
167 }
168 %scanner-token-function[ \t]+ {
169 begin(StartCondition_::pxstring);
170 return Tokens::SCANNER_TOKEN_FUNCTION;
171 }
172 %scanner-matched-text-function[ \t]+ {
173 begin(StartCondition_::pxstring);
174 return
175 Tokens::SCANNER_MATCHED_TEXT_FUNCTION;
176 }
177 %stack-expansion[ \t]+ return Tokens::STACK_EXPANSION;
178 %start return Tokens::START;
179 %stype[ \t]+ {
180 begin(StartCondition_::xstring);
181 return Tokens::STYPE;
182 }
183 %tag-mismatches return Tokens::WARN_TAGS;
184 %target-directory[ \t]+ {
185 begin(StartCondition_::pxstring);
186 return Tokens::TARGET_DIRECTORY;
187 }
188 %thread-safe return Tokens::THREAD_SAFE;
189 %token return Tokens::TOKEN;
190 %token-class return Tokens::TOKEN_CLASS;
191 %token-namespace return Tokens::TOKEN_NAMESPACE;
192 %token-path[ \t]+ {
193 begin(StartCondition_::pxstring);
194 return Tokens::TOKEN_PATH;
195 }
196 %type return Tokens::TYPE;
197 %union return Tokens::UNION;
198 %weak-tags return Tokens::WEAK_TAGS;
199 "%%" return Tokens::TWO_PERCENTS;
200
201 "'" {
202 begin(StartCondition_::quote);
203 more();
204 }
205
206 "\"" {
207 begin(StartCondition_::string);
208 more();
209 }
210
211 {IDENT} return Tokens::IDENTIFIER;
212
213 [[:digit:]]+ {
214 d_number = stoul(d_matched);
215 return Tokens::NUMBER;
216 }
217
218 . return d_matched[0];
219
220 // pxstring is activated after a directive has been sensed.
221 // it extracts a string, pstring or any sequence of non-blank characters,
222 <pxstring>{
223 "\"" {
224 more();
225 begin(StartCondition_::string);
226 }
227 "<" {
228 more();
229 begin(StartCondition_::pstring);
230 }
231 . {
232 accept(0);
233 begin(StartCondition_::xstring);
234 }
235 \n return eoln();
236 }
237 // string may be entered from block and pxstring
238 // strings are all series (including escaped chars, like \") surrounded by
239 // double quotes:
240 <string>{
241 "\"" {
242 if (handleXstring(0))
243 return Tokens::STRING;
244 }
245 "\\". |
246 . more();
247 \n return eoln();
248 }
249
250 // a pstring is a string surrounded by < and >
251 <pstring>{
252 ">" {
253 if (handleXstring(0))
254 return Tokens::STRING;
255 }
256 "\\". |
257 . more();
258 \n return eoln();
259 }
260
261 // xstring returns the next string delimited by either blanks, tabs,
262 // newlines or C/C++ comment.
263 <xstring>{
264 [[:space:]] {
265 if (handleXstring(1))
266 return Tokens::STRING;
267 }
268
269 "//" |
270 "/*" {
271 if (handleXstring(2))
272 return Tokens::STRING;
273 }
274
275 . more();
276 }
277
278 <rawstring>{
279 \){IDENT}?\" checkEndOfRawString();
280
281 .|\n more();
282 }
283
284 <comment>{
285 .
286 \n {
287 setLineNrs();
288 d_commentChar[0] = '\n';
289 }
290 "*/" {
291 if (!d_block)
292 begin(StartCondition_::INITIAL);
293 else
294 {
295 d_block += d_commentChar;
296 begin(StartCondition_::block);
297 }
298 }
299 }
300 // quote may be entered from INITIAL and block.
301 // quoted constants start with a quote. They may be octal or hex numbers,
302 // escaped chars, or quoted constants
303 <quote>{
304
305 "\\"{OCT3}"'" returnQuoted(&Scanner::octal);
306
307 "\\x"{HEX2}"'" returnQuoted(&Scanner::hexadecimal);
308
309 "\\"[abfnrtv]"'" {
310 if (d_block(d_matched))
311 begin(StartCondition_::block);
312 else
313 {
314 begin(StartCondition_::INITIAL);
315 escape(); // quoted escape char
316 return Tokens::QUOTE;
317 }
318 }
319
320 "\\"."'" returnQuoted(&Scanner::matched2);
321
322 ."'" returnQuoted(&Scanner::matched1);
323
324 [^']+"'" returnQuoted(&Scanner::multiCharQuote);
325
326 }
327
328 // a typespec holds all chars after a ':' until a ';' or '%' (which are
329 // pushed back). It is used as a type specification in
330 // parser/inc/directives. Escape characters are interpreted
331 <typespec>{
332 \n d_typeName += ' '; // convert newlines to spaces
333
334 \\. | // add escaped chars as-is
335 [^;%] d_typeName += matched(); // accept all until ; or %
336
337 "//".* // ignore EOLN comment
338
339 // ignore std C comment
340 "/*" begin(StartCondition_::typecomment);
341
342 [;%] returnTypeSpec(); // back to INITIAL, returns IDENTIFIER
343 }
344
345 <typecomment>{
346 .|\n // ignored
347
348 "*/" {
349 d_typeName += ' ';
350 begin(StartCondition_::typespec);
351 }
352 }