This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
/
Grammar.pm
201 lines (151 loc) · 4.96 KB
/
Grammar.pm
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
grammar NQP::Grammar is HLL::Grammar;
token TOP { <comp_unit> }
token comp_unit {
<.newpad>
<statementlist>
[ $ || <.panic: 'Confused'> ]
}
rule statementlist {
| $
| [<statement><.eat_terminator> ]*
}
token statement {
<!before <[\])}]> | $ >
[
| <statement_control>
| <EXPR>
]
}
token eat_terminator {
| ';'
| <?MARKED('endstmt')>
| <?terminator>
| $
}
token xblock {
<EXPR> <.ws> <pblock>
}
token pblock {
<?[{]>
<.newpad>
<blockoid>
}
token blockoid {
<.finishpad>
'{' ~ '}' <statementlist>
[ \h* $$ <.ws> <?MARKER('endstmt')> ]?
}
token newpad { <?> }
token finishpad { <?> }
proto token terminator { <...> }
token terminator:sym<;> { <?[;]> }
token terminator:sym<}> { <?[}]> }
## Statement control
proto token statement_control { <...> }
token statement_control:sym<if> {
<sym> :s
<xblock>
[ 'elsif'\s <xblock> ]*
[ 'else'\s <else=pblock> ]?
}
token statement_control:sym<unless> {
<sym> :s
<xblock>
[ <!before 'else'> || <.panic: 'unless does not take "else", please rewrite using "if"'> ]
}
## Terms
token noun:sym<variable> { <variable> }
token noun:sym<scope_declarator> { <scope_declarator> }
token noun:sym<routine_declarator> { <routine_declarator> }
token variable {
<sigil> <twigil>? <desigilname=ident>
}
token sigil { <[$@%&]> }
token twigil { <[*]> }
proto token scope_declarator { <...> }
token scope_declarator:sym<my> { <sym> <scoped> }
token scope_declarator:sym<our> { <sym> <scoped> }
rule scoped {
| <variable_declarator>
| <routine_declarator>
}
token variable_declarator { <variable> }
proto token routine_declarator { <...> }
token routine_declarator:sym<sub> { <sym> <routine_def> }
rule routine_def {
<deflongname=ident>?
<.newpad>
[ '(' <signature> ')'
|| <.panic: 'Routine declaration requires a signature'> ]
<blockoid>
}
token signature { [ [<.ws><parameter><.ws>] ** ',' ]? }
token parameter { <param_var> }
token param_var { <variable> }
proto token term { <...> }
token term:sym<identifier> {
<identifier=ident> <args>
}
token args {
| '(' <arglist> ')'
}
token arglist {
<.ws>
[
| <EXPR>
| <?>
]
}
token term:sym<value> { <value> }
token value {
| <integer>
| <quote>
}
proto token quote { <...> }
token quote:sym<apos> { <?[']> <quote_EXPR: ':q'> }
token quote:sym<dblq> { <?["]> <quote_EXPR: ':qq'> }
token quote:sym<q> { 'q' <![(]> <.ws> <quote_EXPR: ':q'> }
token quote:sym<qq> { 'qq' <![(]> <.ws> <quote_EXPR: ':qq'> }
token quote:sym<Q> { 'Q' <![(]> <.ws> <quote_EXPR> }
token circumfix:sym<( )> { '(' <EXPR> ')' }
token circumfix:sym<ang> { <?[<]> <quote_EXPR: ':q', ':w'> }
token circumfix:sym<{ }> { <?[{]> <pblock> }
## Operators
token nulltermish {
| <OPER=noun=noun>
| <?>
}
token postcircumfix:sym<[ ]> {
'[' <EXPR> ']'
<O('%methodop')>
}
token prefix:sym<--> { <sym> <O('%autoincrement')> }
token postfix:sym<++> { <sym> <O('%autoincrement')> }
token infix:sym<**> { <sym> <O('%exponentiation, :pirop<pow>')> }
token prefix:sym<+> { <sym> <O('%symbolic_unary, :pirop<set N*>')> }
token prefix:sym<~> { <sym> <O('%symbolic_unary, :pirop<set S*>')> }
token prefix:sym<-> { <sym> <O('%symbolic_unary, :pirop<neg>')> }
token prefix:sym<?> { <sym> <O('%symbolic_unary, :pirop<istrue>')> }
token prefix:sym<!> { <sym> <O('%symbolic_unary, :pirop<isfalse>')> }
token infix:sym<*> { <sym> <O('%multiplicative, :pirop<mul>')> }
token infix:sym</> { <sym> <O('%multiplicative, :pirop<div>')> }
token infix:sym<%> { <sym> <O('%multiplicative, :pirop<mod>')> }
token infix:sym<+> { <sym> <O('%additive, :pirop<add>')> }
token infix:sym<-> { <sym> <O('%additive, :pirop<sub>')> }
token infix:sym<~> { <sym> <O('%concatenation , :pirop<concat>')> }
token infix:sym«==» { <sym> <O('%relational, :pirop<iseq INn>')> }
token infix:sym«!=» { <sym> <O('%relational, :pirop<isne INn>')> }
token infix:sym«<=» { <sym> <O('%relational, :pirop<isle INn>')> }
token infix:sym«>=» { <sym> <O('%relational, :pirop<isge INn>')> }
token infix:sym«<» { <sym> <O('%relational, :pirop<islt INn>')> }
token infix:sym«>» { <sym> <O('%relational, :pirop<isgt INn>')> }
token infix:sym«eq» { <sym> <O('%relational, :pirop<iseq ISs>')> }
token infix:sym«ne» { <sym> <O('%relational, :pirop<isne ISs>')> }
token infix:sym«le» { <sym> <O('%relational, :pirop<isle ISs>')> }
token infix:sym«ge» { <sym> <O('%relational, :pirop<isge ISs>')> }
token infix:sym«lt» { <sym> <O('%relational, :pirop<islt ISs>')> }
token infix:sym«gt» { <sym> <O('%relational, :pirop<isgt ISs>')> }
token infix:sym«=:=» { <sym> <O('%relational, :pirop<issame>')> }
token infix:sym<:=> { <sym> <O('%assignment, :pasttype<bind>')> }
token infix:sym<::=> { <sym> <O('%assignment, :pasttype<bind>')> }
token infix:sym<,> { <sym> <O('%comma, :pasttype<list>')> }