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
28 PROPNODECHAR [a-zA-Z0-9,._+*#?@-]
29 PATHCHAR ({PROPNODECHAR}|[/])
30 LABEL [a-zA-Z_][a-zA-Z0-9_]*
31 STRING \"([^\\"]|\\.)*\"
32 CHAR_LITERAL '([^']|\\')*'
34 COMMENT "/*"([^*]|\*+[^*/])*\*+"/"
40 #include "dtc-parser.tab.h"
43 extern bool treesource_error;
45 /* CAUTION: this will stop working if we ever use yyless() or yyunput() */
46 #define YY_USER_ACTION \
48 srcpos_update(&yylloc, yytext, yyleng); \
51 /*#define LEXDEBUG 1*/
54 #define DPRINT(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
56 #define DPRINT(fmt, ...) do { } while (0)
59 static int dts_version = 1;
61 #define BEGIN_DEFAULT() DPRINT("<V1>\n"); \
64 static void push_input_file(const char *filename);
65 static bool pop_input_file(void);
66 static void 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, *tmp, *fn;
78 /* skip text before line # */
80 while (!isdigit((unsigned char)*line))
82 /* skip digits in line # */
84 while (!isspace((unsigned char)*tmp))
86 /* "NULL"-terminate line # */
88 /* start of filename */
89 fn = strchr(tmp + 1, '"') + 1;
90 /* strip trailing " from filename */
91 tmp = strchr(fn, '"');
93 /* -1 since #line is the number of the next line */
94 srcpos_set_line(xstrdup(fn), atoi(line) - 1);
98 if (!pop_input_file()) {
104 DPRINT("String: %s\n", yytext);
105 yylval.data = data_copy_escape_string(yytext+1,
111 DPRINT("Keyword: /dts-v1/\n");
118 DPRINT("Keyword: /memreserve/\n");
120 return DT_MEMRESERVE;
124 DPRINT("Keyword: /bits/\n");
129 <*>"/delete-property/" {
130 DPRINT("Keyword: /delete-property/\n");
131 DPRINT("<PROPNODENAME>\n");
137 DPRINT("Keyword: /delete-node/\n");
138 DPRINT("<PROPNODENAME>\n");
144 DPRINT("Label: %s\n", yytext);
145 yylval.labelref = xstrdup(yytext);
146 yylval.labelref[yyleng-1] = '\0';
150 <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
152 DPRINT("Integer Literal: '%s'\n", yytext);
155 yylval.integer = strtoull(yytext, &e, 0);
157 assert(!(*e) || !e[strspn(e, "UL")]);
160 lexical_error("Integer literal '%s' out of range",
163 /* ERANGE is the only strtoull error triggerable
164 * by strings matching the pattern */
171 DPRINT("Character literal: %s\n", yytext);
173 d = data_copy_escape_string(yytext+1, yyleng-2);
175 lexical_error("Empty character literal");
177 return DT_CHAR_LITERAL;
180 yylval.integer = (unsigned char)d.val[0];
183 lexical_error("Character literal has %d"
184 " characters instead of 1",
187 return DT_CHAR_LITERAL;
190 <*>\&{LABEL} { /* label reference */
191 DPRINT("Ref: %s\n", yytext+1);
192 yylval.labelref = xstrdup(yytext+1);
196 <*>"&{/"{PATHCHAR}+\} { /* new-style path reference */
197 yytext[yyleng-1] = '\0';
198 DPRINT("Ref: %s\n", yytext+2);
199 yylval.labelref = xstrdup(yytext+2);
203 <BYTESTRING>[0-9a-fA-F]{2} {
204 yylval.byte = strtol(yytext, NULL, 16);
205 DPRINT("Byte: %02x\n", (int)yylval.byte);
210 DPRINT("/BYTESTRING\n");
215 <PROPNODENAME>\\?{PROPNODECHAR}+ {
216 DPRINT("PropNodeName: %s\n", yytext);
217 yylval.propnodename = xstrdup((yytext[0] == '\\') ?
218 yytext + 1 : yytext);
220 return DT_PROPNODENAME;
224 DPRINT("Binary Include\n");
228 <*>{WS}+ /* eat whitespace */
229 <*>{COMMENT}+ /* eat C-style comments */
230 <*>{LINECOMMENT}+ /* eat C++-style comments */
232 <*>"<<" { return DT_LSHIFT; };
233 <*>">>" { return DT_RSHIFT; };
234 <*>"<=" { return DT_LE; };
235 <*>">=" { return DT_GE; };
236 <*>"==" { return DT_EQ; };
237 <*>"!=" { return DT_NE; };
238 <*>"&&" { return DT_AND; };
239 <*>"||" { return DT_OR; };
242 DPRINT("Char: %c (\\x%02x)\n", yytext[0],
243 (unsigned)yytext[0]);
244 if (yytext[0] == '[') {
245 DPRINT("<BYTESTRING>\n");
248 if ((yytext[0] == '{')
249 || (yytext[0] == ';')) {
250 DPRINT("<PROPNODENAME>\n");
258 static void push_input_file(const char *filename)
262 srcfile_push(filename);
264 yyin = current_srcfile->f;
266 yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
270 static bool pop_input_file(void)
272 if (srcfile_pop() == 0)
275 yypop_buffer_state();
276 yyin = current_srcfile->f;
281 static void lexical_error(const char *fmt, ...)
286 srcpos_verror(&yylloc, "Lexical error", fmt, ap);
289 treesource_error = true;