1 # Copyright (C) 2008, The Perl Foundation.
7 This is the grammar for Squaak written as a sequence of Perl 6 rules.
11 grammar Squaak::Grammar is PCT::Grammar;
16 [ $ || <.panic: 'Syntax error'> ]
21 | <statement> {*} #= statement
22 | <sub_definition> {*} #= sub_definition
26 | <if_statement> {*} #= if_statement
27 | <while_statement> {*} #= while_statement
28 | <for_statement> {*} #= for_statement
29 | <try_statement> {*} #= try_statement
30 | <throw_statement> {*} #= throw_statement
31 | <return_statement> {*} #= return_statement
32 | <sub_call> {*} #= sub_call
33 | <assignment> {*} #= assignment
34 | <variable_declaration> {*} #= variable_declaration
35 | <do_block> {*} #= do_block
39 'if' <expression> 'then' <block> ['else' <else=block>]? 'end'
43 rule while_statement {
44 'while' <expression> 'do' <block> 'end'
49 'for' <for_init> ',' <expression> [',' <step=expression>]? 'do'
56 'var' <identifier> '=' <expression>
61 'try' <try=block> 'catch' <exception> <catch=block> 'end'
70 rule throw_statement {
75 rule return_statement {
92 <primary> '=' <expression>
97 'sub' <identifier> <parameters>
104 '(' [ <identifier> [',' <identifier>]* ]? ')'
108 rule variable_declaration {
109 'var' <identifier> ['=' <expression>]?
114 <primary> <arguments>
119 '(' [ <expression> [',' <expression>]* ]? ')'
124 <identifier> <postfix_expression>*
128 rule postfix_expression {
130 | <member> {*} #= member
131 | <index> {*} #= index
150 | <float_constant> {*} #= float_constant
151 | <integer_constant> {*} #= integer_constant
152 | <string_constant> {*} #= string_constant
153 | <hash_constructor> {*} #= hash_constructor
154 | <array_constructor> {*} #= array_constructor
155 | <sub_call> {*} #= sub_call
156 | <primary> {*} #= primary
157 | '(' <expression> ')' {*} #= expression
160 rule hash_constructor {
161 '{' [ <named_field> [',' <named_field>]* ]? '}'
166 <string_constant> '=>' <expression>
170 rule array_constructor {
171 '[' [ <expression> [',' <expression>]* ]? ']'
180 token integer_constant {
185 token float_constant {
193 token string_constant {
194 \" <string_literal: '"'> \"
199 ['and'|'catch' |'do' |'else' |'end' |'for'|'if' |'not'
200 |'or' |'return'|'sub'|'throw'|'try' |'var'|'while']>>
203 rule expression is optable { ... }
205 proto 'infix:or' is precedence('1') is pasttype('unless') { ... }
206 proto 'infix:and' is tighter('infix:or') is pasttype('if') { ... }
208 proto 'infix:<' is tighter('infix:and') { ... }
209 proto 'infix:<=' is equiv('infix:<') { ... }
210 proto 'infix:>' is equiv('infix:<') { ... }
211 proto 'infix:>=' is equiv('infix:<') { ... }
212 proto 'infix:==' is equiv('infix:<') { ... }
213 proto 'infix:!=' is equiv('infix:<') { ... }
215 proto 'infix:+' is tighter('infix:<') is pirop('n_add') { ... }
216 proto 'infix:-' is equiv('infix:+') is pirop('n_sub') { ... }
218 proto 'infix:..' is equiv('infix:+') is pirop('n_concat') { ... }
220 proto 'infix:*' is tighter('infix:+') is pirop('n_mul') { ... }
221 proto 'infix:%' is equiv('infix:*') is pirop('n_mod') { ... }
222 proto 'infix:/' is equiv('infix:*') is pirop('n_div') { ... }
224 proto 'prefix:not' is tighter('infix:*') is pirop('n_not') { ... }
225 proto 'prefix:-' is tighter('prefix:not') is pirop('n_neg') { ... }
227 proto 'term:' is tighter('prefix:-') is parsed(&term) { ... }
230 ## this <ws> rule treats # as "comment to eol"
231 ## you may want to replace it with something appropriate
234 [ '#' \N* \n? | \s+ ]*