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 yylineno
28 PROPNODECHAR [a-zA-Z0-9,._+*#?@-]
29 PATHCHAR ({PROPNODECHAR}|[/])
30 LABEL [a-zA-Z_][a-zA-Z0-9_]*
31 STRING \"([^\\"]|\\.)*\"
33 COMMENT "/*"([^*]|\*+[^*/])*\*+"/"
39 #include "dtc-parser.tab.h"
42 /*#define LEXDEBUG 1*/
45 #define DPRINT(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
47 #define DPRINT(fmt, ...) do { } while (0)
50 static int dts_version; /* = 0 */
52 #define BEGIN_DEFAULT() if (dts_version == 0) { \
53 DPRINT("<INITIAL>\n"); \
60 static void push_input_file(const char *filename);
61 static int pop_input_file(void);
65 <*>"/include/"{WS}*{STRING} {
66 char *name = strchr(yytext, '\"') + 1;
67 yytext[yyleng-1] = '\0';
68 push_input_file(name);
72 if (!pop_input_file()) {
78 yylloc.file = srcpos_file;
79 yylloc.first_line = yylineno;
80 DPRINT("String: %s\n", yytext);
81 yylval.data = data_copy_escape_string(yytext+1,
83 yylloc.first_line = yylineno;
88 yylloc.file = srcpos_file;
89 yylloc.first_line = yylineno;
90 DPRINT("Keyword: /dts-v1/\n");
97 yylloc.file = srcpos_file;
98 yylloc.first_line = yylineno;
99 DPRINT("Keyword: /memreserve/\n");
101 return DT_MEMRESERVE;
105 yylloc.file = srcpos_file;
106 yylloc.first_line = yylineno;
107 DPRINT("Label: %s\n", yytext);
108 yylval.labelref = strdup(yytext);
109 yylval.labelref[yyleng-1] = '\0';
114 yylloc.file = srcpos_file;
115 yylloc.first_line = yylineno;
118 else if (*yytext == 'o')
120 else if (*yytext == 'd')
124 DPRINT("Base: %d\n", yylval.cbase);
128 <INITIAL>[0-9a-fA-F]+ {
129 yylloc.file = srcpos_file;
130 yylloc.first_line = yylineno;
131 yylval.literal = strdup(yytext);
132 DPRINT("Literal: '%s'\n", yylval.literal);
133 return DT_LEGACYLITERAL;
136 <V1>[0-9]+|0[xX][0-9a-fA-F]+ {
137 yylloc.file = srcpos_file;
138 yylloc.first_line = yylineno;
139 yylval.literal = strdup(yytext);
140 DPRINT("Literal: '%s'\n", yylval.literal);
144 \&{LABEL} { /* label reference */
145 yylloc.file = srcpos_file;
146 yylloc.first_line = yylineno;
147 DPRINT("Ref: %s\n", yytext+1);
148 yylval.labelref = strdup(yytext+1);
152 "&{/"{PATHCHAR}+\} { /* new-style path reference */
153 yylloc.file = srcpos_file;
154 yylloc.first_line = yylineno;
155 yytext[yyleng-1] = '\0';
156 DPRINT("Ref: %s\n", yytext+2);
157 yylval.labelref = strdup(yytext+2);
161 <INITIAL>"&/"{PATHCHAR}+ { /* old-style path reference */
162 yylloc.file = srcpos_file;
163 yylloc.first_line = yylineno;
164 DPRINT("Ref: %s\n", yytext+1);
165 yylval.labelref = strdup(yytext+1);
169 <BYTESTRING>[0-9a-fA-F]{2} {
170 yylloc.file = srcpos_file;
171 yylloc.first_line = yylineno;
172 yylval.byte = strtol(yytext, NULL, 16);
173 DPRINT("Byte: %02x\n", (int)yylval.byte);
178 yylloc.file = srcpos_file;
179 yylloc.first_line = yylineno;
180 DPRINT("/BYTESTRING\n");
185 <PROPNODENAME>{PROPNODECHAR}+ {
186 yylloc.file = srcpos_file;
187 yylloc.first_line = yylineno;
188 DPRINT("PropNodeName: %s\n", yytext);
189 yylval.propnodename = strdup(yytext);
191 return DT_PROPNODENAME;
195 yylloc.file = srcpos_file;
196 yylloc.first_line = yylineno;
197 DPRINT("Binary Include\n");
201 <*>{WS}+ /* eat whitespace */
202 <*>{COMMENT}+ /* eat C-style comments */
203 <*>{LINECOMMENT}+ /* eat C++-style comments */
206 yylloc.file = srcpos_file;
207 yylloc.first_line = yylineno;
208 DPRINT("Char: %c (\\x%02x)\n", yytext[0],
209 (unsigned)yytext[0]);
210 if (yytext[0] == '[') {
211 DPRINT("<BYTESTRING>\n");
214 if ((yytext[0] == '{')
215 || (yytext[0] == ';')) {
216 DPRINT("<PROPNODENAME>\n");
226 * Stack of nested include file contexts.
230 struct dtc_file *file;
231 YY_BUFFER_STATE yy_prev_buf;
233 struct incl_file *prev;
236 static struct incl_file *incl_file_stack;
240 * Detect infinite include recursion.
242 #define MAX_INCLUDE_DEPTH (100)
244 static int incl_depth = 0;
247 static void push_input_file(const char *filename)
249 struct incl_file *incl_file;
250 struct dtc_file *newfile;
251 struct search_path search, *searchptr = NULL;
255 if (incl_depth++ >= MAX_INCLUDE_DEPTH)
256 die("Includes nested too deeply");
259 search.dir = srcpos_file->dir;
265 newfile = dtc_open_file(filename, searchptr);
267 incl_file = xmalloc(sizeof(struct incl_file));
270 * Save current context.
272 incl_file->yy_prev_buf = YY_CURRENT_BUFFER;
273 incl_file->yy_prev_lineno = yylineno;
274 incl_file->file = srcpos_file;
275 incl_file->prev = incl_file_stack;
277 incl_file_stack = incl_file;
280 * Establish new context.
282 srcpos_file = newfile;
284 yyin = newfile->file;
285 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
289 static int pop_input_file(void)
291 struct incl_file *incl_file;
293 if (incl_file_stack == 0)
296 dtc_close_file(srcpos_file);
302 incl_file = incl_file_stack;
303 incl_file_stack = incl_file->prev;
306 * Recover old context.
308 yy_delete_buffer(YY_CURRENT_BUFFER);
309 yy_switch_to_buffer(incl_file->yy_prev_buf);
310 yylineno = incl_file->yy_prev_lineno;
311 srcpos_file = incl_file->file;
312 yyin = incl_file->file ? incl_file->file->file : NULL;