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"
41 extern bool treesource_error;
43 /* CAUTION: this will stop working if we ever use yyless() or yyunput() */
44 #define YY_USER_ACTION \
46 srcpos_update(&yylloc, yytext, yyleng); \
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 bool pop_input_file(void);
64 static void lexical_error(const char *fmt, ...);
68 <*>"/include/"{WS}*{STRING} {
69 char *name = strchr(yytext, '\"') + 1;
70 yytext[yyleng-1] = '\0';
71 push_input_file(name);
74 <*>^"#"(line)?[ \t]+[0-9]+[ \t]+{STRING}([ \t]+[0-9]+)? {
75 char *line, *tmp, *fn;
76 /* skip text before line # */
78 while (!isdigit((unsigned char)*line))
80 /* skip digits in line # */
82 while (!isspace((unsigned char)*tmp))
84 /* "NULL"-terminate line # */
86 /* start of filename */
87 fn = strchr(tmp + 1, '"') + 1;
88 /* strip trailing " from filename */
89 tmp = strchr(fn, '"');
91 /* -1 since #line is the number of the next line */
92 srcpos_set_line(xstrdup(fn), atoi(line) - 1);
96 if (!pop_input_file()) {
102 DPRINT("String: %s\n", yytext);
103 yylval.data = data_copy_escape_string(yytext+1,
109 DPRINT("Keyword: /dts-v1/\n");
116 DPRINT("Keyword: /memreserve/\n");
118 return DT_MEMRESERVE;
122 DPRINT("Keyword: /bits/\n");
127 <*>"/delete-property/" {
128 DPRINT("Keyword: /delete-property/\n");
129 DPRINT("<PROPNODENAME>\n");
135 DPRINT("Keyword: /delete-node/\n");
136 DPRINT("<PROPNODENAME>\n");
142 DPRINT("Label: %s\n", yytext);
143 yylval.labelref = xstrdup(yytext);
144 yylval.labelref[yyleng-1] = '\0';
148 <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
150 DPRINT("Integer Literal: '%s'\n", yytext);
153 yylval.integer = strtoull(yytext, &e, 0);
155 assert(!(*e) || !e[strspn(e, "UL")]);
158 lexical_error("Integer literal '%s' out of range",
161 /* ERANGE is the only strtoull error triggerable
162 * by strings matching the pattern */
169 DPRINT("Character literal: %s\n", yytext);
171 d = data_copy_escape_string(yytext+1, yyleng-2);
173 lexical_error("Empty character literal");
175 return DT_CHAR_LITERAL;
178 yylval.integer = (unsigned char)d.val[0];
181 lexical_error("Character literal has %d"
182 " characters instead of 1",
185 return DT_CHAR_LITERAL;
188 <*>\&{LABEL} { /* label reference */
189 DPRINT("Ref: %s\n", yytext+1);
190 yylval.labelref = xstrdup(yytext+1);
194 <*>"&{/"{PATHCHAR}*\} { /* new-style path reference */
195 yytext[yyleng-1] = '\0';
196 DPRINT("Ref: %s\n", yytext+2);
197 yylval.labelref = xstrdup(yytext+2);
201 <BYTESTRING>[0-9a-fA-F]{2} {
202 yylval.byte = strtol(yytext, NULL, 16);
203 DPRINT("Byte: %02x\n", (int)yylval.byte);
208 DPRINT("/BYTESTRING\n");
213 <PROPNODENAME>\\?{PROPNODECHAR}+ {
214 DPRINT("PropNodeName: %s\n", yytext);
215 yylval.propnodename = xstrdup((yytext[0] == '\\') ?
216 yytext + 1 : yytext);
218 return DT_PROPNODENAME;
222 DPRINT("Binary Include\n");
226 <*>{WS}+ /* eat whitespace */
227 <*>{COMMENT}+ /* eat C-style comments */
228 <*>{LINECOMMENT}+ /* eat C++-style comments */
230 <*>"<<" { return DT_LSHIFT; };
231 <*>">>" { return DT_RSHIFT; };
232 <*>"<=" { return DT_LE; };
233 <*>">=" { return DT_GE; };
234 <*>"==" { return DT_EQ; };
235 <*>"!=" { return DT_NE; };
236 <*>"&&" { return DT_AND; };
237 <*>"||" { return DT_OR; };
240 DPRINT("Char: %c (\\x%02x)\n", yytext[0],
241 (unsigned)yytext[0]);
242 if (yytext[0] == '[') {
243 DPRINT("<BYTESTRING>\n");
246 if ((yytext[0] == '{')
247 || (yytext[0] == ';')) {
248 DPRINT("<PROPNODENAME>\n");
256 static void push_input_file(const char *filename)
260 srcfile_push(filename);
262 yyin = current_srcfile->f;
264 yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
268 static bool pop_input_file(void)
270 if (srcfile_pop() == 0)
273 yypop_buffer_state();
274 yyin = current_srcfile->f;
279 static void lexical_error(const char *fmt, ...)
284 srcpos_verror(&yylloc, "Lexical error", fmt, ap);
287 treesource_error = true;