64eee279a3d0f3311c5c3cfe9770dfb5bd69797d
[petitboot] / discover / grub2 / lexer.l
1
2 %{
3 #include "grub2.h"
4 #include "parser.h"
5 #include <talloc/talloc.h>
6 %}
7
8 %option nounput noinput
9 %option warn
10 %option noyywrap
11 %option stack noyy_top_state
12 %option reentrant
13 %option bison-bridge
14 %option noyyalloc noyyfree noyyrealloc
15 %option extra-type="struct grub2_parser *"
16 %option header-file="lexer.h"
17 %option outfile="lexer.c"
18
19 %x sqstring
20 %x dqstring
21
22 WORD    [^{}|&$;<> \t\n'"]+
23 VARNAME ([[:alpha:]][_[:alnum:]]*|[0-9]|[\?@\*#])
24
25 %%
26
27  /* discard leading & trailing whitespace, but keep inter-word delimeters */
28 ^[ \t]+ ;
29 [ \t]+$ ;
30 [ \t]*  return TOKEN_DELIM;
31
32  /* reserved words */
33 "[["         return TOKEN_LDSQBRACKET;
34 "]]"         return TOKEN_RDSQBRACKET;
35 "case"       return TOKEN_CASE;
36 "do"         return TOKEN_DO;
37 "done"       return TOKEN_DONE;
38 "elif"       return TOKEN_ELIF;
39 "esac"       return TOKEN_ESAC;
40 "fi"         return TOKEN_FI;
41 "for"        return TOKEN_FOR;
42 "function"   return TOKEN_FUNCTION;
43 "if"         return TOKEN_IF;
44 "in"         return TOKEN_IN;
45 "menuentry"  return TOKEN_MENUENTRY;
46 "select"     return TOKEN_SELECT;
47 "then"       return TOKEN_THEN;
48 "time"       return TOKEN_TIME;
49 "until"      return TOKEN_UTIL;
50 "while"      return TOKEN_WHILE;
51
52  /* anything that's not a metachar: return as a plain word */
53 {WORD}  {
54                 yylval->word = create_word(yyget_extra(yyscanner), yytext,
55                                                 false, false);
56                 return TOKEN_WORD;
57         }
58
59 \${VARNAME} |
60 \$\{{VARNAME}\} {
61                 yylval->word = create_word(yyget_extra(yyscanner), yytext,
62                                                 true, true);
63                 return TOKEN_WORD;
64         }
65
66  /* single-quoted strings: return a single, non-expanded word token */
67 \'      {
68                 yy_push_state(sqstring, yyscanner);
69         }
70 <sqstring>\' {
71                 yy_pop_state(yyscanner);
72         }
73 <sqstring>[^']+ {
74                 yylval->word = create_word(yyget_extra(yyscanner), yytext,
75                                                 false, false);
76                 return TOKEN_WORD;
77         }
78
79  /* double-quoted strings: return a single, expanded word token */
80 \"      {
81                 yy_push_state(dqstring, yyscanner);
82         }
83 <dqstring>\" {
84                 yy_pop_state(yyscanner);
85         }
86 <dqstring>([^"]|\\\")+ {
87                 yylval->word = create_word(yyget_extra(yyscanner), yytext,
88                                                 true, false);
89                 return TOKEN_WORD;
90         }
91
92
93  /* blocks */
94 "{"     return '{';
95 "}"     return '}';
96
97  /* end-of-line */
98 [ \t]*(;|\n)[ \t]*      return TOKEN_EOL;
99
100  /* strip comments */
101 #.*$    ;
102
103
104 .       printf("unknown token '%s'\n", yytext); exit(1);
105
106 %%
107
108 struct grub2_parser;
109
110 void *yyalloc(size_t bytes, void *yyscanner)
111 {
112         struct grub2_parser *parser = yyget_extra(yyscanner);
113         return talloc_size(parser, bytes);
114 }
115
116 void *yyrealloc(void *ptr, size_t bytes, void *yyscanner)
117 {
118         struct grub2_parser *parser = yyget_extra(yyscanner);
119         return talloc_realloc_size(parser, ptr, bytes);
120 }
121
122 void yyfree(void *ptr, void *yyscanner __attribute__((unused)))
123 {
124         talloc_free(ptr);
125 }