blob: 0a582105f398b8e60dcbe0daf76192d3f44fd118 [file] [log] [blame]
Sol Boucher69b88bf2015-02-26 11:47:19 -08001/*
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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
18 */
19
20%{
21#include "fmd_parser.h"
22
23#include <assert.h>
24#include <string.h>
25
Kyösti Mälkkic8771492015-05-09 08:06:02 +030026int parse_integer(char *src, int base);
27int copy_string(const char *src);
Sol Boucher69b88bf2015-02-26 11:47:19 -080028%}
29
30%option noyywrap
31
32MULTIPLIER [KMG]
33
34%%
35
36[[:space:]]+ /* Eat whitespace. */
37#.*$ /* Eat comments. */
380{MULTIPLIER}? |
39[1-9][0-9]*{MULTIPLIER}? return parse_integer(yytext, 10);
400[0-9]+{MULTIPLIER}? return OCTAL;
410[xX][0-9a-f]+{MULTIPLIER}? return parse_integer(yytext + 2, 16);
42[^#@{}()[:space:]]* return copy_string(yytext);
43. return *yytext;
44
45%%
46
Kyösti Mälkkic8771492015-05-09 08:06:02 +030047int parse_integer(char *src, int base)
Sol Boucher69b88bf2015-02-26 11:47:19 -080048{
49 char *multiplier = NULL;
Kyösti Mälkkic8771492015-05-09 08:06:02 +030050 unsigned val = strtoul(src, &multiplier, base);
Sol Boucher69b88bf2015-02-26 11:47:19 -080051
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älkkic8771492015-05-09 08:06:02 +030074int copy_string(const char *src)
Sol Boucher69b88bf2015-02-26 11:47:19 -080075{
Kyösti Mälkkic8771492015-05-09 08:06:02 +030076 yylval.strval = strdup(src);
Sol Boucher69b88bf2015-02-26 11:47:19 -080077 return STRING;
78}