Blame view

scripts/dtc/dtc-lexer.l 6.14 KB
12869ecd5   Rob Herring   scripts/dtc: Upda...
1
  /* SPDX-License-Identifier: GPL-2.0-or-later */
a4da2e3ec   David Gibson   [POWERPC] Merge d...
2
3
  /*
   * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2005.
a4da2e3ec   David Gibson   [POWERPC] Merge d...
4
   */
658f29a51   John Bonesio   of/flattree: Upda...
5
  %option noyywrap nounput noinput never-interactive
a4da2e3ec   David Gibson   [POWERPC] Merge d...
6

a4da2e3ec   David Gibson   [POWERPC] Merge d...
7
8
9
10
11
12
13
  %x BYTESTRING
  %x PROPNODENAME
  %s V1
  
  PROPNODECHAR	[a-zA-Z0-9,._+*#?@-]
  PATHCHAR	({PROPNODECHAR}|[/])
  LABEL		[a-zA-Z_][a-zA-Z0-9_]*
ed95d7450   David Gibson   powerpc: Update i...
14
  STRING		\"([^\\"]|\\.)*\"
cd296721a   Stephen Warren   dtc: import lates...
15
  CHAR_LITERAL	'([^']|\\')*'
ed95d7450   David Gibson   powerpc: Update i...
16
17
18
  WS		[[:space:]]
  COMMENT		"/*"([^*]|\*+[^*/])*\*+"/"
  LINECOMMENT	"//".*
a4da2e3ec   David Gibson   [POWERPC] Merge d...
19
20
21
22
23
  
  %{
  #include "dtc.h"
  #include "srcpos.h"
  #include "dtc-parser.tab.h"
476059711   Rob Herring   scripts/dtc: Upda...
24
  extern bool treesource_error;
658f29a51   John Bonesio   of/flattree: Upda...
25
26
27
28
29
30
  
  /* CAUTION: this will stop working if we ever use yyless() or yyunput() */
  #define	YY_USER_ACTION \
  	{ \
  		srcpos_update(&yylloc, yytext, yyleng); \
  	}
a4da2e3ec   David Gibson   [POWERPC] Merge d...
31
32
33
34
35
36
37
38
  
  /*#define LEXDEBUG	1*/
  
  #ifdef LEXDEBUG
  #define DPRINT(fmt, ...)	fprintf(stderr, fmt, ##__VA_ARGS__)
  #else
  #define DPRINT(fmt, ...)	do { } while (0)
  #endif
658f29a51   John Bonesio   of/flattree: Upda...
39
  static int dts_version = 1;
a4da2e3ec   David Gibson   [POWERPC] Merge d...
40

658f29a51   John Bonesio   of/flattree: Upda...
41
42
  #define BEGIN_DEFAULT()		DPRINT("<V1>
  "); \
a4da2e3ec   David Gibson   [POWERPC] Merge d...
43
  				BEGIN(V1); \
ed95d7450   David Gibson   powerpc: Update i...
44
45
  
  static void push_input_file(const char *filename);
476059711   Rob Herring   scripts/dtc: Upda...
46
  static bool pop_input_file(void);
89d123106   Rob Herring   scripts/dtc: Upda...
47
  static void PRINTF(1, 2) lexical_error(const char *fmt, ...);
a4da2e3ec   David Gibson   [POWERPC] Merge d...
48
49
50
  %}
  
  %%
ed95d7450   David Gibson   powerpc: Update i...
51
52
53
54
  <*>"/include/"{WS}*{STRING} {
  			char *name = strchr(yytext, '\"') + 1;
  			yytext[yyleng-1] = '\0';
  			push_input_file(name);
a4da2e3ec   David Gibson   [POWERPC] Merge d...
55
  		}
