/peg/peg.peg
http://github.com/feyeleanor/RubyGoLightly · Unknown · 77 lines · 71 code · 6 blank · 0 comment · 0 complexity · 67dac4fef4ce3b75dfc5df2b05cd9370 MD5 · raw file
- # PE Grammar for PE Grammars
- #
- # Adapted from [1] by Ian Piumarta <first-name at last-name point com>.
- #
- # Local modifications (marked '#ikp') to support:
- # C text in '{ ... }' copied verbatim to output as 'semantic action'
- # input consumed between '<' and '>' is 'char yytext[]' in semantic actions
- #
- # Best viewed using 140 columns monospaced with tabs every 8.
- #
- # [1] Bryan Ford. "Parsing Expression Grammars: A Recognition-Based Syntactic
- # Foundation." Symposium on Principles of Programming Languages,
- # January 14--16, 2004, Venice, Italy.
- #
- # Last edited: 2007-05-15 10:32:44 by piumarta on emilia
- # Hierarchical syntax
- Grammar <- Spacing Definition+ EndOfFile
- Definition <- Identifier { if (push(beginRule(findRule(yytext)))->rule.expression) fprintf(stderr, "rule '%s' redefined\n", yytext); }
- LEFTARROW Expression { Node *e= pop(); Rule_setExpression(pop(), e); } &{ YYACCEPT }
- Expression <- Sequence (SLASH Sequence { Node *f= pop(); push(Alternate_append(pop(), f)); }
- )*
- Sequence <- Prefix (Prefix { Node *f= pop(); push(Sequence_append(pop(), f)); } #ikp expanded from 'Seq <- Prefix*'
- )*
- / { push(Predicate{text: "1"}); } #ikp added
- Prefix <- AND Action { push(Predicate{text: yytext}); } #ikp added
- / AND Suffix { push(PeekFor{element: pop()}); } #ikp expanded from 'Prefix <- (AND/NOT)? Suffix'
- / NOT Suffix { push(PeekNot{element: pop()}); }
- / Suffix
- Suffix <- Primary (QUESTION { push(Query{element: pop()}); }
- / STAR { push(Star{element: pop()}); }
- / PLUS { push(Plus{element: pop()}); }
- )?
- Primary <- Identifier !LEFTARROW { push(Name{used: true, variable: nil, rule: findRule(yytext)}); }
- / OPEN Expression CLOSE
- / Literal { push(String{value: yytext}); }
- / Class { push(Class{cclass: yytext}); }
- / DOT { push(Dot{}); }
- / Action { push(makeAction(yytext)); } #ikp added
- / BEGIN { push(Predicate{text: "YY_BEGIN"}); } #ikp added
- / END { push(Predicate{text: "YY_END"}); } #ikp added
- # Lexical syntax
- Identifier <- < IdentStart IdentCont* > Spacing #ikp inserted < ... >
- IdentStart <- [a-zA-Z_]
- IdentCont <- IdentStart / [0-9]
- Literal <- ['] < (!['] Char )* > ['] Spacing #ikp inserted < ... >
- / ["] < (!["] Char )* > ["] Spacing #ikp inserted < ... >
- Class <- '[' < (!']' Range)* > ']' Spacing #ikp inserted < ... >
- Range <- Char '-' Char / Char
- Char <- '\\' [abefnrtv'"\[\]\\] #ikp added missing ANSI escapes: abefv
- / '\\' [0-3][0-7][0-7]
- / '\\' [0-7][0-7]?
- / '\\' '-' #ikp added
- / !'\\' .
- LEFTARROW <- '<-' Spacing
- SLASH <- '/' Spacing
- AND <- '&' Spacing
- NOT <- '!' Spacing
- QUESTION <- '?' Spacing
- STAR <- '*' Spacing
- PLUS <- '+' Spacing
- OPEN <- '(' Spacing
- CLOSE <- ')' Spacing
- DOT <- '.' Spacing
- Spacing <- (Space / Comment)*
- Comment <- '#' (!EndOfLine .)* EndOfLine
- Space <- ' ' / '\t' / EndOfLine
- EndOfLine <- '\r\n' / '\n' / '\r'
- EndOfFile <- !.
- Action <- '{' < [^}]* > '}' Spacing #ikp added
- BEGIN <- '<' Spacing #ikp added
- END <- '>' Spacing #ikp added