blob: 758ce5cb1c5adb6eb03750edf5205c6e00ebbb16 [file] [log] [blame]
Rob Herringacfe84f2019-06-20 15:19:38 -06001/* SPDX-License-Identifier: GPL-2.0-or-later */
David Gibsonfc14dad2005-06-08 17:18:34 +10002/*
3 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
David Gibsonfc14dad2005-06-08 17:18:34 +10004 */
5
David Gibson341df2b2010-04-30 15:27:32 +10006%option noyywrap nounput noinput never-interactive
David Gibsonfc14dad2005-06-08 17:18:34 +10007
David Gibsonfc14dad2005-06-08 17:18:34 +10008%x BYTESTRING
David Gibson9ed27a22007-11-07 11:16:19 +11009%x PROPNODENAME
David Gibson9138db52007-11-07 11:17:17 +110010%s V1
David Gibsonfc14dad2005-06-08 17:18:34 +100011
David Gibson7c44c2f2007-11-22 17:10:07 +110012PROPNODECHAR [a-zA-Z0-9,._+*#?@-]
13PATHCHAR ({PROPNODECHAR}|[/])
David Gibson7c44c2f2007-11-22 17:10:07 +110014LABEL [a-zA-Z_][a-zA-Z0-9_]*
David Gibson76e06222008-06-26 17:08:57 +100015STRING \"([^\\"]|\\.)*\"
Anton Staafa4ea2fa2011-09-09 12:16:30 -070016CHAR_LITERAL '([^']|\\')*'
David Gibson76e06222008-06-26 17:08:57 +100017WS [[:space:]]
18COMMENT "/*"([^*]|\*+[^*/])*\*+"/"
19LINECOMMENT "//".*\n
David Gibsonfc14dad2005-06-08 17:18:34 +100020
David Gibsonfc14dad2005-06-08 17:18:34 +100021%{
22#include "dtc.h"
Jon Loeligerce34ae32007-03-28 17:05:33 -050023#include "srcpos.h"
Chris Phoenixa7686482016-02-22 15:58:11 -080024#include "dtc-parser.h"
Jon Loeligere45e6fd2007-03-23 15:18:41 -050025
David Gibsonb82b9772014-01-03 20:00:01 +110026extern bool treesource_error;
David Gibson728c5e82009-12-08 14:24:42 +110027
28/* CAUTION: this will stop working if we ever use yyless() or yyunput() */
Jon Loeliger83ac55d2008-10-03 11:46:43 -050029#define YY_USER_ACTION \
30 { \
David Gibson728c5e82009-12-08 14:24:42 +110031 srcpos_update(&yylloc, yytext, yyleng); \
Jon Loeliger83ac55d2008-10-03 11:46:43 -050032 }
David Gibsonfc14dad2005-06-08 17:18:34 +100033
David Gibson81f2e892005-06-16 17:04:00 +100034/*#define LEXDEBUG 1*/
35
36#ifdef LEXDEBUG
37#define DPRINT(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
38#else
39#define DPRINT(fmt, ...) do { } while (0)
40#endif
David Gibsonfc14dad2005-06-08 17:18:34 +100041
Jon Loeliger4e1a0a02008-08-14 17:19:37 -050042static int dts_version = 1;
David Gibson86dbcbd2005-10-19 16:00:31 +100043
Jon Loeliger4e1a0a02008-08-14 17:19:37 -050044#define BEGIN_DEFAULT() DPRINT("<V1>\n"); \
David Gibson9138db52007-11-07 11:17:17 +110045 BEGIN(V1); \
David Gibson8a88ad82008-03-06 12:45:41 +110046
47static void push_input_file(const char *filename);
David Gibson17625372013-10-28 21:06:53 +110048static bool pop_input_file(void);
David Gibson672ac092017-03-06 12:06:15 +110049static void PRINTF(1, 2) lexical_error(const char *fmt, ...);
David Gibson34a98862017-02-28 10:09:45 +110050
David Gibsonfc14dad2005-06-08 17:18:34 +100051%}
52
David Gibsonc5c437e2005-07-04 13:53:14 +100053%%
David Gibson76e06222008-06-26 17:08:57 +100054<*>"/include/"{WS}*{STRING} {
55 char *name = strchr(yytext, '\"') + 1;
56 yytext[yyleng-1] = '\0';
57 push_input_file(name);
Jon Loeligere45e6fd2007-03-23 15:18:41 -050058 }
59
Pierre-Clément Tosief1978a2022-05-30 20:57:34 +010060<*>^"#"(line)?[ \t]+[0-9]+[ \t]+{STRING}([ \t]+[0-9]+)* {
David Gibsonb4334502016-01-04 22:56:39 +110061 char *line, *fnstart, *fnend;
62 struct data fn;
Stephen Warren1b6d1942012-09-27 17:11:05 -060063 /* skip text before line # */
64 line = yytext;
Serge Lamikhov-Center17119ab2013-12-25 15:26:03 +110065 while (!isdigit((unsigned char)*line))
Stephen Warren1b6d1942012-09-27 17:11:05 -060066 line++;
David Gibsonb4334502016-01-04 22:56:39 +110067
68 /* regexp ensures that first and list "
69 * in the whole yytext are those at
70 * beginning and end of the filename string */
71 fnstart = memchr(yytext, '"', yyleng);
72 for (fnend = yytext + yyleng - 1;
73 *fnend != '"'; fnend--)
74 ;
75 assert(fnstart && fnend && (fnend > fnstart));
76
77 fn = data_copy_escape_string(fnstart + 1,
78 fnend - fnstart - 1);
79
80 /* Don't allow nuls in filenames */
81 if (memchr(fn.val, '\0', fn.len - 1))
82 lexical_error("nul in line number directive");
83
Stephen Warren1b6d1942012-09-27 17:11:05 -060084 /* -1 since #line is the number of the next line */
David Gibsonb4334502016-01-04 22:56:39 +110085 srcpos_set_line(xstrdup(fn.val), atoi(line) - 1);
86 data_free(fn);
Stephen Warren1b6d1942012-09-27 17:11:05 -060087 }
88
David Gibson9ed27a22007-11-07 11:16:19 +110089<*><<EOF>> {
Jon Loeligere45e6fd2007-03-23 15:18:41 -050090 if (!pop_input_file()) {
91 yyterminate();
92 }
93 }
94
David Gibson76e06222008-06-26 17:08:57 +100095<*>{STRING} {
David Gibson81f2e892005-06-16 17:04:00 +100096 DPRINT("String: %s\n", yytext);
David Gibsonfc14dad2005-06-08 17:18:34 +100097 yylval.data = data_copy_escape_string(yytext+1,
98 yyleng-2);
99 return DT_STRING;
100 }
101
David Gibson9138db52007-11-07 11:17:17 +1100102<*>"/dts-v1/" {
David Gibson9138db52007-11-07 11:17:17 +1100103 DPRINT("Keyword: /dts-v1/\n");
104 dts_version = 1;
105 BEGIN_DEFAULT();
106 return DT_V1;
107 }
108
Pantelis Antoniou01f72ba2016-02-22 15:55:46 -0800109<*>"/plugin/" {
110 DPRINT("Keyword: /plugin/\n");
111 return DT_PLUGIN;
112 }
113
David Gibson9ed27a22007-11-07 11:16:19 +1100114<*>"/memreserve/" {
David Gibsonf0517db2005-07-15 17:14:24 +1000115 DPRINT("Keyword: /memreserve/\n");
David Gibson9138db52007-11-07 11:17:17 +1100116 BEGIN_DEFAULT();
David Gibson9ed27a22007-11-07 11:16:19 +1100117 return DT_MEMRESERVE;
David Gibsonf0517db2005-07-15 17:14:24 +1000118 }
Milton Millerac6a5e22007-07-07 01:18:50 -0500119
Anton Staaf033089f2011-10-11 10:22:29 -0700120<*>"/bits/" {
121 DPRINT("Keyword: /bits/\n");
122 BEGIN_DEFAULT();
123 return DT_BITS;
124 }
125
Stephen Warren45013d82012-08-07 22:50:15 -0600126<*>"/delete-property/" {
127 DPRINT("Keyword: /delete-property/\n");
128 DPRINT("<PROPNODENAME>\n");
129 BEGIN(PROPNODENAME);
130 return DT_DEL_PROP;
131 }
132
133<*>"/delete-node/" {
134 DPRINT("Keyword: /delete-node/\n");
135 DPRINT("<PROPNODENAME>\n");
136 BEGIN(PROPNODENAME);
137 return DT_DEL_NODE;
138 }
139
Maxime Ripard4038fd92018-05-03 22:27:26 +0200140<*>"/omit-if-no-ref/" {
141 DPRINT("Keyword: /omit-if-no-ref/\n");
142 DPRINT("<PROPNODENAME>\n");
143 BEGIN(PROPNODENAME);
144 return DT_OMIT_NO_REF;
145 }
146
David Gibson7c44c2f2007-11-22 17:10:07 +1100147<*>{LABEL}: {
Milton Millerac6a5e22007-07-07 01:18:50 -0500148 DPRINT("Label: %s\n", yytext);
Jon Loeliger879e4d22008-10-03 11:12:33 -0500149 yylval.labelref = xstrdup(yytext);
David Gibson9ed27a22007-11-07 11:16:19 +1100150 yylval.labelref[yyleng-1] = '\0';
Milton Millerac6a5e22007-07-07 01:18:50 -0500151 return DT_LABEL;
152 }
153
Stephen Warren5f0c3b22012-04-03 20:56:00 -0600154<V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
David Gibsonb82b9772014-01-03 20:00:01 +1100155 char *e;
156 DPRINT("Integer Literal: '%s'\n", yytext);
157
158 errno = 0;
159 yylval.integer = strtoull(yytext, &e, 0);
160
David Gibson1ab22052016-01-03 22:54:37 +1100161 if (*e && e[strspn(e, "UL")]) {
162 lexical_error("Bad integer literal '%s'",
163 yytext);
164 }
David Gibsonb82b9772014-01-03 20:00:01 +1100165
166 if (errno == ERANGE)
167 lexical_error("Integer literal '%s' out of range",
168 yytext);
169 else
170 /* ERANGE is the only strtoull error triggerable
171 * by strings matching the pattern */
172 assert(errno == 0);
David Gibson9ed27a22007-11-07 11:16:19 +1100173 return DT_LITERAL;
David Gibsonfc14dad2005-06-08 17:18:34 +1000174 }
175
Anton Staafa4ea2fa2011-09-09 12:16:30 -0700176<*>{CHAR_LITERAL} {
David Gibsoncfc65232014-01-04 10:03:55 +1100177 struct data d;
178 DPRINT("Character literal: %s\n", yytext);
179
180 d = data_copy_escape_string(yytext+1, yyleng-2);
181 if (d.len == 1) {
182 lexical_error("Empty character literal");
183 yylval.integer = 0;
Gabriel Smithf8886542016-12-11 15:13:16 -0500184 } else {
185 yylval.integer = (unsigned char)d.val[0];
186
187 if (d.len > 2)
188 lexical_error("Character literal has %d"
189 " characters instead of 1",
190 d.len - 1);
David Gibsoncfc65232014-01-04 10:03:55 +1100191 }
192
Gabriel Smithf8886542016-12-11 15:13:16 -0500193 data_free(d);
Anton Staafa4ea2fa2011-09-09 12:16:30 -0700194 return DT_CHAR_LITERAL;
195 }
196
David Gibson8773e122010-09-20 16:33:34 -0600197<*>\&{LABEL} { /* label reference */
David Gibson7c44c2f2007-11-22 17:10:07 +1100198 DPRINT("Ref: %s\n", yytext+1);
Jon Loeliger879e4d22008-10-03 11:12:33 -0500199 yylval.labelref = xstrdup(yytext+1);
Fredrik Markstrom0fcffda2018-09-24 13:27:27 +0200200 return DT_LABEL_REF;
David Gibson7c44c2f2007-11-22 17:10:07 +1100201 }
202
Pierre-Clément Tosief1978a2022-05-30 20:57:34 +0100203<*>"&{"{PATHCHAR}*\} { /* new-style path reference */
David Gibson7c44c2f2007-11-22 17:10:07 +1100204 yytext[yyleng-1] = '\0';
205 DPRINT("Ref: %s\n", yytext+2);
Jon Loeliger879e4d22008-10-03 11:12:33 -0500206 yylval.labelref = xstrdup(yytext+2);
Fredrik Markstrom0fcffda2018-09-24 13:27:27 +0200207 return DT_PATH_REF;
David Gibson7c44c2f2007-11-22 17:10:07 +1100208 }
209
David Gibsonfc14dad2005-06-08 17:18:34 +1000210<BYTESTRING>[0-9a-fA-F]{2} {
211 yylval.byte = strtol(yytext, NULL, 16);
David Gibson81f2e892005-06-16 17:04:00 +1000212 DPRINT("Byte: %02x\n", (int)yylval.byte);
David Gibsonfc14dad2005-06-08 17:18:34 +1000213 return DT_BYTE;
214 }
215
216<BYTESTRING>"]" {
David Gibson81f2e892005-06-16 17:04:00 +1000217 DPRINT("/BYTESTRING\n");
David Gibson9138db52007-11-07 11:17:17 +1100218 BEGIN_DEFAULT();
David Gibsonfc14dad2005-06-08 17:18:34 +1000219 return ']';
220 }
221
Stephen Warren1ff3d3f2012-09-27 17:11:04 -0600222<PROPNODENAME>\\?{PROPNODECHAR}+ {
David Gibson9ed27a22007-11-07 11:16:19 +1100223 DPRINT("PropNodeName: %s\n", yytext);
Stephen Warren1ff3d3f2012-09-27 17:11:04 -0600224 yylval.propnodename = xstrdup((yytext[0] == '\\') ?
225 yytext + 1 : yytext);
David Gibson9138db52007-11-07 11:17:17 +1100226 BEGIN_DEFAULT();
David Gibson9ed27a22007-11-07 11:16:19 +1100227 return DT_PROPNODENAME;
David Gibson4102d842005-06-16 14:36:37 +1000228 }
229
David Gibsone37ec7d2008-06-11 11:58:39 +1000230"/incbin/" {
David Gibsone37ec7d2008-06-11 11:58:39 +1000231 DPRINT("Binary Include\n");
232 return DT_INCBIN;
233 }
234
David Gibson76e06222008-06-26 17:08:57 +1000235<*>{WS}+ /* eat whitespace */
236<*>{COMMENT}+ /* eat C-style comments */
237<*>{LINECOMMENT}+ /* eat C++-style comments */
David Gibsonfc14dad2005-06-08 17:18:34 +1000238
Stephen Warren5f0c3b22012-04-03 20:56:00 -0600239<*>"<<" { return DT_LSHIFT; };
240<*>">>" { return DT_RSHIFT; };
241<*>"<=" { return DT_LE; };
242<*>">=" { return DT_GE; };
243<*>"==" { return DT_EQ; };
244<*>"!=" { return DT_NE; };
245<*>"&&" { return DT_AND; };
246<*>"||" { return DT_OR; };
247
David Gibsonf0517db2005-07-15 17:14:24 +1000248<*>. {
David Gibson2b67c632007-12-05 09:50:25 +1100249 DPRINT("Char: %c (\\x%02x)\n", yytext[0],
250 (unsigned)yytext[0]);
David Gibson9ed27a22007-11-07 11:16:19 +1100251 if (yytext[0] == '[') {
252 DPRINT("<BYTESTRING>\n");
253 BEGIN(BYTESTRING);
254 }
255 if ((yytext[0] == '{')
256 || (yytext[0] == ';')) {
257 DPRINT("<PROPNODENAME>\n");
258 BEGIN(PROPNODENAME);
259 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000260 return yytext[0];
261 }
262
263%%
Jon Loeligerce34ae32007-03-28 17:05:33 -0500264
David Gibson8a88ad82008-03-06 12:45:41 +1100265static void push_input_file(const char *filename)
Jon Loeligerce34ae32007-03-28 17:05:33 -0500266{
David Gibson7c635dc2008-02-26 16:44:29 +1100267 assert(filename);
Jon Loeligerce34ae32007-03-28 17:05:33 -0500268
David Gibsond68cb362009-12-08 14:24:42 +1100269 srcfile_push(filename);
Scott Wood910efac2008-01-03 17:43:31 -0600270
David Gibsond68cb362009-12-08 14:24:42 +1100271 yyin = current_srcfile->f;
David Gibsond68cb362009-12-08 14:24:42 +1100272
273 yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
Jon Loeligerce34ae32007-03-28 17:05:33 -0500274}
275
276
David Gibson17625372013-10-28 21:06:53 +1100277static bool pop_input_file(void)
Jon Loeligerce34ae32007-03-28 17:05:33 -0500278{
David Gibsond68cb362009-12-08 14:24:42 +1100279 if (srcfile_pop() == 0)
David Gibson17625372013-10-28 21:06:53 +1100280 return false;
Jon Loeligerce34ae32007-03-28 17:05:33 -0500281
David Gibsond68cb362009-12-08 14:24:42 +1100282 yypop_buffer_state();
David Gibsond68cb362009-12-08 14:24:42 +1100283 yyin = current_srcfile->f;
Jon Loeligerce34ae32007-03-28 17:05:33 -0500284
David Gibson17625372013-10-28 21:06:53 +1100285 return true;
Jon Loeligerce34ae32007-03-28 17:05:33 -0500286}
David Gibsonb82b9772014-01-03 20:00:01 +1100287
288static void lexical_error(const char *fmt, ...)
289{
290 va_list ap;
291
292 va_start(ap, fmt);
293 srcpos_verror(&yylloc, "Lexical error", fmt, ap);
294 va_end(ap);
295
296 treesource_error = true;
297}