706b78f37   Grant Likely   dtc: ensure #line...
56
  <*>^"#"(line)?[ \t]+[0-9]+[ \t]+{STRING}([ \t]+[0-9]+)? {
91feabc2e   Rob Herring   scripts/dtc: Upda...
57
58
  			char *line, *fnstart, *fnend;
  			struct data fn;
cd296721a   Stephen Warren   dtc: import lates...
59
60
  			/* skip text before line # */
  			line = yytext;
476059711   Rob Herring   scripts/dtc: Upda...
61
  			while (!isdigit((unsigned char)*line))
cd296721a   Stephen Warren   dtc: import lates...
62
  				line++;
91feabc2e   Rob Herring   scripts/dtc: Upda...
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  
  			/* regexp ensures that first and list "
  			 * in the whole yytext are those at
  			 * beginning and end of the filename string */
  			fnstart = memchr(yytext, '"', yyleng);
  			for (fnend = yytext + yyleng - 1;
  			     *fnend != '"'; fnend--)
  				;
  			assert(fnstart && fnend && (fnend > fnstart));
  
  			fn = data_copy_escape_string(fnstart + 1,
  						     fnend - fnstart - 1);
  
  			/* Don't allow nuls in filenames */
  			if (memchr(fn.val, '\0', fn.len - 1))
  				lexical_error("nul in line number directive");
cd296721a   Stephen Warren   dtc: import lates...
79
  			/* -1 since #line is the number of the next line */
91feabc2e   Rob Herring   scripts/dtc: Upda...
80
81
  			srcpos_set_line(xstrdup(fn.val), atoi(line) - 1);
  			data_free(fn);
cd296721a   Stephen Warren   dtc: import lates...
82
  		}
a4da2e3ec   David Gibson   [POWERPC] Merge d...
83
84
85
86
87
  <*><<EOF>>		{
  			if (!pop_input_file()) {
  				yyterminate();
  			}
  		}
