blob: e866ea5166ac40a140092c4987170b48a04ef398 [file] [log] [blame]
David Gibsona4da2e32007-12-18 15:06:42 +11001/*
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
John Bonesio658f29a2010-11-17 15:28:20 -080021%option noyywrap nounput noinput never-interactive
David Gibsona4da2e32007-12-18 15:06:42 +110022
23%x INCLUDE
24%x BYTESTRING
25%x PROPNODENAME
26%s V1
27
28PROPNODECHAR [a-zA-Z0-9,._+*#?@-]
29PATHCHAR ({PROPNODECHAR}|[/])
30LABEL [a-zA-Z_][a-zA-Z0-9_]*
David Gibsoned95d742008-08-07 12:24:17 +100031STRING \"([^\\"]|\\.)*\"
32WS [[:space:]]
33COMMENT "/*"([^*]|\*+[^*/])*\*+"/"
34LINECOMMENT "//".*\n
David Gibsona4da2e32007-12-18 15:06:42 +110035
36%{
37#include "dtc.h"
38#include "srcpos.h"
39#include "dtc-parser.tab.h"
40
John Bonesio658f29a2010-11-17 15:28:20 -080041YYLTYPE yylloc;
42
43/* CAUTION: this will stop working if we ever use yyless() or yyunput() */
44#define YY_USER_ACTION \
45 { \
46 srcpos_update(&yylloc, yytext, yyleng); \
47 }
David Gibsona4da2e32007-12-18 15:06:42 +110048
49/*#define LEXDEBUG 1*/
50
51#ifdef LEXDEBUG
52#define DPRINT(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
53#else
54#define DPRINT(fmt, ...) do { } while (0)
55#endif
56
John Bonesio658f29a2010-11-17 15:28:20 -080057static int dts_version = 1;
David Gibsona4da2e32007-12-18 15:06:42 +110058
John Bonesio658f29a2010-11-17 15:28:20 -080059#define BEGIN_DEFAULT() DPRINT("<V1>\n"); \
David Gibsona4da2e32007-12-18 15:06:42 +110060 BEGIN(V1); \
David Gibsoned95d742008-08-07 12:24:17 +100061
62static void push_input_file(const char *filename);
63static int pop_input_file(void);
David Gibsona4da2e32007-12-18 15:06:42 +110064%}
65
66%%
David Gibsoned95d742008-08-07 12:24:17 +100067<*>"/include/"{WS}*{STRING} {
68 char *name = strchr(yytext, '\"') + 1;
69 yytext[yyleng-1] = '\0';
70 push_input_file(name);
David Gibsona4da2e32007-12-18 15:06:42 +110071 }
72
David Gibsona4da2e32007-12-18 15:06:42 +110073<*><<EOF>> {
74 if (!pop_input_file()) {
75 yyterminate();
76 }
77 }
78
David Gibsoned95d742008-08-07 12:24:17 +100079<*>{STRING} {
David Gibsona4da2e32007-12-18 15:06:42 +110080 DPRINT("String: %s\n", yytext);
81 yylval.data = data_copy_escape_string(yytext+1,
82 yyleng-2);
David Gibsona4da2e32007-12-18 15:06:42 +110083 return DT_STRING;
84 }
85
86<*>"/dts-v1/" {
David Gibsona4da2e32007-12-18 15:06:42 +110087 DPRINT("Keyword: /dts-v1/\n");
88 dts_version = 1;
89 BEGIN_DEFAULT();
90 return DT_V1;
91 }
92
93<*>"/memreserve/" {
David Gibsona4da2e32007-12-18 15:06:42 +110094 DPRINT("Keyword: /memreserve/\n");
95 BEGIN_DEFAULT();
96 return DT_MEMRESERVE;
97 }
98
99<*>{LABEL}: {
David Gibsona4da2e32007-12-18 15:06:42 +1100100 DPRINT("Label: %s\n", yytext);
John Bonesio658f29a2010-11-17 15:28:20 -0800101 yylval.labelref = xstrdup(yytext);
David Gibsona4da2e32007-12-18 15:06:42 +1100102 yylval.labelref[yyleng-1] = '\0';
103 return DT_LABEL;
104 }
105
David Gibsona4da2e32007-12-18 15:06:42 +1100106<V1>[0-9]+|0[xX][0-9a-fA-F]+ {
John Bonesio658f29a2010-11-17 15:28:20 -0800107 yylval.literal = xstrdup(yytext);
David Gibsona4da2e32007-12-18 15:06:42 +1100108 DPRINT("Literal: '%s'\n", yylval.literal);
109 return DT_LITERAL;
110 }
111
John Bonesio658f29a2010-11-17 15:28:20 -0800112<*>\&{LABEL} { /* label reference */
David Gibsona4da2e32007-12-18 15:06:42 +1100113 DPRINT("Ref: %s\n", yytext+1);
John Bonesio658f29a2010-11-17 15:28:20 -0800114 yylval.labelref = xstrdup(yytext+1);
David Gibsona4da2e32007-12-18 15:06:42 +1100115 return DT_REF;
116 }
117
John Bonesio658f29a2010-11-17 15:28:20 -0800118<*>"&{/"{PATHCHAR}+\} { /* new-style path reference */
David Gibsona4da2e32007-12-18 15:06:42 +1100119 yytext[yyleng-1] = '\0';
120 DPRINT("Ref: %s\n", yytext+2);
John Bonesio658f29a2010-11-17 15:28:20 -0800121 yylval.labelref = xstrdup(yytext+2);
David Gibsona4da2e32007-12-18 15:06:42 +1100122 return DT_REF;
123 }
124
125<BYTESTRING>[0-9a-fA-F]{2} {
David Gibsona4da2e32007-12-18 15:06:42 +1100126 yylval.byte = strtol(yytext, NULL, 16);
127 DPRINT("Byte: %02x\n", (int)yylval.byte);
128 return DT_BYTE;
129 }
130
131<BYTESTRING>"]" {
David Gibsona4da2e32007-12-18 15:06:42 +1100132 DPRINT("/BYTESTRING\n");
133 BEGIN_DEFAULT();
134 return ']';
135 }
136
137<PROPNODENAME>{PROPNODECHAR}+ {
David Gibsona4da2e32007-12-18 15:06:42 +1100138 DPRINT("PropNodeName: %s\n", yytext);
John Bonesio658f29a2010-11-17 15:28:20 -0800139 yylval.propnodename = xstrdup(yytext);
David Gibsona4da2e32007-12-18 15:06:42 +1100140 BEGIN_DEFAULT();
141 return DT_PROPNODENAME;
142 }
143
David Gibsoned95d742008-08-07 12:24:17 +1000144"/incbin/" {
David Gibsoned95d742008-08-07 12:24:17 +1000145 DPRINT("Binary Include\n");
146 return DT_INCBIN;
David Gibsona4da2e32007-12-18 15:06:42 +1100147 }
148
David Gibsoned95d742008-08-07 12:24:17 +1000149<*>{WS}+ /* eat whitespace */
150<*>{COMMENT}+ /* eat C-style comments */
151<*>{LINECOMMENT}+ /* eat C++-style comments */
David Gibsona4da2e32007-12-18 15:06:42 +1100152
153<*>. {
David Gibsona4da2e32007-12-18 15:06:42 +1100154 DPRINT("Char: %c (\\x%02x)\n", yytext[0],
155 (unsigned)yytext[0]);
156 if (yytext[0] == '[') {
157 DPRINT("<BYTESTRING>\n");
158 BEGIN(BYTESTRING);
159 }
160 if ((yytext[0] == '{')
161 || (yytext[0] == ';')) {
162 DPRINT("<PROPNODENAME>\n");
163 BEGIN(PROPNODENAME);
164 }
165 return yytext[0];
166 }
167
168%%
169
David Gibsoned95d742008-08-07 12:24:17 +1000170static void push_input_file(const char *filename)
David Gibsona4da2e32007-12-18 15:06:42 +1100171{
David Gibsoned95d742008-08-07 12:24:17 +1000172 assert(filename);
173
John Bonesio658f29a2010-11-17 15:28:20 -0800174 srcfile_push(filename);
David Gibsoned95d742008-08-07 12:24:17 +1000175
John Bonesio658f29a2010-11-17 15:28:20 -0800176 yyin = current_srcfile->f;
David Gibsona4da2e32007-12-18 15:06:42 +1100177
John Bonesio658f29a2010-11-17 15:28:20 -0800178 yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
David Gibsona4da2e32007-12-18 15:06:42 +1100179}
180
181
David Gibsoned95d742008-08-07 12:24:17 +1000182static int pop_input_file(void)
David Gibsona4da2e32007-12-18 15:06:42 +1100183{
John Bonesio658f29a2010-11-17 15:28:20 -0800184 if (srcfile_pop() == 0)
David Gibsona4da2e32007-12-18 15:06:42 +1100185 return 0;
186
John Bonesio658f29a2010-11-17 15:28:20 -0800187 yypop_buffer_state();
188 yyin = current_srcfile->f;
David Gibsona4da2e32007-12-18 15:06:42 +1100189
David Gibsona4da2e32007-12-18 15:06:42 +1100190 return 1;
191}