discover/grub2: Create 'conditional' 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 <statement>       conditional
51 %type <argv>            words
52 %type <word>            word
53
54 /* syntax */
55 %token  TOKEN_EOL
56 %token  TOKEN_DELIM
57 %token  <word> TOKEN_WORD
58
59 %start  script
60 %debug
61
62 %%
63
64 script: statements {
65                 parser->script->statements = $1;
66         }
67
68 statements: /* empty */ {
69                 $$ = create_statements(parser);
70         }
71         | statements statement TOKEN_EOL {
72                 statement_append($1, $2);
73                 $$ = $1;
74         }
75         | statements TOKEN_EOL {
76                 $$ = $1;
77         }
78
79 conditional: statement TOKEN_EOL "then" TOKEN_EOL statements {
80                 $$ = create_statement_conditional(parser, $1, $5);
81         }
82
83 statement:
84         words {
85                    $$ = create_statement_simple(parser, $1);
86         }
87         | '{' statements '}' {
88                 $$ = create_statement_block(parser, $2);
89         }
90         | "if" TOKEN_DELIM conditional "fi" {
91                 $$ = create_statement_if(parser, $3, NULL);
92         }
93         | "if" TOKEN_DELIM conditional
94                 "else" TOKEN_EOL
95                 statements
96                 "fi" {
97                 $$ = create_statement_if(parser, $3, $6);
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_conditional(
167                 struct grub2_parser *parser,
168                 struct grub2_statement *condition,
169                 struct grub2_statements *statements)
170 {
171         struct grub2_statement_conditional *stmt =
172                 talloc(parser, struct grub2_statement_conditional);
173         stmt->st.type = STMT_TYPE_CONDITIONAL;
174         stmt->condition = condition;
175         stmt->statements = statements;
176         return &stmt->st;
177 }
178
179 struct grub2_statement *create_statement_if(struct grub2_parser *parser,
180                 struct grub2_statement *conditional,
181                 struct grub2_statements *else_case)
182 {
183         struct grub2_statement_if *stmt =
184                 talloc(parser, struct grub2_statement_if);
185         stmt->st.type = STMT_TYPE_IF;
186         stmt->st.exec = statement_if_execute;
187         stmt->conditional = conditional;
188         stmt->else_case = else_case;
189         return &stmt->st;
190 }
191
192 struct grub2_statement *create_statement_block(struct grub2_parser *parser,
193                 struct grub2_statements *stmts)
194 {
195         struct grub2_statement_block *stmt =
196                 talloc(parser, struct grub2_statement_block);
197         stmt->st.type = STMT_TYPE_BLOCK;
198         stmt->st.exec = NULL;
199         stmt->statements = stmts;
200         return &stmt->st;
201 }
202
203 struct grub2_statement *create_statement_function(struct grub2_parser *parser,
204                 struct grub2_word *name, struct grub2_statements *body)
205 {
206         struct grub2_statement_function *stmt =
207                 talloc(parser, struct grub2_statement_function);
208         stmt->st.exec = statement_function_execute;
209         stmt->name = name;
210         stmt->body = body;
211         return &stmt->st;
212 }
213
214 void statement_append(struct grub2_statements *stmts,
215                 struct grub2_statement *stmt)
216 {
217         if (!stmt)
218                 return;
219         list_add_tail(&stmts->list, &stmt->list);
220 }
221
222 struct grub2_word *create_word_text(struct grub2_parser *parser,
223                 const char *text)
224 {
225         struct grub2_word *word = talloc(parser, struct grub2_word);
226         word->type = GRUB2_WORD_TEXT;
227         word->split = false;
228         word->text = talloc_strdup(word, text);
229         word->next = NULL;
230         word->last = word;
231         return word;
232 }
233
234 struct grub2_word *create_word_var(struct grub2_parser *parser,
235                 const char *name, bool split)
236 {
237         struct grub2_word *word = talloc(parser, struct grub2_word);
238         word->type = GRUB2_WORD_VAR;
239         word->name = talloc_strdup(word, name);
240         word->split = split;
241         word->next = NULL;
242         word->last = word;
243         return word;
244 }
245
246 struct grub2_argv *create_argv(struct grub2_parser *parser)
247 {
248         struct grub2_argv *argv = talloc(parser, struct grub2_argv);
249         list_init(&argv->words);
250         return argv;
251 }
252
253 void argv_append(struct grub2_argv *argv, struct grub2_word *word)
254 {
255         list_add_tail(&argv->words, &word->argv_list);
256 }
257
258 void word_append(struct grub2_word *w1, struct grub2_word *w2)
259 {
260         w1->last->next = w2;
261         w1->last = w2;
262 }
263
264 struct grub2_parser *grub2_parser_create(struct discover_context *ctx)
265 {
266         struct grub2_parser *parser;
267
268         parser = talloc(ctx, struct grub2_parser);
269         yylex_init_extra(parser, &parser->scanner);
270         parser->script = create_script(parser, ctx);
271
272         return parser;
273 }
274
275 void grub2_parser_parse(struct grub2_parser *parser, char *buf, int len)
276 {
277         YY_BUFFER_STATE bufstate;
278         int rc;
279
280         bufstate = yy_scan_bytes(buf, len - 1, parser->scanner);
281
282         rc = yyparse(parser);
283
284         yy_delete_buffer(bufstate, parser->scanner);
285
286         if (!rc)
287                 script_execute(parser->script);
288 }
289