GNU Linux-libre 4.4.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, *tmp, *fn;
76                         /* skip text before line # */
77                         line = yytext;
78                         while (!isdigit((unsigned char)*line))
79                                 line++;
80                         /* skip digits in line # */
81                         tmp = line;
82                         while (!isspace((unsigned char)*tmp))
83                                 tmp++;
84                         /* "NULL"-terminate line # */
85                         *tmp = '\0';
86                         /* start of filename */
87                         fn = strchr(tmp + 1, '"') + 1;
88                         /* strip trailing " from filename */
89                         tmp = strchr(fn, '"');
90                         *tmp = 0;
91                         /* -1 since #line is the number of the next line */
92                         srcpos_set_line(xstrdup(fn), atoi(line) - 1);
93                 }
94
95 <*><<EOF>>              {
96                         if (!pop_input_file()) {
97                                 yyterminate();
98                         }
99                 }
100
101 <*>{STRING}     {
102                         DPRINT("String: %s\n", yytext);
103                         yylval.data = data_copy_escape_string(yytext+1,
104                                         yyleng-2);
105                         return DT_STRING;
106                 }
107
108 <*>"/dts-v1/"   {
109                         DPRINT("Keyword: /dts-v1/\n");
110                         dts_version = 1;
111                         BEGIN_DEFAULT();
112                         return DT_V1;
113                 }
114
115 <*>"/memreserve/"       {
116                         DPRINT("Keyword: /memreserve/\n");
117                         BEGIN_DEFAULT();
118                         return DT_MEMRESERVE;
119                 }
120
121 <*>"/bits/"     {
122                         DPRINT("Keyword: /bits/\n");
123                         BEGIN_DEFAULT();
124                         return DT_BITS;
125                 }
126
127 <*>"/delete-property/"  {
128                         DPRINT("Keyword: /delete-property/\n");
129                         DPRINT("<PROPNODENAME>\n");
130                         BEGIN(PROPNODENAME);
131                         return DT_DEL_PROP;
132                 }
133
134 <*>"/delete-node/"      {
135                         DPRINT("Keyword: /delete-node/\n");
136                         DPRINT("<PROPNODENAME>\n");
137                         BEGIN(PROPNODENAME);
138                         return DT_DEL_NODE;
139                 }
140
141 <*>{LABEL}:     {
142                         DPRINT("Label: %s\n", yytext);
143                         yylval.labelref = xstrdup(yytext);
144                         yylval.labelref[yyleng-1] = '\0';
145                         return DT_LABEL;
146                 }
147
148 <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
149                         char *e;
150                         DPRINT("Integer Literal: '%s'\n", yytext);
151
152                         errno = 0;
153                         yylval.integer = strtoull(yytext, &e, 0);
154
155                         assert(!(*e) || !e[strspn(e, "UL")]);
156
157                         if (errno == ERANGE)
158                                 lexical_error("Integer literal '%s' out of range",
159                                               yytext);
160                         else
161                                 /* ERANGE is the only strtoull error triggerable
162                                  *  by strings matching the pattern */
163                                 assert(errno == 0);
164                         return DT_LITERAL;
165                 }
166
167 <*>{CHAR_LITERAL}       {
168                         struct data d;
169                         DPRINT("Character literal: %s\n", yytext);
170
171                         d = data_copy_escape_string(yytext+1, yyleng-2);
172                         if (d.len == 1) {
173                                 lexical_error("Empty character literal");
174                                 yylval.integer = 0;
175                                 return DT_CHAR_LITERAL;
176                         }
177
178                         yylval.integer = (unsigned char)d.val[0];
179
180                         if (d.len > 2)
181                                 lexical_error("Character literal has %d"
182                                               " characters instead of 1",
183                                               d.len - 1);
184
185                         return DT_CHAR_LITERAL;
186                 }
187
188 <*>\&{LABEL}    {       /* label reference */
189                         DPRINT("Ref: %s\n", yytext+1);
190                         yylval.labelref = xstrdup(yytext+1);
191                         return DT_REF;
192                 }
193
194 <*>"&{/"{PATHCHAR}*\}   {       /* new-style path reference */
195                         yytext[yyleng-1] = '\0';
196                         DPRINT("Ref: %s\n", yytext+2);
197                         yylval.labelref = xstrdup(yytext+2);
198                         return DT_REF;
199                 }
200
201 <BYTESTRING>[0-9a-fA-F]{2} {
202                         yylval.byte = strtol(yytext, NULL, 16);
203                         DPRINT("Byte: %02x\n", (int)yylval.byte);
204                         return DT_BYTE;
205                 }
206
207 <BYTESTRING>"]" {
208                         DPRINT("/BYTESTRING\n");
209                         BEGIN_DEFAULT();
210                         return ']';
211                 }
212
213 <PROPNODENAME>\\?{PROPNODECHAR}+ {
214                         DPRINT("PropNodeName: %s\n", yytext);
215                         yylval.propnodename = xstrdup((yytext[0] == '\\') ?
216                                                         yytext + 1 : yytext);
217                         BEGIN_DEFAULT();
218                         return DT_PROPNODENAME;
219                 }
220
221 "/incbin/"      {
222                         DPRINT("Binary Include\n");
223                         return DT_INCBIN;
224                 }
225
226 <*>{WS}+        /* eat whitespace */
227 <*>{COMMENT}+   /* eat C-style comments */
228 <*>{LINECOMMENT}+ /* eat C++-style comments */
229
230 <*>"<<"         { return DT_LSHIFT; };
231 <*>">>"         { return DT_RSHIFT; };
232 <*>"<="         { return DT_LE; };
233 <*>">="         { return DT_GE; };
234 <*>"=="         { return DT_EQ; };
235 <*>"!="         { return DT_NE; };
236 <*>"&&"         { return DT_AND; };
237 <*>"||"         { return DT_OR; };
238
239 <*>.            {
240                         DPRINT("Char: %c (\\x%02x)\n", yytext[0],
241                                 (unsigned)yytext[0]);
242                         if (yytext[0] == '[') {
243                                 DPRINT("<BYTESTRING>\n");
244                                 BEGIN(BYTESTRING);
245                         }
246                         if ((yytext[0] == '{')
247                             || (yytext[0] == ';')) {
248                                 DPRINT("<PROPNODENAME>\n");
249                                 BEGIN(PROPNODENAME);
250                         }
251                         return yytext[0];
252                 }
253
254 %%
255
256 static void push_input_file(const char *filename)
257 {
258         assert(filename);
259
260         srcfile_push(filename);
261
262         yyin = current_srcfile->f;
263
264         yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
265 }
266
267
268 static bool pop_input_file(void)
269 {
270         if (srcfile_pop() == 0)
271                 return false;
272
273         yypop_buffer_state();
274         yyin = current_srcfile->f;
275
276         return true;
277 }
278
279 static void lexical_error(const char *fmt, ...)
280 {
281         va_list ap;
282
283         va_start(ap, fmt);
284         srcpos_verror(&yylloc, "Lexical error", fmt, ap);
285         va_end(ap);
286
287         treesource_error = true;
288 }