PageRenderTime 20ms CodeModel.GetById 17ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/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
17# Hierarchical syntax
18
19Grammar		<- Spacing Definition+ EndOfFile
20
21Definition	<- Identifier 			{ if (push(beginRule(findRule(yytext)))->rule.expression) fprintf(stderr, "rule '%s' redefined\n", yytext); }
22		     LEFTARROW Expression	{ Node *e= pop();  Rule_setExpression(pop(), e); } &{ YYACCEPT }
23Expression	<- Sequence (SLASH Sequence	{ Node *f= pop();  push(Alternate_append(pop(), f)); }
24			    )*
25Sequence	<- Prefix (Prefix		{ Node *f= pop();  push(Sequence_append(pop(), f)); }	#ikp expanded from 'Seq <- Prefix*'
26			  )*
27		 / 				{ push(makePredicate("1")); }				#ikp added
28Prefix		<- AND Action			{ push(makePredicate(yytext)); }	#ikp added
29		 / AND Suffix			{ push(makePeekFor(pop())); }		#ikp expanded from 'Prefix <- (AND/NOT)? Suffix'
30		 / NOT Suffix			{ push(makePeekNot(pop())); }
31		 /     Suffix
32Suffix		<- Primary (QUESTION		{ push(makeQuery(pop())); }
33			     / STAR		{ push(makeStar (pop())); }
34			     / PLUS		{ push(makePlus (pop())); }
35			   )?
36Primary		<- Identifier !LEFTARROW	{ push(makeName(findRule(yytext))); }
37		 / OPEN Expression CLOSE
38		 / Literal			{ push(makeString(yytext)); }
39		 / Class			{ push(makeClass(yytext)); }
40		 / DOT				{ push(makeDot()); }
41		 / Action			{ push(makeAction(yytext)); }		#ikp added
42		 / BEGIN			{ push(makePredicate("YY_BEGIN")); }	#ikp added
43		 / END				{ push(makePredicate("YY_END")); }	#ikp added
44
45# Lexical syntax
46
47Identifier	<- < IdentStart IdentCont* > Spacing		#ikp inserted < ... >
48IdentStart	<- [a-zA-Z_]
49IdentCont	<- IdentStart / [0-9]
50Literal		<- ['] < (!['] Char )* > ['] Spacing		#ikp inserted < ... >
51		 / ["] < (!["] Char )* > ["] Spacing		#ikp inserted < ... >
52Class		<- '[' < (!']' Range)* > ']' Spacing		#ikp inserted < ... >
53Range		<- Char '-' Char / Char
54Char		<- '\\' [abefnrtv'"\[\]\\]			#ikp added missing ANSI escapes: abefv
55		 / '\\' [0-3][0-7][0-7]
56		 / '\\' [0-7][0-7]?
57		 / '\\' '-'					#ikp added
58		 / !'\\' .
59LEFTARROW	<- '<-' Spacing
60SLASH		<- '/' Spacing
61AND		<- '&' Spacing
62NOT		<- '!' Spacing
63QUESTION	<- '?' Spacing
64STAR		<- '*' Spacing
65PLUS		<- '+' Spacing
66OPEN		<- '(' Spacing
67CLOSE		<- ')' Spacing
68DOT		<- '.' Spacing
69Spacing		<- (Space / Comment)*
70Comment		<- '#' (!EndOfLine .)* EndOfLine
71Space		<- ' ' / '\t' / EndOfLine
72EndOfLine	<- '\r\n' / '\n' / '\r'
73EndOfFile	<- !.
74
75Action		<- '{' < [^}]* > '}' Spacing		#ikp added
76BEGIN		<- '<' Spacing				#ikp added
77END		<- '>' Spacing				#ikp added