blob: 0ee1caf03dd057bee8c212002568941f0658edd7 [file] [log] [blame]
David Gibsona4da2e32007-12-18 15:06:42 +11001/*
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
John Bonesio658f29a2010-11-17 15:28:20 -080021%option noyywrap nounput noinput never-interactive
David Gibsona4da2e32007-12-18 15:06:42 +110022
David Gibsona4da2e32007-12-18 15:06:42 +110023%x BYTESTRING
24%x PROPNODENAME
25%s V1
26
27PROPNODECHAR [a-zA-Z0-9,._+*#?@-]
28PATHCHAR ({PROPNODECHAR}|[/])
29LABEL [a-zA-Z_][a-zA-Z0-9_]*
David Gibsoned95d742008-08-07 12:24:17 +100030STRING \"([^\\"]|\\.)*\"
Stephen Warrencd296722012-09-28 21:25:59 +000031CHAR_LITERAL '([^']|\\')*'
David Gibsoned95d742008-08-07 12:24:17 +100032WS [[:space:]]
33COMMENT "/*"([^*]|\*+[^*/])*\*+"/"
34LINECOMMENT "//".*\n
David Gibsona4da2e32007-12-18 15:06:42 +110035
36%{
37#include "dtc.h"
38#include "srcpos.h"
39#include "dtc-parser.tab.h"
40
John Bonesio658f29a2010-11-17 15:28:20 -080041YYLTYPE yylloc;
Rob Herring47605972015-04-29 16:00:05 -050042extern bool treesource_error;
John Bonesio658f29a2010-11-17 15:28:20 -080043
44/* CAUTION: this will stop working if we ever use yyless() or yyunput() */
45#define YY_USER_ACTION \
46 { \
47 srcpos_update(&yylloc, yytext, yyleng); \
48 }
David Gibsona4da2e32007-12-18 15:06:42 +110049
50/*#define LEXDEBUG 1*/
51
52#ifdef LEXDEBUG
53#define DPRINT(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
54#else
55#define DPRINT(fmt, ...) do { } while (0)
56#endif
57
John Bonesio658f29a2010-11-17 15:28:20 -080058static int dts_version = 1;
David Gibsona4da2e32007-12-18 15:06:42 +110059
John Bonesio658f29a2010-11-17 15:28:20 -080060#define BEGIN_DEFAULT() DPRINT("<V1>\n"); \
David Gibsona4da2e32007-12-18 15:06:42 +110061 BEGIN(V1); \
David Gibsoned95d742008-08-07 12:24:17 +100062
63static void push_input_file(const char *filename);
Rob Herring47605972015-04-29 16:00:05 -050064static bool pop_input_file(void);
65static void lexical_error(const char *fmt, ...);
David Gibsona4da2e32007-12-18 15:06:42 +110066%}
67
68%%
David Gibsoned95d742008-08-07 12:24:17 +100069<*>"/include/"{WS}*{STRING} {
70 char *name = strchr(yytext, '\"') + 1;
71 yytext[yyleng-1] = '\0';
72 push_input_file(name);
David Gibsona4da2e32007-12-18 15:06:42 +110073 }
74
Grant Likely706b78f2013-06-13 12:57:44 +010075<*>^"#"(line)?[ \t]+[0-9]+[ \t]+{STRING}([ \t]+[0-9]+)? {
Stephen Warrencd296722012-09-28 21:25:59 +000076 char *line, *tmp, *fn;
77 /* skip text before line # */
78 line = yytext;
Rob Herring47605972015-04-29 16:00:05 -050079 while (!isdigit((unsigned char)*line))
Stephen Warrencd296722012-09-28 21:25:59 +000080 line++;
81 /* skip digits in line # */
82 tmp = line;
Rob Herring47605972015-04-29 16:00:05 -050083 while (!isspace((unsigned char)*tmp))
Stephen Warrencd296722012-09-28 21:25:59 +000084 tmp++;
85 /* "NULL"-terminate line # */
86 *tmp = '\0';
87 /* start of filename */
88 fn = strchr(tmp + 1, '"') + 1;
89 /* strip trailing " from filename */
90 tmp = strchr(fn, '"');
91 *tmp = 0;
92 /* -1 since #line is the number of the next line */
93 srcpos_set_line(xstrdup(fn), atoi(line) - 1);
94 }
95
David Gibsona4da2e32007-12-18 15:06:42 +110096<*><<EOF>> {
97 if (!pop_input_file()) {
98 yyterminate();
99 }
100 }
101
David Gibsoned95d742008-08-07 12:24:17 +1000102<*>{STRING} {
David Gibsona4da2e32007-12-18 15:06:42 +1100103 DPRINT("String: %s\n", yytext);
104 yylval.data = data_copy_escape_string(yytext+1,
105 yyleng-2);
David Gibsona4da2e32007-12-18 15:06:42 +1100106 return DT_STRING;
107 }
108
109<*>"/dts-v1/" {
David Gibsona4da2e32007-12-18 15:06:42 +1100110 DPRINT("Keyword: /dts-v1/\n");
111 dts_version = 1;
112 BEGIN_DEFAULT();
113 return DT_V1;
114 }
115
116<*>"/memreserve/" {
David Gibsona4da2e32007-12-18 15:06:42 +1100117 DPRINT("Keyword: /memreserve/\n");
118 BEGIN_DEFAULT();
119 return DT_MEMRESERVE;
120 }
121
Stephen Warrencd296722012-09-28 21:25:59 +0000122<*>"/bits/" {
123 DPRINT("Keyword: /bits/\n");
124 BEGIN_DEFAULT();
125 return DT_BITS;
126 }
127
128<*>"/delete-property/" {
129 DPRINT("Keyword: /delete-property/\n");
130 DPRINT("<PROPNODENAME>\n");
131 BEGIN(PROPNODENAME);
132 return DT_DEL_PROP;
133 }
134
135<*>"/delete-node/" {
136 DPRINT("Keyword: /delete-node/\n");
137 DPRINT("<PROPNODENAME>\n");
138 BEGIN(PROPNODENAME);
139 return DT_DEL_NODE;
140 }
141
David Gibsona4da2e32007-12-18 15:06:42 +1100142<*>{LABEL}: {
David Gibsona4da2e32007-12-18 15:06:42 +1100143 DPRINT("Label: %s\n", yytext);
John Bonesio658f29a2010-11-17 15:28:20 -0800144 yylval.labelref = xstrdup(yytext);
David Gibsona4da2e32007-12-18 15:06:42 +1100145 yylval.labelref[yyleng-1] = '\0';
146 return DT_LABEL;
147 }
148
Stephen Warrencd296722012-09-28 21:25:59 +0000149<V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
Rob Herring47605972015-04-29 16:00:05 -0500150 char *e;
151 DPRINT("Integer Literal: '%s'\n", yytext);
152
153 errno = 0;
154 yylval.integer = strtoull(yytext, &e, 0);
155
156 assert(!(*e) || !e[strspn(e, "UL")]);
157
158 if (errno == ERANGE)
159 lexical_error("Integer literal '%s' out of range",
160 yytext);
161 else
162 /* ERANGE is the only strtoull error triggerable
163 * by strings matching the pattern */
164 assert(errno == 0);
David Gibsona4da2e32007-12-18 15:06:42 +1100165 return DT_LITERAL;
166 }
167
Stephen Warrencd296722012-09-28 21:25:59 +0000168<*>{CHAR_LITERAL} {
Rob Herring47605972015-04-29 16:00:05 -0500169 struct data d;
170 DPRINT("Character literal: %s\n", yytext);
171
172 d = data_copy_escape_string(yytext+1, yyleng-2);
173 if (d.len == 1) {
174 lexical_error("Empty character literal");
175 yylval.integer = 0;
176 return DT_CHAR_LITERAL;
177 }
178
179 yylval.integer = (unsigned char)d.val[0];
180
181 if (d.len > 2)
182 lexical_error("Character literal has %d"
183 " characters instead of 1",
184 d.len - 1);
185
Stephen Warrencd296722012-09-28 21:25:59 +0000186 return DT_CHAR_LITERAL;
187 }
188
John Bonesio658f29a2010-11-17 15:28:20 -0800189<*>\&{LABEL} { /* label reference */
David Gibsona4da2e32007-12-18 15:06:42 +1100190 DPRINT("Ref: %s\n", yytext+1);
John Bonesio658f29a2010-11-17 15:28:20 -0800191 yylval.labelref = xstrdup(yytext+1);
David Gibsona4da2e32007-12-18 15:06:42 +1100192 return DT_REF;
193 }
194
Rob Herring47605972015-04-29 16:00:05 -0500195<*>"&{/"{PATHCHAR}*\} { /* new-style path reference */
David Gibsona4da2e32007-12-18 15:06:42 +1100196 yytext[yyleng-1] = '\0';
197 DPRINT("Ref: %s\n", yytext+2);
John Bonesio658f29a2010-11-17 15:28:20 -0800198 yylval.labelref = xstrdup(yytext+2);
David Gibsona4da2e32007-12-18 15:06:42 +1100199 return DT_REF;
200 }
201
202<BYTESTRING>[0-9a-fA-F]{2} {
David Gibsona4da2e32007-12-18 15:06:42 +1100203 yylval.byte = strtol(yytext, NULL, 16);
204 DPRINT("Byte: %02x\n", (int)yylval.byte);
205 return DT_BYTE;
206 }
207
208<BYTESTRING>"]" {
David Gibsona4da2e32007-12-18 15:06:42 +1100209 DPRINT("/BYTESTRING\n");
210 BEGIN_DEFAULT();
211 return ']';
212 }
213
Stephen Warrencd296722012-09-28 21:25:59 +0000214<PROPNODENAME>\\?{PROPNODECHAR}+ {
David Gibsona4da2e32007-12-18 15:06:42 +1100215 DPRINT("PropNodeName: %s\n", yytext);
Stephen Warrencd296722012-09-28 21:25:59 +0000216 yylval.propnodename = xstrdup((yytext[0] == '\\') ?
217 yytext + 1 : yytext);
David Gibsona4da2e32007-12-18 15:06:42 +1100218 BEGIN_DEFAULT();
219 return DT_PROPNODENAME;
220 }
221
David Gibsoned95d742008-08-07 12:24:17 +1000222"/incbin/" {
David Gibsoned95d742008-08-07 12:24:17 +1000223 DPRINT("Binary Include\n");
224 return DT_INCBIN;
David Gibsona4da2e32007-12-18 15:06:42 +1100225 }
226
David Gibsoned95d742008-08-07 12:24:17 +1000227<*>{WS}+ /* eat whitespace */
228<*>{COMMENT}+ /* eat C-style comments */
229<*>{LINECOMMENT}+ /* eat C++-style comments */
David Gibsona4da2e32007-12-18 15:06:42 +1100230
Stephen Warrencd296722012-09-28 21:25:59 +0000231<*>"<<" { return DT_LSHIFT; };
232<*>">>" { return DT_RSHIFT; };
233<*>"<=" { return DT_LE; };
234<*>">=" { return DT_GE; };
235<*>"==" { return DT_EQ; };
236<*>"!=" { return DT_NE; };
237<*>"&&" { return DT_AND; };
238<*>"||" { return DT_OR; };
239
David Gibsona4da2e32007-12-18 15:06:42 +1100240<*>. {
David Gibsona4da2e32007-12-18 15:06:42 +1100241 DPRINT("Char: %c (\\x%02x)\n", yytext[0],
242 (unsigned)yytext[0]);
243 if (yytext[0] == '[') {
244 DPRINT("<BYTESTRING>\n");
245 BEGIN(BYTESTRING);
246 }
247 if ((yytext[0] == '{')
248 || (yytext[0] == ';')) {
249 DPRINT("<PROPNODENAME>\n");
250 BEGIN(PROPNODENAME);
251 }
252 return yytext[0];
253 }
254
255%%
256
David Gibsoned95d742008-08-07 12:24:17 +1000257static void push_input_file(const char *filename)
David Gibsona4da2e32007-12-18 15:06:42 +1100258{
David Gibsoned95d742008-08-07 12:24:17 +1000259 assert(filename);
260
John Bonesio658f29a2010-11-17 15:28:20 -0800261 srcfile_push(filename);
David Gibsoned95d742008-08-07 12:24:17 +1000262
John Bonesio658f29a2010-11-17 15:28:20 -0800263 yyin = current_srcfile->f;
David Gibsona4da2e32007-12-18 15:06:42 +1100264
John Bonesio658f29a2010-11-17 15:28:20 -0800265 yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
David Gibsona4da2e32007-12-18 15:06:42 +1100266}
267
268
Rob Herring47605972015-04-29 16:00:05 -0500269static bool pop_input_file(void)
David Gibsona4da2e32007-12-18 15:06:42 +1100270{
John Bonesio658f29a2010-11-17 15:28:20 -0800271 if (srcfile_pop() == 0)
Rob Herring47605972015-04-29 16:00:05 -0500272 return false;
David Gibsona4da2e32007-12-18 15:06:42 +1100273
John Bonesio658f29a2010-11-17 15:28:20 -0800274 yypop_buffer_state();
275 yyin = current_srcfile->f;
David Gibsona4da2e32007-12-18 15:06:42 +1100276
Rob Herring47605972015-04-29 16:00:05 -0500277 return true;
278}
279
280static void lexical_error(const char *fmt, ...)
281{
282 va_list ap;
283
284 va_start(ap, fmt);
285 srcpos_verror(&yylloc, "Lexical error", fmt, ap);
286 va_end(ap);
287
288 treesource_error = true;
David Gibsona4da2e32007-12-18 15:06:42 +1100289}