blob: be9a5def84c5d8e7ee0201501371bb7d14ae4f11 [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
Hung-Te Lin9497fcb2019-03-04 14:28:37 +080027%s FLAGS
Sol Boucher69b88bf2015-02-26 11:47:19 -080028
29MULTIPLIER [KMG]
30
31%%
Sol Boucher69b88bf2015-02-26 11:47:19 -080032[[:space:]]+ /* Eat whitespace. */
33#.*$ /* Eat comments. */
Hung-Te Lin9497fcb2019-03-04 14:28:37 +080034\( BEGIN(FLAGS); return *yytext;
35<FLAGS>\) BEGIN(INITIAL); return *yytext;
36<FLAGS>CBFS return FLAG_CBFS;
Hung-Te Lin49a44502019-03-04 15:41:09 +080037<FLAGS>PRESERVE return FLAG_PRESERVE;
Sol Boucher69b88bf2015-02-26 11:47:19 -0800380{MULTIPLIER}? |
39[1-9][0-9]*{MULTIPLIER}? return parse_integer(yytext, 10);
400[0-9]+{MULTIPLIER}? return OCTAL;
Patrick Georgi2b807342016-06-28 20:45:34 +0200410[xX][0-9a-fA-F]+{MULTIPLIER}? return parse_integer(yytext + 2, 16);
Sol Boucher69b88bf2015-02-26 11:47:19 -080042[^#@{}()[: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}