blob: e4e0f6a8d07b2d31c15a7e2b4f573ba1e95c3606 [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
Rob Herring47605972015-04-29 16:00:05 -050041extern bool treesource_error;
John Bonesio658f29a2010-11-17 15:28:20 -080042
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 }
David Gibsona4da2e32007-12-18 15:06:42 +110048
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
John Bonesio658f29a2010-11-17 15:28:20 -080057static int dts_version = 1;
David Gibsona4da2e32007-12-18 15:06:42 +110058
John Bonesio658f29a2010-11-17 15:28:20 -080059#define BEGIN_DEFAULT() DPRINT("<V1>\n"); \
David Gibsona4da2e32007-12-18 15:06:42 +110060 BEGIN(V1); \
David Gibsoned95d742008-08-07 12:24:17 +100061
62static void push_input_file(const char *filename);
Rob Herring47605972015-04-29 16:00:05 -050063static bool pop_input_file(void);
64static void lexical_error(const char *fmt, ...);
David Gibsona4da2e32007-12-18 15:06:42 +110065%}
66
67%%
David Gibsoned95d742008-08-07 12:24:17 +100068<*>"/include/"{WS}*{STRING} {
69 char *name = strchr(yytext, '\"') + 1;
70 yytext[yyleng-1] = '\0';
71 push_input_file(name);
David Gibsona4da2e32007-12-18 15:06:42 +110072 }
73
Grant Likely706b78f2013-06-13 12:57:44 +010074<*>^"#"(line)?[ \t]+[0-9]+[ \t]+{STRING}([ \t]+[0-9]+)? {
Rob Herring91feabc2016-01-26 09:04:11 -060075 char *line, *fnstart, *fnend;
76 struct data fn;
Stephen Warrencd296722012-09-28 21:25:59 +000077 /* 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++;
Rob Herring91feabc2016-01-26 09:04:11 -060081
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
Stephen Warrencd296722012-09-28 21:25:59 +000098 /* -1 since #line is the number of the next line */
Rob Herring91feabc2016-01-26 09:04:11 -060099 srcpos_set_line(xstrdup(fn.val), atoi(line) - 1);
100 data_free(fn);
Stephen Warrencd296722012-09-28 21:25:59 +0000101 }
102
David Gibsona4da2e32007-12-18 15:06:42 +1100103<*><<EOF>> {
104 if (!pop_input_file()) {
105 yyterminate();
106 }
107 }
108
David Gibsoned95d742008-08-07 12:24:17 +1000109<*>{STRING} {
David Gibsona4da2e32007-12-18 15:06:42 +1100110 DPRINT("String: %s\n", yytext);
111 yylval.data = data_copy_escape_string(yytext+1,
112 yyleng-2);
David Gibsona4da2e32007-12-18 15:06:42 +1100113 return DT_STRING;
114 }
115
116<*>"/dts-v1/" {
David Gibsona4da2e32007-12-18 15:06:42 +1100117 DPRINT("Keyword: /dts-v1/\n");
118 dts_version = 1;
119 BEGIN_DEFAULT();
120 return DT_V1;
121 }
122
123<*>"/memreserve/" {
David Gibsona4da2e32007-12-18 15:06:42 +1100124 DPRINT("Keyword: /memreserve/\n");
125 BEGIN_DEFAULT();
126 return DT_MEMRESERVE;
127 }
128
Stephen Warrencd296722012-09-28 21:25:59 +0000129<*>"/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
David Gibsona4da2e32007-12-18 15:06:42 +1100149<*>{LABEL}: {
David Gibsona4da2e32007-12-18 15:06:42 +1100150 DPRINT("Label: %s\n", yytext);
John Bonesio658f29a2010-11-17 15:28:20 -0800151 yylval.labelref = xstrdup(yytext);
David Gibsona4da2e32007-12-18 15:06:42 +1100152 yylval.labelref[yyleng-1] = '\0';
153 return DT_LABEL;
154 }
155
Stephen Warrencd296722012-09-28 21:25:59 +0000156<V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
Rob Herring47605972015-04-29 16:00:05 -0500157 char *e;
158 DPRINT("Integer Literal: '%s'\n", yytext);
159
160 errno = 0;
161 yylval.integer = strtoull(yytext, &e, 0);
162
Rob Herring91feabc2016-01-26 09:04:11 -0600163 if (*e && e[strspn(e, "UL")]) {
164 lexical_error("Bad integer literal '%s'",
165 yytext);
166 }
Rob Herring47605972015-04-29 16:00:05 -0500167
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);
David Gibsona4da2e32007-12-18 15:06:42 +1100175 return DT_LITERAL;
176 }
177
Stephen Warrencd296722012-09-28 21:25:59 +0000178<*>{CHAR_LITERAL} {
Rob Herring47605972015-04-29 16:00:05 -0500179 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
Stephen Warrencd296722012-09-28 21:25:59 +0000196 return DT_CHAR_LITERAL;
197 }
198
John Bonesio658f29a2010-11-17 15:28:20 -0800199<*>\&{LABEL} { /* label reference */
David Gibsona4da2e32007-12-18 15:06:42 +1100200 DPRINT("Ref: %s\n", yytext+1);
John Bonesio658f29a2010-11-17 15:28:20 -0800201 yylval.labelref = xstrdup(yytext+1);
David Gibsona4da2e32007-12-18 15:06:42 +1100202 return DT_REF;
203 }
204
Rob Herring47605972015-04-29 16:00:05 -0500205<*>"&{/"{PATHCHAR}*\} { /* new-style path reference */
David Gibsona4da2e32007-12-18 15:06:42 +1100206 yytext[yyleng-1] = '\0';
207 DPRINT("Ref: %s\n", yytext+2);
John Bonesio658f29a2010-11-17 15:28:20 -0800208 yylval.labelref = xstrdup(yytext+2);
David Gibsona4da2e32007-12-18 15:06:42 +1100209 return DT_REF;
210 }
211
212<BYTESTRING>[0-9a-fA-F]{2} {
David Gibsona4da2e32007-12-18 15:06:42 +1100213 yylval.byte = strtol(yytext, NULL, 16);
214 DPRINT("Byte: %02x\n", (int)yylval.byte);
215 return DT_BYTE;
216 }
217
218<BYTESTRING>"]" {
David Gibsona4da2e32007-12-18 15:06:42 +1100219 DPRINT("/BYTESTRING\n");
220 BEGIN_DEFAULT();
221 return ']';
222 }
223
Stephen Warrencd296722012-09-28 21:25:59 +0000224<PROPNODENAME>\\?{PROPNODECHAR}+ {
David Gibsona4da2e32007-12-18 15:06:42 +1100225 DPRINT("PropNodeName: %s\n", yytext);
Stephen Warrencd296722012-09-28 21:25:59 +0000226 yylval.propnodename = xstrdup((yytext[0] == '\\') ?
227 yytext + 1 : yytext);
David Gibsona4da2e32007-12-18 15:06:42 +1100228 BEGIN_DEFAULT();
229 return DT_PROPNODENAME;
230 }
231
David Gibsoned95d742008-08-07 12:24:17 +1000232"/incbin/" {
David Gibsoned95d742008-08-07 12:24:17 +1000233 DPRINT("Binary Include\n");
234 return DT_INCBIN;
David Gibsona4da2e32007-12-18 15:06:42 +1100235 }
236
David Gibsoned95d742008-08-07 12:24:17 +1000237<*>{WS}+ /* eat whitespace */
238<*>{COMMENT}+ /* eat C-style comments */
239<*>{LINECOMMENT}+ /* eat C++-style comments */
David Gibsona4da2e32007-12-18 15:06:42 +1100240
Stephen Warrencd296722012-09-28 21:25:59 +0000241<*>"<<" { 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
David Gibsona4da2e32007-12-18 15:06:42 +1100250<*>. {
David Gibsona4da2e32007-12-18 15:06:42 +1100251 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
David Gibsoned95d742008-08-07 12:24:17 +1000267static void push_input_file(const char *filename)
David Gibsona4da2e32007-12-18 15:06:42 +1100268{
David Gibsoned95d742008-08-07 12:24:17 +1000269 assert(filename);
270
John Bonesio658f29a2010-11-17 15:28:20 -0800271 srcfile_push(filename);
David Gibsoned95d742008-08-07 12:24:17 +1000272
John Bonesio658f29a2010-11-17 15:28:20 -0800273 yyin = current_srcfile->f;
David Gibsona4da2e32007-12-18 15:06:42 +1100274
John Bonesio658f29a2010-11-17 15:28:20 -0800275 yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
David Gibsona4da2e32007-12-18 15:06:42 +1100276}
277
278
Rob Herring47605972015-04-29 16:00:05 -0500279static bool pop_input_file(void)
David Gibsona4da2e32007-12-18 15:06:42 +1100280{
John Bonesio658f29a2010-11-17 15:28:20 -0800281 if (srcfile_pop() == 0)
Rob Herring47605972015-04-29 16:00:05 -0500282 return false;
David Gibsona4da2e32007-12-18 15:06:42 +1100283
John Bonesio658f29a2010-11-17 15:28:20 -0800284 yypop_buffer_state();
285 yyin = current_srcfile->f;
David Gibsona4da2e32007-12-18 15:06:42 +1100286
Rob Herring47605972015-04-29 16:00:05 -0500287 return true;
288}
289
290static 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;
David Gibsona4da2e32007-12-18 15:06:42 +1100299}