Blame view
scripts/dtc/dtc-lexer.l
4.05 KB
a4da2e3ec
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/* * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005. * * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA */ |
658f29a51
|
20 |
%option noyywrap nounput noinput never-interactive |
a4da2e3ec
|
21 22 23 24 25 26 27 28 29 |
%x INCLUDE %x BYTESTRING %x PROPNODENAME %s V1 PROPNODECHAR [a-zA-Z0-9,._+*#?@-] PATHCHAR ({PROPNODECHAR}|[/]) LABEL [a-zA-Z_][a-zA-Z0-9_]* |
ed95d7450
|
30 31 32 33 |
STRING \"([^\\"]|\\.)*\" WS [[:space:]] COMMENT "/*"([^*]|\*+[^*/])*\*+"/" LINECOMMENT "//".* |
a4da2e3ec
|
34 35 36 37 38 |
%{ #include "dtc.h" #include "srcpos.h" #include "dtc-parser.tab.h" |
658f29a51
|
39 40 41 42 43 44 45 |
YYLTYPE yylloc; /* CAUTION: this will stop working if we ever use yyless() or yyunput() */ #define YY_USER_ACTION \ { \ srcpos_update(&yylloc, yytext, yyleng); \ } |
a4da2e3ec
|
46 47 48 49 50 51 52 53 |
/*#define LEXDEBUG 1*/ #ifdef LEXDEBUG #define DPRINT(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__) #else #define DPRINT(fmt, ...) do { } while (0) #endif |
658f29a51
|
54 |
static int dts_version = 1; |
a4da2e3ec
|
55 |
|
658f29a51
|
56 57 |
#define BEGIN_DEFAULT() DPRINT("<V1> "); \ |
a4da2e3ec
|
58 |
BEGIN(V1); \ |
ed95d7450
|
59 60 61 |
static void push_input_file(const char *filename); static int pop_input_file(void); |
a4da2e3ec
|
62 63 64 |
%} %% |
ed95d7450
|
65 66 67 68 |
<*>"/include/"{WS}*{STRING} { char *name = strchr(yytext, '\"') + 1; yytext[yyleng-1] = '\0'; push_input_file(name); |
a4da2e3ec
|
69 |
} |
a4da2e3ec
|
70 71 72 73 74 |
<*><<EOF>> { if (!pop_input_file()) { yyterminate(); } } |
ed95d7450
|
75 |
<*>{STRING} { |
a4da2e3ec
|
76 77 78 79 |
DPRINT("String: %s ", yytext); yylval.data = data_copy_escape_string(yytext+1, yyleng-2); |
a4da2e3ec
|
80 81 82 83 |
return DT_STRING; } <*>"/dts-v1/" { |
a4da2e3ec
|
84 85 86 87 88 89 90 91 |
DPRINT("Keyword: /dts-v1/ "); dts_version = 1; BEGIN_DEFAULT(); return DT_V1; } <*>"/memreserve/" { |
a4da2e3ec
|
92 93 94 95 96 97 98 |
DPRINT("Keyword: /memreserve/ "); BEGIN_DEFAULT(); return DT_MEMRESERVE; } <*>{LABEL}: { |
a4da2e3ec
|
99 100 |
DPRINT("Label: %s ", yytext); |
658f29a51
|
101 |
yylval.labelref = xstrdup(yytext); |
a4da2e3ec
|
102 103 104 |
yylval.labelref[yyleng-1] = '\0'; return DT_LABEL; } |
a4da2e3ec
|
105 |
<V1>[0-9]+|0[xX][0-9a-fA-F]+ { |
658f29a51
|
106 |
yylval.literal = xstrdup(yytext); |
a4da2e3ec
|
107 108 109 110 |
DPRINT("Literal: '%s' ", yylval.literal); return DT_LITERAL; } |
658f29a51
|
111 |
<*>\&{LABEL} { /* label reference */ |
a4da2e3ec
|
112 113 |
DPRINT("Ref: %s ", yytext+1); |
658f29a51
|
114 |
yylval.labelref = xstrdup(yytext+1); |
a4da2e3ec
|
115 116 |
return DT_REF; } |
658f29a51
|
117 |
<*>"&{/"{PATHCHAR}+\} { /* new-style path reference */ |
a4da2e3ec
|
118 119 120 |
yytext[yyleng-1] = '\0'; DPRINT("Ref: %s ", yytext+2); |
658f29a51
|
121 |
yylval.labelref = xstrdup(yytext+2); |
a4da2e3ec
|
122 123 124 125 |
return DT_REF; } <BYTESTRING>[0-9a-fA-F]{2} { |
a4da2e3ec
|
126 127 128 129 130 131 132 |
yylval.byte = strtol(yytext, NULL, 16); DPRINT("Byte: %02x ", (int)yylval.byte); return DT_BYTE; } <BYTESTRING>"]" { |
a4da2e3ec
|
133 134 135 136 137 138 139 |
DPRINT("/BYTESTRING "); BEGIN_DEFAULT(); return ']'; } <PROPNODENAME>{PROPNODECHAR}+ { |
a4da2e3ec
|
140 141 |
DPRINT("PropNodeName: %s ", yytext); |
658f29a51
|
142 |
yylval.propnodename = xstrdup(yytext); |
a4da2e3ec
|
143 144 145 |
BEGIN_DEFAULT(); return DT_PROPNODENAME; } |
ed95d7450
|
146 |
"/incbin/" { |
ed95d7450
|
147 148 149 |
DPRINT("Binary Include "); return DT_INCBIN; |
a4da2e3ec
|
150 |
} |
ed95d7450
|
151 152 153 |
<*>{WS}+ /* eat whitespace */ <*>{COMMENT}+ /* eat C-style comments */ <*>{LINECOMMENT}+ /* eat C++-style comments */ |
a4da2e3ec
|
154 155 |
<*>. { |
a4da2e3ec
|
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
DPRINT("Char: %c (\\x%02x) ", yytext[0], (unsigned)yytext[0]); if (yytext[0] == '[') { DPRINT("<BYTESTRING> "); BEGIN(BYTESTRING); } if ((yytext[0] == '{') || (yytext[0] == ';')) { DPRINT("<PROPNODENAME> "); BEGIN(PROPNODENAME); } return yytext[0]; } %% |
ed95d7450
|
174 |
static void push_input_file(const char *filename) |
a4da2e3ec
|
175 |
{ |
ed95d7450
|
176 |
assert(filename); |
a4da2e3ec
|
177 |
|
658f29a51
|
178 |
srcfile_push(filename); |
a4da2e3ec
|
179 |
|
658f29a51
|
180 |
yyin = current_srcfile->f; |
a4da2e3ec
|
181 |
|
658f29a51
|
182 |
yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE)); |
a4da2e3ec
|
183 |
} |
ed95d7450
|
184 |
static int pop_input_file(void) |
a4da2e3ec
|
185 |
{ |
658f29a51
|
186 |
if (srcfile_pop() == 0) |
a4da2e3ec
|
187 |
return 0; |
658f29a51
|
188 189 |
yypop_buffer_state(); yyin = current_srcfile->f; |
a4da2e3ec
|
190 |
|
a4da2e3ec
|
191 192 |
return 1; } |