/vendor/peg/peg.peg

http://github.com/feyeleanor/RubyGoLightly · Unknown · 77 lines · 71 code · 6 blank · 0 comment · 0 complexity · 3c989393a5884acf5416506c228df218 MD5 · raw file

  1. # PE Grammar for PE Grammars
  2. #
  3. # Adapted from [1] by Ian Piumarta <first-name at last-name point com>.
  4. #
  5. # Local modifications (marked '#ikp') to support:
  6. # C text in '{ ... }' copied verbatim to output as 'semantic action'
  7. # input consumed between '<' and '>' is 'char yytext[]' in semantic actions
  8. #
  9. # Best viewed using 140 columns monospaced with tabs every 8.
  10. #
  11. # [1] Bryan Ford. "Parsing Expression Grammars: A Recognition-Based Syntactic
  12. # Foundation." Symposium on Principles of Programming Languages,
  13. # January 14--16, 2004, Venice, Italy.
  14. #
  15. # Last edited: 2007-05-15 10:32:44 by piumarta on emilia
  16. # Hierarchical syntax
  17. Grammar <- Spacing Definition+ EndOfFile
  18. Definition <- Identifier { if (push(beginRule(findRule(yytext)))->rule.expression) fprintf(stderr, "rule '%s' redefined\n", yytext); }
  19. LEFTARROW Expression { Node *e= pop(); Rule_setExpression(pop(), e); } &{ YYACCEPT }
  20. Expression <- Sequence (SLASH Sequence { Node *f= pop(); push(Alternate_append(pop(), f)); }
  21. )*
  22. Sequence <- Prefix (Prefix { Node *f= pop(); push(Sequence_append(pop(), f)); } #ikp expanded from 'Seq <- Prefix*'
  23. )*
  24. / { push(makePredicate("1")); } #ikp added
  25. Prefix <- AND Action { push(makePredicate(yytext)); } #ikp added
  26. / AND Suffix { push(makePeekFor(pop())); } #ikp expanded from 'Prefix <- (AND/NOT)? Suffix'
  27. / NOT Suffix { push(makePeekNot(pop())); }
  28. / Suffix
  29. Suffix <- Primary (QUESTION { push(makeQuery(pop())); }
  30. / STAR { push(makeStar (pop())); }
  31. / PLUS { push(makePlus (pop())); }
  32. )?
  33. Primary <- Identifier !LEFTARROW { push(makeName(findRule(yytext))); }
  34. / OPEN Expression CLOSE
  35. / Literal { push(makeString(yytext)); }
  36. / Class { push(makeClass(yytext)); }
  37. / DOT { push(makeDot()); }
  38. / Action { push(makeAction(yytext)); } #ikp added
  39. / BEGIN { push(makePredicate("YY_BEGIN")); } #ikp added
  40. / END { push(makePredicate("YY_END")); } #ikp added
  41. # Lexical syntax
  42. Identifier <- < IdentStart IdentCont* > Spacing #ikp inserted < ... >
  43. IdentStart <- [a-zA-Z_]
  44. IdentCont <- IdentStart / [0-9]
  45. Literal <- ['] < (!['] Char )* > ['] Spacing #ikp inserted < ... >
  46. / ["] < (!["] Char )* > ["] Spacing #ikp inserted < ... >
  47. Class <- '[' < (!']' Range)* > ']' Spacing #ikp inserted < ... >
  48. Range <- Char '-' Char / Char
  49. Char <- '\\' [abefnrtv'"\[\]\\] #ikp added missing ANSI escapes: abefv
  50. / '\\' [0-3][0-7][0-7]
  51. / '\\' [0-7][0-7]?
  52. / '\\' '-' #ikp added
  53. / !'\\' .
  54. LEFTARROW <- '<-' Spacing
  55. SLASH <- '/' Spacing
  56. AND <- '&' Spacing
  57. NOT <- '!' Spacing
  58. QUESTION <- '?' Spacing
  59. STAR <- '*' Spacing
  60. PLUS <- '+' Spacing
  61. OPEN <- '(' Spacing
  62. CLOSE <- ')' Spacing
  63. DOT <- '.' Spacing
  64. Spacing <- (Space / Comment)*
  65. Comment <- '#' (!EndOfLine .)* EndOfLine
  66. Space <- ' ' / '\t' / EndOfLine
  67. EndOfLine <- '\r\n' / '\n' / '\r'
  68. EndOfFile <- !.
  69. Action <- '{' < [^}]* > '}' Spacing #ikp added
  70. BEGIN <- '<' Spacing #ikp added
  71. END <- '>' Spacing #ikp added