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