Blame view

scripts/genksyms/lex.l 8.7 KB
77564a482   Masahiro Yamada   genksyms: convert...
1
2
3
4
5
6
7
8
9
10
  /* SPDX-License-Identifier: GPL-2.0-or-later */
  /*
   * Lexical analysis for genksyms.
   * Copyright 1996, 1997 Linux International.
   *
   * New implementation contributed by Richard Henderson <rth@tamu.edu>
   * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
   *
   * Taken from Linux modutils 2.4.22.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
12
13
14
15
16
17
18
19
  
  %{
  
  #include <limits.h>
  #include <stdlib.h>
  #include <string.h>
  #include <ctype.h>
  
  #include "genksyms.h"
880f4499b   Arnaud Lacombe   genksyms: migrate...
20
  #include "parse.tab.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  
  /* We've got a two-level lexer here.  We let flex do basic tokenization
     and then we categorize those basic tokens in the second stage.  */
  #define YY_DECL		static int yylex1(void)
  
  %}
  
  IDENT			[A-Za-z_\$][A-Za-z0-9_\$]*
  
  O_INT			0[0-7]*
  D_INT			[1-9][0-9]*
  X_INT			0[Xx][0-9A-Fa-f]+
  I_SUF			[Uu]|[Ll]|[Uu][Ll]|[Ll][Uu]
  INT			({O_INT}|{D_INT}|{X_INT}){I_SUF}?
  
  FRAC			([0-9]*\.[0-9]+)|([0-9]+\.)
  EXP			[Ee][+-]?[0-9]+
  F_SUF			[FfLl]
  REAL			({FRAC}{EXP}?{F_SUF}?)|([0-9]+{EXP}{F_SUF}?)
  
  STRING			L?\"([^\\\"]*\\.)*[^\\\"]*\"
  CHAR			L?\'([^\\\']*\\.)*[^\\\']*\'
  
  MC_TOKEN		([~%^&*+=|<>/-]=)|(&&)|("||")|(->)|(<<)|(>>)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
46
  /* We don't do multiple input files.  */
  %option noyywrap
11ddad396   Adrian Bunk   kbuild: scripts/g...
47
  %option noinput
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  %%
  
  
   /* Keep track of our location in the original source files.  */
  ^#[ \t]+{INT}[ \t]+\"[^\"
  ]+\".*
  	return FILENAME;
  ^#.*
  					cur_line++;
  
  					cur_line++;
  
   /* Ignore all other whitespace.  */
  [ \t\f\v\r]+				;
  
  
  {STRING}				return STRING;
  {CHAR}					return CHAR;
  {IDENT}					return IDENT;
  
   /* The Pedant requires that the other C multi-character tokens be
      recognized as tokens.  We don't actually use them since we don't
      parse expressions, but we do want whitespace to be arranged
      around them properly.  */
95f1d639a   Michal Marek   genksyms: Simplif...
72
73
74
  {MC_TOKEN}				return OTHER;
  {INT}					return INT;
  {REAL}					return REAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
75
76
77
78
79
80
81
82
83
84
  
  "..."					return DOTS;
  
   /* All other tokens are single characters.  */
  .					return yytext[0];
  
  
  %%
  
  /* Bring in the keyword recognizer.  */
bb3290d91   Linus Torvalds   Remove gperf usag...
85
  #include "keywords.c"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
87
88
  
  
  /* Macros to append to our phrase collection list.  */
