Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef TEST_RAGEL_PARSER
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #define WMQE_INVALID_PROTOCOL_VERSION -1
- #endif
- %%{
- machine braces_parser;
- write data;
- }%%
- #ifdef TEST_RAGEL_PARSER
- int parse_csv_string(const char *data, size_t len )
- #else
- int parse_csv_string(void *context, on_string_cb_t cb, const char *data, size_t len )
- #endif
- {
- int rc;
- const char *p = data, *pe = data + len;
- const char *eof = pe;
- const char *start = data;
- #ifdef TEST_RAGEL_PARSER
- #else
- #endif
- int cs;
- //printf("parse_address: %s %d %d\n", p, (int)(pe-p), (int)(*eof));
- //int rc = 0;
- %%{
- action on_start {
- #ifdef TEST_RAGEL_PARSER
- printf("on_start: %ld\n", fpc-data);
- #else
- #endif
- start = fpc;
- }
- action on_end {
- #ifdef TEST_RAGEL_PARSER
- printf(">>on_end: %ld \"%.*s\"\n", fpc-data, (int)(fpc-start), start);
- #else
- rc = cb(context, start, fpc-start);
- WMQ_CHECK_ERROR_AND_RETURN_RESULT(rc, "on string cb");
- #endif
- //start = fpc;
- }
- action on_comment_end {
- #ifdef TEST_RAGEL_PARSER
- printf(">>on_comment_end: %ld \"%.*s\"\n", fpc-data, (int)(fpc-start), start);
- #else
- rc = cb(context, start, fpc-start);
- WMQ_CHECK_ERROR_AND_RETURN_RESULT(rc, "on string cb");
- #endif
- //start = fpc;
- }
- action on_open_brace {
- #ifdef TEST_RAGEL_PARSER
- printf(">>on_open_brace: \"%.1s\"\n", fpc);
- #else
- rc = cb(context, start, fpc-start);
- WMQ_CHECK_ERROR_AND_RETURN_RESULT(rc, "on string cb");
- #endif
- //start = fpc;
- }
- action on_close_brace {
- #ifdef TEST_RAGEL_PARSER
- printf(">>on_close_brace: \"%.1s\"\n", fpc);
- #else
- rc = cb(context, start, fpc-start);
- WMQ_CHECK_ERROR_AND_RETURN_RESULT(rc, "on string cb");
- #endif
- //start = fpc;
- }
- action on_semicolon {
- #ifdef TEST_RAGEL_PARSER
- printf(">>on_semicolon: \"%.1s\"\n", fpc);
- #else
- rc = cb(context, start, fpc-start);
- WMQ_CHECK_ERROR_AND_RETURN_RESULT(rc, "on string cb");
- #endif
- //start = fpc;
- }
- action error {
- WMQ_LOG(LL_ERROR, "error at %d %s\n", (int)(fpc-start), fpc);
- return WMQE_INVALID_PROTOCOL_VERSION;
- }
- action on_eof {
- #ifdef TEST_RAGEL_PARSER
- printf("eof at %d\n", (int)(fpc-start));
- #endif
- }
- jws = space** ;
- #character = alnum + '_' + '.' + '#' + ??WTF x2D? ;
- character = any - space - '"' - '{' - '}' - ';' ;
- string_term = (character+ >on_start %on_end);
- quoted_term = '"' ((any - '"')** >on_start %on_end) '"' ;
- open_brace = '{' >on_open_brace;
- close_brace = '}' >on_close_brace;
- semicolon = ';' >on_semicolon;
- inline_comment = ('/*' ( any* - ( any* '*/' any* ) ) '*/') >on_start %on_comment_end ;
- lexem = (string_term | quoted_term | open_brace | close_brace | semicolon | inline_comment) jws;
- main := jws lexem** $err(error) $eof(on_eof);
- # Initialize and execute.
- write init;
- write exec;
- }%%
- return 0;
- };
- #ifdef TEST_RAGEL_PARSER
- #define BUFSIZE 1024
- int main()
- {
- int rc;
- char buf[BUFSIZE];
- while ( fgets( buf, sizeof(buf), stdin ) != 0 ) {
- printf( "buf:%s", buf);
- rc = parse_csv_string(buf, strlen(buf));
- printf( "rc:%d\n\n", rc );
- }
- return 0;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement