1 module pegged.examples.peggedgrammar;
2 
3 import pegged.grammar;
4 
5 enum PEGGEDgrammar = `
6 # This is the PEG extended grammar used by Pegged
7 Pegged:
8 
9 # Syntactic rules:
10 Grammar      <- Spacing GrammarName Definition+ :eoi
11 Definition   <- LhsName Arrow Expression
12 Expression   <- FirstExpression / LongestExpression
13 FirstExpression   <- :OR? Sequence (:OR Sequence)+
14 LongestExpression <- :(OR / LONGEST_OR)? Sequence (:LONGEST_OR Sequence)*
15 Sequence     <- Prefix+
16 Prefix       <- (POS / NEG / FUSE / DISCARD / KEEP / DROP / PROPAGATE)* Suffix
17 Suffix       <- Primary (OPTION / ZEROORMORE / ONEORMORE / Action)*
18 Primary      <- !(LhsName Arrow)
19                 ( RhsName
20                 / :OPEN Expression :CLOSE
21                 / Literal
22                 / CILiteral
23                 / CharClass
24                 / ANY)
25 # Lexical syntax
26 Identifier   <- identifier
27 GrammarName  <- Identifier ParamList? Spacing :':' Spacing
28 LhsName      <- Identifier ParamList? Spacing
29 RhsName      <- Identifier ArgList? (NAMESEP Identifier ArgList?)* Spacing         # NAMESEP is *not* discarded
30 ParamList    <- :OPEN Param (:SEPARATOR Param)*  :CLOSE
31 Param        <- DefaultParam / SingleParam
32 DefaultParam <- Identifier Spacing :ASSIGN Expression
33 SingleParam  <- Identifier Spacing
34 ArgList      <- :OPEN Expression (:SEPARATOR Expression)* :CLOSE
35 
36 Literal      <- quote       ~(!quote Char)*       quote       !'i' Spacing
37               / doublequote ~(!doublequote Char)* doublequote !'i' Spacing
38 CILiteral    <- quote       ~(!quote Char)*       quote       :'i' Spacing
39               / doublequote ~(!doublequote Char)* doublequote :'i' Spacing
40 CharClass    <- :'[' (!']' CharRange)* :']' Spacing
41 CharRange    <- Char '-' Char / Char
42 
43 # Terminals
44 Char         <~ backslash ( quote
45                           / doublequote
46                           / backquote
47                           / backslash
48                           / '-'
49                           / '['
50                           / ']'
51                           / [nrt]
52                           / [0-2][0-7][0-7]
53                           / [0-7][0-7]?
54                           / 'x' hexDigit hexDigit
55                           / 'u' hexDigit hexDigit hexDigit hexDigit
56                           / 'U' hexDigit hexDigit hexDigit hexDigit hexDigit hexDigit hexDigit hexDigit
57                           )
58               / . # or anything else
59 
60 Arrow        <- LEFTARROW / FUSEARROW / DISCARDARROW / KEEPARROW / DROPARROW / PROPAGATEARROW / ACTIONARROW / SPACEARROW
61 LEFTARROW    <- '<-' Spacing
62 FUSEARROW    <- '<~' Spacing
63 DISCARDARROW <- '<:' Spacing
64 KEEPARROW    <- '<^' Spacing
65 DROPARROW    <- '<;' Spacing
66 PROPAGATEARROW <- '<%' Spacing
67 SPACEARROW   <- '<' Spacing
68 ACTIONARROW  <- '<' Action Spacing
69 
70 OR           <- '/' Spacing
71 LONGEST_OR   <- '|' Spacing
72 
73 POS          <- '&' Spacing
74 NEG          <- '!' Spacing
75 FUSE         <- '~' Spacing
76 DISCARD      <- ':' Spacing
77 KEEP         <- '^' Spacing
78 DROP         <- ';' Spacing
79 PROPAGATE    <- '%' Spacing
80 
81 OPTION       <- '?' Spacing
82 ZEROORMORE   <- '*' Spacing
83 ONEORMORE    <- '+' Spacing
84 ACTIONOPEN   <- '{' Spacing
85 ACTIONCLOSE  <- '}' Spacing
86 SEPARATOR    <- ',' Spacing
87 ASSIGN       <- '=' Spacing
88 NAMESEP      <- '.'   # No Spacing
89 OPEN         <- '(' Spacing
90 CLOSE        <- ')' Spacing
91 ANY          <- '.' Spacing
92 Spacing      <: (blank / Comment)*
93 Comment      <- '#' (!eol .)* :eol
94 Space        <- spacing / "\\t" / "\\n" / "\\r"
95 
96 # Action Rule
97 Action      <- :ACTIONOPEN Spacing ((Lambda / qualifiedIdentifier)
98 (:SEPARATOR (Lambda / qualifiedIdentifier))*) Spacing :ACTIONCLOSE
99 Lambda      <~ (!(ACTIONCLOSE/SEPARATOR) (LambdaItems / NestedList('{',LambdaItems,'}') / .))*
100 
101 LambdaItems <- ~DComment / ~DString / ~DParamList
102 DString     <- WYSString / DBQString / TKNString / DLMString
103 
104 WYSString   <- 'r' doublequote (!doublequote .)* doublequote /
105                backquote (!backquote .)* backquote
106 
107 DBQString   <- doublequote (!doublequote Char)* doublequote
108 
109 TKNString   <- (&'q{' ('q' NestedList('{',DString,'}')))
110 
111 DLMString   <- ('q' doublequote) ( (&'{' NestedList('{',DString,'}'))
112                                  / (&'[' NestedList('[',DString,']'))
113                                  / (&'(' NestedList('(',DString,')'))
114                                  / (&'<' NestedList('<',DString,'>'))
115                                  ) doublequote
116 
117 DComment             <- DLineComment / DBlockComment / DNestingBlockComment
118 
119 DLineComment         <- "//" (!endOfLine .)* endOfLine
120 DBlockComment        <- "/*" (!"*/" .)* "*/"
121 DNestingBlockComment <- NestedList("/+","+/")
122 
123 DParamList <- NestedList('(',')')
124 
125 # Linear nested lists with and without special items
126 NestedList(L,Items,R)   <- ^L ( !(L/R/Items) . )* ( Items
127                                                   / NestedList(L,Items,R)
128                                                   / ( !(L/R/Items) . )*
129                                                   )* ( !(L/R/Items) . )* ^R
130 
131 NestedList(L,R) <- ^L ( !(L/R) . )* (NestedList(L,R) / ( !(L/R) . )*)* ( !(L/R) . )* ^R
132 `;