treewide: Move device_tree to commonlib
[coreboot2.git] / util / cbfstool / fmd_scanner.l
blob9d05c80d263bc64da21e794ddd00a2ce90d9c497
1 /* scanner generator for flashmap descriptor language */
2 /* SPDX-License-Identifier: GPL-2.0-only */
4 %{
5 #include "fmd_parser.h"
7 #include <assert.h>
8 #include <string.h>
10 int parse_integer(char *src, int base);
11 int copy_string(const char *src);
14 %option noyywrap
15 %s FLAGS
17 MULTIPLIER [KMG]
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;
35 int parse_integer(char *src, int base)
37         char *multiplier = NULL;
38         unsigned val = strtoul(src, &multiplier, base);
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         }
58         yylval.intval = val;
59         return INTEGER;
62 int copy_string(const char *src)
64         yylval.strval = strdup(src);
65         return STRING;