-
Notifications
You must be signed in to change notification settings - Fork 5
/
value.rl
76 lines (61 loc) · 1.22 KB
/
value.rl
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
#include "value.h"
#include <stdexcept>
void value::expect_number() const {
std::string error;
error = "Expected a number when \"";
error += string;
error += "\" was encountered";
throw std::domain_error(error);
}
void value::scan_number(void) noexcept {
%%{
machine scanner;
hexnumber =
('$' | '0x' | '0X')
(
[0-9] ${ value = (value << 4) + fc - '0'; }
|
[A-Fa-f] ${value = (value << 4) + (fc | 0x20) - 'a' + 10; }
)+
;
binnumber =
('0b' | '0B')
[01]+ ${ value = (value << 1) + fc - '0'; }
;
octalnumber =
'0'
[0-7]+ ${ value = (value << 3) + fc - '0'; }
;
# a leading 0 is ambiguous since it could also
# be part of the binary or hex prefix.
# however, setting it to 0 is safe.
decnumber =
'0'
|
([1-9] [0-9]*) ${ value = value * 10 + fc - '0'; }
;
main :=
( hexnumber | decnumber |binnumber)
%{
status = valid;
number = value;
return;
}
;
}%%
if (string.empty()) {
// special case.
status = valid;
number = 0;
return;
}
const char *p = string.data();
const char *pe = string.data() + string.size();
const char *eof = pe;
int cs;
int32_t value = 0;
%%write data;
%%write init;
%%write exec;
status = invalid;
}