summaryrefslogtreecommitdiff
path: root/dm_parser.ragel
diff options
context:
space:
mode:
authorFreeArtMan <=>2015-12-17 20:59:18 +0000
committerFreeArtMan <=>2015-12-17 20:59:18 +0000
commita61ad1cef9ef77f9afd67230df5470b6b55525be (patch)
treed8bae5fc932865e769e4965ce0817b6d107badf2 /dm_parser.ragel
downloaddm-a61ad1cef9ef77f9afd67230df5470b6b55525be.tar.gz
dm-a61ad1cef9ef77f9afd67230df5470b6b55525be.zip
Initial commit
Diffstat (limited to 'dm_parser.ragel')
-rw-r--r--dm_parser.ragel95
1 files changed, 95 insertions, 0 deletions
diff --git a/dm_parser.ragel b/dm_parser.ragel
new file mode 100644
index 0000000..80c873b
--- /dev/null
+++ b/dm_parser.ragel
@@ -0,0 +1,95 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "dm.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) {tl_add_tok(tl,T,S,E);}
+
+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 dm;
+
+
+ hex = '0x' [a-fA-F0-9]+;
+ int = [0-9]+;
+ newline = '\n' >{CTS(NL)} @{CTE()};
+ if = 'if';
+ lbracket = '(';
+ rbracket = ')';
+ and = '&';
+ range = '-';
+ string = ('"' ([a-zA-Z0-9' '])+ '"');
+
+ sp = (' ' | '\t' | '\n');
+
+ main := |*
+ hex {/*printf("hex = ");fwrite(ts,1,te-ts,stdout);printf("\n");*/TADD(TOK_HEX,ts,te);};
+ sp {};
+ string {/*printf("str = ");fwrite(ts,1,te-ts,stdout);printf("\n");*/TADD(TOK_STR,ts,te);};
+ int {/*printf("int = ");fwrite(ts,1,te-ts,stdout);printf("\n");*/TADD(TOK_INT,ts,te);};
+ if {/*printf("if = ");fwrite(ts,1,te-ts,stdout);printf("\n");*/TADD(TOK_IF,ts,te);};
+ lbracket {/*printf("lbr = ");fwrite(ts,1,te-ts,stdout);printf("\n");*/TADD(TOK_LBR,ts,te);};
+ rbracket {/*printf("rbr = ");fwrite(ts,1,te-ts,stdout);printf("\n");*/TADD(TOK_RBR,ts,te);};
+ and {/*printf("and = ");fwrite(ts,1,te-ts,stdout);printf("\n");*/TADD(TOK_EAND,ts,te);};
+ range {/*printf("range = ");fwrite(ts,1,te-ts,stdout);printf("\n");*/TADD(TOK_RANGE,ts,te);};
+ *|;
+
+ write data;
+}%%
+
+int parse_dm( token_list *tl, const char *str )
+{
+ static uint8_t cs;
+ const int stacksize = 10;
+ int res=0, *top=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
+ */
+ char *token_s=NULL, *token_e=NULL;
+ char *value_s=NULL, *value_e=NULL;
+ int token_type=0;
+ int dm_current_tok = TOK_NONE;
+
+ %%write init;
+ %%write exec;
+
+ if ( cs == dm_error )
+ {
+ printf("ERR state [%d] pos[%d]:[%s]\n", res, p-str, p);
+ res = -1;
+ }
+
+ return res;
+}
+
+
+