Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. #ifdef TEST_RAGEL_PARSER
  2.  
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6.  
  7. #define WMQE_INVALID_PROTOCOL_VERSION -1
  8.  
  9. #endif
  10.  
  11. %%{
  12. machine braces_parser;
  13. write data;
  14. }%%
  15.  
  16. #ifdef TEST_RAGEL_PARSER
  17. int parse_csv_string(const char *data, size_t len )
  18. #else
  19. int parse_csv_string(void *context, on_string_cb_t cb, const char *data, size_t len )
  20. #endif
  21. {
  22.  
  23. int rc;
  24. const char *p = data, *pe = data + len;
  25. const char *eof = pe;
  26. const char *start = data;
  27. #ifdef TEST_RAGEL_PARSER
  28. #else
  29. #endif
  30.  
  31.  
  32. int cs;
  33. //printf("parse_address: %s %d %d\n", p, (int)(pe-p), (int)(*eof));
  34.  
  35. //int rc = 0;
  36.  
  37. %%{
  38.  
  39. action on_start {
  40. #ifdef TEST_RAGEL_PARSER
  41. printf("on_start: %ld\n", fpc-data);
  42. #else
  43. #endif
  44. start = fpc;
  45. }
  46.  
  47.  
  48. action on_end {
  49. #ifdef TEST_RAGEL_PARSER
  50. printf(">>on_end: %ld \"%.*s\"\n", fpc-data, (int)(fpc-start), start);
  51. #else
  52. rc = cb(context, start, fpc-start);
  53. WMQ_CHECK_ERROR_AND_RETURN_RESULT(rc, "on string cb");
  54. #endif
  55. //start = fpc;
  56. }
  57.  
  58. action on_comment_end {
  59. #ifdef TEST_RAGEL_PARSER
  60. printf(">>on_comment_end: %ld \"%.*s\"\n", fpc-data, (int)(fpc-start), start);
  61. #else
  62. rc = cb(context, start, fpc-start);
  63. WMQ_CHECK_ERROR_AND_RETURN_RESULT(rc, "on string cb");
  64. #endif
  65. //start = fpc;
  66. }
  67.  
  68. action on_open_brace {
  69. #ifdef TEST_RAGEL_PARSER
  70. printf(">>on_open_brace: \"%.1s\"\n", fpc);
  71. #else
  72. rc = cb(context, start, fpc-start);
  73. WMQ_CHECK_ERROR_AND_RETURN_RESULT(rc, "on string cb");
  74. #endif
  75. //start = fpc;
  76. }
  77.  
  78. action on_close_brace {
  79. #ifdef TEST_RAGEL_PARSER
  80. printf(">>on_close_brace: \"%.1s\"\n", fpc);
  81. #else
  82. rc = cb(context, start, fpc-start);
  83. WMQ_CHECK_ERROR_AND_RETURN_RESULT(rc, "on string cb");
  84. #endif
  85. //start = fpc;
  86. }
  87.  
  88. action on_semicolon {
  89. #ifdef TEST_RAGEL_PARSER
  90. printf(">>on_semicolon: \"%.1s\"\n", fpc);
  91. #else
  92. rc = cb(context, start, fpc-start);
  93. WMQ_CHECK_ERROR_AND_RETURN_RESULT(rc, "on string cb");
  94. #endif
  95. //start = fpc;
  96. }
  97.  
  98.  
  99. action error {
  100. WMQ_LOG(LL_ERROR, "error at %d %s\n", (int)(fpc-start), fpc);
  101. return WMQE_INVALID_PROTOCOL_VERSION;
  102. }
  103.  
  104. action on_eof {
  105. #ifdef TEST_RAGEL_PARSER
  106. printf("eof at %d\n", (int)(fpc-start));
  107. #endif
  108. }
  109.  
  110.  
  111. jws = space** ;
  112. #character = alnum + '_' + '.' + '#' + ??WTF x2D? ;
  113.  
  114. character = any - space - '"' - '{' - '}' - ';' ;
  115.  
  116. string_term = (character+ >on_start %on_end);
  117. quoted_term = '"' ((any - '"')** >on_start %on_end) '"' ;
  118. open_brace = '{' >on_open_brace;
  119. close_brace = '}' >on_close_brace;
  120. semicolon = ';' >on_semicolon;
  121. inline_comment = ('/*' ( any* - ( any* '*/' any* ) ) '*/') >on_start %on_comment_end ;
  122.  
  123. lexem = (string_term | quoted_term | open_brace | close_brace | semicolon | inline_comment) jws;
  124.  
  125. main := jws lexem** $err(error) $eof(on_eof);
  126.  
  127. # Initialize and execute.
  128. write init;
  129. write exec;
  130. }%%
  131.  
  132. return 0;
  133. };
  134.  
  135.  
  136. #ifdef TEST_RAGEL_PARSER
  137.  
  138.  
  139. #define BUFSIZE 1024
  140.  
  141. int main()
  142. {
  143. int rc;
  144. char buf[BUFSIZE];
  145. while ( fgets( buf, sizeof(buf), stdin ) != 0 ) {
  146. printf( "buf:%s", buf);
  147. rc = parse_csv_string(buf, strlen(buf));
  148. printf( "rc:%d\n\n", rc );
  149. }
  150. return 0;
  151. }
  152.  
  153. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement