-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTokenParser.h
315 lines (300 loc) · 7.86 KB
/
TokenParser.h
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#ifndef TOKENPARSER_H
#define TOKENPARSER_H
#include <WProgram.h>
#include <Stream.h>
#include "Core.h"
#include "Variant.h"
class TokenParser {
public:
typedef enum {
InvalidToken = 0,
ValidToken,
JsonToken
} TokenType;
TokenParser(Stream* stream, us8 bufferSize = 100) {
myStream = stream;
buffer = (us8*)malloc(bufferSize + 1);
reset();
save();
length = 0;
stopCharactorFound = false;
type = InvalidToken;
}
/// Read in characters from the stream
bool scan(us8 stopCharactor = '\r') {
if(stopCharactorFound) {
stopCharactorFound = false;
length = 0;
}
if(myStream->available() > 0) {
us8 data = myStream->read();
if(data != '\\') {
buffer[length++] = data;
}
if(data == stopCharactor) {
stopCharactorFound = true;
buffer[length - 1] = ' ';
reset();
save();
nextToken();
}
}
return stopCharactorFound;
}
// Get the length of the entire string in the buffer
us8 getLength() {
return length;
}
us8 getTail() {
return tail;
}
us8 getHead() {
return head;
}
us8 remaining() {
return length - head;
}
bool isJson() {
return (type == JsonToken) ? true : false;
}
/// reset head and tail to 0 (zero)
void reset() {
head = 0;
tail = 0;
}
void save() {
savedHead = head;
savedTail = tail;
}
void restore() {
head = savedHead;
tail = savedTail;
}
/// todo: add bounds checking
bool startsWith(const char* string, bool caseSensitive = false) {
us8 i = 0;
us8 index = tail;
#ifdef DEBUG_TOKEN_PARSER
print(string);
print("=");
print(toString());
print("=>");
#endif
while(string[i] != 0) {
if(string[i] != '?') {
if(!characterCompare(buffer[index], string[i], caseSensitive)) {
#ifdef DEBUG_TOKEN_PARSER
println("false");
#endif
return false;
}
}
i++;
index++;
}
#ifdef DEBUG_TOKEN_PARSER
println("true");
#endif
return true;
}
/// Compares an entire string for equality
bool compare(const char* string, bool caseSensitive = false) {
us8 i = 0;
us8 index = tail;
#ifdef DEBUG_TOKEN_PARSER
print(string);
print("=");
print(toString());
print("=>");
#endif
while(string[i] != 0) {
if(string[i] != '?') {
if(!characterCompare(buffer[index], string[i], caseSensitive)) {
#ifdef DEBUG_TOKEN_PARSER
println("false");
#endif
return false;
}
}
i++;
index++;
}
if((head - tail) != i) {
#ifdef DEBUG_TOKEN_PARSER
println("false (head - tail) != i");
#endif
return false;
}
#ifdef DEBUG_TOKEN_PARSER
println("true");
#endif
return true;
}
/// todo: enforce consecutive characters
bool contains(const char* string, bool caseSensitive = false) {
us8 i = 0;
us8 index = tail;
#ifdef DEBUG_TOKEN_PARSER
print(string);
print("=");
print(toString());
print("=>");
#endif
while(string[i] != 0 && index < length) {
if(characterCompare(buffer[index++], string[i], caseSensitive)) {
i++;
if(string[i] == 0) {
#ifdef DEBUG_TOKEN_PARSER
println("true");
#endif
return true;
}
}
}
#ifdef DEBUG_TOKEN_PARSER
println("false");
#endif
return false;
}
String toString() {
if(head > tail) {
char array[(head - tail) + 1];
us8 index = 0;
for(us8 i = tail; i < head; i++) {
array[index++] = buffer[i];
}
array[index] = 0;
return String((const char*)array);
}
return String();
}
bool nextToken(s8 index = -1) {
if((0 < index) && (index < length)) {
head = index;
}
tail = head;
for(us8 i = tail; tail < length; i++) {
s8 result = match(buffer[i], " \n\t{");
if(result == -1) {
tail = i;
break;
}
else if(result >= 2) {
head = length - 1;
type = JsonToken;
return true;
}
}
head = tail;
for(us8 i = head; head < length; i++) {
s8 result = match(buffer[i], " \n\t");
if(result != -1) {
head = i;
type = ValidToken;
#ifdef DEBUG_TOKEN_PARSER
print("nextToken (head=");
print(String(head,DEC));
print(",tail=");
print(String(tail,DEC));
println(")");
#endif
return true;
}
}
type = InvalidToken;
return false;
}
bool advanceTail(us8 advance) {
if(advance < (head - tail)) {
tail += advance;
return true;
}
return false;
}
bool reverseHead(us8 reverse) {
if(reverse < (head - tail)) {
head = head - reverse;
return true;
}
return false;
}
void advanceHead(us8 advance) {
us8 limit = length - head;
if(advance < limit) {
head += advance;
}
else {
head += (limit - 1);
}
}
us8 hexCharToNibble(us8 c) {
// make upper case
if('a' <= c && c <= 'z') {
c -= 0x20;
}
if('0' <= c && c <= '9') {
c -= 0x30;
}
else if('A' <= c && c <= 'F') {
c -= 0x37;
}
return c & 0xf;
}
Variant toVariant() {
#ifdef DEBUG_TOKEN_PARSER
print("toVariant (head=");
print(String(head,DEC));
print(",tail=");
print(String(tail,DEC));
print(") \"");
print(toString());
println("\"");
#endif
return Variant::fromString(toString());
}
void print(const String &string) {
myStream->print(string);
}
void println(const String &string) {
myStream->println(string);
}
static s8 match(us8 character, const char* delimiters = 0) {
us8 i = 0;
while(delimiters[i] != 0) {
if(character == delimiters[i]) {
return i;
}
i++;
}
return -1;
}
private:
inline bool characterCompare(us8 char1, us8 char2, bool caseSensitive = true) {
if(char1 == char2) {
return true;
}
if(!caseSensitive) {
if(uppercase(char1) == uppercase(char2)) {
return true;
}
}
return false;
}
inline us8 uppercase(us8 x) {
// make upper case
if ('a' <= x && x <= 'z') {
x -= 0x20;
}
return x;
}
Stream* myStream;
us8* buffer;
us8 length; ///< length of all the characters in the buffer (up to stopCharacter)
us8 head; ///< index of delimiter trailing the current token
us8 tail; ///< index of the first character in the current token
us8 savedHead;
us8 savedTail;
bool stopCharactorFound;
TokenType type;
};
#endif // TOKENPARSER_H