]> git.ozlabs.org Git - petitboot/blob - discover/grub2/parser.y
discover/grub2: Implement submenu
[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: statement {
68                 $$ = create_statements(parser);
69                 statement_append($$, $1);
70         }
71         | statements statement {
72                 statement_append($1, $2);
73                 $$ = $1;
74         }
75
76 statement: TOKEN_EOL {
77                 $$ = NULL;
78         }
79         | words TOKEN_EOL {
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" TOKEN_EOL {
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" TOKEN_EOL {
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                 TOKEN_EOL {
105                 $$ = create_statement_menuentry(parser, $3, $6);
106         }
107         | "submenu" TOKEN_DELIM words TOKEN_DELIM
108                 '{' statements '}'
109                 TOKEN_EOL {
110                 /* we just flatten everything */
111                 $$ = create_statement_block(parser, $6);
112         }
113
114 words:  word {
115                 $$ = create_argv(parser);
116                 argv_append($$, $1);
117         }
118         | words TOKEN_DELIM word {
119                 argv_append($1, $3);
120                 $$ = $1;
121         }
122
123 word:   TOKEN_WORD
124         | word TOKEN_WORD {
125                 word_append($1, $2);
126                 $$ = $1;
127         }
128
129 %%
130 void yyerror(struct grub2_parser *parser, char const *s)
131 {
132         fprintf(stderr, "%d: error: %s '%s'\n",
133                         yyget_lineno(parser->scanner),
134                         s, yyget_text(parser->scanner));
135 }
136
137 struct grub2_statements *create_statements(struct grub2_parser *parser)
138 {
139         struct grub2_statements *stmts = talloc(parser,
140                         struct grub2_statements);
141         list_init(&stmts->list);
142         return stmts;
143 }
144
145 struct grub2_statement *create_statement_simple(struct grub2_parser *parser,
146                 struct grub2_argv *argv)
147 {
148         struct grub2_statement_simple *stmt =
149                 talloc(parser, struct grub2_statement_simple);
150         stmt->st.type = STMT_TYPE_SIMPLE;
151         stmt->st.exec = statement_simple_execute;
152         stmt->argv = argv;
153         return &stmt->st;
154 }
155
156 struct grub2_statement *create_statement_menuentry(struct grub2_parser *parser,
157                 struct grub2_argv *argv, struct grub2_statements *stmts)
158 {
159         struct grub2_statement_menuentry *stmt =
160                 talloc(parser, struct grub2_statement_menuentry);
161         stmt->st.type = STMT_TYPE_MENUENTRY;
162         stmt->st.exec = statement_menuentry_execute;
163         stmt->argv = argv;
164         stmt->statements = stmts;
165         return &stmt->st;
166 }
167
168 struct grub2_statement *create_statement_if(struct grub2_parser *parser,
169                 struct grub2_statement *condition,
170                 struct grub2_statements *true_case,
171                 struct grub2_statements *false_case)
172 {
173         struct grub2_statement_if *stmt =
174                 talloc(parser, struct grub2_statement_if);
175         stmt->st.type = STMT_TYPE_IF;
176         stmt->st.exec = statement_if_execute;
177         stmt->condition = condition;
178         stmt->true_case = true_case;
179         stmt->false_case = false_case;
180         return &stmt->st;
181 }
182
183 struct grub2_statement *create_statement_block(struct grub2_parser *parser,
184                 struct grub2_statements *stmts)
185 {
186         struct grub2_statement_block *stmt =
187                 talloc(parser, struct grub2_statement_block);
188         stmt->st.type = STMT_TYPE_BLOCK;
189         stmt->st.exec = NULL;
190         stmt->statements = stmts;
191         return &stmt->st;
192 }
193
194 struct grub2_statement *create_statement_function(struct grub2_parser *parser,
195                 struct grub2_word *name, struct grub2_statements *body)
196 {
197         struct grub2_statement_function *stmt =
198                 talloc(parser, struct grub2_statement_function);
199         stmt->st.exec = statement_function_execute;
200         stmt->name = name;
201         stmt->body = body;
202         return &stmt->st;
203 }
204
205 void statement_append(struct grub2_statements *stmts,
206                 struct grub2_statement *stmt)
207 {
208         if (!stmt)
209                 return;
210         list_add_tail(&stmts->list, &stmt->list);
211 }
212
213 struct grub2_word *create_word_text(struct grub2_parser *parser,
214                 const char *text)
215 {
216         struct grub2_word *word = talloc(parser, struct grub2_word);
217         word->type = GRUB2_WORD_TEXT;
218         word->split = false;
219         word->text = talloc_strdup(word, text);
220         word->next = NULL;
221         word->last = word;
222         return word;
223 }
224
225 struct grub2_word *create_word_var(struct grub2_parser *parser,
226                 const char *name, bool split)
227 {
228         struct grub2_word *word = talloc(parser, struct grub2_word);
229         word->type = GRUB2_WORD_VAR;
230         word->name = talloc_strdup(word, name);
231         word->split = split;
232         word->next = NULL;
233         word->last = word;
234         return word;
235 }
236
237 struct grub2_argv *create_argv(struct grub2_parser *parser)
238 {
239         struct grub2_argv *argv = talloc(parser, struct grub2_argv);
240         list_init(&argv->words);
241         return argv;
242 }
243
244 void argv_append(struct grub2_argv *argv, struct grub2_word *word)
245 {
246         list_add_tail(&argv->words, &word->argv_list);
247 }
248
249 void word_append(struct grub2_word *w1, struct grub2_word *w2)
250 {
251         w1->last->next = w2;
252         w1->last = w2;
253 }
254
255 struct grub2_parser *grub2_parser_create(struct discover_context *ctx)
256 {
257         struct grub2_parser *parser;
258
259         parser = talloc(ctx, struct grub2_parser);
260         yylex_init_extra(parser, &parser->scanner);
261         parser->script = create_script(parser, ctx);
262
263         return parser;
264 }
265
266 void grub2_parser_parse(struct grub2_parser *parser, char *buf, int len)
267 {
268         YY_BUFFER_STATE bufstate;
269         int rc;
270
271         bufstate = yy_scan_bytes(buf, len - 1, parser->scanner);
272
273         rc = yyparse(parser);
274
275         yy_delete_buffer(bufstate, parser->scanner);
276
277         if (!rc)
278                 script_execute(parser->script);
279 }
280