-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.cpp
586 lines (525 loc) · 12.1 KB
/
parser.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*
* parser.cpp
* Implement a command-line parser.
* Written 2014 by Bill Westfield (WestfW)
* Released to the public domain.
*/
#include "Arduino.h"
#include "parser.h"
#include <avr/pgmspace.h>
#include "WString.h"
#ifdef DEBUG
static const int debug = DEBUG;
#else
static const int debug = 0;
#endif
#define DEBUGPRINT(...) if (debug) printf(__VA_ARGS__)
bool terminator(char c);
uint8_t keywd_cmp (const char *in, const char *s);
uint8_t keywd_len (const char *s);
char * keywd_complete (const char *in, const char *list);
class cmd_t {
private:
const char *text;
void *(*func)(char *);
cmd_t *next;
public:
cmd_t() { text=NULL; func = NULL; }
cmd_t(cmd_t *&head, const char *t, void*(*f)(char *)) {
text = t;
func = f;
if (head == NULL) {
head = this;
} else {
cmd_t *p = (cmd_t *)head;
while (p->next != NULL) {
p = p->next;
}
p->next = this;
}
}
void dump() {
cmd_t *p = this;
while (p) {
printf("%s\n", p->text);
p = p->next;
}
}
cmd_t *matchany (cmd_t *head, const char *in) {
uint8_t matchlen, // number of characters matched
keylength, // full size of keyword.
nmatches = 0; // number of matches
cmd_t *pmatch=NULL, *match = head; // candidate partialmatch
DEBUGPRINT("\n cmd_t matchany '%s' in '%s'", in, match->next->text);
do { // for all keyword choices
keylength = keywd_len(match->text);
matchlen = keywd_cmp(in, match->text);
DEBUGPRINT("\n matched %d of %d chars in '%s'", matchlen, keylength, match->text);
if (matchlen == keylength) {
return match; // exact match
} else if (matchlen) {
// partial match. Remember it.
nmatches++;
pmatch = match;
DEBUGPRINT("\npartial match %d at '%s'", nmatches, match->text);
}
/* Next possible keyword */
match = match->next;
} while (match);
if (nmatches == 1) {
return pmatch;
}
DEBUGPRINT("\nno match");
return NULL; // no match
}
char *complete (const char *in) {
return keywd_complete(in, text);
}
};
bool terminator(char c) {
return c == 0 || c == ' ';
}
/*
* Compare keyword
* compare an input keyword (in) with a string in a table (s)
* terminate on any non-printing character (including space)
* return the number of matching characters:
* strlen(in): the input is a partial match of the string
* strlen(s): exact match
* 0: no match (0 chars matched OR in longer than s)
*/
uint8_t keywd_cmp (const char *in, const char *s)
{
uint8_t count = 0;
while (!terminator(*s)) {
if (terminator(*in))
return count;
if (*in++ != *s++)
return 0;
count++;
}
if (terminator(*in)) // "in" same length as "s" ?
return count; // exact match
return 0;
}
/*
* keywd_len
* like strlen(), only the string ends with any terminator
*/
uint8_t keywd_len (const char *s)
{
uint8_t len = 0;
while (!terminator(*s++))
len++;
return len;
}
/*
* keywd_complete
* return string pointer to "completion" of a singly-matched keyword.
*/
char * keywd_complete (const char *in, const char *list)
{
uint8_t matchlen, // number of characters matched
keylen, // keyword length
inlen; // full size of keyword.
static char compstr[10];
inlen = keywd_len(in);
do { // for all keyword choices
matchlen = keywd_cmp(in, list);
if (matchlen == inlen) {
/* this is our partial match */
char *p=compstr;
list += inlen;
while (!terminator(*list)) {
*p++ = *list++;
}
*p = 0;
return compstr;
}
list += keywd_len(list) + 1;
} while (*(list-1));
return 0; // no match
}
/*
* keywd_matchany
* see if the "in" string matches any of the keywords in "list" string
* return ... index val of exact match???
*/
uint8_t keywd_matchany (const char *in, const char *list)
{
uint8_t index = 0;
uint8_t matchlen, // number of characters matched
keylength, // full size of keyword.
nmatches = 0, // number of matches
partialmatch = 0; // candidate partialmatch
DEBUGPRINT("\nmatchany '%s' in '%s'", in, list);
do { // for all keyword choices
keylength = keywd_len(list);
matchlen = keywd_cmp(in, list);
DEBUGPRINT("\n matched %d of %d chars in '%s'", matchlen, keylength, list);
if (matchlen == keylength) {
return index+1; // exact match
} else if (matchlen) {
// partial match. Remember it.
nmatches++;
partialmatch = index+1;
DEBUGPRINT("\npartial match %d at %d", nmatches, partialmatch);
}
/* Next possible keyword */
index++;
list += keylength+1;
} while (*(list-1));
if (nmatches == 1) {
return partialmatch;
}
return 0; // no match
}
#if defined(TEST) && TEST
const char keywords[]= "test terminate end start";
const char *testinputs[] = {
"a",
"mult",
"mul",
"test",
"te",
"ter",
"try",
"exit",
"testing",
"add",
"end",
"start"
};
void *add_f (char *c) {
printf("%s\n", c);
return NULL;
}
void *sub_f (char *c) {
printf("%s\n", c);
return NULL;
}
void *mul_f (char *c) {
printf("%s\n", c);
return NULL;
}
cmd_t *mathcmd = NULL;
cmd_t add(mathcmd, "add", add_f);
cmd_t sub(mathcmd, "sub", sub_f);
cmd_t mul(mathcmd, "mul", mul_f);
void test_keywd_cmp()
{
printf("\ntest_keywd_cmp");
for (uint8_t i=0; i < sizeof(testinputs)/sizeof(testinputs[0]); i++) {
printf("\n Input is \"%s\", string \"%s\". output is %d",
testinputs[i], keywords, keywd_cmp(testinputs[i], keywords));
}
}
void test_keywd_match()
{
int index;
printf("\ntest_keywd_match");
for (uint8_t i=0; i < sizeof(testinputs)/sizeof(testinputs[0]); i++) {
index = keywd_matchany(testinputs[i], keywords);
printf("\n Input is \"%s\", string \"%s\". output is %d",
testinputs[i], keywords, index);
if (index) {
char *p = keywd_complete(testinputs[i], keywords);
if (p && *p) {
printf(" (complete with '%s')", p);
}
}
}
}
void test_cmd_match()
{
cmd_t *match;
printf("\ntest_cmd_match");
for (uint8_t i=0; i < sizeof(testinputs)/sizeof(testinputs[0]); i++) {
match = mathcmd->matchany(mathcmd, testinputs[i]);
printf("\n Input is \"%s\". output is %p",
testinputs[i], match);
if (match) {
char *p = match->complete(testinputs[i]);
if (p && *p) {
printf(" (complete with '%s')", p);
}
}
}
}
#endif
int main() {
printf("\nBuilt up command list:\n");
mathcmd->dump();
printf("\n");
#if defined(TEST) && TEST
test_keywd_cmp();
test_keywd_match();
test_cmd_match();
printf("\n");
#endif
}
char linebuffer[PARSER_LINELEN];
static byte inptr, parseptr, termchar;
char getcwait(void)
{
int c;
do {
c = Serial.read();
} while (c <= 0);
return c;
}
/*
* parserGetline
* Read a line of text from Serial into the internal line buffer.
* With echoing and editing!
*/
uint8_t parseGetline(void)
{
char c;
/*
* Reset the line buffer
*/
parseReset();
do {
c = getcwait();
switch (c) {
case 127:
case CTRL('H'):
/*
* Destructive backspace: remove last character
*/
if (inptr > 0) {
Serial.print("\010 \010");
linebuffer[--inptr] = 0;
}
break;
case CTRL('R'):
/*
* Ctrl-R retypes the line
*/
Serial.print("\r\n");
Serial.print(linebuffer);
break;
case CTRL('U'):
/*
* Ctrl-U deletes the entire line and starts over.
*/
Serial.println("XXX");
memset(linebuffer, 0, sizeof(linebuffer));
inptr = 0;
break;
default:
/*
* Otherwise, echo the character and put it into the buffer
*/
linebuffer[inptr++] = c;
Serial.write(c);
break;
}
} while (c != CTRL('M') && inptr < PARSER_LINELEN);
Serial.println(); /* Echo newline too. */
return(inptr);
}
/*
* parseGetline_nb
* 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.
*/
void parseReset()
{
/*
* Reset the line buffer
*/
memset(linebuffer, 0, sizeof(linebuffer));
inptr = 0;
parseptr = 0;
termchar = 0;
}
uint8_t parseGetline_nb(void)
{
int c;
c = Serial.read();
switch (c) {
case 127:
case CTRL('H'):
/*
* Destructive backspace: remove last character
*/
if (inptr > 0) {
Serial.print("\010 \010");
linebuffer[--inptr] = 0;
}
break;
case CTRL('R'):
/*
* Ctrl-R retypes the line
*/
Serial.print("\r\n");
Serial.print(linebuffer);
break;
case CTRL('U'):
/*
* Ctrl-U deletes the entire line and starts over.
*/
Serial.println("XXX");
memset(linebuffer, 0, sizeof(linebuffer));
inptr = 0;
break;
case CTRL('M'):
Serial.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
*/
linebuffer[inptr++] = c;
Serial.write(c);
}
return 0;
}
bool parseIsWhitespace(char c)
{
return (c == ' ' || c == CTRL('I'));
}
const char parseDelimiters[] PROGMEM = "\r\n ,;:=\t";
bool parseDelim(char c)
{
if (c == 0 || strchr_P(parseDelimiters, c))
return true;
return false;
}
int parseNumber()
{
return atoi(parseToken());
}
/*
* parseTermChar
* return the termination character of the last token
*/
uint8_t parseTermChar()
{
return termchar;
}
/*
* parseCharacter
*/
/*
* parseToken
* 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 * parseToken(void)
{
uint8_t i;
while (parseIsWhitespace(linebuffer[parseptr])) { /* skip leading whitespace */
parseptr++;
}
if (linebuffer[parseptr] == 0) { // reached the end of the line?
return NULL;
}
i = parseptr; // save start position of token
while ((!parseDelim(linebuffer[parseptr])) && (parseptr < PARSER_LINELEN)) {
parseptr++; // advance pointer till we hit a delimiter.
}
termchar = linebuffer[parseptr];
linebuffer[parseptr++] = 0; // replace the delimiter with null
return &linebuffer[i]; // 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, or PARSER_EOL at the end of line
*/
int8_t parseKeyword(const char *keys)
{
char *p = parseToken();
char *key = (char *)keys;
int8_t i = 0;
if (p) {
while (pgm_read_byte(key)) {
key = tokcasecmp(p, key);
if (key == 0) {
return i; // match
}
key++; // skip delimiter
i++; // next keyword
}
} else {
return PARSER_EOL;
}
return PARSER_NOMATCH;
}
/*
* 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.)
*/
char *tokcasecmp(char *tok, char *target)
{
char c1, c2;
char *t = (char *)target;
do {
c1 = TOUPPER(*tok);
c2 = TOUPPER(pgm_read_byte(t));
if (c2 <= ' ') {
/* Match */
if (c1 != 0) {
return t; // oops; both strings didn't end.
}
return 0; //match!
}
tok++; t++;
} while (c1 == c2);
/* Non-match. Advance target ptr */
while (pgm_read_byte(t) > ' ') {
t++;
}
return t;
}
#ifDEF NOT_READY_FOR_PRIME_TIME
cmd_t *cmd_t::head = NULL;
cmd_t::cmd_t() { text=NULL; func = NULL; }
cmd_t::cmd_t(const char *t, void*(*f)(char *)) {
text = t;
func = f;
if (head == NULL) {
head = this;
} else {
cmd_t *p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = this;
}
}
cmd_t::cmd_t(cmd_t *head, const char *t, void*(*f)(char *)) {
text = t;
func = f;
if (head == NULL) {
head = this;
} else {
cmd_t *p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = this;
}
}
cmd_t::void dump() {
cmd_t *p = head;
while (p) {
printf("%s\n", p->text);
p = p->next;
}
}
#endif