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"
42 #define MAX_INCLUDE_NESTING 100
43 YY_BUFFER_STATE include_stack[MAX_INCLUDE_NESTING];
44 int include_stack_pointer = 0;
47 extern bool treesource_error;
49 /* CAUTION: this will stop working if we ever use yyless() or yyunput() */
50 #define YY_USER_ACTION \
52 srcpos_update(&yylloc, yytext, yyleng); \
55 /*#define LEXDEBUG 1*/
58 #define DPRINT(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
60 #define DPRINT(fmt, ...) do { } while (0)
63 static int dts_version = 1;
65 #define BEGIN_DEFAULT() DPRINT("<V1>\n"); \
68 static void push_input_file(const char *filename);
69 static bool pop_input_file(void);
70 static void lexical_error(const char *fmt, ...);
74 <*>"/include/"{WS}*{STRING} {
75 char *name = strchr(yytext, '\"') + 1;
76 yytext[yyleng-1] = '\0';
77 push_input_file(name);
80 <*>^"#"(line)?[ \t]+[0-9]+[ \t]+{STRING}([ \t]+[0-9]+)? {
81 char *line, *tmp, *fn;
82 /* skip text before line # */
84 while (!isdigit((unsigned char)*line))
86 /* skip digits in line # */
88 while (!isspace((unsigned char)*tmp))
90 /* "NULL"-terminate line # */
92 /* start of filename */
93 fn = strchr(tmp + 1, '"') + 1;
94 /* strip trailing " from filename */
95 tmp = strchr(fn, '"');
97 /* -1 since #line is the number of the next line */
98 srcpos_set_line(xstrdup(fn), atoi(line) - 1);
102 if (!pop_input_file()) {
108 DPRINT("String: %s\n", yytext);
109 yylval.data = data_copy_escape_string(yytext+1,
115 DPRINT("Keyword: /dts-v1/\n");
122 DPRINT("Keyword: /memreserve/\n");
124 return DT_MEMRESERVE;
128 DPRINT("Keyword: /bits/\n");
133 <*>"/delete-property/" {
134 DPRINT("Keyword: /delete-property/\n");
135 DPRINT("<PROPNODENAME>\n");
141 DPRINT("Keyword: /delete-node/\n");
142 DPRINT("<PROPNODENAME>\n");
148 DPRINT("Label: %s\n", yytext);
149 yylval.labelref = xstrdup(yytext);
150 yylval.labelref[yyleng-1] = '\0';
154 <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
156 DPRINT("Integer Literal: '%s'\n", yytext);
159 yylval.integer = strtoull(yytext, &e, 0);
161 assert(!(*e) || !e[strspn(e, "UL")]);
164 lexical_error("Integer literal '%s' out of range",
167 /* ERANGE is the only strtoull error triggerable
168 * by strings matching the pattern */
175 DPRINT("Character literal: %s\n", yytext);
177 d = data_copy_escape_string(yytext+1, yyleng-2);
179 lexical_error("Empty character literal");
181 return DT_CHAR_LITERAL;
184 yylval.integer = (unsigned char)d.val[0];
187 lexical_error("Character literal has %d"
188 " characters instead of 1",
191 return DT_CHAR_LITERAL;
194 <*>\&{LABEL} { /* label reference */
195 DPRINT("Ref: %s\n", yytext+1);
196 yylval.labelref = xstrdup(yytext+1);
200 <*>"&{/"{PATHCHAR}+\} { /* new-style path reference */
201 yytext[yyleng-1] = '\0';
202 DPRINT("Ref: %s\n", yytext+2);
203 yylval.labelref = xstrdup(yytext+2);
207 <BYTESTRING>[0-9a-fA-F]{2} {
208 yylval.byte = strtol(yytext, NULL, 16);
209 DPRINT("Byte: %02x\n", (int)yylval.byte);
214 DPRINT("/BYTESTRING\n");
219 <PROPNODENAME>\\?{PROPNODECHAR}+ {
220 DPRINT("PropNodeName: %s\n", yytext);
221 yylval.propnodename = xstrdup((yytext[0] == '\\') ?
222 yytext + 1 : yytext);
224 return DT_PROPNODENAME;
228 DPRINT("Binary Include\n");
232 <*>{WS}+ /* eat whitespace */
233 <*>{COMMENT}+ /* eat C-style comments */
234 <*>{LINECOMMENT}+ /* eat C++-style comments */
236 <*>"<<" { return DT_LSHIFT; };
237 <*>">>" { return DT_RSHIFT; };
238 <*>"<=" { return DT_LE; };
239 <*>">=" { return DT_GE; };
240 <*>"==" { return DT_EQ; };
241 <*>"!=" { return DT_NE; };
242 <*>"&&" { return DT_AND; };
243 <*>"||" { return DT_OR; };
246 DPRINT("Char: %c (\\x%02x)\n", yytext[0],
247 (unsigned)yytext[0]);
248 if (yytext[0] == '[') {
249 DPRINT("<BYTESTRING>\n");
252 if ((yytext[0] == '{')
253 || (yytext[0] == ';')) {
254 DPRINT("<PROPNODENAME>\n");
262 static void push_input_file(const char *filename)
266 assert(include_stack_pointer < MAX_INCLUDE_NESTING);
268 srcfile_push(filename);
270 yyin = current_srcfile->f;
272 include_stack[include_stack_pointer++] = YY_CURRENT_BUFFER;
274 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
278 static bool pop_input_file(void)
280 if (srcfile_pop() == 0)
283 assert(include_stack_pointer > 0);
285 yy_delete_buffer( YY_CURRENT_BUFFER );
287 yy_switch_to_buffer( include_stack[--include_stack_pointer] );
289 yyin = current_srcfile->f;
294 static void lexical_error(const char *fmt, ...)
299 srcpos_verror(&yylloc, "Lexical error", fmt, ap);
302 treesource_error = true;