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 | ||
658f29a5 | 21 | %option noyywrap nounput noinput never-interactive |
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 | ||
658f29a5 JB |
41 | YYLTYPE 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 | } | |
a4da2e3e DG |
48 | |
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 | ||
658f29a5 | 57 | static int dts_version = 1; |
a4da2e3e | 58 | |
658f29a5 | 59 | #define BEGIN_DEFAULT() DPRINT("<V1>\n"); \ |
a4da2e3e | 60 | BEGIN(V1); \ |
ed95d745 DG |
61 | |
62 | static void push_input_file(const char *filename); | |
63 | static int pop_input_file(void); | |
a4da2e3e DG |
64 | %} |
65 | ||
66 | %% | |
ed95d745 DG |
67 | <*>"/include/"{WS}*{STRING} { |
68 | char *name = strchr(yytext, '\"') + 1; | |
69 | yytext[yyleng-1] = '\0'; | |
70 | push_input_file(name); | |
a4da2e3e DG |
71 | } |
72 | ||
a4da2e3e DG |
73 | <*><<EOF>> { |
74 | if (!pop_input_file()) { | |
75 | yyterminate(); | |
76 | } | |
77 | } | |
78 | ||
ed95d745 | 79 | <*>{STRING} { |
a4da2e3e DG |
80 | DPRINT("String: %s\n", yytext); |
81 | yylval.data = data_copy_escape_string(yytext+1, | |
82 | yyleng-2); | |
a4da2e3e DG |
83 | return DT_STRING; |
84 | } | |
85 | ||
86 | <*>"/dts-v1/" { | |
a4da2e3e DG |
87 | DPRINT("Keyword: /dts-v1/\n"); |
88 | dts_version = 1; | |
89 | BEGIN_DEFAULT(); | |
90 | return DT_V1; | |
91 | } | |
92 | ||
93 | <*>"/memreserve/" { | |
a4da2e3e DG |
94 | DPRINT("Keyword: /memreserve/\n"); |
95 | BEGIN_DEFAULT(); | |
96 | return DT_MEMRESERVE; | |
97 | } | |
98 | ||
99 | <*>{LABEL}: { | |
a4da2e3e | 100 | DPRINT("Label: %s\n", yytext); |
658f29a5 | 101 | yylval.labelref = xstrdup(yytext); |
a4da2e3e DG |
102 | yylval.labelref[yyleng-1] = '\0'; |
103 | return DT_LABEL; | |
104 | } | |
105 | ||
a4da2e3e | 106 | <V1>[0-9]+|0[xX][0-9a-fA-F]+ { |
658f29a5 | 107 | yylval.literal = xstrdup(yytext); |
a4da2e3e DG |
108 | DPRINT("Literal: '%s'\n", yylval.literal); |
109 | return DT_LITERAL; | |
110 | } | |
111 | ||
658f29a5 | 112 | <*>\&{LABEL} { /* label reference */ |
a4da2e3e | 113 | DPRINT("Ref: %s\n", yytext+1); |
658f29a5 | 114 | yylval.labelref = xstrdup(yytext+1); |
a4da2e3e DG |
115 | return DT_REF; |
116 | } | |
117 | ||
658f29a5 | 118 | <*>"&{/"{PATHCHAR}+\} { /* new-style path reference */ |
a4da2e3e DG |
119 | yytext[yyleng-1] = '\0'; |
120 | DPRINT("Ref: %s\n", yytext+2); | |
658f29a5 | 121 | yylval.labelref = xstrdup(yytext+2); |
a4da2e3e DG |
122 | return DT_REF; |
123 | } | |
124 | ||
125 | <BYTESTRING>[0-9a-fA-F]{2} { | |
a4da2e3e DG |
126 | yylval.byte = strtol(yytext, NULL, 16); |
127 | DPRINT("Byte: %02x\n", (int)yylval.byte); | |
128 | return DT_BYTE; | |
129 | } | |
130 | ||
131 | <BYTESTRING>"]" { | |
a4da2e3e DG |
132 | DPRINT("/BYTESTRING\n"); |
133 | BEGIN_DEFAULT(); | |
134 | return ']'; | |
135 | } | |
136 | ||
137 | <PROPNODENAME>{PROPNODECHAR}+ { | |
a4da2e3e | 138 | DPRINT("PropNodeName: %s\n", yytext); |
658f29a5 | 139 | yylval.propnodename = xstrdup(yytext); |
a4da2e3e DG |
140 | BEGIN_DEFAULT(); |
141 | return DT_PROPNODENAME; | |
142 | } | |
143 | ||
ed95d745 | 144 | "/incbin/" { |
ed95d745 DG |
145 | DPRINT("Binary Include\n"); |
146 | return DT_INCBIN; | |
a4da2e3e DG |
147 | } |
148 | ||
ed95d745 DG |
149 | <*>{WS}+ /* eat whitespace */ |
150 | <*>{COMMENT}+ /* eat C-style comments */ | |
151 | <*>{LINECOMMENT}+ /* eat C++-style comments */ | |
a4da2e3e DG |
152 | |
153 | <*>. { | |
a4da2e3e DG |
154 | 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 | ||
ed95d745 | 170 | static void push_input_file(const char *filename) |
a4da2e3e | 171 | { |
ed95d745 | 172 | assert(filename); |
a4da2e3e | 173 | |
658f29a5 | 174 | srcfile_push(filename); |
a4da2e3e | 175 | |
658f29a5 | 176 | yyin = current_srcfile->f; |
a4da2e3e | 177 | |
658f29a5 | 178 | yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE)); |
a4da2e3e DG |
179 | } |
180 | ||
181 | ||
ed95d745 | 182 | static int pop_input_file(void) |
a4da2e3e | 183 | { |
658f29a5 | 184 | if (srcfile_pop() == 0) |
a4da2e3e DG |
185 | return 0; |
186 | ||
658f29a5 JB |
187 | yypop_buffer_state(); |
188 | yyin = current_srcfile->f; | |
a4da2e3e | 189 | |
a4da2e3e DG |
190 | return 1; |
191 | } |