3 %lex-param { yyscan_t scanner }
4 %parse-param { struct grub2_parser *parser }
5 %parse-param { void *scanner }
9 #include <talloc/talloc.h>
14 void yyerror(struct grub2_parser *parser, const char *fmt, ...);
18 struct grub2_word *word;
19 struct grub2_argv *argv;
20 struct grub2_statement *statement;
21 struct grub2_statements *statements;
25 %token TOKEN_LDSQBRACKET "[["
26 %token TOKEN_RDSQBRACKET "]]"
27 %token TOKEN_CASE "case"
29 %token TOKEN_DONE "done"
30 %token TOKEN_ELIF "elif"
31 %token TOKEN_ELSE "else"
32 %token TOKEN_ESAC "esac"
34 %token TOKEN_FOR "for"
35 %token TOKEN_FUNCTION "function"
38 %token TOKEN_MENUENTRY "menuentry"
39 %token TOKEN_SELECT "select"
40 %token TOKEN_SUBMENU "submenu"
41 %token TOKEN_THEN "then"
42 %token TOKEN_TIME "time"
43 %token TOKEN_UTIL "until"
44 %token TOKEN_WHILE "while"
46 %type <statement> statement
47 %type <statements> statements
48 %type <statement> conditional
49 %type <statement> elif
50 %type <statements> elifs
57 %token <word> TOKEN_WORD
70 parser->script->statements = $1;
73 eol: TOKEN_EOL | TOKEN_EOF;
75 statements: /* empty */ {
76 $$ = create_statements(parser);
78 | statements statement eol {
79 statement_append($1, $2);
82 | statements TOKEN_EOL {
86 sep: TOKEN_DELIM | TOKEN_EOL;
88 conditional: statement TOKEN_EOL "then" sep statements {
89 $$ = create_statement_conditional(parser, $1, $5);
92 elif: "elif" TOKEN_DELIM conditional {
97 $$ = create_statements(parser);
100 statement_append($1, $2);
106 $$ = create_statement_simple(parser, $1);
108 | '{' statements '}' {
109 $$ = create_statement_block(parser, $2);
111 | "if" TOKEN_DELIM conditional elifs "fi" {
112 $$ = create_statement_if(parser, $3, $4, NULL);
114 | "if" TOKEN_DELIM conditional
119 $$ = create_statement_if(parser, $3, $4, $7);
121 | "function" TOKEN_DELIM word TOKEN_DELIM '{' statements '}' {
122 $$ = create_statement_function(parser, $3, $6);
124 | "menuentry" TOKEN_DELIM words TOKEN_DELIM
126 $$ = create_statement_menuentry(parser, $3, $6);
128 | "submenu" TOKEN_DELIM words TOKEN_DELIM
130 /* we just flatten everything */
131 $$ = create_statement_block(parser, $6);
135 $$ = create_argv(parser);
138 | words TOKEN_DELIM word {
150 void yyerror(struct grub2_parser *parser, const char *fmt, ...)
156 str = talloc_vasprintf(parser, fmt, ap);
159 pb_log("parse error: %d('%s'): %s\n", yyget_lineno(parser->scanner),
160 yyget_text(parser->scanner), str);
163 struct grub2_statements *create_statements(struct grub2_parser *parser)
165 struct grub2_statements *stmts = talloc(parser,
166 struct grub2_statements);
167 list_init(&stmts->list);
171 struct grub2_statement *create_statement_simple(struct grub2_parser *parser,
172 struct grub2_argv *argv)
174 struct grub2_statement_simple *stmt =
175 talloc(parser, struct grub2_statement_simple);
176 stmt->st.type = STMT_TYPE_SIMPLE;
177 stmt->st.exec = statement_simple_execute;
182 struct grub2_statement *create_statement_menuentry(struct grub2_parser *parser,
183 struct grub2_argv *argv, struct grub2_statements *stmts)
185 struct grub2_statement_menuentry *stmt =
186 talloc(parser, struct grub2_statement_menuentry);
187 stmt->st.type = STMT_TYPE_MENUENTRY;
188 stmt->st.exec = statement_menuentry_execute;
190 stmt->statements = stmts;
194 struct grub2_statement *create_statement_conditional(
195 struct grub2_parser *parser,
196 struct grub2_statement *condition,
197 struct grub2_statements *statements)
199 struct grub2_statement_conditional *stmt =
200 talloc(parser, struct grub2_statement_conditional);
201 stmt->st.type = STMT_TYPE_CONDITIONAL;
202 stmt->condition = condition;
203 stmt->statements = statements;
207 struct grub2_statement *create_statement_if(struct grub2_parser *parser,
208 struct grub2_statement *conditional,
209 struct grub2_statements *elifs,
210 struct grub2_statements *else_case)
212 struct grub2_statement_if *stmt =
213 talloc(parser, struct grub2_statement_if);
215 list_add(&elifs->list, &conditional->list);
217 stmt->st.type = STMT_TYPE_IF;
218 stmt->st.exec = statement_if_execute;
219 stmt->conditionals = elifs;
220 stmt->else_case = else_case;
224 struct grub2_statement *create_statement_block(struct grub2_parser *parser,
225 struct grub2_statements *stmts)
227 struct grub2_statement_block *stmt =
228 talloc(parser, struct grub2_statement_block);
229 stmt->st.type = STMT_TYPE_BLOCK;
230 stmt->st.exec = statement_block_execute;
231 stmt->statements = stmts;
235 struct grub2_statement *create_statement_function(struct grub2_parser *parser,
236 struct grub2_word *name, struct grub2_statements *body)
238 struct grub2_statement_function *stmt =
239 talloc(parser, struct grub2_statement_function);
240 stmt->st.exec = statement_function_execute;
246 void statement_append(struct grub2_statements *stmts,
247 struct grub2_statement *stmt)
251 list_add_tail(&stmts->list, &stmt->list);
254 struct grub2_word *create_word_text(struct grub2_parser *parser,
257 struct grub2_word *word = talloc(parser, struct grub2_word);
258 word->type = GRUB2_WORD_TEXT;
260 word->text = talloc_strdup(word, text);
266 struct grub2_word *create_word_var(struct grub2_parser *parser,
267 const char *name, bool split)
269 struct grub2_word *word = talloc(parser, struct grub2_word);
270 word->type = GRUB2_WORD_VAR;
271 word->name = talloc_strdup(word, name);
278 struct grub2_argv *create_argv(struct grub2_parser *parser)
280 struct grub2_argv *argv = talloc(parser, struct grub2_argv);
281 list_init(&argv->words);
285 void argv_append(struct grub2_argv *argv, struct grub2_word *word)
287 list_add_tail(&argv->words, &word->argv_list);
290 void word_append(struct grub2_word *w1, struct grub2_word *w2)
296 struct grub2_parser *grub2_parser_create(struct discover_context *ctx)
298 struct grub2_parser *parser;
300 parser = talloc(ctx, struct grub2_parser);
301 yylex_init_extra(parser, &parser->scanner);
302 parser->script = create_script(parser, ctx);
307 void grub2_parser_parse(struct grub2_parser *parser, const char *filename,
310 YY_BUFFER_STATE bufstate;
313 parser->script->filename = filename;
315 bufstate = yy_scan_bytes(buf, len - 1, parser->scanner);
316 yyset_lineno(1, parser->scanner);
318 rc = yyparse(parser, parser->scanner);
320 yy_delete_buffer(bufstate, parser->scanner);
323 script_execute(parser->script);