discover/grub2: Allow empty statements
[petitboot] / discover / grub2 / parser.y
1
2 %pure-parser
3 %lex-param { yyscan_t scanner }
4 %parse-param { struct grub2_parser *parser }
5 %error-verbose
6
7 %{
8 #include <talloc/talloc.h>
9
10 #include "grub2.h"
11 #include "parser.h"
12 #include "lexer.h"
13
14 #define YYLEX_PARAM parser->scanner
15
16 static void yyerror(struct grub2_parser *, char const *s);
17 %}
18
19 %union {
20         struct grub2_word       *word;
21         struct grub2_argv       *argv;
22         struct grub2_statement  *statement;
23         struct grub2_statements *statements;
24 }
25
26 /* reserved words */
27 %token  TOKEN_LDSQBRACKET       "[["
28 %token  TOKEN_RDSQBRACKET       "]]"
29 %token  TOKEN_CASE              "case"
30 %token  TOKEN_DO                "do"
31 %token  TOKEN_DONE              "done"
32 %token  TOKEN_ELIF              "elif"
33 %token  TOKEN_ELSE              "else"
34 %token  TOKEN_ESAC              "esac"
35 %token  TOKEN_FI                "fi"
36 %token  TOKEN_FOR               "for"
37 %token  TOKEN_FUNCTION          "function"
38 %token  TOKEN_IF                "if"
39 %token  TOKEN_IN                "in"
40 %token  TOKEN_MENUENTRY         "menuentry"
41 %token  TOKEN_SELECT            "select"
42 %token  TOKEN_SUBMENU           "submenu"
43 %token  TOKEN_THEN              "then"
44 %token  TOKEN_TIME              "time"
45 %token  TOKEN_UTIL              "until"
46 %token  TOKEN_WHILE             "while"
47
48 %type <statement>       statement
49 %type <statements>      statements
50 %type <argv>            words
51 %type <word>            word
52
53 /* syntax */
54 %token  TOKEN_EOL
55 %token  TOKEN_DELIM
56 %token  <word> TOKEN_WORD
57
58 %start  script
59 %debug
60
61 %%
62
63 script: statements {
64                 parser->script->statements = $1;
65         }
66
67 statements: /* empty */ {
68                 $$ = create_statements(parser);
69         }
70         | statements statement TOKEN_EOL {
71                 statement_append($1, $2);
72                 $$ = $1;
73         }
74         | statements TOKEN_EOL {
75                 $$ = $1;
76         }
77
78 statement:
79         words {
80                    $$ = create_statement_simple(parser, $1);
81         }
82         | '{' statements '}' {
83                 $$ = create_statement_block(parser, $2);
84         }
85         | "if" TOKEN_DELIM statement
86                 "then" TOKEN_EOL
87                 statements
88                 "fi" {
89                 $$ = create_statement_if(parser, $3, $6, NULL);
90         }
91         | "if" TOKEN_DELIM statement
92                 "then" TOKEN_EOL
93                 statements
94                 "else" TOKEN_EOL
95                 statements
96                 "fi" {
97                 $$ = create_statement_if(parser, $3, $6, $9);
98         }
99         | "function" TOKEN_DELIM word TOKEN_DELIM '{' statements '}' {
100                 $$ = create_statement_function(parser, $3, $6);
101         }
102         | "menuentry" TOKEN_DELIM words TOKEN_DELIM
103                 '{' statements '}' {
104                 $$ = create_statement_menuentry(parser, $3, $6);
105         }
106         | "submenu" TOKEN_DELIM words TOKEN_DELIM
107                 '{' statements '}' {
108                 /* we just flatten everything */
109                 $$ = create_statement_block(parser, $6);
110         }
111
112 words:  word {
113                 $$ = create_argv(parser);
114                 argv_append($$, $1);
115         }
116         | words TOKEN_DELIM word {
117                 argv_append($1, $3);
118                 $$ = $1;
119         }
120
121 word:   TOKEN_WORD
122         | word TOKEN_WORD {
123                 word_append($1, $2);
124                 $$ = $1;
125         }
126
127 %%
128 void yyerror(struct grub2_parser *parser, char const *s)
129 {
130         fprintf(stderr, "%d: error: %s '%s'\n",
131                         yyget_lineno(parser->scanner),
132                         s, yyget_text(parser->scanner));
133 }
134
135 struct grub2_statements *create_statements(struct grub2_parser *parser)
136 {
137         struct grub2_statements *stmts = talloc(parser,
138                         struct grub2_statements);
139         list_init(&stmts->list);
140         return stmts;
141 }
142
143 struct grub2_statement *create_statement_simple(struct grub2_parser *parser,
144                 struct grub2_argv *argv)
145 {
146         struct grub2_statement_simple *stmt =
147                 talloc(parser, struct grub2_statement_simple);
148         stmt->st.type = STMT_TYPE_SIMPLE;
149         stmt->st.exec = statement_simple_execute;
150         stmt->argv = argv;
151         return &stmt->st;
152 }
153
154 struct grub2_statement *create_statement_menuentry(struct grub2_parser *parser,
155                 struct grub2_argv *argv, struct grub2_statements *stmts)
156 {
157         struct grub2_statement_menuentry *stmt =
158                 talloc(parser, struct grub2_statement_menuentry);
159         stmt->st.type = STMT_TYPE_MENUENTRY;
160         stmt->st.exec = statement_menuentry_execute;
161         stmt->argv = argv;
162         stmt->statements = stmts;
163         return &stmt->st;
164 }
165
166 struct grub2_statement *create_statement_if(struct grub2_parser *parser,
167                 struct grub2_statement *condition,
168                 struct grub2_statements *true_case,
169                 struct grub2_statements *false_case)
170 {
171         struct grub2_statement_if *stmt =
172                 talloc(parser, struct grub2_statement_if);
173         stmt->st.type = STMT_TYPE_IF;
174         stmt->st.exec = statement_if_execute;
175         stmt->condition = condition;
176         stmt->true_case = true_case;
177         stmt->false_case = false_case;
178         return &stmt->st;
179 }
180
181 struct grub2_statement *create_statement_block(struct grub2_parser *parser,
182                 struct grub2_statements *stmts)
183 {
184         struct grub2_statement_block *stmt =
185                 talloc(parser, struct grub2_statement_block);
186         stmt->st.type = STMT_TYPE_BLOCK;
187         stmt->st.exec = NULL;
188         stmt->statements = stmts;
189         return &stmt->st;
190 }
191
192 struct grub2_statement *create_statement_function(struct grub2_parser *parser,
193                 struct grub2_word *name, struct grub2_statements *body)
194 {
195         struct grub2_statement_function *stmt =
196                 talloc(parser, struct grub2_statement_function);
197         stmt->st.exec = statement_function_execute;
198         stmt->name = name;
199         stmt->body = body;
200         return &stmt->st;
201 }
202
203 void statement_append(struct grub2_statements *stmts,
204                 struct grub2_statement *stmt)
205 {
206         if (!stmt)
207                 return;
208         list_add_tail(&stmts->list, &stmt->list);
209 }
210
211 struct grub2_word *create_word_text(struct grub2_parser *parser,
212                 const char *text)
213 {
214         struct grub2_word *word = talloc(parser, struct grub2_word);
215         word->type = GRUB2_WORD_TEXT;
216         word->split = false;
217         word->text = talloc_strdup(word, text);
218         word->next = NULL;
219         word->last = word;
220         return word;
221 }
222
223 struct grub2_word *create_word_var(struct grub2_parser *parser,
224                 const char *name, bool split)
225 {
226         struct grub2_word *word = talloc(parser, struct grub2_word);
227         word->type = GRUB2_WORD_VAR;
228         word->name = talloc_strdup(word, name);
229         word->split = split;
230         word->next = NULL;
231         word->last = word;
232         return word;
233 }
234
235 struct grub2_argv *create_argv(struct grub2_parser *parser)
236 {
237         struct grub2_argv *argv = talloc(parser, struct grub2_argv);
238         list_init(&argv->words);
239         return argv;
240 }
241
242 void argv_append(struct grub2_argv *argv, struct grub2_word *word)
243 {
244         list_add_tail(&argv->words, &word->argv_list);
245 }
246
247 void word_append(struct grub2_word *w1, struct grub2_word *w2)
248 {
249         w1->last->next = w2;
250         w1->last = w2;
251 }
252
253 struct grub2_parser *grub2_parser_create(struct discover_context *ctx)
254 {
255         struct grub2_parser *parser;
256
257         parser = talloc(ctx, struct grub2_parser);
258         yylex_init_extra(parser, &parser->scanner);
259         parser->script = create_script(parser, ctx);
260
261         return parser;
262 }
263
264 void grub2_parser_parse(struct grub2_parser *parser, char *buf, int len)
265 {
266         YY_BUFFER_STATE bufstate;
267         int rc;
268
269         bufstate = yy_scan_bytes(buf, len - 1, parser->scanner);
270
271         rc = yyparse(parser);
272
273         yy_delete_buffer(bufstate, parser->scanner);
274
275         if (!rc)
276                 script_execute(parser->script);
277 }
278