/
cf3lex.l
497 lines (381 loc) · 15.3 KB
/
cf3lex.l
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
%{
/*
Copyright (C) CFEngine AS
This file is part of CFEngine 3 - written and maintained by CFEngine AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
/*******************************************************************/
/* */
/* LEXER for cfengine 3 */
/* */
/*******************************************************************/
#include <platform.h>
#include <cf3parse.h>
#include <parser_state.h>
#include <file_lib.h>
/* yyinput/input are generated and unused */
#if defined(__GNUC__)
# define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
# if GCC_VERSION >= 40200
# pragma GCC diagnostic ignored "-Wunused-function"
# endif
#endif
#undef malloc
#undef realloc
#define malloc xmalloc
#define realloc xrealloc
#define ParserDebug if (LogGetGlobalLevel() >= LOG_LEVEL_DEBUG) printf
static int DeEscapeQuotedString(const char *from, char *to);
int yywrap(void)
{
return 1;
}
static void yyuseraction()
{
P.offsets.current += yyleng;
}
#define YY_USER_ACTION yyuseraction();
#define MAX_INCLUDE_DEPTH 1
YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
int include_stack_ptr = 0;
%}
%x incl_state
space [ \t]+
newline ([\n]|[\xd][\xa])
line ^.*$
comment #[^\n]*
include ^@include
promises bundle
body body
nakedvar [$@][(][a-zA-Z0-9_\[\]\200-\377.:]+[)]|[$@][{][a-zA-Z0-9_\[\]\200-\377.:]+[}]
id [a-zA-Z0-9_\200-\377]+
symbol [a-zA-Z0-9_\200-\377]+[:][a-zA-Z0-9_\200-\377]+
assign =>
arrow ->
op [(]
cp [)]
ob [{]
cb [}]
/*
* Three types of quoted strings:
*
* - string in double quotes, starts with double quote, runs until another
* double quote, \" masks the double quote.
* - string in single quotes, starts with single quote, runs until another
* single quote, \' masks the single quote.
* - string in backquotes, starts with backquote, runs until another backquote.
*
* The same rule formatted for the better readability:
*
* <qstring> := \" <dq> \" | \' <sq> \' | ` <bq> `
* <dq> = <dqs>*
* <dqs> = \\ <any> | [^"\\]
* <sq> = <sqs>*
* <sqs> = \\ <any> | [^'\\]
* <bq> = <bqs>*
* <bqs> = [^`]
* <any> = . | \n
*
*/
qstring \"((\\(.|\n))|[^"\\])*\"|\'((\\(.|\n))|[^'\\])*\'|`[^`]*`
class [.|&!()a-zA-Z0-9_\200-\377:]+::
promise_type [a-zA-Z_]+:
%%
{line} {
free(P.current_line);
P.current_line = xstrdup(yytext);
ParserDebug("L:line %s\n", P.current_line);
REJECT;
}
{newline} {
P.line_no++;
P.line_pos = 0;
}
{promises} {
/* Note this has to come before "id" since it is a subset of id */
if (P.currentclasses != NULL)
{
free(P.currentclasses);
P.currentclasses = NULL;
}
P.line_pos += yyleng;
ParserDebug("\tL:bundle %d\n", P.line_pos);
return BUNDLE;
}
{body} {
/* Note this has to come before "id" since it is a subset of id */
if (P.currentclasses != NULL)
{
free(P.currentclasses);
P.currentclasses = NULL;
}
P.line_pos += yyleng;
ParserDebug("\tL:body %d\n", P.line_pos);
return BODY;
}
{include} {
ParserDebug("\tL:include %d\n", P.line_pos);
if ( include_stack_ptr >= MAX_INCLUDE_DEPTH )
{
ParserDebug("\tI: Includes nested too deeply\n" );
exit(EXIT_FAILURE);
}
BEGIN(incl_state);
}
<incl_state>[ \t]*
<incl_state>{qstring} {
ParserDebug("\tL:include state:qstring:%s\n", yytext);
include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
char *fname = NULL;
fname = xstrdup(yytext+1); // skip up to the opening quote
fname[strlen(fname)-1] = '\0'; // clear the ending quote
// check for leading path components, attempting to catch Windows paths
if (fname == strchr(fname, '/') || (strlen(fname) > 3 && fname[2] == ':'))
{
ParserDebug("\tL:include_state:%s (absolute path, included in %s)\n", fname, P.filename);
}
else
{
ParserDebug("\tL:include_state:%s (relative path, included in %s)\n", fname, P.filename);
char current[CFE_PARSER_PATH_MAX];
char dirname[CFE_PARSER_PATH_MAX];
if (NULL == realpath(P.filename, dirname))
{
char error_str[CF_MAXVARSIZE];
snprintf(error_str, CF_MAXVARSIZE, "\tI: Could not resolve realpath of file '%s': %s\n", P.filename, strerror(errno));
yyerror(error_str);
exit(EXIT_FAILURE);
}
char *chop = strrchr(dirname, '/');
if (NULL == chop)
{
chop = strrchr(dirname, '\\');
}
if (NULL != chop)
{
*chop = '\0';
}
// this should work on Windows too
snprintf(current, CFE_PARSER_PATH_MAX-1, "%s/%s", dirname, fname);
free(fname);
fname = xstrdup(current);
}
yyin = safe_fopen(fname, "r" );
if ( ! yyin )
{
char error_str[CF_MAXVARSIZE];
snprintf(error_str, CF_MAXVARSIZE, "\tI: Could not open file '%s' for reading: %s\n", fname, strerror(errno));
yyerror(error_str);
exit(EXIT_FAILURE);
}
else
{
ParserDebug("\tI: @Including file '%s'\n", fname);
}
strncpy(P.include_filename, fname, CFE_PARSER_PATH_MAX);
free(fname);
yy_switch_to_buffer( yy_create_buffer( yyin, YY_BUF_SIZE ) );
BEGIN(INITIAL);
}
<incl_state>.[^'"]* {
yyerror("Not a valid include statement");
exit(EXIT_FAILURE);
}
<<EOF>> {
P.include_filename[0] = '\0';
if ( --include_stack_ptr < 0 || !YY_CURRENT_BUFFER)
{
yyterminate();
}
else
{
yy_delete_buffer( YY_CURRENT_BUFFER );
yy_switch_to_buffer(include_stack[include_stack_ptr] );
}
}
{id} {
P.offsets.last_id = P.offsets.current - yyleng;
P.line_pos += yyleng;
ParserDebug("\tL:id %s %d\n", yytext, P.line_pos);
if (yyleng > CF_MAXVARSIZE-1)
{
yyerror("identifier too long");
}
strncpy(P.currentid, yytext, CF_MAXVARSIZE);
return IDSYNTAX;
}
{symbol} {
P.offsets.last_id = P.offsets.current - yyleng;
P.line_pos += yyleng;
ParserDebug("\tL:symbol %s %d\n", yytext, P.line_pos);
if (yyleng > CF_MAXVARSIZE-1)
{
yyerror("qualified identifier too long");
}
strncpy(P.currentid, yytext, CF_MAXVARSIZE);
return IDSYNTAX;
}
{assign} {
P.line_pos += yyleng;
ParserDebug("\tL:assign %d\n", P.line_pos);
return ASSIGN;
}
{arrow} {
P.line_pos += yyleng;
ParserDebug("\tL:arrow %d\n", P.line_pos);
return ARROW;
}
{class} {
char *tmp = NULL;
P.line_pos += yyleng;
ParserDebug("\tL:class %s %d\n", yytext, P.line_pos);
tmp = xstrdup(yytext);
tmp[yyleng-2] = '\0';
if (P.currentclasses != NULL)
{
free(P.currentclasses);
}
P.currentclasses = xstrdup(tmp);
free(tmp);
return CLASS;
}
{promise_type} {
char *tmp = NULL;
P.line_pos += yyleng;
ParserDebug("\tL:promise_type %s %d\n", yytext, P.line_pos);
P.offsets.last_promise_type_id = P.offsets.current - yyleng;
tmp = xstrdup(yytext);
tmp[yyleng - 1] = '\0';
strncpy(P.currenttype, tmp, CF_MAXVARSIZE);
if (P.currentclasses != NULL)
{
free(P.currentclasses);
P.currentclasses = NULL;
}
free(tmp);
return PROMISE_TYPE;
}
{qstring} {
char *tmp = NULL;
int less = 0;
P.offsets.last_string = P.offsets.current - yyleng;
P.line_pos += yyleng;
ParserDebug("\tL:qstring %s %d\n", yytext, P.line_pos);
for (char *c = yytext; *c; ++c)
{
if (*c == '\n')
{
P.line_no++;
}
}
tmp = xmalloc(yyleng + 1);
if ((less = DeEscapeQuotedString(yytext,tmp)) > 0)
{
yyless(less);
P.offsets.current -= less;
}
if (P.currentstring)
{
free(P.currentstring);
}
P.currentstring = xstrdup(tmp);
free(tmp);
return QSTRING;
}
{nakedvar} {
P.line_pos += yyleng;
ParserDebug("\tL: %s %d\n", yytext, P.line_pos);
if (P.currentstring)
{
free(P.currentstring);
}
P.currentstring = xstrdup(yytext);
return NAKEDVAR;
}
{op} {
P.line_pos += yyleng;
ParserDebug("\tL: %s %d\n", yytext, P.line_pos);
return OP;
}
{cp} {
P.line_pos += yyleng;
ParserDebug("\tL: %s %d\n", yytext, P.line_pos);
return CP;
}
{ob} {
P.line_pos += yyleng;
ParserDebug("\tL: %s %d\n", yytext, P.line_pos);
return OB;
}
{cb} {
P.line_pos += yyleng;
ParserDebug("\tL: %s %d\n", yytext, P.line_pos);
return CB;
}
{space}+ {
P.line_pos += yyleng;
}
{comment} {
}
. {
P.line_pos++;
return yytext[0];
}
%%
static int DeEscapeQuotedString(const char *from, char *to)
{
char *cp;
const char *sp;
char start = *from;
int len = strlen(from);
if (len == 0)
{
return 0;
}
for (sp = from + 1, cp = to; (sp - from) < len; sp++, cp++)
{
if ((*sp == start))
{
*(cp) = '\0';
if (*(sp + 1) != '\0')
{
return (2 + (sp - from));
}
return 0;
}
if (*sp == '\\')
{
switch (*(sp + 1))
{
case '\n':
sp += 2;
break;
case ' ':
break;
case '\\':
case '\"':
case '\'':
sp++;
break;
}
}
*cp = *sp;
}
yyerror("Runaway string");
*(cp) = '\0';
return 0;
}
/* EOF */