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
|
#include "cmd_parse.h"
//#define CTS(X) {if (!dm_current_tok)token_s = p; printf("%s ",#X);if (!dm_current_tok) dm_current_tok = TOK_##X;}
//#define CTE() {token_e = p;}
#define TADD(T,S,E,SZ) {cmd_tok_t *t=cmd_tok_create(S,E,SZ,T);cmd_tok_add(tl,t);tl=tl->next;}
//#define TADD(T,S,E)
#define PR_TK_DBG(S) printf(S);fwrite(ts,1,te-ts,stdout);printf("\n");
//#define PR_TK_DBG(S) ;
char *new_string( const char *start, const char *end )
{
int str_s = end-start+1;
char *new_str=malloc( str_s+1 );
memcpy( new_str, start, str_s );
if ( new_str != NULL )
new_str[str_s]=0x0;
return new_str;
}
int print_token( char *s, char *e, int tok)
{
char *p = new_string( s, e );
printf("t=%d,p=%s\n",tok,p);
free( p );
return 0;
}
%%{
machine cmd;
hex = '0x' [a-fA-F0-9]+;
decimal = [0-9]+;
binary = '0b' [0-1]+;
float = [0-9]* '.' [0-9]+;
quoted_string = '"'
(
' '
|(any-['\\' '"' ' '])+
|'\\\\'
|'\\\"'
|'\\ '
)*
'"';
quote = '"' (any-'"')* '"';
#seperate = ';';
word = [a-zA-Z] ([a-zA-Z0-9])*;
sp = ([' ','\t','\n']);
main := |*
hex {PR_TK_DBG("hex = ");TADD(CMDT_HEX, ts,te,te-ts);};
decimal {PR_TK_DBG("dec = ");TADD(CMDT_INT, ts,te,te-ts);};
binary {PR_TK_DBG("bin = ");TADD(CMDT_BIN, ts,te,te-ts);};
quoted_string {PR_TK_DBG("qstr = ");TADD(CMDT_QSTR, ts,te,te-ts);};
word {PR_TK_DBG("word = ");TADD(CMDT_WORD, ts,te,te-ts);};
sp {PR_TK_DBG("sp = ");TADD(CMDT_SP, ts,te,te-ts);};
float {PR_TK_DBG("float= ");TADD(CMDT_FLOAT, ts,te,te-ts);};
*|;
write data;
}%%
int parse_cmd( cmd_tok_t *tl, const char *str, size_t str_size )
{
static uint8_t cs;
const int stacksize = 10;
int res=0, *stack=NULL, act=0,top=0;
stack = malloc( sizeof(stack)*stacksize );
char *p=(char *)str, *pe = (char *)str + str_size, *eof=pe;
char *ts, *te = 0;
/*save for cmd_begin state*/
//char *ts_cb=0, *te_cb=0;
printf("PE:%s %02x\n",pe,(unsigned char)*pe);
/*
variables used in state machine
*/
%%write init;
%%write exec;
if ( cs == cmd_error )
{
printf("CLIPARSE ERR state [%d] pos[%ld]:[%s]\n", res, p-str, p);
res = -1;
}
free( stack );
return res;
}
|