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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/*
Ousía
Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cmath>
#include <sstream>
#include "CharReader.hpp"
#include "Logger.hpp"
#include "Number.hpp"
#include "Utils.hpp"
namespace ousia {
// TODO: Invent common system for error messages which allows localization
// TODO: Possibly adapt the clang error logging system
static const char *ERR_UNEXPECTED_CHAR = "Unexpected character";
static const char *ERR_UNEXPECTED_END = "Unexpected end of number literal";
static const char *ERR_TOO_LARGE = "Value too large to represent";
static std::string unexpectedMsg(const char *expected, const char got)
{
std::stringstream ss;
ss << ERR_UNEXPECTED_CHAR << ": Expected " << expected << " but got \'"
<< got << "\'";
return ss.str();
}
/* Class Number */
/**
* Returns the numeric value of the given ASCII character (returns 0 for
* '0', 1 for '1', 10 for 'A' and so on).
*
* @param c is the character for which the numeric value should be returned.
* @return the numeric value the character represents.
*/
static int charValue(char c)
{
if (c >= '0' && c <= '9') {
return c & 0x0F;
}
if ((c >= 'A' && c <= 'O') || (c >= 'a' && c <= 'o')) {
return (c & 0x0F) + 9;
}
return -1;
}
double Number::doubleValue()
{
return s * (a + ((double)n / (double)d)) * pow(10.0, (double)(sE * e));
}
int64_t Number::intValue() { return s * a; }
bool Number::appendChar(char c, int base, Part p, CharReader &reader,
Logger &logger)
{
// Check whether the given character is valid
int v = charValue(c);
if (v < 0 || v >= base) {
logger.error(unexpectedMsg("digit", c), reader);
return false;
}
// Append the number to the specified part
switch (p) {
case Part::A:
a = a * base + v;
break;
case Part::N:
n = n * base + v;
d = d * base;
break;
case Part::E:
e = e * base + v;
break;
}
// Check for any overflows
if (a < 0 || n < 0 || d < 0 || e < 0) {
logger.error(ERR_TOO_LARGE, reader);
return false;
}
return true;
}
bool Number::parse(CharReader &reader, Logger &logger,
const std::unordered_set<char> &delims)
{
State state = State::INIT;
char c;
// Consume the first whitespace characters
reader.consumeWhitespace();
// Iterate over the FSM to extract numbers
while (reader.peek(c)) {
// Abort, once a delimiter or whitespace is reached
if (Utils::isWhitespace(c) || delims.count(c)) {
reader.resetPeek();
break;
}
// The character is not a whitespace character and not a delimiter
switch (state) {
case State::INIT:
case State::HAS_MINUS:
switch (c) {
case '-':
// Do not allow multiple minus signs
if (state == State::HAS_MINUS) {
logger.error(unexpectedMsg("digit", c), reader);
return false;
}
state = State::HAS_MINUS;
s = -1;
break;
case '0':
// Remember a leading zero for the detection of "0x"
state = State::LEADING_ZERO;
break;
case '.':
// Remember a leading point as ".eXXX" is invalid
state = State::LEADING_POINT;
validInteger = false;
break;
default:
state = State::INT;
if (!appendChar(c, 10, Part::A, reader, logger)) {
return false;
}
break;
}
break;
case State::LEADING_ZERO:
if (c == 'x' || c == 'X') {
state = State::HEX;
break;
}
// fallthrough
case State::INT:
switch (c) {
case '.':
state = State::POINT;
validInteger = false;
break;
case 'e':
case 'E':
state = State::EXP_INIT;
break;
default:
state = State::INT;
if (!appendChar(c, 10, Part::A, reader, logger)) {
return false;
}
break;
}
break;
case State::HEX:
if (!appendChar(c, 16, Part::A, reader, logger)) {
return false;
}
break;
case State::LEADING_POINT:
case State::POINT:
switch (c) {
case 'e':
case 'E':
if (state == State::LEADING_POINT) {
logger.error(unexpectedMsg("digit", c), reader);
return false;
}
state = State::EXP_INIT;
break;
default:
state = State::POINT;
if (!appendChar(c, 10, Part::N, reader, logger)) {
return false;
}
break;
}
break;
case State::EXP_HAS_MINUS:
case State::EXP_INIT:
if (c == '-') {
if (state == State::EXP_HAS_MINUS) {
logger.error(unexpectedMsg("digit", c), reader);
return false;
}
state = State::EXP_HAS_MINUS;
sE = -1;
} else {
state = State::EXP;
if (!appendChar(c, 10, Part::E, reader, logger)) {
return false;
}
}
break;
case State::EXP:
if (!appendChar(c, 10, Part::E, reader, logger)) {
return false;
}
break;
}
reader.consumePeek();
}
// States in which ending is valid. Log an error in other states
if (state == State::LEADING_ZERO || state == State::HEX ||
state == State::INT || state == State::POINT || state == State::EXP) {
return true;
}
logger.error(ERR_UNEXPECTED_END, reader);
return false;
}
bool Number::parse(const std::string &str, Logger &logger)
{
// Create a char reader instance with the given string and call the actual
// parse function
CharReader reader(str);
return parse(reader, logger);
}
bool Number::parseFixedLenInt(CharReader &reader, int len, int base, Logger &logger)
{
char c;
reader.consumePeek();
for (int i = 0; i < len; i++) {
if (!reader.peek(c)) {
logger.error("Unexpected end of escape sequence", reader);
return false;
}
if (!appendChar(c, base, Number::Part::A, reader, logger)) {
return false;
}
reader.consumePeek();
}
return true;
}
}
|