From 45203582ff8708042c5f4f7134ad7d4ec71d2050 Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 14 Jun 2019 12:04:11 +0000 Subject: A couple of minor parser changes around conditions: 1) only treat #{ specially after a condition, otherwise as a comment (which is more as most people expect) 2) allow formats to be quoted after a condition. --- cmd-parse.y | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'cmd-parse.y') diff --git a/cmd-parse.y b/cmd-parse.y index a7c12f62..1a6af3e7 100644 --- a/cmd-parse.y +++ b/cmd-parse.y @@ -59,6 +59,7 @@ struct cmd_parse_state { size_t len; size_t off; + int condition; int eol; int eof; struct cmd_parse_input *input; @@ -104,7 +105,7 @@ static void cmd_parse_print_commands(struct cmd_parse_input *, u_int, %token ENDIF %token FORMAT TOKEN EQUALS -%type argument expanded +%type argument expanded format %type arguments %type if_open if_elif %type elif elif1 @@ -160,7 +161,16 @@ statement : condition } } -expanded : FORMAT +format : FORMAT + { + $$ = $1; + } + | TOKEN + { + $$ = $1; + } + +expanded : format { struct cmd_parse_state *ps = &parse_state; struct cmd_parse_input *pi = ps->input; @@ -967,12 +977,15 @@ yylex(void) { struct cmd_parse_state *ps = &parse_state; char *token, *cp; - int ch, next; + int ch, next, condition; if (ps->eol) ps->input->line++; ps->eol = 0; + condition = ps->condition; + ps->condition = 0; + for (;;) { ch = yylex_getc(); @@ -1012,11 +1025,11 @@ yylex(void) if (ch == '#') { /* - * #{ opens a format; anything else is a comment, - * ignore up to the end of the line. + * #{ after a condition opens a format; anything else + * is a comment, ignore up to the end of the line. */ next = yylex_getc(); - if (next == '{') { + if (condition && next == '{') { yylval.token = yylex_format(); if (yylval.token == NULL) return (ERROR); @@ -1043,6 +1056,7 @@ yylex(void) } if (*cp == '\0') return (TOKEN); + ps->condition = 1; if (strcmp(yylval.token, "%if") == 0) { free(yylval.token); return (IF); -- cgit v1.2.3