Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * fmd_scanner.l, scanner generator for flashmap descriptor language |
| 3 | * |
| 4 | * Copyright (C) 2015 Google, Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | %{ |
| 17 | #include "fmd_parser.h" |
| 18 | |
| 19 | #include <assert.h> |
| 20 | #include <string.h> |
| 21 | |
Kyösti Mälkki | c877149 | 2015-05-09 08:06:02 +0300 | [diff] [blame] | 22 | int parse_integer(char *src, int base); |
| 23 | int copy_string(const char *src); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 24 | %} |
| 25 | |
| 26 | %option noyywrap |
Hung-Te Lin | 9497fcb | 2019-03-04 14:28:37 +0800 | [diff] [blame] | 27 | %s FLAGS |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 28 | |
| 29 | MULTIPLIER [KMG] |
| 30 | |
| 31 | %% |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 32 | [[:space:]]+ /* Eat whitespace. */ |
| 33 | #.*$ /* Eat comments. */ |
Hung-Te Lin | 9497fcb | 2019-03-04 14:28:37 +0800 | [diff] [blame] | 34 | \( BEGIN(FLAGS); return *yytext; |
| 35 | <FLAGS>\) BEGIN(INITIAL); return *yytext; |
| 36 | <FLAGS>CBFS return FLAG_CBFS; |
Hung-Te Lin | 49a4450 | 2019-03-04 15:41:09 +0800 | [diff] [blame] | 37 | <FLAGS>PRESERVE return FLAG_PRESERVE; |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 38 | 0{MULTIPLIER}? | |
| 39 | [1-9][0-9]*{MULTIPLIER}? return parse_integer(yytext, 10); |
| 40 | 0[0-9]+{MULTIPLIER}? return OCTAL; |
Patrick Georgi | 2b80734 | 2016-06-28 20:45:34 +0200 | [diff] [blame] | 41 | 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] | 42 | [^#@{}()[:space:]]* return copy_string(yytext); |
| 43 | . return *yytext; |
| 44 | |
| 45 | %% |
| 46 | |
Kyösti Mälkki | c877149 | 2015-05-09 08:06:02 +0300 | [diff] [blame] | 47 | int parse_integer(char *src, int base) |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 48 | { |
| 49 | char *multiplier = NULL; |
Kyösti Mälkki | c877149 | 2015-05-09 08:06:02 +0300 | [diff] [blame] | 50 | unsigned val = strtoul(src, &multiplier, base); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 51 | |
| 52 | if (*multiplier) { |
| 53 | switch(*multiplier) { |
| 54 | case 'K': |
| 55 | val *= 1024; |
| 56 | break; |
| 57 | case 'M': |
| 58 | val *= 1024*1024; |
| 59 | break; |
| 60 | case 'G': |
| 61 | val *= 1024*1024*1024; |
| 62 | break; |
| 63 | default: |
| 64 | // If we ever get here, the MULTIPLIER regex is allowing |
| 65 | // multiplier suffixes not handled by this code. |
| 66 | assert(false); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | yylval.intval = val; |
| 71 | return INTEGER; |
| 72 | } |
| 73 | |
Kyösti Mälkki | c877149 | 2015-05-09 08:06:02 +0300 | [diff] [blame] | 74 | int copy_string(const char *src) |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 75 | { |
Kyösti Mälkki | c877149 | 2015-05-09 08:06:02 +0300 | [diff] [blame] | 76 | yylval.strval = strdup(src); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 77 | return STRING; |
| 78 | } |