ed95d7450   David Gibson   powerpc: Update i...
88
  <*>{STRING}	{
a4da2e3ec   David Gibson   [POWERPC] Merge d...
89
90
91
92
  			DPRINT("String: %s
  ", yytext);
  			yylval.data = data_copy_escape_string(yytext+1,
  					yyleng-2);
a4da2e3ec   David Gibson   [POWERPC] Merge d...
93
94
95
96
  			return DT_STRING;
  		}
  
  <*>"/dts-v1/"	{
a4da2e3ec   David Gibson   [POWERPC] Merge d...
97
98
99
100
101
102
  			DPRINT("Keyword: /dts-v1/
  ");
  			dts_version = 1;
  			BEGIN_DEFAULT();
  			return DT_V1;
  		}
6f05afcbb   Rob Herring   scripts/dtc: Upda...
103
104
105
106
107
  <*>"/plugin/"	{
  			DPRINT("Keyword: /plugin/
  ");
  			return DT_PLUGIN;
  		}
a4da2e3ec   David Gibson   [POWERPC] Merge d...
108
  <*>"/memreserve/"	{
a4da2e3ec   David Gibson   [POWERPC] Merge d...
109
110
111
112
113
  			DPRINT("Keyword: /memreserve/
  ");
  			BEGIN_DEFAULT();
  			return DT_MEMRESERVE;
  		}
cd296721a   Stephen Warren   dtc: import lates...
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  <*>"/bits/"	{
  			DPRINT("Keyword: /bits/
  ");
  			BEGIN_DEFAULT();
  			return DT_BITS;
  		}
  
  <*>"/delete-property/"	{
  			DPRINT("Keyword: /delete-property/
  ");
  			DPRINT("<PROPNODENAME>
  ");
  			BEGIN(PROPNODENAME);
  			return DT_DEL_PROP;
  		}
  
  <*>"/delete-node/"	{
  			DPRINT("Keyword: /delete-node/
  ");
  			DPRINT("<PROPNODENAME>
  ");
  			BEGIN(PROPNODENAME);
  			return DT_DEL_NODE;
  		}
50aafd608   Rob Herring   scripts/dtc: Upda...
138
139
140
141
142
143
144
145
  <*>"/omit-if-no-ref/"	{
  			DPRINT("Keyword: /omit-if-no-ref/
  ");
  			DPRINT("<PROPNODENAME>
  ");
  			BEGIN(PROPNODENAME);
  			return DT_OMIT_NO_REF;
  		}
a4da2e3ec   David Gibson   [POWERPC] Merge d...
146
  <*>{LABEL}:	{
a4da2e3ec   David Gibson   [POWERPC] Merge d...
147
148
  			DPRINT("Label: %s
  ", yytext);
658f29a51   John Bonesio   of/flattree: Upda...
149
  			yylval.labelref = xstrdup(yytext);
a4da2e3ec   David Gibson   [POWERPC] Merge d...
150
151
152
  			yylval.labelref[yyleng-1] = '\0';
  			return DT_LABEL;
  		}
cd296721a   Stephen Warren   dtc: import lates...
153
  <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
476059711   Rob Herring   scripts/dtc: Upda...
154
155
156
157
158
159
  			char *e;
  			DPRINT("Integer Literal: '%s'
  ", yytext);
  
  			errno = 0;
  			yylval.integer = strtoull(yytext, &e, 0);
91feabc2e   Rob Herring   scripts/dtc: Upda...
160
161
162
163
  			if (*e && e[strspn(e, "UL")]) {
  				lexical_error("Bad integer literal '%s'",
  					      yytext);
  			}
476059711   Rob Herring   scripts/dtc: Upda...
164
165
166
167
168
169
170
171
  
  			if (errno == ERANGE)
  				lexical_error("Integer literal '%s' out of range",
  					      yytext);
  			else
  				/* ERANGE is the only strtoull error triggerable
  				 *  by strings matching the pattern */
  				assert(errno == 0);
a4da2e3ec   David Gibson   [POWERPC] Merge d...
172
173
  			return DT_LITERAL;
  		}
cd296721a   Stephen Warren   dtc: import lates...
174
  <*>{CHAR_LITERAL}	{
476059711   Rob Herring   scripts/dtc: Upda...
175
176
177
178
179
180
181
182
  			struct data d;
  			DPRINT("Character literal: %s
  ", yytext);
  
  			d = data_copy_escape_string(yytext+1, yyleng-2);
  			if (d.len == 1) {
  				lexical_error("Empty character literal");
  				yylval.integer = 0;
6f05afcbb   Rob Herring   scripts/dtc: Upda...
183
184
  			} else {
  				yylval.integer = (unsigned char)d.val[0];
476059711   Rob Herring   scripts/dtc: Upda...
185

6f05afcbb   Rob Herring   scripts/dtc: Upda...
186
187
188
189
190
  				if (d.len > 2)
  					lexical_error("Character literal has %d"
  						      " characters instead of 1",
  						      d.len - 1);
  			}
476059711   Rob Herring   scripts/dtc: Upda...
191

6f05afcbb   Rob Herring   scripts/dtc: Upda...
192
  			data_free(d);
cd296721a   Stephen Warren   dtc: import lates...
193
194
  			return DT_CHAR_LITERAL;
  		}
658f29a51   John Bonesio   of/flattree: Upda...
195
  <*>\&{LABEL}	{	/* label reference */
a4da2e3ec   David Gibson   [POWERPC] Merge d...
196
197
  			DPRINT("Ref: %s
  ", yytext+1);
658f29a51   John Bonesio   of/flattree: Upda...
198
  			yylval.labelref = xstrdup(yytext+1);
c2e7075ca   Rob Herring   scripts/dtc: Upda...
199
  			return DT_LABEL_REF;
a4da2e3ec   David Gibson   [POWERPC] Merge d...
200
  		}
476059711   Rob Herring   scripts/dtc: Upda...
201
  <*>"&{/"{PATHCHAR}*\}	{	/* new-style path reference */
a4da2e3ec   David Gibson   [POWERPC] Merge d...
202
203
204
  			yytext[yyleng-1] = '\0';
  			DPRINT("Ref: %s
  ", yytext+2);
658f29a51   John Bonesio   of/flattree: Upda...
205
  			yylval.labelref = xstrdup(yytext+2);
c2e7075ca   Rob Herring   scripts/dtc: Upda...
206
  			return DT_PATH_REF;
a4da2e3ec   David Gibson   [POWERPC] Merge d...
207
208
209
  		}
  
  <BYTESTRING>[0-9a-fA-F]{2} {
a4da2e3ec   David Gibson   [POWERPC] Merge d...
210
211
212
213
214
215
216
  			yylval.byte = strtol(yytext, NULL, 16);
  			DPRINT("Byte: %02x
  ", (int)yylval.byte);
  			return DT_BYTE;
  		}
  
  <BYTESTRING>"]"	{
a4da2e3ec   David Gibson   [POWERPC] Merge d...
217
218
219
220
221
  			DPRINT("/BYTESTRING
  ");
  			BEGIN_DEFAULT();
  			return ']';
  		}
cd296721a   Stephen Warren   dtc: import lates...
222
  <PROPNODENAME>\\?{PROPNODECHAR}+ {
a4da2e3ec   David Gibson   [POWERPC] Merge d...
223
224
  			DPRINT("PropNodeName: %s
  ", yytext);
cd296721a   Stephen Warren   dtc: import lates...
225
226
  			yylval.propnodename = xstrdup((yytext[0] == '\\') ?
  							yytext + 1 : yytext);
a4da2e3ec   David Gibson   [POWERPC] Merge d...
227
228
229
  			BEGIN_DEFAULT();
  			return DT_PROPNODENAME;
  		}
ed95d7450   David Gibson   powerpc: Update i...
230
  "/incbin/"	{
ed95d7450   David Gibson   powerpc: Update i...
231
232
233
  			DPRINT("Binary Include
  ");
  			return DT_INCBIN;
a4da2e3ec   David Gibson   [POWERPC] Merge d...
234
  		}
ed95d7450   David Gibson   powerpc: Update i...
235
236
237
  <*>{WS}+	/* eat whitespace */
  <*>{COMMENT}+	/* eat C-style comments */
  <*>{LINECOMMENT}+ /* eat C++-style comments */
a4da2e3ec   David Gibson   [POWERPC] Merge d...
238

cd296721a   Stephen Warren   dtc: import lates...
239
240
241
242
243
244
245
246
  <*>"<<"		{ return DT_LSHIFT; };
  <*>">>"		{ return DT_RSHIFT; };
  <*>"<="		{ return DT_LE; };
  <*>">="		{ return DT_GE; };
  <*>"=="		{ return DT_EQ; };
  <*>"!="		{ return DT_NE; };
  <*>"&&"		{ return DT_AND; };
  <*>"||"		{ return DT_OR; };
a4da2e3ec   David Gibson   [POWERPC] Merge d...
247
  <*>.		{
a4da2e3ec   David Gibson   [POWERPC] Merge d...
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  			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   David Gibson   powerpc: Update i...
266
  static void push_input_file(const char *filename)
a4da2e3ec   David Gibson   [POWERPC] Merge d...
267
  {
ed95d7450   David Gibson   powerpc: Update i...
268
  	assert(filename);
a4da2e3ec   David Gibson   [POWERPC] Merge d...
269

658f29a51   John Bonesio   of/flattree: Upda...
270
  	srcfile_push(filename);
a4da2e3ec   David Gibson   [POWERPC] Merge d...
271

658f29a51   John Bonesio   of/flattree: Upda...
272
  	yyin = current_srcfile->f;
a4da2e3ec   David Gibson   [POWERPC] Merge d...
273

658f29a51   John Bonesio   of/flattree: Upda...
274
  	yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
a4da2e3ec   David Gibson   [POWERPC] Merge d...
275
  }
476059711   Rob Herring   scripts/dtc: Upda...
276
  static bool pop_input_file(void)
a4da2e3ec   David Gibson   [POWERPC] Merge d...
277
  {
658f29a51   John Bonesio   of/flattree: Upda...
278
  	if (srcfile_pop() == 0)
476059711   Rob Herring   scripts/dtc: Upda...
279
  		return false;
a4da2e3ec   David Gibson   [POWERPC] Merge d...
280

658f29a51   John Bonesio   of/flattree: Upda...
281
282
  	yypop_buffer_state();
  	yyin = current_srcfile->f;
a4da2e3ec   David Gibson   [POWERPC] Merge d...
283

476059711   Rob Herring   scripts/dtc: Upda...
284
285
286
287
288
289
290
291
292
293
294
295
  	return true;
  }
  
  static void lexical_error(const char *fmt, ...)
  {
  	va_list ap;
  
  	va_start(ap, fmt);
  	srcpos_verror(&yylloc, "Lexical error", fmt, ap);
  	va_end(ap);
  
  	treesource_error = true;
a4da2e3ec   David Gibson   [POWERPC] Merge d...
296
  }