summaryrefslogtreecommitdiff
path: root/libcmd/cmd_parse.ragel
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2016-05-10 22:42:11 +0100
committerFreeArtMan <dos21h@gmail.com>2016-05-10 22:42:11 +0100
commit44dedb90f005bb0de9bddbd592a5c78f28bc67d1 (patch)
tree274b9cea44df3f96a965c564e26cdac42d3af94f /libcmd/cmd_parse.ragel
parentbb426577c85f6d97cc28ab677e98e2b1f73eadac (diff)
downloadihe-44dedb90f005bb0de9bddbd592a5c78f28bc67d1.tar.gz
ihe-44dedb90f005bb0de9bddbd592a5c78f28bc67d1.zip
Replace cmd first part
Diffstat (limited to 'libcmd/cmd_parse.ragel')
-rw-r--r--libcmd/cmd_parse.ragel86
1 files changed, 86 insertions, 0 deletions
diff --git a/libcmd/cmd_parse.ragel b/libcmd/cmd_parse.ragel
new file mode 100644
index 0000000..85aa7d8
--- /dev/null
+++ b/libcmd/cmd_parse.ragel
@@ -0,0 +1,86 @@
+#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]+;
+ string = ('"' ([a-zA-Z0-9' '])+ '"');
+ 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);};
+ sp {};
+ string {PR_TK_DBG("str = ");TADD(CMDT_STR, 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);};
+ seperate {PR_TK_DBG("sep = ");TADD(CMDT_SP, ts,te,te-ts);};
+ word {PR_TK_DBG("wrd = ");TADD(CMDT_WORD,ts,te,te-ts);};
+ *|;
+
+ #main := lang;
+
+ write data;
+}%%
+
+int parse_cmd( cmd_tok_t *tl, const char *str )
+{
+ static uint8_t cs;
+ const int stacksize = 10;
+ int res=0, *stack=NULL, act=0;
+ stack = malloc( sizeof(stack)*stacksize );
+ char *p=(char *)str, *pe = (char *)str + strlen( str ), *eof=NULL;
+ char *ts, *te = 0;
+
+ /*
+ variables used in state machine
+ */
+
+ %%write init;
+ %%write exec;
+
+ if ( cs == cmd_error )
+ {
+ printf("CLIPARSE ERR state [%d] pos[%d]:[%s]\n", res, p-str, p);
+ res = -1;
+ }
+
+ free( stack );
+
+ return res;
+}
+
+
+