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