-
Notifications
You must be signed in to change notification settings - Fork 3
/
simpleParser.cpp
326 lines (293 loc) · 7.38 KB
/
simpleParser.cpp
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
316
317
318
319
320
321
322
323
324
325
326
/*
* simpleParser
* Implement a command-line parser.
* Written 2014 by Bill Westfield (WestfW)
* refactored 2020
* Released to the public domain.
*/
#include "Arduino.h"
#include "simpleParser.h"
// uncomment to display parser output to serial console
//#define DEBUG
/*
* Reset the line buffer
*/
void parserCore::reset ()
{
memset(buffer, 0, lineLen);
inptr = 0;
parsePtr = 0;
termchar = 0;
}
/*
* getLine
* Read a line of text from Serial into the internal line buffer.
* With echoing and editing!
* Non-blocking. Returns 0 until end-of-line seen.
*/
uint8_t parserCore::getLine ()
{
int c;
c = S->read();
switch (c) {
case 127:
case CTRL('H'):
/*
Destructive backspace: remove last character
*/
if (inptr > 0) {
S->print("\010 \010");
buffer[--inptr] = 0;
}
break;
case CTRL('R'):
/*
Ctrl-R retypes the line
*/
S->print("\r\n");
S->print(buffer);
break;
case CTRL('U'):
/*
Ctrl-U deletes the entire line and starts over.
*/
S->println("XXX");
reset();
break;
case CTRL('J'):
case CTRL('M'):
buffer[inptr++] = '\n';
S->println(); /* Echo newline too. */
return inptr;
case -1:
/*
No character present; don't do anything.
*/
return 0;
default:
/*
Otherwise, echo the character and put it into the buffer
*/
buffer[inptr++] = c;
S->write(c);
}
return 0;
}
/*
* getLineWait
* like getLine, but block until a complete line is read
*/
uint8_t parserCore::getLineWait (void)
{
uint8_t status;
do {
status = getLine();
} while (status == 0);
return status;
}
bool parserCore::IsWhitespace (char c)
{
return (c == ' ' || c == CTRL('I'));
}
bool parserCore::delim(char c)
{
static const char Delimiters[] = "\r\n ,;:=\t";
if (c == 0 || strchr(Delimiters, c))
return true;
return false;
}
/*
* Number
* Advance the token and parse a number. Accept decimal, hex, octal.
*/
int parserCore::number()
{
char *p = token();
if (p) {
return strtol(p, 0, 0);
}
return -1;
}
int parserCore::lastNumber()
{
if (lastToken && *lastToken) {
return strtol(lastToken, 0, 0);
}
return -1;
}
/*
* eol
* return true if we're at the end of the line.
*/
boolean parserCore::eol ()
{
while (IsWhitespace(buffer[parsePtr])) { /* skip leading whitespace */
parsePtr++;
}
return buffer[parsePtr] == '\n' || buffer[parsePtr] == 0;
}
/*
* cliTermChar
* return the termination character of the last token
*/
uint8_t parserCore::termChar ()
{
return termchar;
}
/*
* Character
*/
/*
* token
* A token is a set of non-delimiter characters ending at a delimiter.
* As a line is parsed from the internal buffer, parsePtr is advanced, and
* the delimiters of parsed tokens are replaced with nulls.
* Note that a line always ends with the newline character AND a null.
*/
char *parserCore::token ()
{
uint8_t i;
if (eol()) { // reached the end of the line?
return NULL;
}
i = parsePtr; // save start position of token
while ((!delim(buffer[parsePtr])) && (parsePtr < lineLen)) {
parsePtr++; // advance pointer till we hit a delimiter.
}
termchar = buffer[parsePtr];
buffer[parsePtr++] = 0; // replace the delimiter with null
lastToken = &buffer[i];
return lastToken; // convert position to pointer for retval
}
/*
* Match the next token with a list of keywords.
* The list of keywords is in PROGMEM, separated by spaces.
* returns either the position of the found keyword (0..n),
* PARSER_NOMATCH, PARSER_AMB, or PARSER_EOL at the end of line
*/
int8_t parserCore::keyword (const char *keys)
{
char *p = token();
char *thisKey = (char *)keys;
int8_t i = 0, match, first=PARSER_NOMATCH;
if (!p)
return PARSER_EOL;
while (pgm_read_byte(thisKey)) {
match = tokcasecmp(p, thisKey);
#if defined(DEBUG)
//extern char spbuffer[];
char spbuffer[128];
sprintf(spbuffer, "key='%s', p='%s', match = %d\n", thisKey, p, match);
S->print(spbuffer);
#endif
if (match == CMP_MATCH) {
return i;
}
while (pgm_read_byte(thisKey) > ' ') {
thisKey++; // advance to next keyword
}
thisKey++; // skip delimiter
if (match == CMP_PARTIALMATCH) {
// There was a partial match; check for another...
if (first != PARSER_NOMATCH) { // already another match?
return (PARSER_AMB);
} else {
first = i;
continue;
}
return i; // match
}
i++; // next keyword
}
return first;
}
/*
* tokcasecmp
* tokcasecmp is like strcasecmp_P, except that the strings are terminated
* by any char < 32. Return value is 0 for match, or pointer to the delimiter
* for non-match (to expedite comparing against strings of targets.)
*/
uint8_t parserCore::tokcasecmp(const char *tok, const char *target)
{
char tokc, keyc;
const char *t = (char *)target;
do {
tokc = toupper(*tok++);
keyc = toupper(pgm_read_byte(t++));
// tok++; t++;
if (tokc == 0) {
// End of token; see if end of keyword as well
if (keyc <= ' ') {
return CMP_MATCH; // both ended - exact match
}
return CMP_PARTIALMATCH; // keyword is longer - partial
}
// Not end of token
if (keyc <= ' ') {
return CMP_NONMATCH; // key ended before tok - non match
}
} while (tokc == keyc);
return CMP_NONMATCH; // neither string ended, but non-match
}
/*
hexton
Turn a Hex digit (0..9, A..F) into the equivalent binary value (0-16)
*/
uint8_t parserCore::hexton (uint8_t h)
{
if (h >= '0' && h <= '9')
return (h - '0');
if (h >= 'A' && h <= 'F')
return ((h - 'A') + 10);
return (0);
}
#define error(a) S->print(a)
/*
* parse a line worth of Intel hex format
* returns byte count on successs, -1 if error.
*/
int parserCore::tryihex(int16_t *iaddr, uint8_t *bytes)
{
uint16_t addr = 0;
uint8_t b, cksum = 0;
byte len;
if (buffer[parsePtr] != ':')
return -1;
parsePtr++;
len = hexton(buffer[parsePtr++]); /* Length */
len = (len << 4) + hexton(buffer[parsePtr++]);
cksum = len;
b = hexton(buffer[parsePtr++]); /* address */
b = (b << 4) + hexton(buffer[parsePtr++]);
cksum += b;
addr = b;
b = hexton(buffer[parsePtr++]); /* address 2nd byte */
b = (b << 4) + hexton(buffer[parsePtr++]);
cksum += b;
addr = (addr << 8) + b;
*iaddr = addr;
b = hexton(buffer[parsePtr++]); /* record type */
b = (b << 4) + hexton(buffer[parsePtr++]);
cksum += b;
for (uint8_t i = 0; i < len; i++) { /* <len> bytes of data */
b = hexton(buffer[parsePtr++]);
b = (b << 4) + hexton(buffer[parsePtr++]);
*bytes++ = b;
cksum += b;
}
b = hexton(buffer[parsePtr++]); /* checksum */
b = (b << 4) + hexton(buffer[parsePtr++]);
cksum += b;
if (cksum != 0) {
error("Bad checksum: ");
Serial.print(cksum, HEX);
}
/* line terminator */
if ((buffer[parsePtr++] == '\n') ||
(buffer[parsePtr++] == '\r') ||
(buffer[parsePtr++] == 0)) {
return len;
}
error("No end of line");
return -1;
}