-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniparser.cpp
2903 lines (2610 loc) · 102 KB
/
uniparser.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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
** OrangeBot Project
*****************************************************************************
** /
** /
** /
** ______ \
** \
** \
*****************************************************************************
** UNIVERSAL PARSER
*****************************************************************************
** Author: Orso Eric
** Creation Date: 2019-06-17
** Last Edit Date: 2019-10-09
** Revision: 2
** Version: 4.1
****************************************************************************/
/****************************************************************************
** DESCRIPTION
*****************************************************************************
** Do a universap parser that does not require malloc/free and do not use explicit dictionary
** I create a special register command function in which the message and an handler is provided
****************************************************************************/
/****************************************************************************
** COMMAND RULES
*****************************************************************************
** ADD COMMAND TO PARSER
** A command is composed by fixed alphanumeric chars and argument descriptors
** Each command is associated with a unique function callback
** A command can only start with letters
**
** EXAMPLE ADD COMMAND TO PARSER
** myparser.add_cmd("P", (void *)&my_ping_handler );
** Add a new command that is triggered when the string P\0 is received.
** function my_ping_handler will be automatically executed when the \0 is processed
** myparser.exe( 'P' );
** myparser.exe( '\0' );
** send manually bytes to the parser to test the system
*****************************************************************************
** Command restriction:
** >Can only start with a letter
** >Valid arguments are %u %s %U %S %d
** >There can be no commands with same start but two different argument type. Only the first one will be considered in case
** >After valid ID access parameters using Parser Macros
****************************************************************************/
/****************************************************************************
** KNOWN BUG
*****************************************************************************
**
****************************************************************************/
/****************************************************************************
** INCLUDES
****************************************************************************/
#include <stdint.h>
//#define ENABLE_DEBUG
#include "debug.h"
//Class Header
#include "uniparser.h"
/****************************************************************************
** NAMESPACES
****************************************************************************/
namespace Orangebot
{
/****************************************************************************
** GLOBAL VARIABILES
****************************************************************************/
/****************************************************************************
*****************************************************************************
** CONSTRUCTORS
*****************************************************************************
****************************************************************************/
/***************************************************************************/
//! @brief Empty Constructor
//! Uniparser | void
/***************************************************************************/
// @param
//! @return no return
//! @details
//! Empty constructor
/***************************************************************************/
Uniparser::Uniparser( void )
{
//----------------------------------------------------------------
// VARS
//----------------------------------------------------------------
//----------------------------------------------------------------
// INIT
//----------------------------------------------------------------
//----------------------------------------------------------------
// BODY
//----------------------------------------------------------------
//! @details algorithm:
//Initialize structure to safe values
this -> init();
//Pass a terminator to the parser to have it initialize itself
this -> exe( '\0' );
//----------------------------------------------------------------
// RETURN
//----------------------------------------------------------------
return;
} //end constructor:
/****************************************************************************
*****************************************************************************
** DESTRUCTORS
*****************************************************************************
****************************************************************************/
/***************************************************************************/
//! @brief Empty Destructor
//! Uniparser | void
/***************************************************************************/
// @param
//! @return no return
//! @details
//! Empty destructor
/***************************************************************************/
Uniparser::~Uniparser( void )
{
//----------------------------------------------------------------
// VARS
//----------------------------------------------------------------
//----------------------------------------------------------------
// INIT
//----------------------------------------------------------------
//----------------------------------------------------------------
// BODY
//----------------------------------------------------------------
//! @details algorithm:
//----------------------------------------------------------------
// RETURN
//----------------------------------------------------------------
return;
} //end destructor:
/****************************************************************************
*****************************************************************************
** OPERATORS
*****************************************************************************
****************************************************************************/
/****************************************************************************
*****************************************************************************
** SETTERS
*****************************************************************************
****************************************************************************/
/****************************************************************************
*****************************************************************************
** GETTERS
*****************************************************************************
****************************************************************************/
/***************************************************************************/
//! @brief Public Getter
//! get_syntax_error | void
/***************************************************************************/
// @param
//! @return no return
//! @details
//! Decode syntax error of the parser in string form. nullptr means no syntax error detected
/***************************************************************************/
const char *Uniparser::get_syntax_error( void )
{
DENTER();
//----------------------------------------------------------------
// VARS
//----------------------------------------------------------------
const char *ret_str;
//----------------------------------------------------------------
// INIT
//----------------------------------------------------------------
//----------------------------------------------------------------
// BODY
//----------------------------------------------------------------
//Decode error code
ret_str = this -> decode_syntax_err( this -> g_cmd_err );
//----------------------------------------------------------------
// RETURN
//----------------------------------------------------------------
DRETURN_ARG(">%s<\n",ret_str);
return ret_str;
} //end method: get_syntax_error | void
/****************************************************************************
*****************************************************************************
** TESTERS
*****************************************************************************
****************************************************************************/
/****************************************************************************
*****************************************************************************
** PUBLIC METHODS
*****************************************************************************
****************************************************************************/
/***************************************************************************/
//! @brief Public Method
//! add_command | const char *, void *
/***************************************************************************/
//! @param cmd | const char * Text that will trigger the command
//! @param handler | void * Pointer to callback function for this command
//! @return bool | false: OK | true: fail
//! @details
//! Add a string and a function pointer to the parser
//! @todo check that command is valid
/***************************************************************************/
bool Uniparser::add_cmd( const char *cmd, void *handler )
{
DENTER_ARG("cmd: %p >%s<\n", (void *)cmd, cmd );
//----------------------------------------------------------------
// VARS
//----------------------------------------------------------------
//index
uint8_t t;
//----------------------------------------------------------------
// INIT
//----------------------------------------------------------------
//if: input is invalid
if ((cmd == nullptr) || (handler == nullptr))
{
this -> g_err = ERR_INVALID_CMD;
DRETURN_ARG("ERR%d: ERR_INVALID_CMD\n", this -> g_err);
return true; //fail
}
//If: num command is invalid
if ((UNIPARSER_PENDANTIC_CHECKS) && ((this -> g_num_cmd < 0) || (this->g_num_cmd >= UNIPARSER_MAX_CMD)) )
{
this -> g_err = ERR_GENERIC;
DRETURN_ARG("ERR%d: ERR_GENERIC in line: %d\n", this -> g_err, __LINE__ );
return true; //fail
}
//if: maximum number of command has been reached
if (this -> g_num_cmd >= (UNIPARSER_MAX_CMD-1))
{
this -> g_err = ERR_ADD_MAX_CMD;
DRETURN_ARG("ERR%d: ERR_ADD_MAX_CMD in line: %d\n", this -> g_err, __LINE__ );
return true; //fail
}
// check the validity of the string
this -> g_cmd_err = this -> chk_cmd((const uint8_t *)cmd);
//If: command had a syntax error
if (this -> g_cmd_err != Cmd_syntax_error::SYNTAX_OK)
{
DRETURN_ARG("command didnt get past argument descriptor check\n");
return true;
}
//----------------------------------------------------------------
// BODY
//----------------------------------------------------------------
//Fetch currently used command
t = this -> g_num_cmd;
//Link command handler and command text
this -> g_cmd_txt[t] = (uint8_t *)cmd;
this -> g_cmd_handler[t] = handler;
DPRINT("Command >%s< with handler >%p< has been added with index: %d\n", cmd, (void *)handler, t);
//A command has been added
this -> g_num_cmd = t +1;
DPRINT("Total number of commands: %d\n", this -> g_num_cmd);
//----------------------------------------------------------------
// RETURN
//----------------------------------------------------------------
DRETURN();
return false;
} //end method: add_command | const char *, void *
/***************************************************************************/
//! @brief Public Method
//! add_cmd | const char *, void *, Cmd_syntax_error &
/***************************************************************************/
//! @param cmd | string containing the command
//! @param handler | pointer to callback function for the command
//! @param err_code | return the error code of the command if any
//! @return Cmd_syntax_error::SYNTAX_OK if all is good. other codes specify error. decode_err will return a string with error descritpion.
//! @details
//! Add command to dictionary
/***************************************************************************/
bool Uniparser::add_cmd( const char *cmd, void *handler, Cmd_syntax_error &err_code )
{
DENTER_ARG("cmd: %p >%s<\n", (void *)cmd, cmd );
//----------------------------------------------------------------
// VARS
//----------------------------------------------------------------
//index
uint8_t t;
//----------------------------------------------------------------
// INIT
//----------------------------------------------------------------
//if: input is invalid
if ((cmd == nullptr) || (handler == nullptr))
{
this -> g_err = ERR_INVALID_CMD;
DRETURN_ARG("ERR%d: ERR_INVALID_CMD\n", this -> g_err);
return true; //fail
}
//If: num command is invalid
if ((UNIPARSER_PENDANTIC_CHECKS) && ((this->g_num_cmd < 0) || (this->g_num_cmd >= UNIPARSER_MAX_CMD)) )
{
this -> g_err = ERR_GENERIC;
DRETURN_ARG("ERR%d: ERR_GENERIC in line: %d\n", this -> g_err, __LINE__ );
return true; //fail
}
//if: maximum number of command has been reached
if (this -> g_num_cmd >= (UNIPARSER_MAX_CMD-1))
{
this -> g_err = ERR_ADD_MAX_CMD;
DRETURN_ARG("ERR%d: ERR_ADD_MAX_CMD in line: %d\n", this -> g_err, __LINE__ );
return true; //fail
}
// check the validity of the string
err_code = this -> chk_cmd((const uint8_t *)cmd);
//If: command had a syntax error
if (err_code != Cmd_syntax_error::SYNTAX_OK)
{
DRETURN_ARG("command didnt get past argument descriptor check\n");
return true;
}
//----------------------------------------------------------------
// BODY
//----------------------------------------------------------------
//Fetch currently used command
t = this -> g_num_cmd;
//Link command handler and command text
this -> g_cmd_txt[t] = (uint8_t *)cmd;
this -> g_cmd_handler[t] = handler;
DPRINT("Command >%s< with handler >%p< has been added with index: %d\n", cmd, (void *)handler, t);
//A command has been added
this -> g_num_cmd = t +1;
DPRINT("Total number of commands: %d\n", this -> g_num_cmd);
//----------------------------------------------------------------
// RETURN
//----------------------------------------------------------------
DRETURN();
return false;
} //end method: add_cmd | const char *, void *, Cmd_syntax_error &
/***************************************************************************/
//! @brief Public Method
//! exe | char
/***************************************************************************/
// @param
//! @return no return
//! @details
//! Intricate FSM. The objective is to check the incoming character against
//! charaters inside the various commands and decode a command when a \0 is detected
/***************************************************************************/
bool Uniparser::exe( uint8_t data )
{
if ((data < '0') || (data > 'z'))
{
DENTER_ARG("exe: >0x%x<\n", data );
}
else
{
DENTER_ARG("exe: >0x%x< >%c<\n", data, data );
}
//----------------------------------------------------------------
// VARS
//----------------------------------------------------------------
//when true, reset the FMS
bool f_rst_fsm = false;
//Index of the handler to be executer
int8_t exe_index = -1;
//----------------------------------------------------------------
// INIT
//----------------------------------------------------------------
//----------------------------------------------------------------
// BODY
//----------------------------------------------------------------
//! @details algorithm:
//!
//----------------------------------------------------------------
// TERMINATOR
//----------------------------------------------------------------
//! @details when terminator is detected, close the command.
//! I either have a full match, or the command is invalid and I have to reset the FSM
//If: input terminator from user
if (data == '\0')
{
DPRINT("Terminator detected | Number of partial matches: %d\n", this -> g_num_match);
//counter
uint8_t t;
//If: i was decoding an argument
if (this -> g_status == Orangebot::Parser_status::PARSER_ARG)
{
//I'm done decoding
DPRINT("Terminator after ARG\n");
//Close current argument and update the argument detector FSM.
bool f_ret = this -> close_arg();
//if could not close argument
if (f_ret == true)
{
//I can recover from this.
//no matches and reset the FSM.
this -> g_num_match = 0;
}
}
//Index inside the command
uint8_t cmd_index;
//if: I have at least one parser_tmp.id_index entry
if (this -> g_num_match > 0)
{
DPRINT("Scanning partially matched commands for terminators\n");
//For: scan all commands
for (t = 0;t<this -> g_num_cmd;t++)
{
//Fetch command index
cmd_index = this -> g_cmd_index[t];
//If: this command is a partial match
if (cmd_index > 0)
{
//If: i was decoding an argument
if (this -> g_status == Parser_status::PARSER_ARG)
{
//If: index is not pointing to an argument descriptor inside the command
if ((UNIPARSER_PENDANTIC_CHECKS) && (this -> g_cmd_txt[t][cmd_index] != '%'))
{
DPRINT("ERR: This command should have an argument descriptor in this position. | cmd: %d | cmd_index: %d | actual content: >0x%x<\n", t, cmd_index, this -> g_cmd_txt[t][cmd_index]);
this -> g_err = Err_codes::ERR_GENERIC;
DRETURN_ARG("ERR%d: ERR_GENERIC in line: %d\n", this -> g_err, __LINE__ );
return true; //fail
}
//The argument has been closed. I need to skip the argument descriptor "%?"
cmd_index += 2;
//With argument closed I'm now doing an ID matching for terminator. Can be skipped since it's the last char
this -> g_status = Parser_status::PARSER_ID;
//I should write back the index. Can be skipped since it's the last char
this -> g_cmd_index[t] = cmd_index;
}
//Check match against the character after the one already matched.
//If: the next command data would be the terminator '\0'
if ( this -> g_cmd_txt[t][ cmd_index ] == '\0')
{
//If there is a pending execution already
if ((UNIPARSER_PENDANTIC_CHECKS) && (exe_index != -1))
{
//this should never happen
this -> g_err = Err_codes::ERR_GENERIC;
DRETURN_ARG("ERR%d: ERR_GENERIC in line: %d\n", this -> g_err, __LINE__ );
return true; //fail
}
//Issue execution of the callback function linked
exe_index = t;
DPRINT("Valid command ID%d decoded\n", t);
//I can stop the search
t = this -> g_num_cmd;
}
else
{
DPRINT("no match in %x cmd %x | " ,data , this -> g_cmd_txt[t][ cmd_index ] );
}
} //End If: this command is a partial match
} //End For: scan all commands
} //end if: I have at least one parser_tmp.id_index entry
//If I have just one match. g_num_match now holds the index of the match
else if (this -> g_num_match < 0)
{
//decode command index
t = -this -> g_num_match;
DPRINT("just one partial match: %d\n", t);
//Fetch command index
cmd_index = this -> g_cmd_index[t];
//If: i was decoding an argument
if (this -> g_status == Parser_status::PARSER_ARG)
{
//If: index is not pointing to an argument descriptor inside the command
if ((UNIPARSER_PENDANTIC_CHECKS) && (this -> g_cmd_txt[t][cmd_index] != '%'))
{
DPRINT("ERR: This command should have an argument descriptor in this position. | cmd: %d | cmd_index: %d | actual content: >0x%x<\n", t, cmd_index, this -> g_cmd_txt[t][cmd_index]);
this -> g_err = Err_codes::ERR_GENERIC;
DRETURN_ARG("ERR%d: ERR_GENERIC in line: %d\n", this -> g_err, __LINE__ );
return true; //fail
}
//The argument has been closed. I need to skip the argument descriptor "%?"
cmd_index += 2;
//With argument closed I'm now doing an ID matching for terminator. Can be skipped since it's the last char
//this -> g_status == Parser_status::PARSER_ID;
}
//Check match against the character after the one already matched.
//If: the next command data would be the terminator '\0'
if ( this -> g_cmd_txt[t][ cmd_index ] == '\0')
{
//If there is a pending execution already
if ((UNIPARSER_PENDANTIC_CHECKS) && (exe_index != -1))
{
//this should never happen
this -> g_err = Err_codes::ERR_GENERIC;
DRETURN_ARG("ERR%d: ERR_GENERIC in line: %d\n", this -> g_err, __LINE__ );
return true; //fail
}
//Issue execution of the callback function linked
exe_index = t;
DPRINT("Valid command ID%d decoded\n", t);
//I can stop the search
t = this -> g_num_cmd;
}
//if: I'm given a terminator but dictionary does not contain a terminator
else
{
//This happen if user gives a command that lack one char
DPRINT("no match. given: >0x%x< expected: >0x%x<\n" ,data , this -> g_cmd_txt[t][ cmd_index ] );
//Issue a FSM reset
f_rst_fsm = true;
}
}
//if: no partial matches
else
{
//do nothing
}
//Issue a FSM reset
f_rst_fsm = true;
} //End If: input terminator from user
//--------------------------------------------------------------------------
// PARSER_IDLE
//--------------------------------------------------------------------------
// Only letters can be used as first character in a command
// This section matches the first character in each command
// This section takes care of initializing g_cmd_index[] to valid partial match values
//If: PARSER_IDLE
else if (this -> g_status == Parser_status::PARSER_IDLE)
{
DPRINT("PARSER_IDLE\n");
//If: letter First character in a command can only be a letter
if (IS_LETTER( data ))
{
//counter
uint8_t t;
//for each command
for (t = 0; t < this -> g_num_cmd;t++)
{
//if: dictionary is unlinked
if ((UNIPARSER_PENDANTIC_CHECKS) && (this -> g_cmd_txt[t] == nullptr))
{
this -> g_err = Err_codes::ERR_GENERIC;
DRETURN_ARG("ERR%d: ERR_GENERIC in line: %d\n", this -> g_err, __LINE__ );
return true; //fail
}
//If: partial match
if (this -> g_cmd_txt[t][0] == data)
{
//A partial match has been found
this -> g_num_match++;
//Match has been found up to first character. Point to the next unmatched char
this -> g_cmd_index[t] = 1;
//Next, I'm matching ID entries
this -> g_status = Parser_status::PARSER_ID;
//TIP: I can't speculatively detect % here because two commands may have the same first section and diverge later.
DPRINT("Match command %d, total partial matches: %d\n", t, this -> g_num_match);
}
//if: no match
else
{
//special code for no match found
this -> g_cmd_index[t] = 0;
}
} //end for: each command
} //End If: letter
//Non letter can never be a first character
else
{
//Issue a FSM reset
f_rst_fsm = true;
}
} //End If: PARSER_IDLE
//--------------------------------------------------------------------------
// ID matching
//--------------------------------------------------------------------------
//if: I'm ID matching
else if (this -> g_status == Parser_status::PARSER_ID)
{
DPRINT("PARSER_ID ");
//! @todo only go ARG if a command has an arg descriptor if there are number or sign. allow number or sign to be used as command ID
//if: I'm being fed an argument
if (IS_NUMBER( data ) || IS_SIGN( data ))
{
DPRINT_NOTAB("- ARG | num_match %d\n", this -> g_num_match);
//if: I have at least one partial match
if (this -> g_num_match > 0)
{
//for each dictionary command
for (uint8_t t = 0;t < this -> g_num_cmd;t++)
{
//if: the command is a partial match
if (this -> g_cmd_index[t] > 0)
{
//Search in the dictionary for a % entry. An argument descriptor
if (this -> g_cmd_txt[t][ this -> g_cmd_index[t] ] == '%')
{
//! This is the fi
//Do not increment index but go in ARG parsing mode
this -> g_status = Parser_status::PARSER_ARG;
//Do not advance index until argument decoding is complete
DPRINT("ARG begins | command: %d\n", t);
//Add an argument using current partial match as template
bool f_ret = this -> add_arg( t );
//if: adding argument failed
if ((UNIPARSER_PENDANTIC_CHECKS) && (f_ret == true))
{
this -> g_err = Err_codes::ERR_GENERIC;
DPRINT("ERR%d: ERR_GENERIC in line: %d\n", this -> g_err, __LINE__ );
//I can recover from this by resetting the FSM
f_rst_fsm = true;
}
//initialize agrument
this -> accumulate_arg( data );
//argument detection make the detection unique. Remove all other partial matches
this -> g_num_match = -t;
//Single match has been found. Break the cycle
t = UNIPARSER_MAX_CMD;
}
//Else: this dictionary entry does not contain an argument descriptor
else
{
//Prune away the partial match
this -> g_cmd_index[t] = 0;
DPRINT("Prune away partial match: %d | ", t);
//if: I still have partial matches after pruning
if (this -> g_num_match >= 2)
{
//One fewer partial match
this -> g_num_match--;
DPRINT_NOTAB(" num_match: %d\n", this -> g_num_match );
}
//if: all partial matches have been pruned away
else
{
//No more commands. Reset the machine
f_rst_fsm = true;
//No more matches. Can breack cycle early
t = UNIPARSER_MAX_CMD;
DPRINT_NOTAB("Last partial match has been pruned away... RESET\n");
//! @todo command refeed function. Safe and refeed last char to detect other partial commands
}
}
}
else
{
//do nothing
}
} //end for each dictionary command
} //end if: I have at least one partial match
//If I only have one match
else if (this -> g_num_match < 0)
{
//decode command index
uint8_t t = -this -> g_num_match;
//Search in the dictionary for a % entry. An argument descriptor
if (this -> g_cmd_txt[t][ this -> g_cmd_index[t] ] == '%')
{
//! This is the first char of an argument
//Do not increment index but go in ARG parsing mode
this -> g_status = Parser_status::PARSER_ARG;
//Do not advance index until argument decoding is complete
DPRINT("ARG begins | command: %d\n", t);
//Add an argument using current partial match as template
bool f_ret = this -> add_arg( t );
//if: adding argument failed
if ((UNIPARSER_PENDANTIC_CHECKS) && (f_ret == true))
{
this -> g_err = Err_codes::ERR_GENERIC;
DPRINT("ERR%d: ERR_GENERIC in line: %d\n", this -> g_err, __LINE__ );
//I can recover from this by resetting the FSM
f_rst_fsm = true;
}
//! accumulate argument character inside argument.
f_ret = this -> accumulate_arg( data );
//if: adding argument failed
if ((UNIPARSER_PENDANTIC_CHECKS) && (f_ret == true))
{
this -> g_err = Err_codes::ERR_GENERIC;
DPRINT("ERR%d: ERR_GENERIC in line: %d\n", this -> g_err, __LINE__ );
//I can recover from this by resetting the FSM
f_rst_fsm = true;
}
}
//Else: this dictionary entry does not contain an argument descriptor
else
{
DPRINT("Command does not have argument in this position. Prune away partial match: %d\n", t);
//Issue a FSM reset
f_rst_fsm = true;
}
} //If I only have one match
//if: no matches
else
{
//I shouldn't have been in ID to begin with, but I can recover from this error
DPRINT("ERR: FSM was in ID matching but no partial matches were detected.\n");
//Issue a FSM reset
f_rst_fsm = true;
}
} //end if: I'm being fed an argument
//if: I'm matching a non argument non terminator
else
{
DPRINT_NOTAB("- ID\n");
//if: I have at least one partial match
if (this -> g_num_match > 0)
{
//If I have just one match, I can upgrade num_match to skip for next time
bool f_match = (this -> g_num_match == 1);
//for each dictionary command
for (uint8_t t = 0;t < this -> g_num_cmd;t++)
{
//if: the command is a partial match
if (this -> g_cmd_index[t] > 0)
{
//check that the dictionary holds the same value as data
if (this -> g_cmd_txt[t][ this -> g_cmd_index[t] ] == data)
{
//Advance to the next dictionary entry for this command
this -> g_cmd_index[t]++;
//if: This is the sole partial match surviving
if (f_match == true)
{
//Save the index inside num match to skip the for next time
this -> g_num_match = -t;
//Single match has been found. Break the cycle
t = UNIPARSER_MAX_CMD;
DPRINT("Only one partial match remains. Num_match: %d\n", this -> g_num_match);
}
}
//Else: this dictionary entry does not contain an argument descriptor
else
{
//Prune away the partial match
this -> g_cmd_index[t] = 0;
DPRINT("Prune away partial match: %d | ", t);
//if: I still have partial matches after pruning
if (this -> g_num_match >= 2)
{
//One fewer partial match
this -> g_num_match--;
DPRINT_NOTAB(" num_match: %d\n", this -> g_num_match );
}
//if: all partial matches have been pruned away
else
{
//No more commands. Reset the machine
f_rst_fsm = true;
//No more matches. Can breack cycle early
t = UNIPARSER_MAX_CMD;
DPRINT_NOTAB("Last partial match has been pruned away... RESET\n");
//! @todo command refeed function. Safe and refeed last char to detect other partial commands
}
}
}
//if: the command was pruned away long ago
else
{
//do nothing
}
} //end for each dictionary command
} //end if: I have at least one partial match
//If I only have one match
else if (this -> g_num_match < 0)
{
//decode command index
uint8_t t = -this -> g_num_match;
//check that the dictionary holds the same value as data
if (this -> g_cmd_txt[t][ this -> g_cmd_index[t] ] == data)
{
//Match! Scan next entry
this -> g_cmd_index[t]++;
}
//no match
else
{
//Last partial match has been pruned away. Issue a FSM reset
f_rst_fsm = true;
DPRINT("Last match was pruned away. Expected >0x%x< got >0x%x< instead\n", this -> g_cmd_txt[t][ this -> g_cmd_index[t] ], data);
}
} //If I only have one match
//if: no matches
else
{
//I shouldn't have been in ID to begin with, but I can recover from this error
DPRINT("ERR: FSM was in ID matching but no partial matches were detected.\n");
//Issue a FSM reset
f_rst_fsm = true;
}
} //end if: I'm matching a non argument non terminator
} //end if: I'm ID matching
//--------------------------------------------------------------------------
// ARG decoder
//--------------------------------------------------------------------------
//if: I'm decoding arguments
else if (this -> g_status == Parser_status::PARSER_ARG)
{
DPRINT("PARSER_ARG\n");
//If: I'm fed a number
if (IS_NUMBER(data))
{
//! accumulate argument character inside argument.
bool f_ret = this -> accumulate_arg( data );
//if: adding argument failed
if ((UNIPARSER_PENDANTIC_CHECKS) && (f_ret == true))
{
this -> g_err = Err_codes::ERR_GENERIC;
DPRINT("ERR%d: ERR_GENERIC in line: %d\n", this -> g_err, __LINE__ );
//I can recover from this by resetting the FSM
f_rst_fsm = true;
}
//Do not advance to next dictionary entry
} //If: I'm fed a number
//if: I'm fed a non number
else
{
DPRINT("Closing argument\n");
//! Exit argument mode
//Close current argument and update argument FSM
this -> close_arg();
//if: I have no matches or multiple matches. I should have just one match by this point.
if ((UNIPARSER_PENDANTIC_CHECKS) && (this -> g_num_match >= 0))
{
this -> g_err = Err_codes::ERR_GENERIC;
DPRINT("ERR%d: ERR_GENERIC in line: %d\n", this -> g_err, __LINE__ );
//I can recover from this by resetting the FSM
f_rst_fsm = true;
}
else
{
//After argument, I can only have one match.
uint8_t cmd_id = -this -> g_num_match;
//Update the parser index by skipping % and the argument descriptor
this -> g_cmd_index[ cmd_id ] += 2;
//check that the dictionary holds the same value as data
if (this -> g_cmd_txt[ cmd_id ][ this -> g_cmd_index[ cmd_id ] ] == data)
{
//Advance to the next dictionary entry for this command
this -> g_cmd_index[ cmd_id ]++;
}
//No match
else
{
DPRINT("Pruning away last match\n");
//I can recover from this by resetting the FSM
f_rst_fsm = true;
}
this -> g_status = Parser_status::PARSER_ID;
}
} //if: I'm fed a non number
} //if: I'm decoding arguments
//----------------------------------------------------------------
// FSM RESET
//----------------------------------------------------------------
//! @detail
//! FSM reset. Clear up the machine for the next clean execution
//If: a reset was issued
if (f_rst_fsm == true)
{
DPRINT("FSM RESET\n");
//Clear reset flag
f_rst_fsm = false;
//Status becomes IDLE
this -> g_status = Orangebot::Parser_status::PARSER_IDLE;
//I have no partial matches anymore
this -> g_num_match = 0;
//If I don't have a pending execution
if (exe_index == -1)
{
//Reset the argument decoder and prepare for a new command
this -> init_arg_decoder();
}
} //If: a reset was issued
//----------------------------------------------------------------
// HANDLER EXECUTION
//----------------------------------------------------------------
//! @detail
//! FSM reset. Clear up the machine for the next clean execution
//If: an execution event has been launched
if (exe_index > -1)
{
//if execution index is out of range.
if ((UNIPARSER_PENDANTIC_CHECKS) && (exe_index == this -> g_num_cmd))
{
this -> g_err = Err_codes::ERR_GENERIC;
DRETURN_ARG("ERR%d: ERR_GENERIC in line: %d\n", this -> g_err, __LINE__ );
return true; //fail
}
DPRINT("Executing handler of command %d | num arguments: %d\n", exe_index, this -> g_arg_fsm_status.num_arg);
//Execute handler of given function. Automatically deduce arguments from argument vector
this -> exe_handler( exe_index );
//Reset the argument decoder and prepare for a new command
this -> init_arg_decoder();
} //If: a reset was issued
//----------------------------------------------------------------
// RETURN
//----------------------------------------------------------------
//Trace Return from main
DRETURN();
return false; //OK
} //end method:
/****************************************************************************
*****************************************************************************
** PUBLIC STATIC METHODS
*****************************************************************************
****************************************************************************/
/****************************************************************************
*****************************************************************************
** PRIVATE METHODS
*****************************************************************************
****************************************************************************/
/***************************************************************************/
//! @brief Private Method
//! init | void
/***************************************************************************/
//! @return no return
//! @details
//! initialize structure
/***************************************************************************/
inline void Uniparser::init( void )
{
DENTER();
//----------------------------------------------------------------
// VARS
//----------------------------------------------------------------
//counter
uint8_t t;
//----------------------------------------------------------------
// INIT
//----------------------------------------------------------------