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