gml.peg 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # gml graph language lexer+parser to use with packcc parser generator
  2. %prefix "gml"
  3. %source{
  4. #include <stdio.h>
  5. static const char *dbg_str[] = { "Evaluating rule", "Matched rule", "Abandoning rule" };
  6. #define PCC_DEBUG(event, rule, level, pos, buffer, length) \
  7. fprintf(stdout, "%*s%s %s @%d [%.*s]\n", (int)(level * 2), "", dbg_str[event], rule, (int)pos, (int)length, buffer); fflush(stdout)
  8. /* NOTE: To guarantee the output order, stderr, which can lead a race condition with stdout, is not used. */
  9. }
  10. # start of input
  11. file <- _ head _ 'graph' _ list _ !.
  12. head <- (pair)*
  13. list <- '[' _ some_items* _ ']' _
  14. some_items <- id list2 / pair
  15. list2 <- '[' _ some_items* _ ']' _
  16. pair <- (id string) / (id id) / (id fpnum) / (id digit)
  17. digit <- ( ('-' / '+')? [0-9]+ _ ) { printf("NUMBER=%s\n",$0); }
  18. fpnum <- ( ('-' / '+')? [0-9]* '.' [0-9]+ (['e''E'] ('-' / '+')? [0-9]+ )? _ ) { printf("FPNUMBER=%s\n",$0); }
  19. id <- < [a-zA-Z_]+[a-zA-Z_0-9]* > _ { printf("ID=%s\n",$0); }
  20. string <- '"' < char* > '"' _ { printf("STRING=%s\n",$0); }
  21. char <-
  22. '\\' "\""
  23. / '\\' '\\'
  24. / '\\' 'b'
  25. / '\\' 'f'
  26. / '\\' 'n'
  27. / '\\' 'r'
  28. / '\\' 't'
  29. / (!"\"" .)
  30. _ <- (space / comment)*
  31. space <- (' ' / '\t' / endofline)
  32. comment <- '#' (!endofline .)*
  33. endofline <- ( '\r\n' / '\n' / '\r' / '\n\r' ) { }
  34. %%
  35. int main() {
  36. gml_context_t *ctx = gml_create(NULL);
  37. while (gml_parse(ctx, NULL)){;}
  38. gml_destroy(ctx);
  39. return 0;
  40. }