discover/grub2/lexer: Require 1+ spaces for a TOKEN_DELIM
[petitboot] / discover / grub2 / lexer.l
1
2 %{
3 #include "grub2.h"
4 #include "parser.h"
5 #include <talloc/talloc.h>
6 %}
7
8 %option nounput noinput
9 %option batch never-interactive
10 %option warn
11 %option noyywrap
12 %option stack noyy_top_state
13 %option reentrant
14 %option bison-bridge
15 %option yylineno
16 %option noyyalloc noyyfree noyyrealloc
17 %option extra-type="struct grub2_parser *"
18 %option header-file="lexer.h"
19 %option outfile="lexer.c"
20
21 %x sqstring
22 %x dqstring
23
24 WORD    [^{}|&$;<> \t\n'"#]+
25 VARNAME ([[:alpha:]][_[:alnum:]]*|[0-9]|[\?@\*#])
26
27 %%
28
29  /* discard leading & trailing whitespace, but keep inter-word delimeters */
30 ^[ \t]+ ;
31 [ \t]+$ ;
32 [ \t]+  return TOKEN_DELIM;
33
34  /* reserved words */
35 "[["         return TOKEN_LDSQBRACKET;
36 "]]"         return TOKEN_RDSQBRACKET;
37 "case"       return TOKEN_CASE;
38 "do"         return TOKEN_DO;
39 "done"       return TOKEN_DONE;
40 "elif"       return TOKEN_ELIF;
41 "else"       return TOKEN_ELSE;
42 "esac"       return TOKEN_ESAC;
43 "fi"         return TOKEN_FI;
44 "for"        return TOKEN_FOR;
45 "function"   return TOKEN_FUNCTION;
46 "if"         return TOKEN_IF;
47 "in"         return TOKEN_IN;
48 "menuentry"  return TOKEN_MENUENTRY;
49 "select"     return TOKEN_SELECT;
50 "submenu"    return TOKEN_SUBMENU;
51 "then"       return TOKEN_THEN;
52 "time"       return TOKEN_TIME;
53 "until"      return TOKEN_UTIL;
54 "while"      return TOKEN_WHILE;
55
56  /* anything that's not a metachar: return as a plain word */
57 {WORD}  {
58                 yylval->word = create_word_text(yyget_extra(yyscanner), yytext);
59                 return TOKEN_WORD;
60         }
61
62 \${VARNAME} |
63 \$\{{VARNAME}\} {
64                 if (yytext[1] == '{') {
65                         yytext[yyleng-1] = '\0';
66                         yytext++;
67                 }
68                 yytext++;
69                 yylval->word = create_word_var(yyget_extra(yyscanner), yytext,
70                                                 true);
71                 return TOKEN_WORD;
72         }
73
74  /* single-quoted strings: return a single, non-expanded word token */
75 \'      {
76                 yy_push_state(sqstring, yyscanner);
77         }
78 <sqstring>\' {
79                 yy_pop_state(yyscanner);
80         }
81 <sqstring>[^']+ {
82                 yylval->word = create_word_text(yyget_extra(yyscanner), yytext);
83                 return TOKEN_WORD;
84         }
85
86  /* double-quoted strings: return a single, expanded word token */
87 \"      {
88                 yy_push_state(dqstring, yyscanner);
89         }
90 <dqstring>\" {
91                 yy_pop_state(yyscanner);
92         }
93 <dqstring>([^"\$]|\\\")+ {
94                 yylval->word = create_word_text(yyget_extra(yyscanner), yytext);
95                 return TOKEN_WORD;
96         }
97 <dqstring>\${VARNAME} |
98 <dqstring>\$\{{VARNAME}\} {
99                 if (yytext[1] == '{') {
100                         yytext[yyleng-1] = '\0';
101                         yytext++;
102                 }
103                 yytext++;
104                 yylval->word = create_word_var(yyget_extra(yyscanner), yytext,
105                                                 false);
106                 return TOKEN_WORD;
107         }
108
109
110
111  /* blocks */
112 "{"     return '{';
113 "}"     return '}';
114
115  /* end-of-line */
116 [ \t]*(;|\n)[ \t]*      return TOKEN_EOL;
117
118  /* strip comments */
119 #.*     ;
120
121 .       printf("unknown token '%s'\n", yytext); exit(1);
122
123 %%
124
125 struct grub2_parser;
126
127 void *yyalloc(size_t bytes, void *yyscanner)
128 {
129         struct grub2_parser *parser = yyget_extra(yyscanner);
130         return talloc_size(parser, bytes);
131 }
132
133 void *yyrealloc(void *ptr, size_t bytes, void *yyscanner)
134 {
135         struct grub2_parser *parser = yyget_extra(yyscanner);
136         return talloc_realloc_size(parser, ptr, bytes);
137 }
138
139 void yyfree(void *ptr, void *yyscanner __attribute__((unused)))
140 {
141         talloc_free(ptr);
142 }