wast.peg 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # wasm assembly language lexer+parser to use with packcc parser generator
  2. %prefix "wast"
  3. %source{
  4. #include <stdio.h>
  5. /* set to 1 to get parse debug info */
  6. static int debug = 0;
  7. static int linenr = 1;
  8. static const char *dbg_str[] = { "Evaluating rule", "Matched rule", "Abandoning rule" };
  9. #define PCC_DEBUG(event, rule, level, pos, buffer, length) \
  10. if (debug) fprintf(stdout, "%*s%s %s @%d [%.*s]\n", (int)(level * 2), "", dbg_str[event], rule, (int)pos, (int)length, buffer); fflush(stdout)
  11. /* NOTE: To guarantee the output order, stderr, which can lead a race condition with stdout, is not used. */
  12. }
  13. # start of input
  14. file <- lines* !.
  15. lines <- line
  16. line <- moduledef endofline*
  17. # / (!endofline .)* endofline { printf("skipped line: %s",$0); }
  18. moduledef <- '(' _ 'module' _{ printf("(module\n"); } moduledef2 _
  19. moduledef2 <- moduledata* _ ')' _ { printf(")\n"); }
  20. string <- '"' < char* > '"' { printf (" '%s'\n",$0); }
  21. char <-
  22. '\\' "\""
  23. / '\\' '\\'
  24. / '\\' 'b'
  25. / '\\' 'f'
  26. / '\\' 'n'
  27. / '\\' 'r'
  28. / '\\' 't'
  29. / (!"\"" .)
  30. symsp <- ( space / ')' / endofline)
  31. symbol <- (!(symsp) .)* { printf (" '%s'\n",$0); }
  32. moduledata <- _ expression* _
  33. expression <- _ (list / atom) _
  34. atom <- (string / symbol) _
  35. list <- ( liststart expression* listend ) _
  36. liststart <- '('
  37. listend <- ')'
  38. _ <- (space / endofline / comment)*
  39. comment <- ';;' (!endofline .)*
  40. space <- (' ' / '\t')
  41. endofline <- ( '\r\n' / '\n' / '\r' / '\n\r' ) { linenr++; }
  42. %%
  43. int main() {
  44. wast_context_t *ctx = wast_create(NULL);
  45. while (wast_parse(ctx, NULL)){;}
  46. wast_destroy(ctx);
  47. printf ("parsed %d lines\n", linenr);
  48. return 0;
  49. }