/
Grammar.pm
114 lines (85 loc) · 1.72 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
=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 { [ <statement> | <?> ] ** ';' }
proto rule statement { <...> }
rule statement:sym<assignment> {
<primary> '=' <expression>
}
rule statement:sym<do> {
<sym> <block> 'end'
}
rule statement:sym<if> {
<sym> <expression> 'then' $<then>=<block>
['else' $<else>=<block> ]?
'end'
}
rule statement:sym<sub> {
<sym> <identifier> <parameters>
<statement>*
'end'
}
rule parameters {
'(' [<identifier> ** ',']? ')'
}
rule statement:sym<throw> {
<sym> <expression>
}
rule statement:sym<try> {
<sym> $<try>=<block>
'catch' <exception>
$<catch>=<block>
'end'
}
rule exception {
<identifier>
}
rule statement:sym<var> {
<sym> <identifier> ['=' <expression>]?
}
rule statement:sym<while> {
<sym> <expression> 'do' <block> 'end'
}
token begin_block {
<?>
}
rule block {
<.begin_block>
<statement>*
}
## Terms
rule primary {
<identifier>
}
token identifier {
<!keyword> <ident>
}
token keyword {
['and'|'catch'|'do' |'else' |'end' |'for' |'if'
|'not'|'or' |'sub' |'throw'|'try' |'var'|'while']>>
}
rule expression {
| <integer_constant>
| <string_constant>
}
token integer_constant { <integer> }
token string_constant { <quote> }
proto token quote { <...> }
token quote:sym<'> { <?[']> <quote_EXPR: ':q'> }
token quote:sym<"> { <?["]> <quote_EXPR: ':qq'> }