-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTokenParser.h
253 lines (227 loc) · 5.67 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
#ifndef TOKENPARSER_H
#define TOKENPARSER_H
#include "WProgram.h"
#include "../pic32lib/core.h"
class TokenParser {
public:
TokenParser(us8* newBuffer, us8 newLength) {
buffer = newBuffer;
length = newLength;
head = 0;
tail = 0;
}
boolean compare(const char* string, char wild = '?' )
{
us8 i = 0;
us8 temp = tail;
while(string[i] != 0) {
if(string[i] != wild) {
if((buffer[temp]>0x60 ? buffer[temp]-0x20 : buffer[temp]) != string[i]) {
return false;
}
}
i++;
temp++;
}
return true;
}
int replace(char a, char b )
{
us8 i = 0;
int count = 0;
while(buffer[i] != 0) {
if(buffer[i] == a) {
buffer[i] = b;
count++;
}
i++;
}
return count;
}
// todo: enforce consecutive charactors
boolean contains(const char* string)
{
us8 i = 0;
us8 index = tail;
while(string[i] != 0 && index < head) {
if(buffer[index++] == string[i]) {
i++;
if(string[i] == 0) {
return true;
}
}
}
return false;
}
void print()
{
Serial.print(tail, DEC);
Serial.print(" - ");
Serial.println(head, DEC);
Serial.print("'");
for(us8 i = tail; i <= head; i++) {
Serial.print(buffer[i]);
}
Serial.print("'");
Serial.println();
}
boolean nextToken()
{
tail = head;
tail = skip(tail, true);
head = tail;
head = skip(tail, false);
return (tail < head) ? true : false;
}
boolean advanceTail(us8 advance)
{
if(advance < (head - tail)) {
tail += advance;
return true;
}
return false;
}
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;
}
e16 to_e16()
{
bool neg = false;
e16 temp;
temp.value = 0;
temp.exp = 0;
if(contains("-")) { // todo: change to startsWith("-")
advanceTail(1);
neg = true;
}
// todo: change to startsWith("0x")
if(contains("0x")) { // HEX
advanceTail(2);
us8 index = tail;
do {
temp.value <<= 4;
temp.value |= hexCharToNibble(buffer[index++]);
} while(index < head);
}
else if(contains("e")) { // Exp.Notation
Serial.println("Is Exp.Notation");
}
else if(contains(".")) { // REAL
us16 base = pow(10, (head - tail - 2));
boolean ad = false; // after decimal, of course...
us8 index = tail;
while(index < head) {
us8 data = buffer[index++];
if(data == '.') {
ad = true;
}
else {
temp.value += (data - 0x30) * base;
base /= 10;
if(ad) {
temp.exp--;
}
}
}
}
else { // WHOLE
us16 base;
while(tail < head) {
base = pow(10, head - tail - 1);
temp.value += (buffer[tail] - 0x30) * base;
tail++;
}
}
if (neg)
temp.value *=-1;
return temp;
}
e32 to_e32()
{
bool neg = false;
e32 temp;
temp.value = 0;
temp.exp = 0;
if(contains("-")) { // todo: change to startsWith("-")
advanceTail(1);
neg = true;
}
// todo: change to startsWith("0x")
if(contains("0x")) { // HEX
advanceTail(2);
us8 index = tail;
do {
temp.value <<= 4;
temp.value |= hexCharToNibble(buffer[index++]);
} while(index < head);
}
else if(contains("e")) { // Exp.Notation
Serial.println("Is Exp.Notation");
}
else if(contains(".")) { // REAL
us32 base = pow(10, (head - tail - 2));
boolean ad = false; // after decimal, of course...
us8 index = tail;
while(index < head) {
us8 data = buffer[index++];
if(data == '.') {
ad = true;
}
else {
temp.value += (data - 0x30) * base;
base /= 10;
if(ad) {
temp.exp--;
}
}
}
}
else { // WHOLE
us32 base;
while(tail < head) {
base = pow(10, head - tail - 1);
temp.value += (buffer[tail] - 0x30) * base;
tail++;
}
}
if (neg)
temp.value *=-1;
return temp;
}
private:
us8 skip(us8 index, boolean whitespace)
{
while(0 <= index && index <= length) {
if(whitespace) {
if(buffer[index] != 0x20) {
break;
}
}
else {
if(buffer[index] == 0x20) {
break;
}
}
index++;
}
if(index > length) {
return 0;
}
return index;
}
us8* buffer;
us8 length;
us8 head;
us8 tail;
};
#endif // TOKENPARSER_H