blob: 5c6c3fd557d7fcbf4cde4a757ac1c4c862019b05 [file] [log] [blame]
Rob Herring12869ec2019-06-21 08:18:32 -06001/* SPDX-License-Identifier: GPL-2.0-or-later */
David Gibsona4da2e32007-12-18 15:06:42 +11002/*
3 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
David Gibsona4da2e32007-12-18 15:06:42 +11004 */
5
John Bonesio658f29a2010-11-17 15:28:20 -08006%option noyywrap nounput noinput never-interactive
David Gibsona4da2e32007-12-18 15:06:42 +11007
David Gibsona4da2e32007-12-18 15:06:42 +11008%x BYTESTRING
9%x PROPNODENAME
10%s V1
11
12PROPNODECHAR [a-zA-Z0-9,._+*#?@-]
13PATHCHAR ({PROPNODECHAR}|[/])
14LABEL [a-zA-Z_][a-zA-Z0-9_]*
David Gibsoned95d742008-08-07 12:24:17 +100015STRING \"([^\\"]|\\.)*\"
Stephen Warrencd296722012-09-28 21:25:59 +000016CHAR_LITERAL '([^']|\\')*'
David Gibsoned95d742008-08-07 12:24:17 +100017WS [[:space:]]
18COMMENT "/*"([^*]|\*+[^*/])*\*+"/"
19LINECOMMENT "//".*\n
David Gibsona4da2e32007-12-18 15:06:42 +110020
21%{
22#include "dtc.h"
23#include "srcpos.h"
24#include "dtc-parser.tab.h"
25
John Bonesio658f29a2010-11-17 15:28:20 -080026YYLTYPE yylloc;
Rob Herring47605972015-04-29 16:00:05 -050027extern bool treesource_error;
John Bonesio658f29a2010-11-17 15:28:20 -080028
29/* CAUTION: this will stop working if we ever use yyless() or yyunput() */
30#define YY_USER_ACTION \
31 { \
32 srcpos_update(&yylloc, yytext, yyleng); \
33 }
David Gibsona4da2e32007-12-18 15:06:42 +110034
35/*#define LEXDEBUG 1*/
36
37#ifdef LEXDEBUG
38#define DPRINT(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
39#else
40#define DPRINT(fmt, ...) do { } while (0)
41#endif
42
John Bonesio658f29a2010-11-17 15:28:20 -080043static int dts_version = 1;
David Gibsona4da2e32007-12-18 15:06:42 +110044
John Bonesio658f29a2010-11-17 15:28:20 -080045#define BEGIN_DEFAULT() DPRINT("<V1>\n"); \
David Gibsona4da2e32007-12-18 15:06:42 +110046 BEGIN(V1); \
David Gibsoned95d742008-08-07 12:24:17 +100047
48static void push_input_file(const char *filename);
Rob Herring47605972015-04-29 16:00:05 -050049static bool pop_input_file(void);
Rob Herring89d12312017-03-21 09:01:08 -050050static void PRINTF(1, 2) lexical_error(const char *fmt, ...);
51
David Gibsona4da2e32007-12-18 15:06:42 +110052%}
53
54%%
David Gibsoned95d742008-08-07 12:24:17 +100055<*>"/include/"{WS}*{STRING} {
56 char *name = strchr(yytext, '\"') + 1;
57 yytext[yyleng-1] = '\0';
58 push_input_file(name);
David Gibsona4da2e32007-12-18 15:06:42 +110059 }
60
Grant Likely706b78f2013-06-13 12:57:44 +010061<*>^"#"(line)?[ \t]+[0-9]+[ \t]+{STRING}([ \t]+[0-9]+)? {
Rob Herring91feabc2016-01-26 09:04:11 -060062 char *line, *fnstart, *fnend;
63 struct data fn;
Stephen Warrencd296722012-09-28 21:25:59 +000064 /* skip text before line # */
65 line = yytext;
Rob Herring47605972015-04-29 16:00:05 -050066 while (!isdigit((unsigned char)*line))
Stephen Warrencd296722012-09-28 21:25:59 +000067 line++;
Rob Herring91feabc2016-01-26 09:04:11 -060068
69 /* regexp ensures that first and list "
70 * in the whole yytext are those at
71 * beginning and end of the filename string */
72 fnstart = memchr(yytext, '"', yyleng);
73 for (fnend = yytext + yyleng - 1;
74 *fnend != '"'; fnend--)
75 ;
76 assert(fnstart && fnend && (fnend > fnstart));
77
78 fn = data_copy_escape_string(fnstart + 1,
79 fnend - fnstart - 1);
80
81 /* Don't allow nuls in filenames */
82 if (memchr(fn.val, '\0', fn.len - 1))
83 lexical_error("nul in line number directive");
84
Stephen Warrencd296722012-09-28 21:25:59 +000085 /* -1 since #line is the number of the next line */
Rob Herring91feabc2016-01-26 09:04:11 -060086 srcpos_set_line(xstrdup(fn.val), atoi(line) - 1);
87 data_free(fn);
Stephen Warrencd296722012-09-28 21:25:59 +000088 }
89
David Gibsona4da2e32007-12-18 15:06:42 +110090<*><<EOF>> {
91 if (!pop_input_file()) {
92 yyterminate();
93 }
94 }
95
David Gibsoned95d742008-08-07 12:24:17 +100096<*>{STRING} {
David Gibsona4da2e32007-12-18 15:06:42 +110097 DPRINT("String: %s\n", yytext);
98 yylval.data = data_copy_escape_string(yytext+1,
99 yyleng-2);
David Gibsona4da2e32007-12-18 15:06:42 +1100100 return DT_STRING;
101 }
102
103<*>"/dts-v1/" {
David Gibsona4da2e32007-12-18 15:06:42 +1100104 DPRINT("Keyword: /dts-v1/\n");
105 dts_version = 1;
106 BEGIN_DEFAULT();
107 return DT_V1;
108 }
109
Rob Herring6f05afc2017-01-04 10:45:20 -0600110<*>"/plugin/" {
111 DPRINT("Keyword: /plugin/\n");
112 return DT_PLUGIN;
113 }
114
David Gibsona4da2e32007-12-18 15:06:42 +1100115<*>"/memreserve/" {
David Gibsona4da2e32007-12-18 15:06:42 +1100116 DPRINT("Keyword: /memreserve/\n");
117 BEGIN_DEFAULT();
118 return DT_MEMRESERVE;
119 }
120
Stephen Warrencd296722012-09-28 21:25:59 +0000121<*>"/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
Rob Herring50aafd62018-05-08 13:07:49 -0500141<*>"/omit-if-no-ref/" {
142 DPRINT("Keyword: /omit-if-no-ref/\n");
143 DPRINT("<PROPNODENAME>\n");
144 BEGIN(PROPNODENAME);
145 return DT_OMIT_NO_REF;
146 }
147
David Gibsona4da2e32007-12-18 15:06:42 +1100148<*>{LABEL}: {
David Gibsona4da2e32007-12-18 15:06:42 +1100149 DPRINT("Label: %s\n", yytext);
John Bonesio658f29a2010-11-17 15:28:20 -0800150 yylval.labelref = xstrdup(yytext);
David Gibsona4da2e32007-12-18 15:06:42 +1100151 yylval.labelref[yyleng-1] = '\0';
152 return DT_LABEL;
153 }
154
Stephen Warrencd296722012-09-28 21:25:59 +0000155<V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
Rob Herring47605972015-04-29 16:00:05 -0500156 char *e;
157 DPRINT("Integer Literal: '%s'\n", yytext);
158
159 errno = 0;
160 yylval.integer = strtoull(yytext, &e, 0);
161
Rob Herring91feabc2016-01-26 09:04:11 -0600162 if (*e && e[strspn(e, "UL")]) {
163 lexical_error("Bad integer literal '%s'",
164 yytext);
165 }
Rob Herring47605972015-04-29 16:00:05 -0500166
167 if (errno == ERANGE)
168 lexical_error("Integer literal '%s' out of range",
169 yytext);
170 else
171 /* ERANGE is the only strtoull error triggerable
172 * by strings matching the pattern */
173 assert(errno == 0);
David Gibsona4da2e32007-12-18 15:06:42 +1100174 return DT_LITERAL;
175 }
176
Stephen Warrencd296722012-09-28 21:25:59 +0000177<*>{CHAR_LITERAL} {
Rob Herring47605972015-04-29 16:00:05 -0500178 struct data d;
179 DPRINT("Character literal: %s\n", yytext);
180
181 d = data_copy_escape_string(yytext+1, yyleng-2);
182 if (d.len == 1) {
183 lexical_error("Empty character literal");
184 yylval.integer = 0;
Rob Herring6f05afc2017-01-04 10:45:20 -0600185 } else {
186 yylval.integer = (unsigned char)d.val[0];
187
188 if (d.len > 2)
189 lexical_error("Character literal has %d"
190 " characters instead of 1",
191 d.len - 1);
Rob Herring47605972015-04-29 16:00:05 -0500192 }
193
Rob Herring6f05afc2017-01-04 10:45:20 -0600194 data_free(d);
Stephen Warrencd296722012-09-28 21:25:59 +0000195 return DT_CHAR_LITERAL;
196 }
197
John Bonesio658f29a2010-11-17 15:28:20 -0800198<*>\&{LABEL} { /* label reference */
David Gibsona4da2e32007-12-18 15:06:42 +1100199 DPRINT("Ref: %s\n", yytext+1);
John Bonesio658f29a2010-11-17 15:28:20 -0800200 yylval.labelref = xstrdup(yytext+1);
Rob Herringc2e70752018-11-28 18:37:35 -0600201 return DT_LABEL_REF;
David Gibsona4da2e32007-12-18 15:06:42 +1100202 }
203
Rob Herring47605972015-04-29 16:00:05 -0500204<*>"&{/"{PATHCHAR}*\} { /* new-style path reference */
David Gibsona4da2e32007-12-18 15:06:42 +1100205 yytext[yyleng-1] = '\0';
206 DPRINT("Ref: %s\n", yytext+2);
John Bonesio658f29a2010-11-17 15:28:20 -0800207 yylval.labelref = xstrdup(yytext+2);
Rob Herringc2e70752018-11-28 18:37:35 -0600208 return DT_PATH_REF;
David Gibsona4da2e32007-12-18 15:06:42 +1100209 }
210
211<BYTESTRING>[0-9a-fA-F]{2} {
David Gibsona4da2e32007-12-18 15:06:42 +1100212 yylval.byte = strtol(yytext, NULL, 16);
213 DPRINT("Byte: %02x\n", (int)yylval.byte);
214 return DT_BYTE;
215 }
216
217<BYTESTRING>"]" {
David Gibsona4da2e32007-12-18 15:06:42 +1100218 DPRINT("/BYTESTRING\n");
219 BEGIN_DEFAULT();
220 return ']';
221 }
222
Stephen Warrencd296722012-09-28 21:25:59 +0000223<PROPNODENAME>\\?{PROPNODECHAR}+ {
David Gibsona4da2e32007-12-18 15:06:42 +1100224 DPRINT("PropNodeName: %s\n", yytext);
Stephen Warrencd296722012-09-28 21:25:59 +0000225 yylval.propnodename = xstrdup((yytext[0] == '\\') ?
226 yytext + 1 : yytext);
David Gibsona4da2e32007-12-18 15:06:42 +1100227 BEGIN_DEFAULT();
228 return DT_PROPNODENAME;
229 }
230
David Gibsoned95d742008-08-07 12:24:17 +1000231"/incbin/" {
David Gibsoned95d742008-08-07 12:24:17 +1000232 DPRINT("Binary Include\n");
233 return DT_INCBIN;
David Gibsona4da2e32007-12-18 15:06:42 +1100234 }
235
David Gibsoned95d742008-08-07 12:24:17 +1000236<*>{WS}+ /* eat whitespace */
237<*>{COMMENT}+ /* eat C-style comments */
238<*>{LINECOMMENT}+ /* eat C++-style comments */
David Gibsona4da2e32007-12-18 15:06:42 +1100239
Stephen Warrencd296722012-09-28 21:25:59 +0000240<*>"<<" { return DT_LSHIFT; };
241<*>">>" { return DT_RSHIFT; };
242<*>"<=" { return DT_LE; };
243<*>">=" { return DT_GE; };
244<*>"==" { return DT_EQ; };
245<*>"!=" { return DT_NE; };
246<*>"&&" { return DT_AND; };
247<*>"||" { return DT_OR; };
248
David Gibsona4da2e32007-12-18 15:06:42 +1100249<*>. {
David Gibsona4da2e32007-12-18 15:06:42 +1100250 DPRINT("Char: %c (\\x%02x)\n", yytext[0],
251 (unsigned)yytext[0]);
252 if (yytext[0] == '[') {
253 DPRINT("<BYTESTRING>\n");
254 BEGIN(BYTESTRING);
255 }
256 if ((yytext[0] == '{')
257 || (yytext[0] == ';')) {
258 DPRINT("<PROPNODENAME>\n");
259 BEGIN(PROPNODENAME);
260 }
261 return yytext[0];
262 }
263
264%%
265
David Gibsoned95d742008-08-07 12:24:17 +1000266static void push_input_file(const char *filename)
David Gibsona4da2e32007-12-18 15:06:42 +1100267{
David Gibsoned95d742008-08-07 12:24:17 +1000268 assert(filename);
269
John Bonesio658f29a2010-11-17 15:28:20 -0800270 srcfile_push(filename);
David Gibsoned95d742008-08-07 12:24:17 +1000271
John Bonesio658f29a2010-11-17 15:28:20 -0800272 yyin = current_srcfile->f;
David Gibsona4da2e32007-12-18 15:06:42 +1100273
John Bonesio658f29a2010-11-17 15:28:20 -0800274 yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
David Gibsona4da2e32007-12-18 15:06:42 +1100275}
276
277
Rob Herring47605972015-04-29 16:00:05 -0500278static bool pop_input_file(void)
David Gibsona4da2e32007-12-18 15:06:42 +1100279{
John Bonesio658f29a2010-11-17 15:28:20 -0800280 if (srcfile_pop() == 0)
Rob Herring47605972015-04-29 16:00:05 -0500281 return false;
David Gibsona4da2e32007-12-18 15:06:42 +1100282
John Bonesio658f29a2010-11-17 15:28:20 -0800283 yypop_buffer_state();
284 yyin = current_srcfile->f;
David Gibsona4da2e32007-12-18 15:06:42 +1100285
Rob Herring47605972015-04-29 16:00:05 -0500286 return true;
287}
288
289static void lexical_error(const char *fmt, ...)
290{
291 va_list ap;
292
293 va_start(ap, fmt);
294 srcpos_verror(&yylloc, "Lexical error", fmt, ap);
295 va_end(ap);
296
297 treesource_error = true;
David Gibsona4da2e32007-12-18 15:06:42 +1100298}