]> git.ozlabs.org Git - petitboot/commitdiff
discover/grub2: Implement parser
authorJeremy Kerr <jk@ozlabs.org>
Tue, 10 Sep 2013 06:04:41 +0000 (14:04 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Tue, 24 Sep 2013 05:14:59 +0000 (13:14 +0800)
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 <jk@ozlabs.org>
discover/grub2/parser.y

index 2af213218070a0be6cac707b5e51e8fd9a569785..dbdde51fe57fbc7377de1c7132f97d831aeee4e6 100644 (file)
@@ -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 {
 %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));
+}