gml.peg 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # gml graph language lexer+parser to use with packcc parser generator
  2. #
  3. # /*
  4. # * This program is free software: you can redistribute it and/or modify
  5. # * it under the terms of the GNU General Public License as published by
  6. # * the Free Software Foundation, either version 3 of the License, or
  7. # * (at your option) any later version.
  8. # *
  9. # * This program is distributed in the hope that it will be useful,
  10. # * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # * GNU General Public License for more details.
  13. # *
  14. # * You should have received a copy of the GNU General Public License
  15. # * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. # *
  17. # * SPDX-License-Identifier: GPL-3.0+
  18. # * License-Filename: LICENSE
  19. # *
  20. # */
  21. #
  22. %prefix "gml"
  23. %source{
  24. #include <stdio.h>
  25. static const char *dbg_str[] = { "Evaluating rule", "Matched rule", "Abandoning rule" };
  26. #define PCC_DEBUG(event, rule, level, pos, buffer, length) \
  27. fprintf(stdout, "%*s%s %s @%d [%.*s]\n", (int)(level * 2), "", dbg_str[event], rule, (int)pos, (int)length, buffer); fflush(stdout)
  28. /* NOTE: To guarantee the output order, stderr, which can lead a race condition with stdout, is not used. */
  29. }
  30. # start of input
  31. file <- _ head _ 'graph' _ list _ !.
  32. head <- (pair)*
  33. list <- '[' _ some_items* _ ']' _
  34. some_items <- id list2 / pair
  35. list2 <- '[' _ some_items* _ ']' _
  36. pair <- (id string) / (id id) / (id fpnum) / (id digit)
  37. digit <- ( ('-' / '+')? [0-9]+ _ ) { printf("NUMBER=%s\n",$0); }
  38. fpnum <- ( ('-' / '+')? [0-9]* '.' [0-9]+ (['e''E'] ('-' / '+')? [0-9]+ )? _ ) { printf("FPNUMBER=%s\n",$0); }
  39. id <- < [a-zA-Z_]+[a-zA-Z_0-9]* > _ { printf("ID=%s\n",$0); }
  40. string <- '"' < char* > '"' _ { printf("STRING=%s\n",$0); }
  41. char <-
  42. '\\' "\""
  43. / '\\' '\\'
  44. / '\\' 'b'
  45. / '\\' 'f'
  46. / '\\' 'n'
  47. / '\\' 'r'
  48. / '\\' 't'
  49. / (!"\"" .)
  50. _ <- (space / comment)*
  51. space <- (' ' / '\t' / endofline)
  52. comment <- '#' (!endofline .)*
  53. endofline <- ( '\r\n' / '\n' / '\r' / '\n\r' ) { }
  54. %%
  55. int main() {
  56. gml_context_t *ctx = gml_create(NULL);
  57. while (gml_parse(ctx, NULL)){;}
  58. gml_destroy(ctx);
  59. return 0;
  60. }