2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
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.
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.
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
21 %option noyywrap nounput noinput never-interactive
27 PROPNODECHAR [a-zA-Z0-9,._+*#?@-]
28 PATHCHAR ({PROPNODECHAR}|[/])
29 LABEL [a-zA-Z_][a-zA-Z0-9_]*
30 STRING \"([^\\"]|\\.)*\"
31 CHAR_LITERAL '([^']|\\')*'
33 COMMENT "/*"([^*]|\*+[^*/])*\*+"/"
39 #include "dtc-parser.tab.h"
42 extern bool treesource_error;
44 /* CAUTION: this will stop working if we ever use yyless() or yyunput() */
45 #define YY_USER_ACTION \
47 srcpos_update(&yylloc, yytext, yyleng); \
50 /*#define LEXDEBUG 1*/
53 #define DPRINT(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
55 #define DPRINT(fmt, ...) do { } while (0)
58 static int dts_version = 1;
60 #define BEGIN_DEFAULT() DPRINT("<V1>\n"); \
63 static void push_input_file(const char *filename);
64 static bool pop_input_file(void);
66 static void lexical_error(const char *fmt, ...)
67 __attribute__((format (printf, 1, 2)));
69 static void lexical_error(const char *fmt, ...);
75 <*>"/include/"{WS}*{STRING} {
76 char *name = strchr(yytext, '\"') + 1;
77 yytext[yyleng-1] = '\0';
78 push_input_file(name);
81 <*>^"#"(line)?[ \t]+[0-9]+[ \t]+{STRING}([ \t]+[0-9]+)? {
82 char *line, *fnstart, *fnend;
84 /* skip text before line # */
86 while (!isdigit((unsigned char)*line))
89 /* regexp ensures that first and list "
90 * in the whole yytext are those at
91 * beginning and end of the filename string */
92 fnstart = memchr(yytext, '"', yyleng);
93 for (fnend = yytext + yyleng - 1;
94 *fnend != '"'; fnend--)
96 assert(fnstart && fnend && (fnend > fnstart));
98 fn = data_copy_escape_string(fnstart + 1,
101 /* Don't allow nuls in filenames */
102 if (memchr(fn.val, '\0', fn.len - 1))
103 lexical_error("nul in line number directive");
105 /* -1 since #line is the number of the next line */
106 srcpos_set_line(xstrdup(fn.val), atoi(line) - 1);
111 if (!pop_input_file()) {
117 DPRINT("String: %s\n", yytext);
118 yylval.data = data_copy_escape_string(yytext+1,
124 DPRINT("Keyword: /dts-v1/\n");
131 DPRINT("Keyword: /plugin/\n");
136 DPRINT("Keyword: /memreserve/\n");
138 return DT_MEMRESERVE;
142 DPRINT("Keyword: /bits/\n");
147 <*>"/delete-property/" {
148 DPRINT("Keyword: /delete-property/\n");
149 DPRINT("<PROPNODENAME>\n");
155 DPRINT("Keyword: /delete-node/\n");
156 DPRINT("<PROPNODENAME>\n");
162 DPRINT("Label: %s\n", yytext);
163 yylval.labelref = xstrdup(yytext);
164 yylval.labelref[yyleng-1] = '\0';
168 <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
170 DPRINT("Integer Literal: '%s'\n", yytext);
173 yylval.integer = strtoull(yytext, &e, 0);
175 if (*e && e[strspn(e, "UL")]) {
176 lexical_error("Bad integer literal '%s'",
181 lexical_error("Integer literal '%s' out of range",
184 /* ERANGE is the only strtoull error triggerable
185 * by strings matching the pattern */
192 DPRINT("Character literal: %s\n", yytext);
194 d = data_copy_escape_string(yytext+1, yyleng-2);
196 lexical_error("Empty character literal");
199 yylval.integer = (unsigned char)d.val[0];
202 lexical_error("Character literal has %d"
203 " characters instead of 1",
208 return DT_CHAR_LITERAL;
211 <*>\&{LABEL} { /* label reference */
212 DPRINT("Ref: %s\n", yytext+1);
213 yylval.labelref = xstrdup(yytext+1);
217 <*>"&{/"{PATHCHAR}*\} { /* new-style path reference */
218 yytext[yyleng-1] = '\0';
219 DPRINT("Ref: %s\n", yytext+2);
220 yylval.labelref = xstrdup(yytext+2);
224 <BYTESTRING>[0-9a-fA-F]{2} {
225 yylval.byte = strtol(yytext, NULL, 16);
226 DPRINT("Byte: %02x\n", (int)yylval.byte);
231 DPRINT("/BYTESTRING\n");
236 <PROPNODENAME>\\?{PROPNODECHAR}+ {
237 DPRINT("PropNodeName: %s\n", yytext);
238 yylval.propnodename = xstrdup((yytext[0] == '\\') ?
239 yytext + 1 : yytext);
241 return DT_PROPNODENAME;
245 DPRINT("Binary Include\n");
249 <*>{WS}+ /* eat whitespace */
250 <*>{COMMENT}+ /* eat C-style comments */
251 <*>{LINECOMMENT}+ /* eat C++-style comments */
253 <*>"<<" { return DT_LSHIFT; };
254 <*>">>" { return DT_RSHIFT; };
255 <*>"<=" { return DT_LE; };
256 <*>">=" { return DT_GE; };
257 <*>"==" { return DT_EQ; };
258 <*>"!=" { return DT_NE; };
259 <*>"&&" { return DT_AND; };
260 <*>"||" { return DT_OR; };
263 DPRINT("Char: %c (\\x%02x)\n", yytext[0],
264 (unsigned)yytext[0]);
265 if (yytext[0] == '[') {
266 DPRINT("<BYTESTRING>\n");
269 if ((yytext[0] == '{')
270 || (yytext[0] == ';')) {
271 DPRINT("<PROPNODENAME>\n");
279 static void push_input_file(const char *filename)
283 srcfile_push(filename);
285 yyin = current_srcfile->f;
287 yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
291 static bool pop_input_file(void)
293 if (srcfile_pop() == 0)
296 yypop_buffer_state();
297 yyin = current_srcfile->f;
302 static void lexical_error(const char *fmt, ...)
307 srcpos_verror(&yylloc, "Lexical error", fmt, ap);
310 treesource_error = true;