-
Notifications
You must be signed in to change notification settings - Fork 2
/
parseconfig-lex.l
129 lines (101 loc) · 2.17 KB
/
parseconfig-lex.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
%option noyywrap
%option 8bit
%option reentrant
%option warn nodefault
%option yylineno
%option bison-bridge
%option never-interactive
%option noinput
%option nounput
%{
#include "parseconfig-rename.h"
#include <stdlib.h>
#include <limits.h>
#include "list.h"
#include "parseconfig-yacc.h"
#define YY_EXTRA_TYPE struct list_head*
extern int yyparse();
static long parse_scientific(const char * const number);
%}
%%
[ \t] ;
[#][^\n]* ;
[\n] return EOL;
[{] return LEFTCURLY;
[}] return RIGHTCURLY;
0x[0-9a-fA-F]+ {
yylval->l = strtol(yytext, NULL, 16);
return NUMBER;
}
-?[0-9]+[eE][0-9]+ {
yylval->l = parse_scientific(yytext);
return NUMBER;
}
-?[0-9]+ {
yylval->l = strtol(yytext, NULL, 10);
return NUMBER;
}
[a-zA-Z][a-zA-Z0-9\.]* {
yylval->str = strdup(yytext);
return WORD;
}
["][^"\n]*["] {
yylval->str = strdup(yytext + 1);
yylval->str[strlen(yylval->str) -1] = '\0';
return WORD;
}
%%
static long raise_ten_to_power(long exp)
{
if (exp == 0)
return 1;
long r = 1;
for (long l = 0; l < exp; l++) {
r *= 10;
if (r > LONG_MAX / 10)
return LONG_MAX;
}
return r;
}
static long parse_scientific(const char * const string)
{
const char *pexp = string;
while (*pexp != 'e' && *pexp != 'E')
pexp++;
pexp++;
long exp = strtol(pexp, NULL, 10);
long coeff = strtol(string, NULL, 10);
exp = raise_ten_to_power(exp);
if (LONG_MAX / exp < coeff)
return LONG_MAX;
else if (LONG_MIN / exp > coeff)
return LONG_MIN;
else
return exp * coeff;
}
int parse(char *begin, size_t size, struct list_head * const root)
{
void *scanner;
if (yylex_init_extra(root, &scanner))
return -1;
YY_BUFFER_STATE yybs = yy_scan_buffer(begin, size, scanner);
if (!yybs)
goto err_destroy;
yy_switch_to_buffer(yybs, scanner);
/*
* Some flex builds do not seem to initialize yylineno correctly.
* It is unknown why, but it is trivial to always initialize it
* ourselves.
*/
yybs->yy_bs_lineno = 0;
if (yyparse(scanner))
goto err_delete;
yy_delete_buffer(yybs, scanner);
yylex_destroy(scanner);
return 0;
err_delete:
yy_delete_buffer(yybs, scanner);
err_destroy:
yylex_destroy(scanner);
return -1;
}