eb61f556491c23c503caea2958fa29a408cd866a
[petitboot] / discover / grub2 / parser.y
1
2 %pure-parser
3 %lex-param { yyscan_t scanner }
4 %parse-param { struct grub2_parser *parser }
5
6 %{
7 #include "grub2.h"
8 #include "parser.h"
9 #include "lexer.h"
10
11 #define YYLEX_PARAM parser->scanner
12
13 static void yyerror(struct grub2_parser *, char const *s);
14 %}
15
16 %union {
17         struct grub2_word       *word;
18         struct grub2_argv       *argv;
19         struct grub2_statement  *statement;
20         struct grub2_statements *statements;
21 }
22
23 /* reserved words */
24 %token  TOKEN_LDSQBRACKET       "[["
25 %token  TOKEN_RDSQBRACKET       "]]"
26 %token  TOKEN_CASE              "case"
27 %token  TOKEN_DO                "do"
28 %token  TOKEN_DONE              "done"
29 %token  TOKEN_ELIF              "elif"
30 %token  TOKEN_ESAC              "esac"
31 %token  TOKEN_FI                "fi"
32 %token  TOKEN_FOR               "for"
33 %token  TOKEN_FUNCTION          "function"
34 %token  TOKEN_IF                "if"
35 %token  TOKEN_IN                "in"
36 %token  TOKEN_MENUENTRY         "menuentry"
37 %token  TOKEN_SELECT            "select"
38 %token  TOKEN_THEN              "then"
39 %token  TOKEN_TIME              "time"
40 %token  TOKEN_UTIL              "until"
41 %token  TOKEN_WHILE             "while"
42
43 %type <statement>       statement
44 %type <statements>      statements
45 %type <argv>            words
46 %type <word>            word
47
48 /* syntax */
49 %token  TOKEN_EOL
50 %token  TOKEN_DELIM
51 %token  <word> TOKEN_WORD
52
53 %start  script
54 %debug
55
56 %%
57
58 script: statements {
59                 parser->script->statements = $1;
60         }
61
62 statements: statement {
63                 $$ = create_statements(parser);
64                 statement_append($$, $1);
65         }
66         | statements statement {
67                 statement_append($1, $2);
68                 $$ = $1;
69         }
70
71 statement: TOKEN_EOL {
72                 $$ = NULL;
73         }
74         | words TOKEN_EOL {
75                    $$ = create_statement_simple(parser, $1);
76         }
77         | '{' statements '}' {
78                 $$ = create_statement_block(parser, $2);
79         }
80         | "if" TOKEN_DELIM statement
81                 "then" TOKEN_EOL
82                 statements
83                 "fi" TOKEN_EOL {
84                 $$ = create_statement_if(parser, $3, $6, NULL);
85         }
86         | "menuentry" TOKEN_DELIM words TOKEN_DELIM
87                 '{' statements '}'
88                 TOKEN_EOL {
89                 $$ = create_statement_menuentry(parser, $3, $6);
90         }
91
92 words:  word {
93                 $$ = create_argv(parser);
94                 argv_append($$, $1);
95         }
96         | words TOKEN_DELIM word {
97                 argv_append($1, $3);
98                 $$ = $1;
99         }
100
101 word:   TOKEN_WORD
102         | word TOKEN_WORD {
103                 word_append($1, $2);
104                 $$ = $1;
105         }
106
107 %%
108 void yyerror(struct grub2_parser *parser, char const *s)
109 {
110         fprintf(stderr, "%d: error: %s '%s'\n",
111                         yyget_lineno(parser->scanner),
112                         s, yyget_text(parser->scanner));
113 }