blob: efcd3422903e9a7bdbd3f987fab3d4cfb285eb31 [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.
Sol Boucher69b88bf2015-02-26 11:47:19 -080014 */
15
16%{
17#include "fmd_parser.h"
18
19#include <assert.h>
20#include <string.h>
21
Kyösti Mälkkic8771492015-05-09 08:06:02 +030022int parse_integer(char *src, int base);
23int copy_string(const char *src);
Sol Boucher69b88bf2015-02-26 11:47:19 -080024%}
25
26%option noyywrap
27
28MULTIPLIER [KMG]
29
30%%
31
32[[:space:]]+ /* Eat whitespace. */
33#.*$ /* Eat comments. */
340{MULTIPLIER}? |
35[1-9][0-9]*{MULTIPLIER}? return parse_integer(yytext, 10);
360[0-9]+{MULTIPLIER}? return OCTAL;
370[xX][0-9a-f]+{MULTIPLIER}? return parse_integer(yytext + 2, 16);
38[^#@{}()[:space:]]* return copy_string(yytext);
39. return *yytext;
40
41%%
42
Kyösti Mälkkic8771492015-05-09 08:06:02 +030043int parse_integer(char *src, int base)
Sol Boucher69b88bf2015-02-26 11:47:19 -080044{
45 char *multiplier = NULL;
Kyösti Mälkkic8771492015-05-09 08:06:02 +030046 unsigned val = strtoul(src, &multiplier, base);
Sol Boucher69b88bf2015-02-26 11:47:19 -080047
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älkkic8771492015-05-09 08:06:02 +030070int copy_string(const char *src)
Sol Boucher69b88bf2015-02-26 11:47:19 -080071{
Kyösti Mälkkic8771492015-05-09 08:06:02 +030072 yylval.strval = strdup(src);
Sol Boucher69b88bf2015-02-26 11:47:19 -080073 return STRING;
74}