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