]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - dtc-lexer.l
Import dtc git at 6a15eb2350426d285130e4c9d84c0bdb6575547a (last rev before bison...
[FreeBSD/FreeBSD.git] / dtc-lexer.l
1 /*
2  * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2005.
3  *
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
18  *                                                                   USA
19  */
20
21 %option noyywrap nounput noinput never-interactive
22
23 %x INCLUDE
24 %x BYTESTRING
25 %x PROPNODENAME
26 %s V1
27
28 PROPNODECHAR    [a-zA-Z0-9,._+*#?@-]
29 PATHCHAR        ({PROPNODECHAR}|[/])
30 LABEL           [a-zA-Z_][a-zA-Z0-9_]*
31 STRING          \"([^\\"]|\\.)*\"
32 CHAR_LITERAL    '([^']|\\')*'
33 WS              [[:space:]]
34 COMMENT         "/*"([^*]|\*+[^*/])*\*+"/"
35 LINECOMMENT     "//".*\n
36
37 %{
38 #include "dtc.h"
39 #include "srcpos.h"
40 #include "dtc-parser.tab.h"
41
42 YYLTYPE yylloc;
43 extern bool treesource_error;
44
45 /* CAUTION: this will stop working if we ever use yyless() or yyunput() */
46 #define YY_USER_ACTION \
47         { \
48                 srcpos_update(&yylloc, yytext, yyleng); \
49         }
50
51 /*#define LEXDEBUG      1*/
52
53 #ifdef LEXDEBUG
54 #define DPRINT(fmt, ...)        fprintf(stderr, fmt, ##__VA_ARGS__)
55 #else
56 #define DPRINT(fmt, ...)        do { } while (0)
57 #endif
58
59 static int dts_version = 1;
60
61 #define BEGIN_DEFAULT()         DPRINT("<V1>\n"); \
62                                 BEGIN(V1); \
63
64 static void push_input_file(const char *filename);
65 static bool pop_input_file(void);
66 static void lexical_error(const char *fmt, ...);
67 %}
68
69 %%
70 <*>"/include/"{WS}*{STRING} {
71                         char *name = strchr(yytext, '\"') + 1;
72                         yytext[yyleng-1] = '\0';
73                         push_input_file(name);
74                 }
75
76 <*>^"#"(line)?[ \t]+[0-9]+[ \t]+{STRING}([ \t]+[0-9]+)? {
77                         char *line, *tmp, *fn;
78                         /* skip text before line # */
79                         line = yytext;
80                         while (!isdigit((unsigned char)*line))
81                                 line++;
82                         /* skip digits in line # */
83                         tmp = line;
84                         while (!isspace((unsigned char)*tmp))
85                                 tmp++;
86                         /* "NULL"-terminate line # */
87                         *tmp = '\0';
88                         /* start of filename */
89                         fn = strchr(tmp + 1, '"') + 1;
90                         /* strip trailing " from filename */
91                         tmp = strchr(fn, '"');
92                         *tmp = 0;
93                         /* -1 since #line is the number of the next line */
94                         srcpos_set_line(xstrdup(fn), atoi(line) - 1);
95                 }
96
97 <*><<EOF>>              {
98                         if (!pop_input_file()) {
99                                 yyterminate();
100                         }
101                 }
102
103 <*>{STRING}     {
104                         DPRINT("String: %s\n", yytext);
105                         yylval.data = data_copy_escape_string(yytext+1,
106                                         yyleng-2);
107                         return DT_STRING;
108                 }
109
110 <*>"/dts-v1/"   {
111                         DPRINT("Keyword: /dts-v1/\n");
112                         dts_version = 1;
113                         BEGIN_DEFAULT();
114                         return DT_V1;
115                 }
116
117 <*>"/memreserve/"       {
118                         DPRINT("Keyword: /memreserve/\n");
119                         BEGIN_DEFAULT();
120                         return DT_MEMRESERVE;
121                 }
122
123 <*>"/bits/"     {
124                         DPRINT("Keyword: /bits/\n");
125                         BEGIN_DEFAULT();
126                         return DT_BITS;
127                 }
128
129 <*>"/delete-property/"  {
130                         DPRINT("Keyword: /delete-property/\n");
131                         DPRINT("<PROPNODENAME>\n");
132                         BEGIN(PROPNODENAME);
133                         return DT_DEL_PROP;
134                 }
135
136 <*>"/delete-node/"      {
137                         DPRINT("Keyword: /delete-node/\n");
138                         DPRINT("<PROPNODENAME>\n");
139                         BEGIN(PROPNODENAME);
140                         return DT_DEL_NODE;
141                 }
142
143 <*>{LABEL}:     {
144                         DPRINT("Label: %s\n", yytext);
145                         yylval.labelref = xstrdup(yytext);
146                         yylval.labelref[yyleng-1] = '\0';
147                         return DT_LABEL;
148                 }
149
150 <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
151                         char *e;
152                         DPRINT("Integer Literal: '%s'\n", yytext);
153
154                         errno = 0;
155                         yylval.integer = strtoull(yytext, &e, 0);
156
157                         assert(!(*e) || !e[strspn(e, "UL")]);
158
159                         if (errno == ERANGE)
160                                 lexical_error("Integer literal '%s' out of range",
161                                               yytext);
162                         else
163                                 /* ERANGE is the only strtoull error triggerable
164                                  *  by strings matching the pattern */
165                                 assert(errno == 0);
166                         return DT_LITERAL;
167                 }
168
169 <*>{CHAR_LITERAL}       {
170                         struct data d;
171                         DPRINT("Character literal: %s\n", yytext);
172
173                         d = data_copy_escape_string(yytext+1, yyleng-2);
174                         if (d.len == 1) {
175                                 lexical_error("Empty character literal");
176                                 yylval.integer = 0;
177                                 return DT_CHAR_LITERAL;
178                         }
179
180                         yylval.integer = (unsigned char)d.val[0];
181
182                         if (d.len > 2)
183                                 lexical_error("Character literal has %d"
184                                               " characters instead of 1",
185                                               d.len - 1);
186
187                         return DT_CHAR_LITERAL;
188                 }
189
190 <*>\&{LABEL}    {       /* label reference */
191                         DPRINT("Ref: %s\n", yytext+1);
192                         yylval.labelref = xstrdup(yytext+1);
193                         return DT_REF;
194                 }
195
196 <*>"&{/"{PATHCHAR}+\}   {       /* new-style path reference */
197                         yytext[yyleng-1] = '\0';
198                         DPRINT("Ref: %s\n", yytext+2);
199                         yylval.labelref = xstrdup(yytext+2);
200                         return DT_REF;
201                 }
202
203 <BYTESTRING>[0-9a-fA-F]{2} {
204                         yylval.byte = strtol(yytext, NULL, 16);
205                         DPRINT("Byte: %02x\n", (int)yylval.byte);
206                         return DT_BYTE;
207                 }
208
209 <BYTESTRING>"]" {
210                         DPRINT("/BYTESTRING\n");
211                         BEGIN_DEFAULT();
212                         return ']';
213                 }
214
215 <PROPNODENAME>\\?{PROPNODECHAR}+ {
216                         DPRINT("PropNodeName: %s\n", yytext);
217                         yylval.propnodename = xstrdup((yytext[0] == '\\') ?
218                                                         yytext + 1 : yytext);
219                         BEGIN_DEFAULT();
220                         return DT_PROPNODENAME;
221                 }
222
223 "/incbin/"      {
224                         DPRINT("Binary Include\n");
225                         return DT_INCBIN;
226                 }
227
228 <*>{WS}+        /* eat whitespace */
229 <*>{COMMENT}+   /* eat C-style comments */
230 <*>{LINECOMMENT}+ /* eat C++-style comments */
231
232 <*>"<<"         { return DT_LSHIFT; };
233 <*>">>"         { return DT_RSHIFT; };
234 <*>"<="         { return DT_LE; };
235 <*>">="         { return DT_GE; };
236 <*>"=="         { return DT_EQ; };
237 <*>"!="         { return DT_NE; };
238 <*>"&&"         { return DT_AND; };
239 <*>"||"         { return DT_OR; };
240
241 <*>.            {
242                         DPRINT("Char: %c (\\x%02x)\n", yytext[0],
243                                 (unsigned)yytext[0]);
244                         if (yytext[0] == '[') {
245                                 DPRINT("<BYTESTRING>\n");
246                                 BEGIN(BYTESTRING);
247                         }
248                         if ((yytext[0] == '{')
249                             || (yytext[0] == ';')) {
250                                 DPRINT("<PROPNODENAME>\n");
251                                 BEGIN(PROPNODENAME);
252                         }
253                         return yytext[0];
254                 }
255
256 %%
257
258 static void push_input_file(const char *filename)
259 {
260         assert(filename);
261
262         srcfile_push(filename);
263
264         yyin = current_srcfile->f;
265
266         yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
267 }
268
269
270 static bool pop_input_file(void)
271 {
272         if (srcfile_pop() == 0)
273                 return false;
274
275         yypop_buffer_state();
276         yyin = current_srcfile->f;
277
278         return true;
279 }
280
281 static void lexical_error(const char *fmt, ...)
282 {
283         va_list ap;
284
285         va_start(ap, fmt);
286         srcpos_verror(&yylloc, "Lexical error", fmt, ap);
287         va_end(ap);
288
289         treesource_error = true;
290 }