From: Jeremy Kerr Date: Tue, 10 Sep 2013 06:04:41 +0000 (+0800) Subject: discover/grub2: Implement parser X-Git-Tag: v1.0.0~477 X-Git-Url: https://git.ozlabs.org/?p=petitboot;a=commitdiff_plain;h=2013dd8075a57e5399f080fa8094cc4b464f35cd discover/grub2: Implement parser Add our simple parser to handle a grub2 script. Since we're not building a parse tree at the moment (we have no reduce rules), we just have a simple word-based %union for our tokens. Signed-off-by: Jeremy Kerr --- diff --git a/discover/grub2/parser.y b/discover/grub2/parser.y index 2af2132..dbdde51 100644 --- a/discover/grub2/parser.y +++ b/discover/grub2/parser.y @@ -4,7 +4,13 @@ %parse-param { struct grub2_parser *parser } %{ +#include "grub2.h" +#include "parser.h" #include "lexer.h" + +#define YYLEX_PARAM parser->scanner + +static void yyerror(struct grub2_parser *, char const *s); %} %union { @@ -41,9 +47,41 @@ %token TOKEN_WORD %start script +%debug %% -script: /* empty */ +script: statements + ; + +statements: statement + | statements statement + ; + +statement: TOKEN_EOL + | words TOKEN_EOL + | '{' statements '}' + | "if" TOKEN_DELIM statement + "then" TOKEN_EOL + statements + "fi" TOKEN_EOL + | "menuentry" TOKEN_DELIM words TOKEN_DELIM + '{' statements '}' + TOKEN_EOL + ; + +words: | word + | words TOKEN_DELIM word + ; + +word: TOKEN_WORD + | word TOKEN_WORD + ; %% +void yyerror(struct grub2_parser *parser, char const *s) +{ + fprintf(stderr, "%d: error: %s '%s'\n", + yyget_lineno(parser->scanner), + s, yyget_text(parser->scanner)); +}