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 |
| 27 | |
| 28 | MULTIPLIER [KMG] |
| 29 | |
| 30 | %% |
| 31 | |
| 32 | [[:space:]]+ /* Eat whitespace. */ |
| 33 | #.*$ /* Eat comments. */ |
| 34 | 0{MULTIPLIER}? | |
| 35 | [1-9][0-9]*{MULTIPLIER}? return parse_integer(yytext, 10); |
| 36 | 0[0-9]+{MULTIPLIER}? return OCTAL; |
Patrick Georgi | 2b80734 | 2016-06-28 20:45:34 +0200 | [diff] [blame] | 37 | 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] | 38 | [^#@{}()[:space:]]* return copy_string(yytext); |
| 39 | . return *yytext; |
| 40 | |
| 41 | %% |
| 42 | |
Kyösti Mälkki | c877149 | 2015-05-09 08:06:02 +0300 | [diff] [blame] | 43 | int parse_integer(char *src, int base) |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 44 | { |
| 45 | char *multiplier = NULL; |
Kyösti Mälkki | c877149 | 2015-05-09 08:06:02 +0300 | [diff] [blame] | 46 | unsigned val = strtoul(src, &multiplier, base); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 47 | |
| 48 | if (*multiplier) { |
| 49 | switch(*multiplier) { |
| 50 | case 'K': |
| 51 | val *= 1024; |
| 52 | break; |
| 53 | case 'M': |
| 54 | val *= 1024*1024; |
| 55 | break; |
| 56 | case 'G': |
| 57 | val *= 1024*1024*1024; |
| 58 | break; |
| 59 | default: |
| 60 | // If we ever get here, the MULTIPLIER regex is allowing |
| 61 | // multiplier suffixes not handled by this code. |
| 62 | assert(false); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | yylval.intval = val; |
| 67 | return INTEGER; |
| 68 | } |
| 69 | |
Kyösti Mälkki | c877149 | 2015-05-09 08:06:02 +0300 | [diff] [blame] | 70 | int copy_string(const char *src) |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 71 | { |
Kyösti Mälkki | c877149 | 2015-05-09 08:06:02 +0300 | [diff] [blame] | 72 | yylval.strval = strdup(src); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 73 | return STRING; |
| 74 | } |