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