blob: 13f4b06e97bfc3a9cd2a79cefba6c8216a5d8118 [file] [log] [blame]
David Gibsonfc14dad2005-06-08 17:18:34 +10001/*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
3 *
David Gibson63dc9c72007-09-18 11:44:04 +10004 *
David Gibsonfc14dad2005-06-08 17:18:34 +10005 * 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.
David Gibson63dc9c72007-09-18 11:44:04 +100014 *
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
David Gibsonfc14dad2005-06-08 17:18:34 +100019 */
20
David Gibson341df2b2010-04-30 15:27:32 +100021%option noyywrap nounput noinput never-interactive
David Gibsonfc14dad2005-06-08 17:18:34 +100022
David Gibsonfc14dad2005-06-08 17:18:34 +100023%x BYTESTRING
David Gibson9ed27a22007-11-07 11:16:19 +110024%x PROPNODENAME
David Gibson9138db52007-11-07 11:17:17 +110025%s V1
David Gibsonfc14dad2005-06-08 17:18:34 +100026
David Gibson7c44c2f2007-11-22 17:10:07 +110027PROPNODECHAR [a-zA-Z0-9,._+*#?@-]
28PATHCHAR ({PROPNODECHAR}|[/])
David Gibson7c44c2f2007-11-22 17:10:07 +110029LABEL [a-zA-Z_][a-zA-Z0-9_]*
David Gibson76e06222008-06-26 17:08:57 +100030STRING \"([^\\"]|\\.)*\"
Anton Staafa4ea2fa2011-09-09 12:16:30 -070031CHAR_LITERAL '([^']|\\')*'
David Gibson76e06222008-06-26 17:08:57 +100032WS [[:space:]]
33COMMENT "/*"([^*]|\*+[^*/])*\*+"/"
34LINECOMMENT "//".*\n
David Gibsonfc14dad2005-06-08 17:18:34 +100035
David Gibsonfc14dad2005-06-08 17:18:34 +100036%{
37#include "dtc.h"
Jon Loeligerce34ae32007-03-28 17:05:33 -050038#include "srcpos.h"
Chris Phoenixa7686482016-02-22 15:58:11 -080039#include "dtc-parser.h"
Jon Loeligere45e6fd2007-03-23 15:18:41 -050040
Lukasz Wojcik5c8d2e22010-01-29 17:31:51 +010041YYLTYPE yylloc;
David Gibsonb82b9772014-01-03 20:00:01 +110042extern bool treesource_error;
David Gibson728c5e82009-12-08 14:24:42 +110043
44/* CAUTION: this will stop working if we ever use yyless() or yyunput() */
Jon Loeliger83ac55d2008-10-03 11:46:43 -050045#define YY_USER_ACTION \
46 { \
David Gibson728c5e82009-12-08 14:24:42 +110047 srcpos_update(&yylloc, yytext, yyleng); \
Jon Loeliger83ac55d2008-10-03 11:46:43 -050048 }
David Gibsonfc14dad2005-06-08 17:18:34 +100049
David Gibson81f2e892005-06-16 17:04:00 +100050/*#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
David Gibsonfc14dad2005-06-08 17:18:34 +100057
Jon Loeliger4e1a0a02008-08-14 17:19:37 -050058static int dts_version = 1;
David Gibson86dbcbd2005-10-19 16:00:31 +100059
Jon Loeliger4e1a0a02008-08-14 17:19:37 -050060#define BEGIN_DEFAULT() DPRINT("<V1>\n"); \
David Gibson9138db52007-11-07 11:17:17 +110061 BEGIN(V1); \
David Gibson8a88ad82008-03-06 12:45:41 +110062
63static void push_input_file(const char *filename);
David Gibson17625372013-10-28 21:06:53 +110064static bool pop_input_file(void);
David Gibson672ac092017-03-06 12:06:15 +110065static void PRINTF(1, 2) lexical_error(const char *fmt, ...);
David Gibson34a98862017-02-28 10:09:45 +110066
David Gibsonfc14dad2005-06-08 17:18:34 +100067%}
68
David Gibsonc5c437e2005-07-04 13:53:14 +100069%%
David Gibson76e06222008-06-26 17:08:57 +100070<*>"/include/"{WS}*{STRING} {
71 char *name = strchr(yytext, '\"') + 1;
72 yytext[yyleng-1] = '\0';
73 push_input_file(name);
Jon Loeligere45e6fd2007-03-23 15:18:41 -050074 }
75
Stephen Warrena1ee6f02013-05-31 12:33:04 -060076<*>^"#"(line)?[ \t]+[0-9]+[ \t]+{STRING}([ \t]+[0-9]+)? {
David Gibsonb4334502016-01-04 22:56:39 +110077 char *line, *fnstart, *fnend;
78 struct data fn;
Stephen Warren1b6d1942012-09-27 17:11:05 -060079 /* skip text before line # */
80 line = yytext;
Serge Lamikhov-Center17119ab2013-12-25 15:26:03 +110081 while (!isdigit((unsigned char)*line))
Stephen Warren1b6d1942012-09-27 17:11:05 -060082 line++;
David Gibsonb4334502016-01-04 22:56:39 +110083
84 /* regexp ensures that first and list "
85 * in the whole yytext are those at
86 * beginning and end of the filename string */
87 fnstart = memchr(yytext, '"', yyleng);
88 for (fnend = yytext + yyleng - 1;
89 *fnend != '"'; fnend--)
90 ;
91 assert(fnstart && fnend && (fnend > fnstart));
92
93 fn = data_copy_escape_string(fnstart + 1,
94 fnend - fnstart - 1);
95
96 /* Don't allow nuls in filenames */
97 if (memchr(fn.val, '\0', fn.len - 1))
98 lexical_error("nul in line number directive");
99
Stephen Warren1b6d1942012-09-27 17:11:05 -0600100 /* -1 since #line is the number of the next line */
David Gibsonb4334502016-01-04 22:56:39 +1100101 srcpos_set_line(xstrdup(fn.val), atoi(line) - 1);
102 data_free(fn);
Stephen Warren1b6d1942012-09-27 17:11:05 -0600103 }
104
David Gibson9ed27a22007-11-07 11:16:19 +1100105<*><<EOF>> {
Jon Loeligere45e6fd2007-03-23 15:18:41 -0500106 if (!pop_input_file()) {
107 yyterminate();
108 }
109 }
110
David Gibson76e06222008-06-26 17:08:57 +1000111<*>{STRING} {
David Gibson81f2e892005-06-16 17:04:00 +1000112 DPRINT("String: %s\n", yytext);
David Gibsonfc14dad2005-06-08 17:18:34 +1000113 yylval.data = data_copy_escape_string(yytext+1,
114 yyleng-2);
115 return DT_STRING;
116 }
117
David Gibson9138db52007-11-07 11:17:17 +1100118<*>"/dts-v1/" {
David Gibson9138db52007-11-07 11:17:17 +1100119 DPRINT("Keyword: /dts-v1/\n");
120 dts_version = 1;
121 BEGIN_DEFAULT();
122 return DT_V1;
123 }
124
Pantelis Antoniou01f72ba2016-02-22 15:55:46 -0800125<*>"/plugin/" {
126 DPRINT("Keyword: /plugin/\n");
127 return DT_PLUGIN;
128 }
129
David Gibson9ed27a22007-11-07 11:16:19 +1100130<*>"/memreserve/" {
David Gibsonf0517db2005-07-15 17:14:24 +1000131 DPRINT("Keyword: /memreserve/\n");
David Gibson9138db52007-11-07 11:17:17 +1100132 BEGIN_DEFAULT();
David Gibson9ed27a22007-11-07 11:16:19 +1100133 return DT_MEMRESERVE;
David Gibsonf0517db2005-07-15 17:14:24 +1000134 }
Milton Millerac6a5e22007-07-07 01:18:50 -0500135
Anton Staaf033089f2011-10-11 10:22:29 -0700136<*>"/bits/" {
137 DPRINT("Keyword: /bits/\n");
138 BEGIN_DEFAULT();
139 return DT_BITS;
140 }
141
Stephen Warren45013d82012-08-07 22:50:15 -0600142<*>"/delete-property/" {
143 DPRINT("Keyword: /delete-property/\n");
144 DPRINT("<PROPNODENAME>\n");
145 BEGIN(PROPNODENAME);
146 return DT_DEL_PROP;
147 }
148
149<*>"/delete-node/" {
150 DPRINT("Keyword: /delete-node/\n");
151 DPRINT("<PROPNODENAME>\n");
152 BEGIN(PROPNODENAME);
153 return DT_DEL_NODE;
154 }
155
David Gibson7c44c2f2007-11-22 17:10:07 +1100156<*>{LABEL}: {
Milton Millerac6a5e22007-07-07 01:18:50 -0500157 DPRINT("Label: %s\n", yytext);
Jon Loeliger879e4d22008-10-03 11:12:33 -0500158 yylval.labelref = xstrdup(yytext);
David Gibson9ed27a22007-11-07 11:16:19 +1100159 yylval.labelref[yyleng-1] = '\0';
Milton Millerac6a5e22007-07-07 01:18:50 -0500160 return DT_LABEL;
161 }
162
Stephen Warren5f0c3b22012-04-03 20:56:00 -0600163<V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
David Gibsonb82b9772014-01-03 20:00:01 +1100164 char *e;
165 DPRINT("Integer Literal: '%s'\n", yytext);
166
167 errno = 0;
168 yylval.integer = strtoull(yytext, &e, 0);
169
David Gibson1ab22052016-01-03 22:54:37 +1100170 if (*e && e[strspn(e, "UL")]) {
171 lexical_error("Bad integer literal '%s'",
172 yytext);
173 }
David Gibsonb82b9772014-01-03 20:00:01 +1100174
175 if (errno == ERANGE)
176 lexical_error("Integer literal '%s' out of range",
177 yytext);
178 else
179 /* ERANGE is the only strtoull error triggerable
180 * by strings matching the pattern */
181 assert(errno == 0);
David Gibson9ed27a22007-11-07 11:16:19 +1100182 return DT_LITERAL;
David Gibsonfc14dad2005-06-08 17:18:34 +1000183 }
184
Anton Staafa4ea2fa2011-09-09 12:16:30 -0700185<*>{CHAR_LITERAL} {
David Gibsoncfc65232014-01-04 10:03:55 +1100186 struct data d;
187 DPRINT("Character literal: %s\n", yytext);
188
189 d = data_copy_escape_string(yytext+1, yyleng-2);
190 if (d.len == 1) {
191 lexical_error("Empty character literal");
192 yylval.integer = 0;
Gabriel Smithf8886542016-12-11 15:13:16 -0500193 } else {
194 yylval.integer = (unsigned char)d.val[0];
195
196 if (d.len > 2)
197 lexical_error("Character literal has %d"
198 " characters instead of 1",
199 d.len - 1);
David Gibsoncfc65232014-01-04 10:03:55 +1100200 }
201
Gabriel Smithf8886542016-12-11 15:13:16 -0500202 data_free(d);
Anton Staafa4ea2fa2011-09-09 12:16:30 -0700203 return DT_CHAR_LITERAL;
204 }
205
David Gibson8773e122010-09-20 16:33:34 -0600206<*>\&{LABEL} { /* label reference */
David Gibson7c44c2f2007-11-22 17:10:07 +1100207 DPRINT("Ref: %s\n", yytext+1);
Jon Loeliger879e4d22008-10-03 11:12:33 -0500208 yylval.labelref = xstrdup(yytext+1);
David Gibson7c44c2f2007-11-22 17:10:07 +1100209 return DT_REF;
210 }
211
David Gibsonf2405272014-05-09 20:48:49 +1000212<*>"&{/"{PATHCHAR}*\} { /* new-style path reference */
David Gibson7c44c2f2007-11-22 17:10:07 +1100213 yytext[yyleng-1] = '\0';
214 DPRINT("Ref: %s\n", yytext+2);
Jon Loeliger879e4d22008-10-03 11:12:33 -0500215 yylval.labelref = xstrdup(yytext+2);
David Gibson7c44c2f2007-11-22 17:10:07 +1100216 return DT_REF;
217 }
218
David Gibsonfc14dad2005-06-08 17:18:34 +1000219<BYTESTRING>[0-9a-fA-F]{2} {
220 yylval.byte = strtol(yytext, NULL, 16);
David Gibson81f2e892005-06-16 17:04:00 +1000221 DPRINT("Byte: %02x\n", (int)yylval.byte);
David Gibsonfc14dad2005-06-08 17:18:34 +1000222 return DT_BYTE;
223 }
224
225<BYTESTRING>"]" {
David Gibson81f2e892005-06-16 17:04:00 +1000226 DPRINT("/BYTESTRING\n");
David Gibson9138db52007-11-07 11:17:17 +1100227 BEGIN_DEFAULT();
David Gibsonfc14dad2005-06-08 17:18:34 +1000228 return ']';
229 }
230
Stephen Warren1ff3d3f2012-09-27 17:11:04 -0600231<PROPNODENAME>\\?{PROPNODECHAR}+ {
David Gibson9ed27a22007-11-07 11:16:19 +1100232 DPRINT("PropNodeName: %s\n", yytext);
Stephen Warren1ff3d3f2012-09-27 17:11:04 -0600233 yylval.propnodename = xstrdup((yytext[0] == '\\') ?
234 yytext + 1 : yytext);
David Gibson9138db52007-11-07 11:17:17 +1100235 BEGIN_DEFAULT();
David Gibson9ed27a22007-11-07 11:16:19 +1100236 return DT_PROPNODENAME;
David Gibson4102d842005-06-16 14:36:37 +1000237 }
238
David Gibsone37ec7d2008-06-11 11:58:39 +1000239"/incbin/" {
David Gibsone37ec7d2008-06-11 11:58:39 +1000240 DPRINT("Binary Include\n");
241 return DT_INCBIN;
242 }
243
David Gibson76e06222008-06-26 17:08:57 +1000244<*>{WS}+ /* eat whitespace */
245<*>{COMMENT}+ /* eat C-style comments */
246<*>{LINECOMMENT}+ /* eat C++-style comments */
David Gibsonfc14dad2005-06-08 17:18:34 +1000247
Stephen Warren5f0c3b22012-04-03 20:56:00 -0600248<*>"<<" { return DT_LSHIFT; };
249<*>">>" { return DT_RSHIFT; };
250<*>"<=" { return DT_LE; };
251<*>">=" { return DT_GE; };
252<*>"==" { return DT_EQ; };
253<*>"!=" { return DT_NE; };
254<*>"&&" { return DT_AND; };
255<*>"||" { return DT_OR; };
256
David Gibsonf0517db2005-07-15 17:14:24 +1000257<*>. {
David Gibson2b67c632007-12-05 09:50:25 +1100258 DPRINT("Char: %c (\\x%02x)\n", yytext[0],
259 (unsigned)yytext[0]);
David Gibson9ed27a22007-11-07 11:16:19 +1100260 if (yytext[0] == '[') {
261 DPRINT("<BYTESTRING>\n");
262 BEGIN(BYTESTRING);
263 }
264 if ((yytext[0] == '{')
265 || (yytext[0] == ';')) {
266 DPRINT("<PROPNODENAME>\n");
267 BEGIN(PROPNODENAME);
268 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000269 return yytext[0];
270 }
271
272%%
Jon Loeligerce34ae32007-03-28 17:05:33 -0500273
David Gibson8a88ad82008-03-06 12:45:41 +1100274static void push_input_file(const char *filename)
Jon Loeligerce34ae32007-03-28 17:05:33 -0500275{
David Gibson7c635dc2008-02-26 16:44:29 +1100276 assert(filename);
Jon Loeligerce34ae32007-03-28 17:05:33 -0500277
David Gibsond68cb362009-12-08 14:24:42 +1100278 srcfile_push(filename);
Scott Wood910efac2008-01-03 17:43:31 -0600279
David Gibsond68cb362009-12-08 14:24:42 +1100280 yyin = current_srcfile->f;
David Gibsond68cb362009-12-08 14:24:42 +1100281
282 yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
Jon Loeligerce34ae32007-03-28 17:05:33 -0500283}
284
285
David Gibson17625372013-10-28 21:06:53 +1100286static bool pop_input_file(void)
Jon Loeligerce34ae32007-03-28 17:05:33 -0500287{
David Gibsond68cb362009-12-08 14:24:42 +1100288 if (srcfile_pop() == 0)
David Gibson17625372013-10-28 21:06:53 +1100289 return false;
Jon Loeligerce34ae32007-03-28 17:05:33 -0500290
David Gibsond68cb362009-12-08 14:24:42 +1100291 yypop_buffer_state();
David Gibsond68cb362009-12-08 14:24:42 +1100292 yyin = current_srcfile->f;
Jon Loeligerce34ae32007-03-28 17:05:33 -0500293
David Gibson17625372013-10-28 21:06:53 +1100294 return true;
Jon Loeligerce34ae32007-03-28 17:05:33 -0500295}
David Gibsonb82b9772014-01-03 20:00:01 +1100296
297static void lexical_error(const char *fmt, ...)
298{
299 va_list ap;
300
301 va_start(ap, fmt);
302 srcpos_verror(&yylloc, "Lexical error", fmt, ap);
303 va_end(ap);
304
305 treesource_error = true;
306}