Commit | Line | Data |
---|---|---|
a4da2e3e DG |
1 | /* |
2 | * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005. | |
3 | * | |
4 | * | |
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. | |
9 | * | |
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. | |
14 | * | |
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 | |
18 | * USA | |
19 | */ | |
20 | ||
23c4ace5 | 21 | %option noyywrap noinput nounput yylineno |
a4da2e3e DG |
22 | |
23 | %x INCLUDE | |
24 | %x BYTESTRING | |
25 | %x PROPNODENAME | |
26 | %s V1 | |
27 | ||
28 | PROPNODECHAR [a-zA-Z0-9,._+*#?@-] | |
29 | PATHCHAR ({PROPNODECHAR}|[/]) | |
30 | LABEL [a-zA-Z_][a-zA-Z0-9_]* | |
ed95d745 DG |
31 | STRING \"([^\\"]|\\.)*\" |
32 | WS [[:space:]] | |
33 | COMMENT "/*"([^*]|\*+[^*/])*\*+"/" | |
34 | LINECOMMENT "//".*\n | |
a4da2e3e DG |
35 | |
36 | %{ | |
37 | #include "dtc.h" | |
38 | #include "srcpos.h" | |
39 | #include "dtc-parser.tab.h" | |
40 | ||
41 | ||
42 | /*#define LEXDEBUG 1*/ | |
43 | ||
44 | #ifdef LEXDEBUG | |
45 | #define DPRINT(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__) | |
46 | #else | |
47 | #define DPRINT(fmt, ...) do { } while (0) | |
48 | #endif | |
49 | ||
50 | static int dts_version; /* = 0 */ | |
51 | ||
52 | #define BEGIN_DEFAULT() if (dts_version == 0) { \ | |
53 | DPRINT("<INITIAL>\n"); \ | |
54 | BEGIN(INITIAL); \ | |
55 | } else { \ | |
56 | DPRINT("<V1>\n"); \ | |
57 | BEGIN(V1); \ | |
58 | } | |
ed95d745 DG |
59 | |
60 | static void push_input_file(const char *filename); | |
61 | static int pop_input_file(void); | |
a4da2e3e DG |
62 | %} |
63 | ||
64 | %% | |
ed95d745 DG |
65 | <*>"/include/"{WS}*{STRING} { |
66 | char *name = strchr(yytext, '\"') + 1; | |
67 | yytext[yyleng-1] = '\0'; | |
68 | push_input_file(name); | |
a4da2e3e DG |
69 | } |
70 | ||
a4da2e3e DG |
71 | <*><<EOF>> { |
72 | if (!pop_input_file()) { | |
73 | yyterminate(); | |
74 | } | |
75 | } | |
76 | ||
ed95d745 DG |
77 | <*>{STRING} { |
78 | yylloc.file = srcpos_file; | |
a4da2e3e DG |
79 | yylloc.first_line = yylineno; |
80 | DPRINT("String: %s\n", yytext); | |
81 | yylval.data = data_copy_escape_string(yytext+1, | |
82 | yyleng-2); | |
83 | yylloc.first_line = yylineno; | |
84 | return DT_STRING; | |
85 | } | |
86 | ||
87 | <*>"/dts-v1/" { | |
ed95d745 | 88 | yylloc.file = srcpos_file; |
a4da2e3e DG |
89 | yylloc.first_line = yylineno; |
90 | DPRINT("Keyword: /dts-v1/\n"); | |
91 | dts_version = 1; | |
92 | BEGIN_DEFAULT(); | |
93 | return DT_V1; | |
94 | } | |
95 | ||
96 | <*>"/memreserve/" { | |
ed95d745 | 97 | yylloc.file = srcpos_file; |
a4da2e3e DG |
98 | yylloc.first_line = yylineno; |
99 | DPRINT("Keyword: /memreserve/\n"); | |
100 | BEGIN_DEFAULT(); | |
101 | return DT_MEMRESERVE; | |
102 | } | |
103 | ||
104 | <*>{LABEL}: { | |
ed95d745 | 105 | yylloc.file = srcpos_file; |
a4da2e3e DG |
106 | yylloc.first_line = yylineno; |
107 | DPRINT("Label: %s\n", yytext); | |
108 | yylval.labelref = strdup(yytext); | |
109 | yylval.labelref[yyleng-1] = '\0'; | |
110 | return DT_LABEL; | |
111 | } | |
112 | ||
113 | <INITIAL>[bodh]# { | |
ed95d745 | 114 | yylloc.file = srcpos_file; |
a4da2e3e DG |
115 | yylloc.first_line = yylineno; |
116 | if (*yytext == 'b') | |
117 | yylval.cbase = 2; | |
118 | else if (*yytext == 'o') | |
119 | yylval.cbase = 8; | |
120 | else if (*yytext == 'd') | |
121 | yylval.cbase = 10; | |
122 | else | |
123 | yylval.cbase = 16; | |
124 | DPRINT("Base: %d\n", yylval.cbase); | |
125 | return DT_BASE; | |
126 | } | |
127 | ||
128 | <INITIAL>[0-9a-fA-F]+ { | |
ed95d745 | 129 | yylloc.file = srcpos_file; |
a4da2e3e DG |
130 | yylloc.first_line = yylineno; |
131 | yylval.literal = strdup(yytext); | |
132 | DPRINT("Literal: '%s'\n", yylval.literal); | |
133 | return DT_LEGACYLITERAL; | |
134 | } | |
135 | ||
136 | <V1>[0-9]+|0[xX][0-9a-fA-F]+ { | |
ed95d745 | 137 | yylloc.file = srcpos_file; |
a4da2e3e DG |
138 | yylloc.first_line = yylineno; |
139 | yylval.literal = strdup(yytext); | |
140 | DPRINT("Literal: '%s'\n", yylval.literal); | |
141 | return DT_LITERAL; | |
142 | } | |
143 | ||
144 | \&{LABEL} { /* label reference */ | |
ed95d745 | 145 | yylloc.file = srcpos_file; |
a4da2e3e DG |
146 | yylloc.first_line = yylineno; |
147 | DPRINT("Ref: %s\n", yytext+1); | |
148 | yylval.labelref = strdup(yytext+1); | |
149 | return DT_REF; | |
150 | } | |
151 | ||
152 | "&{/"{PATHCHAR}+\} { /* new-style path reference */ | |
ed95d745 | 153 | yylloc.file = srcpos_file; |
a4da2e3e DG |
154 | yylloc.first_line = yylineno; |
155 | yytext[yyleng-1] = '\0'; | |
156 | DPRINT("Ref: %s\n", yytext+2); | |
157 | yylval.labelref = strdup(yytext+2); | |
158 | return DT_REF; | |
159 | } | |
160 | ||
161 | <INITIAL>"&/"{PATHCHAR}+ { /* old-style path reference */ | |
ed95d745 | 162 | yylloc.file = srcpos_file; |
a4da2e3e DG |
163 | yylloc.first_line = yylineno; |
164 | DPRINT("Ref: %s\n", yytext+1); | |
165 | yylval.labelref = strdup(yytext+1); | |
166 | return DT_REF; | |
167 | } | |
168 | ||
169 | <BYTESTRING>[0-9a-fA-F]{2} { | |
ed95d745 | 170 | yylloc.file = srcpos_file; |
a4da2e3e DG |
171 | yylloc.first_line = yylineno; |
172 | yylval.byte = strtol(yytext, NULL, 16); | |
173 | DPRINT("Byte: %02x\n", (int)yylval.byte); | |
174 | return DT_BYTE; | |
175 | } | |
176 | ||
177 | <BYTESTRING>"]" { | |
ed95d745 | 178 | yylloc.file = srcpos_file; |
a4da2e3e DG |
179 | yylloc.first_line = yylineno; |
180 | DPRINT("/BYTESTRING\n"); | |
181 | BEGIN_DEFAULT(); | |
182 | return ']'; | |
183 | } | |
184 | ||
185 | <PROPNODENAME>{PROPNODECHAR}+ { | |
ed95d745 | 186 | yylloc.file = srcpos_file; |
a4da2e3e DG |
187 | yylloc.first_line = yylineno; |
188 | DPRINT("PropNodeName: %s\n", yytext); | |
189 | yylval.propnodename = strdup(yytext); | |
190 | BEGIN_DEFAULT(); | |
191 | return DT_PROPNODENAME; | |
192 | } | |
193 | ||
ed95d745 DG |
194 | "/incbin/" { |
195 | yylloc.file = srcpos_file; | |
a4da2e3e | 196 | yylloc.first_line = yylineno; |
ed95d745 DG |
197 | DPRINT("Binary Include\n"); |
198 | return DT_INCBIN; | |
a4da2e3e DG |
199 | } |
200 | ||
ed95d745 DG |
201 | <*>{WS}+ /* eat whitespace */ |
202 | <*>{COMMENT}+ /* eat C-style comments */ | |
203 | <*>{LINECOMMENT}+ /* eat C++-style comments */ | |
a4da2e3e DG |
204 | |
205 | <*>. { | |
ed95d745 | 206 | yylloc.file = srcpos_file; |
a4da2e3e DG |
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"); | |
212 | BEGIN(BYTESTRING); | |
213 | } | |
214 | if ((yytext[0] == '{') | |
215 | || (yytext[0] == ';')) { | |
216 | DPRINT("<PROPNODENAME>\n"); | |
217 | BEGIN(PROPNODENAME); | |
218 | } | |
219 | return yytext[0]; | |
220 | } | |
221 | ||
222 | %% | |
223 | ||
224 | ||
225 | /* | |
226 | * Stack of nested include file contexts. | |
227 | */ | |
228 | ||
229 | struct incl_file { | |
ed95d745 | 230 | struct dtc_file *file; |
a4da2e3e DG |
231 | YY_BUFFER_STATE yy_prev_buf; |
232 | int yy_prev_lineno; | |
233 | struct incl_file *prev; | |
234 | }; | |
235 | ||
ed95d745 | 236 | static struct incl_file *incl_file_stack; |
a4da2e3e DG |
237 | |
238 | ||
239 | /* | |
240 | * Detect infinite include recursion. | |
241 | */ | |
242 | #define MAX_INCLUDE_DEPTH (100) | |
243 | ||
244 | static int incl_depth = 0; | |
245 | ||
246 | ||
ed95d745 | 247 | static void push_input_file(const char *filename) |
a4da2e3e | 248 | { |
a4da2e3e | 249 | struct incl_file *incl_file; |
ed95d745 DG |
250 | struct dtc_file *newfile; |
251 | struct search_path search, *searchptr = NULL; | |
a4da2e3e | 252 | |
ed95d745 | 253 | assert(filename); |
a4da2e3e | 254 | |
ed95d745 DG |
255 | if (incl_depth++ >= MAX_INCLUDE_DEPTH) |
256 | die("Includes nested too deeply"); | |
257 | ||
258 | if (srcpos_file) { | |
259 | search.dir = srcpos_file->dir; | |
260 | search.next = NULL; | |
261 | search.prev = NULL; | |
262 | searchptr = &search; | |
a4da2e3e DG |
263 | } |
264 | ||
ed95d745 | 265 | newfile = dtc_open_file(filename, searchptr); |
a4da2e3e | 266 | |
ed95d745 | 267 | incl_file = xmalloc(sizeof(struct incl_file)); |
a4da2e3e DG |
268 | |
269 | /* | |
270 | * Save current context. | |
271 | */ | |
272 | incl_file->yy_prev_buf = YY_CURRENT_BUFFER; | |
273 | incl_file->yy_prev_lineno = yylineno; | |
ed95d745 | 274 | incl_file->file = srcpos_file; |
a4da2e3e DG |
275 | incl_file->prev = incl_file_stack; |
276 | ||
277 | incl_file_stack = incl_file; | |
278 | ||
279 | /* | |
280 | * Establish new context. | |
281 | */ | |
ed95d745 | 282 | srcpos_file = newfile; |
a4da2e3e | 283 | yylineno = 1; |
ed95d745 | 284 | yyin = newfile->file; |
a4da2e3e | 285 | yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE)); |
a4da2e3e DG |
286 | } |
287 | ||
288 | ||
ed95d745 | 289 | static int pop_input_file(void) |
a4da2e3e DG |
290 | { |
291 | struct incl_file *incl_file; | |
292 | ||
293 | if (incl_file_stack == 0) | |
294 | return 0; | |
295 | ||
ed95d745 | 296 | dtc_close_file(srcpos_file); |
a4da2e3e DG |
297 | |
298 | /* | |
299 | * Pop. | |
300 | */ | |
301 | --incl_depth; | |
302 | incl_file = incl_file_stack; | |
303 | incl_file_stack = incl_file->prev; | |
304 | ||
305 | /* | |
306 | * Recover old context. | |
307 | */ | |
308 | yy_delete_buffer(YY_CURRENT_BUFFER); | |
309 | yy_switch_to_buffer(incl_file->yy_prev_buf); | |
310 | yylineno = incl_file->yy_prev_lineno; | |
ed95d745 DG |
311 | srcpos_file = incl_file->file; |
312 | yyin = incl_file->file ? incl_file->file->file : NULL; | |
a4da2e3e DG |
313 | |
314 | /* | |
315 | * Free old state. | |
316 | */ | |
317 | free(incl_file); | |
318 | ||
a4da2e3e DG |
319 | return 1; |
320 | } |