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 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"
43 #define YY_USER_ACTION \
45 yylloc.file = srcpos_file; \
46 yylloc.first_line = yylineno; \
49 /*#define LEXDEBUG 1*/
52 #define DPRINT(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
54 #define DPRINT(fmt, ...) do { } while (0)
57 static int dts_version = 1;
59 #define BEGIN_DEFAULT() DPRINT("<V1>\n"); \
62 static void push_input_file(const char *filename);
63 static int pop_input_file(void);
67 <*>"/include/"{WS}*{STRING} {
68 char *name = strchr(yytext, '\"') + 1;
69 yytext[yyleng-1] = '\0';
70 push_input_file(name);
74 if (!pop_input_file()) {
80 DPRINT("String: %s\n", yytext);
81 yylval.data = data_copy_escape_string(yytext+1,
87 DPRINT("Keyword: /dts-v1/\n");
94 DPRINT("Keyword: /memreserve/\n");
100 DPRINT("Label: %s\n", yytext);
101 yylval.labelref = xstrdup(yytext);
102 yylval.labelref[yyleng-1] = '\0';
106 <V1>[0-9]+|0[xX][0-9a-fA-F]+ {
107 yylval.literal = xstrdup(yytext);
108 DPRINT("Literal: '%s'\n", yylval.literal);
112 \&{LABEL} { /* label reference */
113 DPRINT("Ref: %s\n", yytext+1);
114 yylval.labelref = xstrdup(yytext+1);
118 "&{/"{PATHCHAR}+\} { /* new-style path reference */
119 yytext[yyleng-1] = '\0';
120 DPRINT("Ref: %s\n", yytext+2);
121 yylval.labelref = xstrdup(yytext+2);
125 <BYTESTRING>[0-9a-fA-F]{2} {
126 yylval.byte = strtol(yytext, NULL, 16);
127 DPRINT("Byte: %02x\n", (int)yylval.byte);
132 DPRINT("/BYTESTRING\n");
137 <PROPNODENAME>{PROPNODECHAR}+ {
138 DPRINT("PropNodeName: %s\n", yytext);
139 yylval.propnodename = xstrdup(yytext);
141 return DT_PROPNODENAME;
145 DPRINT("Binary Include\n");
149 <*>{WS}+ /* eat whitespace */
150 <*>{COMMENT}+ /* eat C-style comments */
151 <*>{LINECOMMENT}+ /* eat C++-style comments */
154 DPRINT("Char: %c (\\x%02x)\n", yytext[0],
155 (unsigned)yytext[0]);
156 if (yytext[0] == '[') {
157 DPRINT("<BYTESTRING>\n");
160 if ((yytext[0] == '{')
161 || (yytext[0] == ';')) {
162 DPRINT("<PROPNODENAME>\n");
172 * Stack of nested include file contexts.
176 struct dtc_file *file;
177 YY_BUFFER_STATE yy_prev_buf;
179 struct incl_file *prev;
182 static struct incl_file *incl_file_stack;
186 * Detect infinite include recursion.
188 #define MAX_INCLUDE_DEPTH (100)
190 static int incl_depth = 0;
193 static void push_input_file(const char *filename)
195 struct incl_file *incl_file;
196 struct dtc_file *newfile;
197 struct search_path search, *searchptr = NULL;
201 if (incl_depth++ >= MAX_INCLUDE_DEPTH)
202 die("Includes nested too deeply");
205 search.dir = srcpos_file->dir;
211 newfile = dtc_open_file(filename, searchptr);
213 incl_file = xmalloc(sizeof(struct incl_file));
216 * Save current context.
218 incl_file->yy_prev_buf = YY_CURRENT_BUFFER;
219 incl_file->yy_prev_lineno = yylineno;
220 incl_file->file = srcpos_file;
221 incl_file->prev = incl_file_stack;
223 incl_file_stack = incl_file;
226 * Establish new context.
228 srcpos_file = newfile;
230 yyin = newfile->file;
231 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
235 static int pop_input_file(void)
237 struct incl_file *incl_file;
239 if (incl_file_stack == 0)
242 dtc_close_file(srcpos_file);
248 incl_file = incl_file_stack;
249 incl_file_stack = incl_file->prev;
252 * Recover old context.
254 yy_delete_buffer(YY_CURRENT_BUFFER);
255 yy_switch_to_buffer(incl_file->yy_prev_buf);
256 yylineno = incl_file->yy_prev_lineno;
257 srcpos_file = incl_file->file;
258 yyin = incl_file->file ? incl_file->file->file : NULL;