automake: silence make output
[petitboot] / discover / grub2 / lexer.l
1
2 %{
3 #include "grub2.h"
4 #include "parser.h"
5 #include <talloc/talloc.h>
6
7 void yyerror(struct grub2_parser *parser, const char *fmt, ...);
8 %}
9
10 %option nounput noinput
11 %option batch never-interactive
12 %option warn
13 %option noyywrap
14 %option stack noyy_top_state
15 %option reentrant
16 %option bison-bridge
17 %option yylineno
18 %option noyyalloc noyyfree noyyrealloc
19 %option extra-type="struct grub2_parser *"
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 .       {
122                 yyerror(yyget_extra(yyscanner), "unknown token '%s'\n", yytext);
123                 yyterminate();
124         }
125
126 %%
127
128 struct grub2_parser;
129
130 void *yyalloc(size_t bytes, void *yyscanner)
131 {
132         struct grub2_parser *parser = yyget_extra(yyscanner);
133         return talloc_size(parser, bytes);
134 }
135
136 void *yyrealloc(void *ptr, size_t bytes, void *yyscanner)
137 {
138         struct grub2_parser *parser = yyget_extra(yyscanner);
139         return talloc_realloc_size(parser, ptr, bytes);
140 }
141
142 void yyfree(void *ptr, void *yyscanner __attribute__((unused)))
143 {
144         talloc_free(ptr);
145 }