e37ddb825   Michal Marek   genksyms: Track c...
89
90
91
92
93
94
95
96
97
  /*
   * We mark any token, that that equals to a known enumerator, as
   * SYM_ENUM_CONST. The parser will change this for struct and union tags later,
   * the only problem is struct and union members:
   *    enum e { a, b }; struct s { int a, b; }
   * but in this case, the only effect will be, that the ABI checksums become
   * more volatile, which is acceptable. Also, such collisions are quite rare,
   * so far it was only observed in include/linux/telephony.h.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98
99
100
101
102
  #define _APP(T,L)	do {						   \
  			  cur_node = next_node;				   \
  			  next_node = xmalloc(sizeof(*next_node));	   \
  			  next_node->next = cur_node;			   \
  			  cur_node->string = memcpy(xmalloc(L+1), T, L+1); \
e37ddb825   Michal Marek   genksyms: Track c...
103
104
105
  			  cur_node->tag =				   \
  			    find_symbol(cur_node->string, SYM_ENUM_CONST, 1)?\
  			    SYM_ENUM_CONST : SYM_NORMAL ;		   \
2c5925d6b   Michal Marek   genksyms: Do not ...
106
  			  cur_node->in_source_file = in_source_file;       \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107
108
109
110
111
112
113
114
115
116
117
118
  			} while (0)
  
  #define APP		_APP(yytext, yyleng)
  
  
  /* The second stage lexer.  Here we incorporate knowledge of the state
     of the parser to tailor the tokens that are returned.  */
  
  int
  yylex(void)
  {
    static enum {
dc5332406   Jan Beulich   genksyms: fix typ...
119
120
121
      ST_NOTSTARTED, ST_NORMAL, ST_ATTRIBUTE, ST_ASM, ST_TYPEOF, ST_TYPEOF_1,
      ST_BRACKET, ST_BRACE, ST_EXPRESSION,
      ST_TABLE_1, ST_TABLE_2, ST_TABLE_3, ST_TABLE_4,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
123
124
125
126
127
128
129
130
131
132
      ST_TABLE_5, ST_TABLE_6
    } lexstate = ST_NOTSTARTED;
  
    static int suppress_type_lookup, dont_want_brace_phrase;
    static struct string_list *next_node;
  
    int token, count = 0;
    struct string_list *cur_node;
  
    if (lexstate == ST_NOTSTARTED)
      {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
        next_node = xmalloc(sizeof(*next_node));
        next_node->next = NULL;
        lexstate = ST_NORMAL;
      }
  
  repeat:
    token = yylex1();
  
    if (token == 0)
      return 0;
    else if (token == FILENAME)
      {
        char *file, *e;
  
        /* Save the filename and line number for later error messages.  */
  
        if (cur_filename)
  	free(cur_filename);
  
        file = strchr(yytext, '\"')+1;
        e = strchr(file, '\"');
        *e = '\0';
        cur_filename = memcpy(xmalloc(e-file+1), file, e-file+1);
        cur_line = atoi(yytext+2);
2c5925d6b   Michal Marek   genksyms: Do not ...
157
158
159
160
161
162
        if (!source_file) {
          source_file = xstrdup(cur_filename);
          in_source_file = 1;
        } else {
          in_source_file = (strcmp(cur_filename, source_file) == 0);
        }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
163
164
165
166
167
168
169
170
171
172
173
        goto repeat;
      }
  
    switch (lexstate)
      {
      case ST_NORMAL:
        switch (token)
  	{
  	case IDENT:
  	  APP;
  	  {
bb3290d91   Linus Torvalds   Remove gperf usag...
174
175
  	    int r = is_reserved_word(yytext, yyleng);
  	    if (r >= 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
176
  	      {
bb3290d91   Linus Torvalds   Remove gperf usag...
177
  		switch (token = r)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178
179
180
181
182
183
184
185
186
  		  {
  		  case ATTRIBUTE_KEYW:
  		    lexstate = ST_ATTRIBUTE;
  		    count = 0;
  		    goto repeat;
  		  case ASM_KEYW:
  		    lexstate = ST_ASM;
  		    count = 0;
  		    goto repeat;
dc5332406   Jan Beulich   genksyms: fix typ...
187
188
189
190
  		  case TYPEOF_KEYW:
  		    lexstate = ST_TYPEOF;
  		    count = 0;
  		    goto repeat;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
191
192
193
  
  		  case STRUCT_KEYW:
  		  case UNION_KEYW:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194
  		  case ENUM_KEYW:
e37ddb825   Michal Marek   genksyms: Track c...
195
  		    dont_want_brace_phrase = 3;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
196
197
198
199
200
201
202
203
204
  		    suppress_type_lookup = 2;
  		    goto fini;
  
  		  case EXPORT_SYMBOL_KEYW:
  		      goto fini;
  		  }
  	      }
  	    if (!suppress_type_lookup)
  	      {
01762c4ec   Michal Marek   genksyms: simplif...
205
  		if (find_symbol(yytext, SYM_TYPEDEF, 1))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
  		  token = TYPE;
  	      }
  	  }
  	  break;
  
  	case '[':
  	  APP;
  	  lexstate = ST_BRACKET;
  	  count = 1;
  	  goto repeat;
  
  	case '{':
  	  APP;
  	  if (dont_want_brace_phrase)
  	    break;
  	  lexstate = ST_BRACE;
  	  count = 1;
  	  goto repeat;
  
  	case '=': case ':':
  	  APP;
  	  lexstate = ST_EXPRESSION;
  	  break;
  
  	case DOTS:
  	default:
  	  APP;
  	  break;
  	}
        break;
  
      case ST_ATTRIBUTE:
        APP;
        switch (token)
  	{
  	case '(':
  	  ++count;
  	  goto repeat;
  	case ')':
  	  if (--count == 0)
  	    {
  	      lexstate = ST_NORMAL;
  	      token = ATTRIBUTE_PHRASE;
  	      break;
  	    }
  	  goto repeat;
  	default:
  	  goto repeat;
  	}
        break;
  
      case ST_ASM:
        APP;
        switch (token)
  	{
  	case '(':
  	  ++count;
  	  goto repeat;
  	case ')':
  	  if (--count == 0)
  	    {
  	      lexstate = ST_NORMAL;
  	      token = ASM_PHRASE;
  	      break;
  	    }
  	  goto repeat;
  	default:
  	  goto repeat;
  	}
        break;
4fab91605   Nicholas Piggin   kbuild: genksyms ...
276
277
278
      case ST_TYPEOF_1:
        if (token == IDENT)
  	{
3aea311c1   Linus Torvalds   genksyms: fix gpe...
279
  	  if (is_reserved_word(yytext, yyleng) >= 0
4fab91605   Nicholas Piggin   kbuild: genksyms ...
280
281
282
283
284
285
286
287
288
289
290
291
  	      || find_symbol(yytext, SYM_TYPEDEF, 1))
  	    {
  	      yyless(0);
  	      unput('(');
  	      lexstate = ST_NORMAL;
  	      token = TYPEOF_KEYW;
  	      break;
  	    }
  	  _APP("(", 1);
  	}
  	lexstate = ST_TYPEOF;
  	/* FALLTHRU */
dc5332406   Jan Beulich   genksyms: fix typ...
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
      case ST_TYPEOF:
        switch (token)
  	{
  	case '(':
  	  if ( ++count == 1 )
  	    lexstate = ST_TYPEOF_1;
  	  else
  	    APP;
  	  goto repeat;
  	case ')':
  	  APP;
  	  if (--count == 0)
  	    {
  	      lexstate = ST_NORMAL;
  	      token = TYPEOF_PHRASE;
  	      break;
  	    }
  	  goto repeat;
  	default:
  	  APP;
  	  goto repeat;
  	}
        break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
      case ST_BRACKET:
        APP;
        switch (token)
  	{
  	case '[':
  	  ++count;
  	  goto repeat;
  	case ']':
  	  if (--count == 0)
  	    {
  	      lexstate = ST_NORMAL;
  	      token = BRACKET_PHRASE;
  	      break;
  	    }
  	  goto repeat;
  	default:
  	  goto repeat;
  	}
        break;
  
      case ST_BRACE:
        APP;
        switch (token)
  	{
  	case '{':
  	  ++count;
  	  goto repeat;
  	case '}':
  	  if (--count == 0)
  	    {
  	      lexstate = ST_NORMAL;
  	      token = BRACE_PHRASE;
  	      break;
  	    }
  	  goto repeat;
  	default:
  	  goto repeat;
  	}
        break;
  
      case ST_EXPRESSION:
        switch (token)
  	{
  	case '(': case '[': case '{':
  	  ++count;
  	  APP;
  	  goto repeat;
e37ddb825   Michal Marek   genksyms: Track c...
362
363
364
365
366
367
368
369
370
371
372
373
374
375
  	case '}':
  	  /* is this the last line of an enum declaration? */
  	  if (count == 0)
  	    {
  	      /* Put back the token we just read so's we can find it again
  		 after registering the expression.  */
  	      unput(token);
  
  	      lexstate = ST_NORMAL;
  	      token = EXPRESSION_PHRASE;
  	      break;
  	    }
  	  /* FALLTHRU */
  	case ')': case ']':
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
  	  --count;
  	  APP;
  	  goto repeat;
  	case ',': case ';':
  	  if (count == 0)
  	    {
  	      /* Put back the token we just read so's we can find it again
  		 after registering the expression.  */
  	      unput(token);
  
  	      lexstate = ST_NORMAL;
  	      token = EXPRESSION_PHRASE;
  	      break;
  	    }
  	  APP;
  	  goto repeat;
  	default:
  	  APP;
  	  goto repeat;
  	}
        break;
  
      case ST_TABLE_1:
        goto repeat;
  
      case ST_TABLE_2:
        if (token == IDENT && yyleng == 1 && yytext[0] == 'X')
  	{
  	  token = EXPORT_SYMBOL_KEYW;
  	  lexstate = ST_TABLE_5;
  	  APP;
  	  break;
  	}
        lexstate = ST_TABLE_6;
        /* FALLTHRU */
  
      case ST_TABLE_6:
        switch (token)
  	{
  	case '{': case '[': case '(':
  	  ++count;
  	  break;
  	case '}': case ']': case ')':
  	  --count;
  	  break;
  	case ',':
  	  if (count == 0)
  	    lexstate = ST_TABLE_2;
  	  break;
  	};
        goto repeat;
  
      case ST_TABLE_3:
        goto repeat;
  
      case ST_TABLE_4:
        if (token == ';')
  	lexstate = ST_NORMAL;
        goto repeat;
  
      case ST_TABLE_5:
        switch (token)
  	{
  	case ',':
  	  token = ';';
  	  lexstate = ST_TABLE_2;
  	  APP;
  	  break;
  	default:
  	  APP;
  	  break;
  	}
        break;
  
      default:
6803dc0ea   Sam Ravnborg   kbuild: replace a...
451
        exit(1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
452
453
454
455
456
457
458
459
460
461
462
463
      }
  fini:
  
    if (suppress_type_lookup > 0)
      --suppress_type_lookup;
    if (dont_want_brace_phrase > 0)
      --dont_want_brace_phrase;
  
    yylval = &next_node->next;
  
    return token;
  }