/ util / cbfstool / fmd_scanner.l
fmd_scanner.l
 1  /* scanner generator for flashmap descriptor language */
 2  /* SPDX-License-Identifier: GPL-2.0-only */
 3  
 4  %{
 5  #include "fmd_parser.h"
 6  
 7  #include <assert.h>
 8  #include <string.h>
 9  
10  int parse_integer(char *src, int base);
11  int copy_string(const char *src);
12  %}
13  
14  %option noyywrap
15  %s FLAGS
16  
17  MULTIPLIER [KMG]
18  
19  %%
20  [[:space:]]+                /* Eat whitespace. */
21  #.*$                        /* Eat comments. */
22  \(                          BEGIN(FLAGS); return *yytext;
23  <FLAGS>\)                   BEGIN(INITIAL); return *yytext;
24  <FLAGS>CBFS                 return FLAG_CBFS;
25  <FLAGS>PRESERVE             return FLAG_PRESERVE;
26  0{MULTIPLIER}?              |
27  [1-9][0-9]*{MULTIPLIER}?    return parse_integer(yytext, 10);
28  0[0-9]+{MULTIPLIER}?        return OCTAL;
29  0[xX][0-9a-fA-F]+{MULTIPLIER}? return parse_integer(yytext + 2, 16);
30  [^#@{}()[:space:]]*         return copy_string(yytext);
31  .                           return *yytext;
32  
33  %%
34  
35  int parse_integer(char *src, int base)
36  {
37  	char *multiplier = NULL;
38  	unsigned val = strtoul(src, &multiplier, base);
39  
40  	if (*multiplier) {
41  		switch(*multiplier) {
42  		case 'K':
43  			val *= 1024;
44  			break;
45  		case 'M':
46  			val *= 1024*1024;
47  			break;
48  		case 'G':
49  			val *= 1024*1024*1024;
50  			break;
51  		default:
52  			// If we ever get here, the MULTIPLIER regex is allowing
53  			// multiplier suffixes not handled by this code.
54  			assert(false);
55  		}
56  	}
57  
58  	yylval.intval = val;
59  	return INTEGER;
60  }
61  
62  int copy_string(const char *src)
63  {
64  	yylval.strval = strdup(src);
65  	return STRING;
66  }