blob: 9d05c80d263bc64da21e794ddd00a2ce90d9c497 [file] [log] [blame]
Patrick Georgiea063cb2020-05-08 19:28:13 +02001/* scanner generator for flashmap descriptor language */
Patrick Georgi7333a112020-05-08 20:48:04 +02002/* SPDX-License-Identifier: GPL-2.0-only */
Sol Boucher69b88bf2015-02-26 11:47:19 -08003
4%{
5#include "fmd_parser.h"
6
7#include <assert.h>
8#include <string.h>
9
Kyösti Mälkkic8771492015-05-09 08:06:02 +030010int parse_integer(char *src, int base);
11int copy_string(const char *src);
Sol Boucher69b88bf2015-02-26 11:47:19 -080012%}
13
14%option noyywrap
Hung-Te Lin9497fcb2019-03-04 14:28:37 +080015%s FLAGS
Sol Boucher69b88bf2015-02-26 11:47:19 -080016
17MULTIPLIER [KMG]
18
19%%
Sol Boucher69b88bf2015-02-26 11:47:19 -080020[[:space:]]+ /* Eat whitespace. */
21#.*$ /* Eat comments. */
Hung-Te Lin9497fcb2019-03-04 14:28:37 +080022\( BEGIN(FLAGS); return *yytext;
23<FLAGS>\) BEGIN(INITIAL); return *yytext;
24<FLAGS>CBFS return FLAG_CBFS;
Hung-Te Lin49a44502019-03-04 15:41:09 +080025<FLAGS>PRESERVE return FLAG_PRESERVE;
Sol Boucher69b88bf2015-02-26 11:47:19 -0800260{MULTIPLIER}? |
27[1-9][0-9]*{MULTIPLIER}? return parse_integer(yytext, 10);
280[0-9]+{MULTIPLIER}? return OCTAL;
Patrick Georgi2b807342016-06-28 20:45:34 +0200290[xX][0-9a-fA-F]+{MULTIPLIER}? return parse_integer(yytext + 2, 16);
Sol Boucher69b88bf2015-02-26 11:47:19 -080030[^#@{}()[:space:]]* return copy_string(yytext);
31. return *yytext;
32
33%%
34
Kyösti Mälkkic8771492015-05-09 08:06:02 +030035int parse_integer(char *src, int base)
Sol Boucher69b88bf2015-02-26 11:47:19 -080036{
37 char *multiplier = NULL;
Kyösti Mälkkic8771492015-05-09 08:06:02 +030038 unsigned val = strtoul(src, &multiplier, base);
Sol Boucher69b88bf2015-02-26 11:47:19 -080039
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
Kyösti Mälkkic8771492015-05-09 08:06:02 +030062int copy_string(const char *src)
Sol Boucher69b88bf2015-02-26 11:47:19 -080063{
Kyösti Mälkkic8771492015-05-09 08:06:02 +030064 yylval.strval = strdup(src);
Sol Boucher69b88bf2015-02-26 11:47:19 -080065 return STRING;
66}