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);
65 static void PRINTF(1, 2) lexical_error(const char *fmt, ...);
70 <*>"/include/"{WS}*{STRING} {
71 char *name = strchr(yytext, '\"') + 1;
72 yytext[yyleng-1] = '\0';
73 push_input_file(name);
76 <*>^"#"(line)?[ \t]+[0-9]+[ \t]+{STRING}([ \t]+[0-9]+)? {
77 char *line, *fnstart, *fnend;
79 /* skip text before line # */
81 while (!isdigit((unsigned char)*line))
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--)
91 assert(fnstart && fnend && (fnend > fnstart));
93 fn = data_copy_escape_string(fnstart + 1,
96 /* Don't allow nuls in filenames */
97 if (memchr(fn.val, '\0', fn.len - 1))
98 lexical_error("nul in line number directive");
100 /* -1 since #line is the number of the next line */
101 srcpos_set_line(xstrdup(fn.val), atoi(line) - 1);
106 if (!pop_input_file()) {
112 DPRINT("String: %s\n", yytext);
113 yylval.data = data_copy_escape_string(yytext+1,
119 DPRINT("Keyword: /dts-v1/\n");
126 DPRINT("Keyword: /plugin/\n");
131 DPRINT("Keyword: /memreserve/\n");
133 return DT_MEMRESERVE;
137 DPRINT("Keyword: /bits/\n");
142 <*>"/delete-property/" {
143 DPRINT("Keyword: /delete-property/\n");
144 DPRINT("<PROPNODENAME>\n");
150 DPRINT("Keyword: /delete-node/\n");
151 DPRINT("<PROPNODENAME>\n");
156 <*>"/omit-if-no-ref/" {
157 DPRINT("Keyword: /omit-if-no-ref/\n");
158 DPRINT("<PROPNODENAME>\n");
160 return DT_OMIT_NO_REF;
164 DPRINT("Label: %s\n", yytext);
165 yylval.labelref = xstrdup(yytext);
166 yylval.labelref[yyleng-1] = '\0';
170 <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
172 DPRINT("Integer Literal: '%s'\n", yytext);
175 yylval.integer = strtoull(yytext, &e, 0);
177 if (*e && e[strspn(e, "UL")]) {
178 lexical_error("Bad integer literal '%s'",
183 lexical_error("Integer literal '%s' out of range",
186 /* ERANGE is the only strtoull error triggerable
187 * by strings matching the pattern */
194 DPRINT("Character literal: %s\n", yytext);
196 d = data_copy_escape_string(yytext+1, yyleng-2);
198 lexical_error("Empty character literal");
201 yylval.integer = (unsigned char)d.val[0];
204 lexical_error("Character literal has %d"
205 " characters instead of 1",
210 return DT_CHAR_LITERAL;
213 <*>\&{LABEL} { /* label reference */
214 DPRINT("Ref: %s\n", yytext+1);
215 yylval.labelref = xstrdup(yytext+1);
219 <*>"&{/"{PATHCHAR}*\} { /* new-style path reference */
220 yytext[yyleng-1] = '\0';
221 DPRINT("Ref: %s\n", yytext+2);
222 yylval.labelref = xstrdup(yytext+2);
226 <BYTESTRING>[0-9a-fA-F]{2} {
227 yylval.byte = strtol(yytext, NULL, 16);
228 DPRINT("Byte: %02x\n", (int)yylval.byte);
233 DPRINT("/BYTESTRING\n");
238 <PROPNODENAME>\\?{PROPNODECHAR}+ {
239 DPRINT("PropNodeName: %s\n", yytext);
240 yylval.propnodename = xstrdup((yytext[0] == '\\') ?
241 yytext + 1 : yytext);
243 return DT_PROPNODENAME;
247 DPRINT("Binary Include\n");
251 <*>{WS}+ /* eat whitespace */
252 <*>{COMMENT}+ /* eat C-style comments */
253 <*>{LINECOMMENT}+ /* eat C++-style comments */
255 <*>"<<" { return DT_LSHIFT; };
256 <*>">>" { return DT_RSHIFT; };
257 <*>"<=" { return DT_LE; };
258 <*>">=" { return DT_GE; };
259 <*>"==" { return DT_EQ; };
260 <*>"!=" { return DT_NE; };
261 <*>"&&" { return DT_AND; };
262 <*>"||" { return DT_OR; };
265 DPRINT("Char: %c (\\x%02x)\n", yytext[0],
266 (unsigned)yytext[0]);
267 if (yytext[0] == '[') {
268 DPRINT("<BYTESTRING>\n");
271 if ((yytext[0] == '{')
272 || (yytext[0] == ';')) {
273 DPRINT("<PROPNODENAME>\n");
281 static void push_input_file(const char *filename)
285 srcfile_push(filename);
287 yyin = current_srcfile->f;
289 yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
293 static bool pop_input_file(void)
295 if (srcfile_pop() == 0)
298 yypop_buffer_state();
299 yyin = current_srcfile->f;
304 static void lexical_error(const char *fmt, ...)
309 srcpos_verror(&yylloc, "Lexical error", fmt, ap);
312 treesource_error = true;