/
Grammar.pm
197 lines (149 loc) · 4.01 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
=begin overview
This is the grammar for Squaak in Perl 6 rules.
=end overview
grammar Squaak::Grammar is HLL::Grammar;
token begin_TOP {
<?>
}
token TOP {
<.begin_TOP>
<statementlist>
[ $ || <.panic: "Syntax error"> ]
}
## Lexer items
# This <ws> rule treats # as "comment to eol".
token ws {
<!ww>
[ '#' \N* \n? | \s+ ]*
}
## Statements
rule statementlist {
<stat_or_def>*
}
rule stat_or_def {
| <statement>
| <sub_definition>
}
rule sub_definition {
'sub' <identifier> <parameters>
<statement>*
'end'
}
rule parameters {
'(' [<identifier> ** ',']? ')'
}
proto rule statement { <...> }
rule statement:sym<assignment> {
<primary> '=' <EXPR>
}
rule statement:sym<do> {
<sym> <block> 'end'
}
rule statement:sym<for> {
<sym> <for_init> ',' <EXPR> <step>?
'do' <statement>* 'end'
}
rule step {
',' <EXPR>
}
rule for_init {
'var' <identifier> '=' <EXPR>
}
rule statement:sym<if> {
<sym> <EXPR> 'then' $<then>=<block>
['else' $<else>=<block> ]?
'end'
}
rule statement:sym<sub_call> {
<primary> <arguments>
}
rule arguments {
'(' [<EXPR> ** ',']? ')'
}
rule statement:sym<throw> {
<sym> <EXPR>
}
rule statement:sym<try> {
<sym> $<try>=<block>
'catch' <exception>
$<catch>=<block>
'end'
}
rule exception {
<identifier>
}
rule statement:sym<var> {
<sym> <identifier> ['=' <EXPR>]?
}
rule statement:sym<while> {
<sym> <EXPR> 'do' <block> 'end'
}
token begin_block {
<?>
}
rule block {
<.begin_block>
<statement>*
}
## Terms
rule primary {
<identifier> <postfix_expression>*
}
proto rule postfix_expression { <...> }
rule postfix_expression:sym<index> { '[' <EXPR> ']' }
rule postfix_expression:sym<key> { '{' <EXPR> '}' }
token identifier {
<!keyword> <ident>
}
token keyword {
['and'|'catch'|'do' |'else' |'end' |'for' |'if'
|'not'|'or' |'sub' |'throw'|'try' |'var'|'while']>>
}
token term:sym<integer_constant> { <integer> }
token term:sym<string_constant> { <string_constant> }
token string_constant { <quote> }
token term:sym<float_constant_long> { # longer to work-around lack of LTM
[
| \d+ '.' \d*
| \d* '.' \d+
]
}
proto token quote { <...> }
token quote:sym<'> { <?[']> <quote_EXPR: ':q'> }
token quote:sym<"> { <?["]> <quote_EXPR: ':qq'> }
## Operators
INIT {
Squaak::Grammar.O(':prec<w>, :assoc<unary>', '%unary-negate');
Squaak::Grammar.O(':prec<v>, :assoc<unary>', '%unary-not');
Squaak::Grammar.O(':prec<u>, :assoc<left>', '%multiplicative');
Squaak::Grammar.O(':prec<t>, :assoc<left>', '%additive');
Squaak::Grammar.O(':prec<s>, :assoc<left>', '%relational');
Squaak::Grammar.O(':prec<r>, :assoc<left>', '%conjunction');
Squaak::Grammar.O(':prec<q>, :assoc<left>', '%disjunction');
}
token circumfix:sym<( )> { '(' <.ws> <EXPR> ')' }
rule circumfix:sym<[ ]> {
'[' [<EXPR> ** ',']? ']'
}
rule circumfix:sym<{ }> {
'{' [<named_field> ** ',']? '}'
}
rule named_field {
<string_constant> '=>' <EXPR>
}
token prefix:sym<-> { <sym> <O('%unary-negate, :pirop<neg>')> }
token prefix:sym<not> { <sym> <O('%unary-not, :pirop<isfalse>')> }
token infix:sym<*> { <sym> <O('%multiplicative, :pirop<mul>')> }
token infix:sym<%> { <sym> <O('%multiplicative, :pirop<mod>')> }
token infix:sym</> { <sym> <O('%multiplicative, :pirop<div>')> }
token infix:sym<+> { <sym> <O('%additive, :pirop<add>')> }
token infix:sym<-> { <sym> <O('%additive, :pirop<sub>')> }
token infix:sym<..> { <sym> <O('%additive, :pirop<concat>')> }
token infix:sym«<» { <sym> <O('%relational, :pirop<isle>')> }
token infix:sym«<=» { <sym> <O('%relational, :pirop<islt>')> }
token infix:sym«>» { <sym> <O('%relational, :pirop<isgt>')> }
token infix:sym«>=» { <sym> <O('%relational, :pirop<isge>')> }
token infix:sym«==» { <sym> <O('%relational, :pirop<iseq>')> }
token infix:sym«!=» { <sym> <O('%relational, :pirop<isne>')> }
token infix:sym<and> { <sym> <O('%conjunction, :pasttype<if>')> }
token infix:sym<or> { <sym> <O('%disjunction, :pasttype<unless>')> }