Patrick Georgi | ea063cb | 2020-05-08 19:28:13 +0200 | [diff] [blame] | 1 | /* scanner generator for flashmap descriptor language */ |
Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 2 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 3 | |
| 4 | %{ |
| 5 | #include "fmd_parser.h" |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <string.h> |
| 9 | |
Kyösti Mälkki | c877149 | 2015-05-09 08:06:02 +0300 | [diff] [blame] | 10 | int parse_integer(char *src, int base); |
| 11 | int copy_string(const char *src); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 12 | %} |
| 13 | |
| 14 | %option noyywrap |
Hung-Te Lin | 9497fcb | 2019-03-04 14:28:37 +0800 | [diff] [blame] | 15 | %s FLAGS |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 16 | |
| 17 | MULTIPLIER [KMG] |
| 18 | |
| 19 | %% |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 20 | [[:space:]]+ /* Eat whitespace. */ |
| 21 | #.*$ /* Eat comments. */ |
Hung-Te Lin | 9497fcb | 2019-03-04 14:28:37 +0800 | [diff] [blame] | 22 | \( BEGIN(FLAGS); return *yytext; |
| 23 | <FLAGS>\) BEGIN(INITIAL); return *yytext; |
| 24 | <FLAGS>CBFS return FLAG_CBFS; |
Hung-Te Lin | 49a4450 | 2019-03-04 15:41:09 +0800 | [diff] [blame] | 25 | <FLAGS>PRESERVE return FLAG_PRESERVE; |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 26 | 0{MULTIPLIER}? | |
| 27 | [1-9][0-9]*{MULTIPLIER}? return parse_integer(yytext, 10); |
| 28 | 0[0-9]+{MULTIPLIER}? return OCTAL; |
Patrick Georgi | 2b80734 | 2016-06-28 20:45:34 +0200 | [diff] [blame] | 29 | 0[xX][0-9a-fA-F]+{MULTIPLIER}? return parse_integer(yytext + 2, 16); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 30 | [^#@{}()[:space:]]* return copy_string(yytext); |
| 31 | . return *yytext; |
| 32 | |
| 33 | %% |
| 34 | |
Kyösti Mälkki | c877149 | 2015-05-09 08:06:02 +0300 | [diff] [blame] | 35 | int parse_integer(char *src, int base) |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 36 | { |
| 37 | char *multiplier = NULL; |
Kyösti Mälkki | c877149 | 2015-05-09 08:06:02 +0300 | [diff] [blame] | 38 | unsigned val = strtoul(src, &multiplier, base); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 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 | |
Kyösti Mälkki | c877149 | 2015-05-09 08:06:02 +0300 | [diff] [blame] | 62 | int copy_string(const char *src) |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 63 | { |
Kyösti Mälkki | c877149 | 2015-05-09 08:06:02 +0300 | [diff] [blame] | 64 | yylval.strval = strdup(src); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 65 | return STRING; |
| 66 | } |