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 lexical_error(const char *fmt, ...);
69 <*>"/include/"{WS}*{STRING} {
70 char *name = strchr(yytext, '\"') + 1;
71 yytext[yyleng-1] = '\0';
72 push_input_file(name);
75 <*>^"#"(line)?[ \t]+[0-9]+[ \t]+{STRING}([ \t]+[0-9]+)? {
76 char *line, *tmp, *fn;
77 /* skip text before line # */
79 while (!isdigit((unsigned char)*line))
81 /* skip digits in line # */
83 while (!isspace((unsigned char)*tmp))
85 /* "NULL"-terminate line # */
87 /* start of filename */
88 fn = strchr(tmp + 1, '"') + 1;
89 /* strip trailing " from filename */
90 tmp = strchr(fn, '"');
92 /* -1 since #line is the number of the next line */
93 srcpos_set_line(xstrdup(fn), atoi(line) - 1);
97 if (!pop_input_file()) {
103 DPRINT("String: %s\n", yytext);
104 yylval.data = data_copy_escape_string(yytext+1,
110 DPRINT("Keyword: /dts-v1/\n");
117 DPRINT("Keyword: /memreserve/\n");
119 return DT_MEMRESERVE;
123 DPRINT("Keyword: /bits/\n");
128 <*>"/delete-property/" {
129 DPRINT("Keyword: /delete-property/\n");
130 DPRINT("<PROPNODENAME>\n");
136 DPRINT("Keyword: /delete-node/\n");
137 DPRINT("<PROPNODENAME>\n");
143 DPRINT("Label: %s\n", yytext);
144 yylval.labelref = xstrdup(yytext);
145 yylval.labelref[yyleng-1] = '\0';
149 <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
151 DPRINT("Integer Literal: '%s'\n", yytext);
154 yylval.integer = strtoull(yytext, &e, 0);
156 assert(!(*e) || !e[strspn(e, "UL")]);
159 lexical_error("Integer literal '%s' out of range",
162 /* ERANGE is the only strtoull error triggerable
163 * by strings matching the pattern */
170 DPRINT("Character literal: %s\n", yytext);
172 d = data_copy_escape_string(yytext+1, yyleng-2);
174 lexical_error("Empty character literal");
176 return DT_CHAR_LITERAL;
179 yylval.integer = (unsigned char)d.val[0];
182 lexical_error("Character literal has %d"
183 " characters instead of 1",
186 return DT_CHAR_LITERAL;
189 <*>\&{LABEL} { /* label reference */
190 DPRINT("Ref: %s\n", yytext+1);
191 yylval.labelref = xstrdup(yytext+1);
195 <*>"&{/"{PATHCHAR}*\} { /* new-style path reference */
196 yytext[yyleng-1] = '\0';
197 DPRINT("Ref: %s\n", yytext+2);
198 yylval.labelref = xstrdup(yytext+2);
202 <BYTESTRING>[0-9a-fA-F]{2} {
203 yylval.byte = strtol(yytext, NULL, 16);
204 DPRINT("Byte: %02x\n", (int)yylval.byte);
209 DPRINT("/BYTESTRING\n");
214 <PROPNODENAME>\\?{PROPNODECHAR}+ {
215 DPRINT("PropNodeName: %s\n", yytext);
216 yylval.propnodename = xstrdup((yytext[0] == '\\') ?
217 yytext + 1 : yytext);
219 return DT_PROPNODENAME;
223 DPRINT("Binary Include\n");
227 <*>{WS}+ /* eat whitespace */
228 <*>{COMMENT}+ /* eat C-style comments */
229 <*>{LINECOMMENT}+ /* eat C++-style comments */
231 <*>"<<" { return DT_LSHIFT; };
232 <*>">>" { return DT_RSHIFT; };
233 <*>"<=" { return DT_LE; };
234 <*>">=" { return DT_GE; };
235 <*>"==" { return DT_EQ; };
236 <*>"!=" { return DT_NE; };
237 <*>"&&" { return DT_AND; };
238 <*>"||" { return DT_OR; };
241 DPRINT("Char: %c (\\x%02x)\n", yytext[0],
242 (unsigned)yytext[0]);
243 if (yytext[0] == '[') {
244 DPRINT("<BYTESTRING>\n");
247 if ((yytext[0] == '{')
248 || (yytext[0] == ';')) {
249 DPRINT("<PROPNODENAME>\n");
257 static void push_input_file(const char *filename)
261 srcfile_push(filename);
263 yyin = current_srcfile->f;
265 yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
269 static bool pop_input_file(void)
271 if (srcfile_pop() == 0)
274 yypop_buffer_state();
275 yyin = current_srcfile->f;
280 static void lexical_error(const char *fmt, ...)
285 srcpos_verror(&yylloc, "Lexical error", fmt, ap);
288 treesource_error = true;