Debian Codegraph @

src/lexer.l

raw
1 %top{
2 /*
3 * boxes - Command line filter to draw/remove ASCII boxes around text
4 * Copyright (c) 1999-2024 Thomas Jensen and the boxes contributors
5 *
6 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
7 * License, version 3, as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10 * details.
11 * You should have received a copy of the GNU General Public License along with this program.
12 * If not, see <https://www.gnu.org/licenses/>.
13 *
14 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
15 */
16
17 /*
18 * flex lexical analyzer for boxes configuration files
19 */
20
21 #include "config.h"
22 #include "bxstring.h"
23
24 typedef struct {
25 int yyerrcnt;
26
27 /** the currently active string delimiter character */
28 char sdel;
29
30 /** the currently active string escape character */
31 char sesc;
32 } pass_to_flex;
33
34
35 /*
36 * Valid characters to be used as string delimiters.
37 * The following list must correspond to the SDELIM definition below.
38 */
39 #define LEX_SDELIM "\"~'`!@%&*=:;<>?/|.\\"
40 #define LEX_SDELIM_RECOMMENDED "\"~'!|"
41
42
43 /**
44 * User-defined initializations for the lexer.
45 *
46 * Since this scanner must use REJECT in order to be able to process the string delimiter commands, it cannot
47 * dynamically enlarge its input buffer to accomodate larger tokens. Thus, we simply set the buffer size to the
48 * input file size plus 10 bytes margin-of-error.
49 *
50 * @param yyscanner pointer to the scanner data block
51 * @param configfile the path to the config file we are reading
52 */
53 void inflate_inbuf(void *yyscanner, const bxstr_t *configfile);
54
55 }
56
57 %{
58 #include <string.h>
59 #include <sys/stat.h>
60 #include <unistd.h>
61 #include <unitypes.h>
62
63 #include "boxes.h"
64 #include "logging.h"
65 #include "shape.h"
66 #include "tools.h"
67 #include "parsing.h"
68 #include "parser.h"
69 #include "unicode.h"
70
71
72 #define LEX_MAX_WARN 3 /* number of lex errors per design */
73
74 static void report_state_char(char *symbol, char c, char *expected_state_str);
75
76 static void report_state(char *symbol, char *text, char *expected_state_str);
77
78 static int change_string_delimiters(pass_to_flex *extra, char *delim_expr);
79
80 %}
81
82
83 %option 8bit
84 %option bison-bridge
85 %option case-insensitive
86 %option ecs
87 %option extra-type="pass_to_flex *"
88 %option never-interactive
89 %option nodefault
90 %option noinput
91 %option nounput
92 %option noyywrap
93 %option reentrant
94 %option warn
95 %option yylineno
96
97
98 %x BOX
99 %x SAMPLE
100 %x SHAPES
101 %x ELASTIC
102 %x DELIMSPEC
103 %x PARENT
104
105 /*
106 * The following paragraph contains patterns to recognize UTF-8 characters from a byte stream, based on
107 * - https://stackoverflow.com/a/10253320/1005481 by Zack Weinberg (under CC-BY-SA 3.0 license)
108 * - https://www.w3.org/2005/03/23-lex-U by Eric Prud'hommeaux, W3C (under the W3C Document License)
109 */
110 PBOM \xEF\xBB\xBF
111 U2A [\xC2-\xDF][\x80-\xBF]
112 U2B \xE0[\xA0-\xBF][\x80-\xBF]
113 U3A [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}
114 U3B \xED[\x80-\x9F][\x80-\xBF]
115 U4A \xF0[\x90-\xBF][\x80-\xBF]{2}
116 U4B [\xF1-\xF3][\x80-\xBF]{3}
117 U4C \xF4[\x80-\x8F][\x80-\xBF]{2}
118 UTF_8 {U2A}|{U2B}|{U3A}|{U3B}|{U4A}|{U4B}|{U4C}
119
120 PWORD (?:[a-zA-Z]|{UTF_8})(?:[a-zA-Z0-9_-]|{UTF_8})*
121 PASCII_ID [a-zA-Z][a-zA-Z0-9_-]*
122 PWHITE [ \t\r\n]
123 SDELIM [\"~\'`!@\%\&\*=:;<>\?/|\.\\]
124 PPARENT parent
125 PFILENAME [^\r\n]+
126
127
128 %%
129
130 /*
131 * Precedence of rules:
132 * - The rule that matches the most text wins.
133 * - If two rules match the same amount of text, the one defined first (further up) wins.
134 */
135
136
137 <INITIAL,BOX,DELIMSPEC,ELASTIC,SHAPES>{PWHITE}|{PBOM} /* ignore whitespace and a byte order mark */
138
139 <DELIMSPEC>[^ \t\r\n]+ {
140 /*
141 * String delimiter spec - like BXWORD, but allow any character
142 */
143 yylval->s = bxs_from_ascii("IGNORED");
144 char *str = (char *) strdup(yytext);
145 BEGIN(BOX);
146 report_state("YDELIMS", str, "INITIAL");
147 if (change_string_delimiters(yyextra, str) != 0) {
148 return YUNREC;
149 }
150 return YDELIMSPEC;
151 }
152
153
154 <BOX,SHAPES>{SDELIM}.*$ {
155 /*
156 * Strings -- first match everything starting from a potential string delimiter until the end of the line. We
157 * will give back what we don't need and also detect unterminated strings. Strings always end on the same line.
158 */
159 int rest_len = yyleng - 1; /* length of string pointed to by p */
160 int qcnt = 0; /* esc char count in current string */
161
162 if (yytext[0] != yyextra->sdel) {
163 REJECT; /* that was not our delimiter */
164 }
165
166 char *str = (char *) strdup(yytext + 1);
167 if (str == NULL) {
168 perror (PROJECT);
169 exit (EXIT_FAILURE);
170 }
171 char *p = str;
172
173 while (*p) {
174 if (*p == yyextra->sesc) {
175 memmove (p, p+1, rest_len); /* incl. '\0' */
176 ++qcnt;
177 --rest_len;
178 if (*p == '\0') {
179 break;
180 }
181 }
182 else if (*p == yyextra->sdel) {
183 *p = '\0';
184 yyless ((p - str) + 2 + qcnt); /* string plus quotes */
185 log_debug(__FILE__, LEXER, " STRING: \"%s\"\n", str);
186
187 uint32_t *utf8 = u32_strconv_from_arg(str, CONFIG_FILE_ENCODING);
188 yylval->s = bxs_from_unicode(utf8);
189 BFREE(utf8);
190 BFREE(str);
191 return STRING;
192 }
193 --rest_len;
194 ++p;
195 }
196 if ((yyextra->yyerrcnt)++ < 5) {
197 yyerror(NULL, "Unterminated String -- %s", yytext);
198 }
199 BFREE(str);
200 return YUNREC;
201 }
202
203
204 <INITIAL>{PPARENT} {
205 BEGIN(PARENT);
206 report_state("YPARENT", yytext, "PARENT");
207 return YPARENT;
208 }
209
210 <PARENT>{PFILENAME} {
211 uint32_t *utf8 = u32_strconv_from_arg(yytext, CONFIG_FILE_ENCODING);
212 bxstr_t *bxstr = bxs_from_unicode(utf8);
213 yylval->s = bxs_trim(bxstr);
214
215 BFREE(utf8);
216 bxs_free(bxstr);
217
218 BEGIN(INITIAL);
219 report_state("FILENAM", bxs_to_output(yylval->s), "INITIAL");
220 return FILENAME;
221 }
222
223 <PARENT>\r?\n {
224 /* This is triggered only when no parent filename was specified. */
225 BEGIN(INITIAL);
226 report_state(" NL", "", "INITIAL");
227 }
228
229
230 <BOX>Sample {
231 BEGIN(SAMPLE);
232 report_state("YSAMPLE", yytext, "SAMPLE");
233 return YSAMPLE;
234 }
235
236 <SAMPLE>\n {
237 if (yyleng > 1)
238 yymore();
239 }
240
241 <SAMPLE>^[ \t]*ends[ \t\r]*$ {
242 char *p = yytext + yyleng -1;
243 size_t len; /* length of sample */
244
245 while (*p == ' ' || *p == '\t' || *p == '\r')
246 --p; /* skip trailing whitespace */
247 p -= 2; /* almost skip "ends" statement */
248 *p = '\0'; /* p now points to 'n' */
249 char *sample = (char *) strdup(yytext);
250 if (sample == NULL) {
251 perror (PROJECT);
252 exit (EXIT_FAILURE);
253 }
254 *p-- = 'n';
255
256 len = p - yytext; /* yyless(n): push back all but the first n */
257 yyless (len); /* allow the lexer to return YENDSAMPLE */
258
259 sample[len] = '\n'; /* replace 'e' with newline */
260 btrim(sample, &len);
261 if (len > 0) {
262 uint32_t *utf8 = u32_strconv_from_arg(sample, CONFIG_FILE_ENCODING);
263 uint32_t *nl = u32_strconv_from_arg("\n", CONFIG_FILE_ENCODING);
264 bxstr_t *bxstr = bxs_from_unicode(utf8);
265 bxstr_t *bxstr2 = bxs_rtrim(bxstr);
266 bxs_free(bxstr);
267 bxstr = bxs_strcat(bxstr2, nl);
268 BFREE(nl);
269 BFREE(utf8);
270 BFREE(sample);
271 bxs_free(bxstr2);
272 yylval->s = bxstr;
273 return STRING;
274 }
275 else {
276 if ((yyextra->yyerrcnt)++ < 5) {
277 yyerror(NULL, "SAMPLE block must not be empty");
278 }
279 BFREE(sample);
280 return YUNREC;
281 }
282 }
283
284 <SAMPLE>. yymore();
285
286 <SAMPLE>ends[ \t\r]*$ {
287 /* reached because the other rule pushes it back so a proper end token can be returned */
288 BEGIN(BOX);
289 report_state("YENDSAM", yytext, "BOX");
290 return YENDSAMPLE;
291 }
292
293
294 <BOX>Tags {
295 log_debug(__FILE__, LEXER, " YTAGS: %s\n", yytext);
296 return YTAGS;
297 }
298
299 <BOX>Elastic {
300 BEGIN(ELASTIC);
301 report_state("YELASTC", yytext, "ELASTIC");
302 return YELASTIC;
303 }
304
305 <BOX>Shapes {
306 BEGIN(SHAPES);
307 report_state("YSHAPES", yytext, "SHAPES");
308 return YSHAPES;
309 }
310
311 <INITIAL>Box {
312 BEGIN(BOX);
313 report_state(" YBOX", yytext, "BOX");
314 yyextra->yyerrcnt = 0;
315 change_string_delimiters(yyextra, "\\\"");
316 return YBOX;
317 }
318
319 <BOX>Replace { return YREPLACE; }
320 <BOX>Reverse { return YREVERSE; }
321 <BOX>Padding { return YPADDING; }
322 <BOX>To { return YTO; }
323 <BOX>With { return YWITH; }
324 <BOX>Global { yylval->c = 'g'; return YRXPFLAG; }
325 <BOX>Once { yylval->c = 'o'; return YRXPFLAG; }
326 <BOX>End {
327 BEGIN(INITIAL);
328 report_state(" YEND", yytext, "INITIAL");
329 change_string_delimiters(yyextra, "\\\"");
330 return YEND;
331 }
332
333
334 <SHAPES,ELASTIC>nw { yylval->shape = NW; return SHAPE; }
335 <SHAPES,ELASTIC>nnw { yylval->shape = NNW; return SHAPE; }
336 <SHAPES,ELASTIC>n { yylval->shape = N; return SHAPE; }
337 <SHAPES,ELASTIC>nne { yylval->shape = NNE; return SHAPE; }
338 <SHAPES,ELASTIC>ne { yylval->shape = NE; return SHAPE; }
339 <SHAPES,ELASTIC>ene { yylval->shape = ENE; return SHAPE; }
340 <SHAPES,ELASTIC>e { yylval->shape = E; return SHAPE; }
341 <SHAPES,ELASTIC>ese { yylval->shape = ESE; return SHAPE; }
342 <SHAPES,ELASTIC>se { yylval->shape = SE; return SHAPE; }
343 <SHAPES,ELASTIC>sse { yylval->shape = SSE; return SHAPE; }
344 <SHAPES,ELASTIC>s { yylval->shape = S; return SHAPE; }
345 <SHAPES,ELASTIC>ssw { yylval->shape = SSW; return SHAPE; }
346 <SHAPES,ELASTIC>sw { yylval->shape = SW; return SHAPE; }
347 <SHAPES,ELASTIC>wsw { yylval->shape = WSW; return SHAPE; }
348 <SHAPES,ELASTIC>w { yylval->shape = W; return SHAPE; }
349 <SHAPES,ELASTIC>wnw { yylval->shape = WNW; return SHAPE; }
350
351 <ELASTIC>\) {
352 BEGIN(BOX);
353 report_state_char("SYMBOL", yytext[0], "BOX");
354 return yytext[0];
355 }
356
357 <SHAPES>\} {
358 BEGIN(BOX);
359 report_state_char("SYMBOL", yytext[0], "BOX");
360 return yytext[0];
361 }
362
363
364 <BOX>author|designer|indent {
365 /*
366 * general key words
367 */
368 log_debug(__FILE__, LEXER, "KEYWORD: %s\n", yytext);
369 yylval->ascii = strdup(yytext);
370 if (yylval->ascii == NULL) {
371 perror (PROJECT);
372 exit (EXIT_FAILURE);
373 }
374 return KEYWORD;
375 }
376
377
378 <BOX>Delimiter|Delim {
379 /*
380 * Change string delimiting characters
381 */
382 BEGIN(DELIMSPEC);
383 report_state("YCHGDEL", yytext, "DELIMSPEC");
384 return YCHGDEL;
385 }
386
387 <INITIAL,BOX>{PASCII_ID} {
388 /*
389 * a free-floating word which is not a string, i.e. it does not have delimiting characters (ASCII version)
390 */
391 yylval->ascii = strdup(yytext);
392 if (yylval->ascii == NULL) {
393 perror (PROJECT);
394 exit (EXIT_FAILURE);
395 }
396 log_debug(__FILE__, LEXER, "ASCIIID: %s\n", yylval->ascii);
397 return ASCII_ID;
398 }
399
400 <INITIAL,BOX>{PWORD} {
401 /*
402 * a free-floating word which is not a string, i.e. it does not have delimiting characters
403 */
404 uint32_t *utf8 = u32_strconv_from_arg(yytext, CONFIG_FILE_ENCODING);
405 yylval->s = bxs_from_unicode(utf8);
406 if (yylval->s == NULL) {
407 perror (PROJECT);
408 exit (EXIT_FAILURE);
409 }
410 if (is_debug_logging(LEXER)) {
411 char *out_utf8 = u32_strconv_to_output(utf8);
412 log_debug(__FILE__, LEXER, " BXWORD: %s\n", out_utf8);
413 BFREE(out_utf8);
414 }
415 BFREE(utf8);
416 return BXWORD;
417 }
418
419
420 <BOX>[\+-]?[0-9]+ {
421 log_debug(__FILE__, LEXER, "YNUMBER: %s\n", yytext);
422 yylval->num = atoi (yytext);
423 return YNUMBER;
424 }
425
426
427 <BOX,SHAPES,ELASTIC>[,(){}] {
428 log_debug(__FILE__, LEXER, " SYMBOL: \'%c\'\n", yytext[0]);
429 return yytext[0];
430 }
431
432
433 <INITIAL,BOX,SHAPES,ELASTIC>#.*$ {
434 /* ignore comments */
435 log_debug(__FILE__, LEXER, "COMMENT: %s\n", yytext + 1);
436 }
437
438
439 <INITIAL,BOX,SHAPES,ELASTIC>. {
440 /* a character that made no sense where it was encountered. Let the parser handle it. */
441 log_debug(__FILE__, LEXER, " YUNREC: \'%c\'\n", yytext[0]);
442 return YUNREC;
443 }
444
445
446 %%
447
448
449 void inflate_inbuf(void *yyscanner, const bxstr_t *configfile)
450 {
451 struct stat sinf;
452
453 char *utf8 = u32_strconv_to_arg(configfile->memory, "UTF-8");
454 if (stat(utf8, &sinf)) {
455 perror (PROJECT);
456 BFREE(utf8);
457 exit (EXIT_FAILURE);
458 }
459 BFREE(utf8);
460 struct yyguts_t *yyg = (struct yyguts_t *) yyscanner;
461 yy_delete_buffer(YY_CURRENT_BUFFER, yyscanner);
462 yy_switch_to_buffer (yy_create_buffer(yyin, sinf.st_size+10, yyscanner), yyscanner);
463 BEGIN(INITIAL);
464 }
465
466
467
468 static void report_state_char(char *symbol, char c, char *expected_state_str)
469 {
470 char *s = (char *) malloc(4);
471 sprintf(s, "'%c'", c >= ' ' && c <= 126 ? c : '?');
472 report_state(symbol, s, expected_state_str);
473 BFREE(s);
474 }
475
476
477
478 static void report_state(char *symbol, char *text, char *expected_state_str)
479 {
480 if (is_debug_logging(LEXER)) {
481 size_t text_len = 0;
482 if (text != NULL) {
483 text_len = strlen(text);
484 if (text_len > 0 && (text[text_len - 1] == '\r' || text[text_len - 1] == '\t')) {
485 text[text_len - 1] = '\0';
486 }
487 }
488 log_debug(__FILE__, LEXER, "%7s: %s (%d) -- STATE %s\n", symbol, text, text_len, expected_state_str);
489 }
490 }
491
492
493
494 static int change_string_delimiters(pass_to_flex *extra, char *delim_expr)
495 {
496 if (strlen(delim_expr) != 2) {
497 yyerror(NULL, "invalid string delimiter specification -- %s", delim_expr);
498 return 1;
499 }
500 if (delim_expr[0] == delim_expr[1]) {
501 yyerror(NULL, "string delimiter and escape char may not be the same");
502 return 1;
503 }
504 if (strchr (LEX_SDELIM, delim_expr[1]) == NULL) {
505 yyerror(NULL, "invalid string delimiter -- %c (try one of %s)", delim_expr[1], LEX_SDELIM_RECOMMENDED);
506 return 1;
507 }
508
509 log_debug(__FILE__, LEXER, "YDELIMS: change_string_delimiters('%c', '%c')\n", delim_expr[0], delim_expr[1]);
510 extra->sesc = delim_expr[0];
511 extra->sdel = delim_expr[1];
512
513 return 0;
514 }
515
516
517 /* vim: set cindent sw=4: */