GNU Linux-libre 4.9.288-gnu1
[releases.git] / scripts / dtc / 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 BYTESTRING
24 %x PROPNODENAME
25 %s V1
26
27 PROPNODECHAR    [a-zA-Z0-9,._+*#?@-]
28 PATHCHAR        ({PROPNODECHAR}|[/])
29 LABEL           [a-zA-Z_][a-zA-Z0-9_]*
30 STRING          \"([^\\"]|\\.)*\"
31 CHAR_LITERAL    '([^']|\\')*'
32 WS              [[:space:]]
33 COMMENT         "/*"([^*]|\*+[^*/])*\*+"/"
34 LINECOMMENT     "//".*\n
35
36 %{
37 #include "dtc.h"
38 #include "srcpos.h"
39 #include "dtc-parser.tab.h"
40
41 extern bool treesource_error;
42
43 /* CAUTION: this will stop working if we ever use yyless() or yyunput() */
44 #define YY_USER_ACTION \
45         { \
46                 srcpos_update(&yylloc, yytext, yyleng); \
47         }
48
49 /*#define LEXDEBUG      1*/
50
51 #ifdef LEXDEBUG
52 #define DPRINT(fmt, ...)        fprintf(stderr, fmt, ##__VA_ARGS__)
53 #else
54 #define DPRINT(fmt, ...)        do { } while (0)
55 #endif
56
57 static int dts_version = 1;
58
59 #define BEGIN_DEFAULT()         DPRINT("<V1>\n"); \
60                                 BEGIN(V1); \
61
62 static void push_input_file(const char *filename);
63 static bool pop_input_file(void);
64 static void lexical_error(const char *fmt, ...);
65 %}
66
67 %%
68 <*>"/include/"{WS}*{STRING} {
69                         char *name = strchr(yytext, '\"') + 1;
70                         yytext[yyleng-1] = '\0';
71                         push_input_file(name);
72                 }
73
74 <*>^"#"(line)?[ \t]+[0-9]+[ \t]+{STRING}([ \t]+[0-9]+)? {
75                         char *line, *fnstart, *fnend;
76                         struct data fn;
77                         /* skip text before line # */
78                         line = yytext;
79                         while (!isdigit((unsigned char)*line))
80                                 line++;
81
82                         /* regexp ensures that first and list "
83                          * in the whole yytext are those at
84                          * beginning and end of the filename string */
85                         fnstart = memchr(yytext, '"', yyleng);
86                         for (fnend = yytext + yyleng - 1;
87                              *fnend != '"'; fnend--)
88                                 ;
89                         assert(fnstart && fnend && (fnend > fnstart));
90
91                         fn = data_copy_escape_string(fnstart + 1,
92                                                      fnend - fnstart - 1);
93
94                         /* Don't allow nuls in filenames */
95                         if (memchr(fn.val, '\0', fn.len - 1))
96                                 lexical_error("nul in line number directive");
97
98                         /* -1 since #line is the number of the next line */
99                         srcpos_set_line(xstrdup(fn.val), atoi(line) - 1);
100                         data_free(fn);
101                 }
102
103 <*><<EOF>>              {
104                         if (!pop_input_file()) {
105                                 yyterminate();
106                         }
107                 }
108
109 <*>{STRING}     {
110                         DPRINT("String: %s\n", yytext);
111                         yylval.data = data_copy_escape_string(yytext+1,
112                                         yyleng-2);
113                         return DT_STRING;
114                 }
115
116 <*>"/dts-v1/"   {
117                         DPRINT("Keyword: /dts-v1/\n");
118                         dts_version = 1;
119                         BEGIN_DEFAULT();
120                         return DT_V1;
121                 }
122
123 <*>"/memreserve/"       {
124                         DPRINT("Keyword: /memreserve/\n");
125                         BEGIN_DEFAULT();
126                         return DT_MEMRESERVE;
127                 }
128
129 <*>"/bits/"     {
130                         DPRINT("Keyword: /bits/\n");
131                         BEGIN_DEFAULT();
132                         return DT_BITS;
133                 }
134
135 <*>"/delete-property/"  {
136                         DPRINT("Keyword: /delete-property/\n");
137                         DPRINT("<PROPNODENAME>\n");
138                         BEGIN(PROPNODENAME);
139                         return DT_DEL_PROP;
140                 }
141
142 <*>"/delete-node/"      {
143                         DPRINT("Keyword: /delete-node/\n");
144                         DPRINT("<PROPNODENAME>\n");
145                         BEGIN(PROPNODENAME);
146                         return DT_DEL_NODE;
147                 }
148
149 <*>{LABEL}:     {
150                         DPRINT("Label: %s\n", yytext);
151                         yylval.labelref = xstrdup(yytext);
152                         yylval.labelref[yyleng-1] = '\0';
153                         return DT_LABEL;
154                 }
155
156 <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
157                         char *e;
158                         DPRINT("Integer Literal: '%s'\n", yytext);
159
160                         errno = 0;
161                         yylval.integer = strtoull(yytext, &e, 0);
162
163                         if (*e && e[strspn(e, "UL")]) {
164                                 lexical_error("Bad integer literal '%s'",
165                                               yytext);
166                         }
167
168                         if (errno == ERANGE)
169                                 lexical_error("Integer literal '%s' out of range",
170                                               yytext);
171                         else
172                                 /* ERANGE is the only strtoull error triggerable
173                                  *  by strings matching the pattern */
174                                 assert(errno == 0);
175                         return DT_LITERAL;
176                 }
177
178 <*>{CHAR_LITERAL}       {
179                         struct data d;
180                         DPRINT("Character literal: %s\n", yytext);
181
182                         d = data_copy_escape_string(yytext+1, yyleng-2);
183                         if (d.len == 1) {
184                                 lexical_error("Empty character literal");
185                                 yylval.integer = 0;
186                                 return DT_CHAR_LITERAL;
187                         }
188
189                         yylval.integer = (unsigned char)d.val[0];
190
191                         if (d.len > 2)
192                                 lexical_error("Character literal has %d"
193                                               " characters instead of 1",
194                                               d.len - 1);
195
196                         return DT_CHAR_LITERAL;
197                 }
198
199 <*>\&{LABEL}    {       /* label reference */
200                         DPRINT("Ref: %s\n", yytext+1);
201                         yylval.labelref = xstrdup(yytext+1);
202                         return DT_REF;
203                 }
204
205 <*>"&{/"{PATHCHAR}*\}   {       /* new-style path reference */
206                         yytext[yyleng-1] = '\0';
207                         DPRINT("Ref: %s\n", yytext+2);
208                         yylval.labelref = xstrdup(yytext+2);
209                         return DT_REF;
210                 }
211
212 <BYTESTRING>[0-9a-fA-F]{2} {
213                         yylval.byte = strtol(yytext, NULL, 16);
214                         DPRINT("Byte: %02x\n", (int)yylval.byte);
215                         return DT_BYTE;
216                 }
217
218 <BYTESTRING>"]" {
219                         DPRINT("/BYTESTRING\n");
220                         BEGIN_DEFAULT();
221                         return ']';
222                 }
223
224 <PROPNODENAME>\\?{PROPNODECHAR}+ {
225                         DPRINT("PropNodeName: %s\n", yytext);
226                         yylval.propnodename = xstrdup((yytext[0] == '\\') ?
227                                                         yytext + 1 : yytext);
228                         BEGIN_DEFAULT();
229                         return DT_PROPNODENAME;
230                 }
231
232 "/incbin/"      {
233                         DPRINT("Binary Include\n");
234                         return DT_INCBIN;
235                 }
236
237 <*>{WS}+        /* eat whitespace */
238 <*>{COMMENT}+   /* eat C-style comments */
239 <*>{LINECOMMENT}+ /* eat C++-style comments */
240
241 <*>"<<"         { return DT_LSHIFT; };
242 <*>">>"         { return DT_RSHIFT; };
243 <*>"<="         { return DT_LE; };
244 <*>">="         { return DT_GE; };
245 <*>"=="         { return DT_EQ; };
246 <*>"!="         { return DT_NE; };
247 <*>"&&"         { return DT_AND; };
248 <*>"||"         { return DT_OR; };
249
250 <*>.            {
251                         DPRINT("Char: %c (\\x%02x)\n", yytext[0],
252                                 (unsigned)yytext[0]);
253                         if (yytext[0] == '[') {
254                                 DPRINT("<BYTESTRING>\n");
255                                 BEGIN(BYTESTRING);
256                         }
257                         if ((yytext[0] == '{')
258                             || (yytext[0] == ';')) {
259                                 DPRINT("<PROPNODENAME>\n");
260                                 BEGIN(PROPNODENAME);
261                         }
262                         return yytext[0];
263                 }
264
265 %%
266
267 static void push_input_file(const char *filename)
268 {
269         assert(filename);
270
271         srcfile_push(filename);
272
273         yyin = current_srcfile->f;
274
275         yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
276 }
277
278
279 static bool pop_input_file(void)
280 {
281         if (srcfile_pop() == 0)
282                 return false;
283
284         yypop_buffer_state();
285         yyin = current_srcfile->f;
286
287         return true;
288 }
289
290 static void lexical_error(const char *fmt, ...)
291 {
292         va_list ap;
293
294         va_start(ap, fmt);
295         srcpos_verror(&yylloc, "Lexical error", fmt, ap);
296         va_end(ap);
297
298         treesource_error = true;
299 }