discover/grub2: Implement 'elif'
[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 <statement>       conditional
51 %type <statement>       elif
52 %type <statements>      elifs
53 %type <argv>            words
54 %type <word>            word
55
56 /* syntax */
57 %token  TOKEN_EOL
58 %token  TOKEN_DELIM
59 %token  <word> TOKEN_WORD
60
61 %start  script
62 %debug
63
64 %%
65
66 script: statements {
67                 parser->script->statements = $1;
68         }
69
70 statements: /* empty */ {
71                 $$ = create_statements(parser);
72         }
73         | statements statement TOKEN_EOL {
74                 statement_append($1, $2);
75                 $$ = $1;
76         }
77         | statements TOKEN_EOL {
78                 $$ = $1;
79         }
80
81 conditional: statement TOKEN_EOL "then" TOKEN_EOL statements {
82                 $$ = create_statement_conditional(parser, $1, $5);
83         }
84
85 elif: "elif" TOKEN_DELIM conditional {
86                 $$ = $3;
87       }
88
89 elifs: /* empty */ {
90                 $$ = create_statements(parser);
91         }
92         | elifs elif {
93                 statement_append($1, $2);
94                 $$ = $1;
95         }
96
97 statement:
98         words {
99                    $$ = create_statement_simple(parser, $1);
100         }
101         | '{' statements '}' {
102                 $$ = create_statement_block(parser, $2);
103         }
104         | "if" TOKEN_DELIM conditional elifs "fi" {
105                 $$ = create_statement_if(parser, $3, $4, NULL);
106         }
107         | "if" TOKEN_DELIM conditional
108                 elifs
109                 "else" TOKEN_EOL
110                 statements
111                 "fi" {
112                 $$ = create_statement_if(parser, $3, $4, $7);
113         }
114         | "function" TOKEN_DELIM word TOKEN_DELIM '{' statements '}' {
115                 $$ = create_statement_function(parser, $3, $6);
116         }
117         | "menuentry" TOKEN_DELIM words TOKEN_DELIM
118                 '{' statements '}' {
119                 $$ = create_statement_menuentry(parser, $3, $6);
120         }
121         | "submenu" TOKEN_DELIM words TOKEN_DELIM
122                 '{' statements '}' {
123                 /* we just flatten everything */
124                 $$ = create_statement_block(parser, $6);
125         }
126
127 words:  word {
128                 $$ = create_argv(parser);
129                 argv_append($$, $1);
130         }
131         | words TOKEN_DELIM word {
132                 argv_append($1, $3);
133                 $$ = $1;
134         }
135
136 word:   TOKEN_WORD
137         | word TOKEN_WORD {
138                 word_append($1, $2);
139                 $$ = $1;
140         }
141
142 %%
143 void yyerror(struct grub2_parser *parser, char const *s)
144 {
145         fprintf(stderr, "%d: error: %s '%s'\n",
146                         yyget_lineno(parser->scanner),
147                         s, yyget_text(parser->scanner));
148 }
149
150 struct grub2_statements *create_statements(struct grub2_parser *parser)
151 {
152         struct grub2_statements *stmts = talloc(parser,
153                         struct grub2_statements);
154         list_init(&stmts->list);
155         return stmts;
156 }
157
158 struct grub2_statement *create_statement_simple(struct grub2_parser *parser,
159                 struct grub2_argv *argv)
160 {
161         struct grub2_statement_simple *stmt =
162                 talloc(parser, struct grub2_statement_simple);
163         stmt->st.type = STMT_TYPE_SIMPLE;
164         stmt->st.exec = statement_simple_execute;
165         stmt->argv = argv;
166         return &stmt->st;
167 }
168
169 struct grub2_statement *create_statement_menuentry(struct grub2_parser *parser,
170                 struct grub2_argv *argv, struct grub2_statements *stmts)
171 {
172         struct grub2_statement_menuentry *stmt =
173                 talloc(parser, struct grub2_statement_menuentry);
174         stmt->st.type = STMT_TYPE_MENUENTRY;
175         stmt->st.exec = statement_menuentry_execute;
176         stmt->argv = argv;
177         stmt->statements = stmts;
178         return &stmt->st;
179 }
180
181 struct grub2_statement *create_statement_conditional(
182                 struct grub2_parser *parser,
183                 struct grub2_statement *condition,
184                 struct grub2_statements *statements)
185 {
186         struct grub2_statement_conditional *stmt =
187                 talloc(parser, struct grub2_statement_conditional);
188         stmt->st.type = STMT_TYPE_CONDITIONAL;
189         stmt->condition = condition;
190         stmt->statements = statements;
191         return &stmt->st;
192 }
193
194 struct grub2_statement *create_statement_if(struct grub2_parser *parser,
195                 struct grub2_statement *conditional,
196                 struct grub2_statements *elifs,
197                 struct grub2_statements *else_case)
198 {
199         struct grub2_statement_if *stmt =
200                 talloc(parser, struct grub2_statement_if);
201
202         list_add(&elifs->list, &conditional->list);
203
204         stmt->st.type = STMT_TYPE_IF;
205         stmt->st.exec = statement_if_execute;
206         stmt->conditionals = elifs;
207         stmt->else_case = else_case;
208         return &stmt->st;
209 }
210
211 struct grub2_statement *create_statement_block(struct grub2_parser *parser,
212                 struct grub2_statements *stmts)
213 {
214         struct grub2_statement_block *stmt =
215                 talloc(parser, struct grub2_statement_block);
216         stmt->st.type = STMT_TYPE_BLOCK;
217         stmt->st.exec = NULL;
218         stmt->statements = stmts;
219         return &stmt->st;
220 }
221
222 struct grub2_statement *create_statement_function(struct grub2_parser *parser,
223                 struct grub2_word *name, struct grub2_statements *body)
224 {
225         struct grub2_statement_function *stmt =
226                 talloc(parser, struct grub2_statement_function);
227         stmt->st.exec = statement_function_execute;
228         stmt->name = name;
229         stmt->body = body;
230         return &stmt->st;
231 }
232
233 void statement_append(struct grub2_statements *stmts,
234                 struct grub2_statement *stmt)
235 {
236         if (!stmt)
237                 return;
238         list_add_tail(&stmts->list, &stmt->list);
239 }
240
241 struct grub2_word *create_word_text(struct grub2_parser *parser,
242                 const char *text)
243 {
244         struct grub2_word *word = talloc(parser, struct grub2_word);
245         word->type = GRUB2_WORD_TEXT;
246         word->split = false;
247         word->text = talloc_strdup(word, text);
248         word->next = NULL;
249         word->last = word;
250         return word;
251 }
252
253 struct grub2_word *create_word_var(struct grub2_parser *parser,
254                 const char *name, bool split)
255 {
256         struct grub2_word *word = talloc(parser, struct grub2_word);
257         word->type = GRUB2_WORD_VAR;
258         word->name = talloc_strdup(word, name);
259         word->split = split;
260         word->next = NULL;
261         word->last = word;
262         return word;
263 }
264
265 struct grub2_argv *create_argv(struct grub2_parser *parser)
266 {
267         struct grub2_argv *argv = talloc(parser, struct grub2_argv);
268         list_init(&argv->words);
269         return argv;
270 }
271
272 void argv_append(struct grub2_argv *argv, struct grub2_word *word)
273 {
274         list_add_tail(&argv->words, &word->argv_list);
275 }
276
277 void word_append(struct grub2_word *w1, struct grub2_word *w2)
278 {
279         w1->last->next = w2;
280         w1->last = w2;
281 }
282
283 struct grub2_parser *grub2_parser_create(struct discover_context *ctx)
284 {
285         struct grub2_parser *parser;
286
287         parser = talloc(ctx, struct grub2_parser);
288         yylex_init_extra(parser, &parser->scanner);
289         parser->script = create_script(parser, ctx);
290
291         return parser;
292 }
293
294 void grub2_parser_parse(struct grub2_parser *parser, char *buf, int len)
295 {
296         YY_BUFFER_STATE bufstate;
297         int rc;
298
299         bufstate = yy_scan_bytes(buf, len - 1, parser->scanner);
300
301         rc = yyparse(parser);
302
303         yy_delete_buffer(bufstate, parser->scanner);
304
305         if (!rc)
306                 script_execute(parser->script);
307 }
308