-
Notifications
You must be signed in to change notification settings - Fork 1
/
grammar.y
executable file
·193 lines (169 loc) · 5.73 KB
/
grammar.y
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
class Parser
# Declare tokens produced by the lexer
token IF
token ELSE
token DEF
token CLASS
token NEWLINE
token NUMBER
token STRING
token TRUE FALSE NIL
token IDENTIFIER
token CONSTANT
token INDENT DEDENT
token WHILE
# Precedence table
prechigh
left '.'
right '!'
left '*' '/'
left '+' '-'
left '>' '>=' '<' '<='
left '==' '!='
left '&&'
left '||'
right '='
left ','
preclow
rule
# All rules are declared in this format:
#
# RuleName:
# OtherRule TOKEN AnotherRule { code to run when this matches }
# | OtherRule { ... }
# ;
#
# In the code section (inside the {...} on the right):
# - Assign to "result" the value returned by the rule.
# - Use val[index of expression] to reference expressions on the left.
# All parsing will end in this rule, being the trunk of the AST.
Root:
/* nothing */ { result = Nodes.new([]) }
| Expressions { result = val[0] }
;
# Any list of expressions, class or method body, seperated by line breaks.
Expressions:
Expression { result = Nodes.new(val) }
| Expressions Terminator Expression { result = val[0] << val[2] }
# To ignore trailing line breaks
| Expressions Terminator { result = val[0] }
| Terminator { result = Nodes.new([]) }
;
# All types of expressions in our language
Expression:
Literal
| Call
| Operator
| Constant
| Assign
| Def
| Class
| While
| If
| '(' Expression ')' { result = val[1] }
;
# All tokens that can terminate an expression
Terminator:
NEWLINE
| ";"
;
# All hard-coded values
Literal:
NUMBER { result = NumberNode.new(val[0]) }
| STRING { result = StringNode.new(val[0]) }
| TRUE { result = TrueNode.new }
| FALSE { result = FalseNode.new }
| NIL { result = NilNode.new }
;
# A method call
Call:
# method
IDENTIFIER { result = CallNode.new(nil, val[0], []) }
# method(arguments)
| IDENTIFIER "(" ArgList ")" { result = CallNode.new(nil, val[0], val[2]) }
# receiver.method
| Expression "." IDENTIFIER { result = CallNode.new(val[0], val[2], []) }
# receiver.method(arguments)
| Expression "."
IDENTIFIER "(" ArgList ")" { result = CallNode.new(val[0], val[2], val[4]) }
;
ArgList:
/* nothing */ { result = [] }
| Expression { result = val }
| ArgList "," Expression { result = val[0] << val[2] }
;
Operator:
# Binary operators
Expression '||' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| Expression '&&' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| Expression '==' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| Expression '!=' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| Expression '>' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| Expression '>=' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| Expression '<' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| Expression '<=' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| Expression '+' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| Expression '-' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| Expression '*' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| Expression '/' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| '!' Expression { result = CallNode.new(val[1],val[0],[]) }
;
Constant:
CONSTANT { result = GetConstantNode.new(val[0]) }
;
# Assignment to a variable or constant
Assign:
IDENTIFIER "=" Expression { result = SetLocalNode.new(val[0], val[2]) }
| CONSTANT "=" Expression { result = SetConstantNode.new(val[0], val[2]) }
;
# Method definition
Def:
DEF IDENTIFIER Block { result = DefNode.new(val[1], [], val[2]) }
| DEF IDENTIFIER
"(" ParamList ")" Block { result = DefNode.new(val[1], val[3], val[5]) }
;
ParamList:
/* nothing */ { result = [] }
| IDENTIFIER { result = val }
| ParamList "," IDENTIFIER { result = val[0] << val[2] }
;
# Class definition
Class:
CLASS CONSTANT Block { result = ClassNode.new(val[1], val[2]) }
;
# if block
If:
IF Expression Block Else { result = IfNode.new(val[1], val[2], val[3]) }
;
# else block
Else:
NEWLINE ELSE Block { result = val[2] }
| NEWLINE { result = NIL }
| { result = NIL }
;
# while block
While:
WHILE Expression Block { result = WhileNode.new(val[1], val[2]) }
# A block of indented code. You see here that all the hard work was done by the
# lexer.
Block:
INDENT Expressions DEDENT { result = val[1] }
# If you don't like indentation you could replace the previous rule with the
# following one to separate blocks w/ curly brackets. You'll also need to remove the
# indentation magic section in the lexer.
# "{" Expressions "}" { replace = val[1] }
;
end
---- header
require "lexer"
require "nodes"
---- inner
# This code will be put as-is in the Parser class.
def parse(code, show_tokens=false)
@tokens = Lexer.new.tokenize(code) # Tokenize the code using our lexer
puts @tokens.inspect if show_tokens
do_parse # Kickoff the parsing process
end
def next_token
@tokens.shift
end