-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
265 lines (211 loc) · 6.56 KB
/
tree.go
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
// Use of this file is governed by the BSD 3-clause license that
// can be found in the LICENSE.txt file in the project root.
package antlr
// The basic notion of a tree has a parent, a payload, and a list of children.
// It is the most abstract interface for all the trees used by ANTLR.
///
var TreeInvalidInterval = NewInterval(-1, -2)
type Tree interface {
GetParent() Tree
SetParent(Tree)
GetPayload() interface{}
GetChild(i int) Tree
GetChildCount() int
GetChildren() []Tree
}
type SyntaxTree interface {
Tree
GetSourceInterval() *Interval
}
type ParseTree interface {
SyntaxTree
Visit(visitor ParseTreeVisitor, args ...interface{}) interface{}
GetText() string
ToStringTree([]string, Recognizer) string
}
type RuleNode interface {
ParseTree
GetRuleContext() RuleContext
GetBaseRuleContext() *BaseRuleContext
}
type TerminalNode interface {
ParseTree
GetSymbol() Token
}
type ErrorNode interface {
TerminalNode
errorNode()
}
type ParseTreeVisitor interface {
VisitNext(next Tree, currentResult interface{}) bool
VisitRest(next RuleNode, currentResult interface{}) bool
VisitTerminal(node TerminalNode, args ...interface{}) (result interface{})
VisitErrorNode(node ErrorNode, args ...interface{}) (result interface{})
VisitChildren(node RuleNode, delegate ParseTreeVisitor, args ...interface{}) (result interface{})
AggregateResult(aggregate, nextResult interface{}) (result interface{})
}
type BaseParseTreeVisitor struct{}
func (*BaseParseTreeVisitor) VisitNext(node Tree, currentResult interface{}) bool {
return true
}
func (*BaseParseTreeVisitor) VisitRest(node RuleNode, currentResult interface{}) bool {
return true
}
func (*BaseParseTreeVisitor) VisitTerminal(node TerminalNode, args ...interface{}) (result interface{}) {
return
}
func (*BaseParseTreeVisitor) VisitErrorNode(node ErrorNode, args ...interface{}) (result interface{}) {
return
}
func (*BaseParseTreeVisitor) AggregateResult(aggregate, nextResult interface{}) (result interface{}) {
return nextResult
}
func (*BaseParseTreeVisitor) VisitChildren(node RuleNode, delegate ParseTreeVisitor, args ...interface{}) (result interface{}) {
for _, child := range node.GetChildren() {
if !delegate.VisitNext(child, result) {
continue
}
switch child := child.(type) {
case TerminalNode:
r := delegate.VisitTerminal(child, args)
result = delegate.AggregateResult(result, r)
case ErrorNode:
r := delegate.VisitErrorNode(child, args)
result = delegate.AggregateResult(result, r)
case RuleNode:
if !delegate.VisitRest(child, result) {
break
}
r := child.Visit(delegate, args)
result = delegate.AggregateResult(result, r)
default:
// can this happen??
}
}
return
}
type ParseTreeListener interface {
VisitTerminal(node TerminalNode)
VisitErrorNode(node ErrorNode)
EnterEveryRule(ctx ParserRuleContext)
ExitEveryRule(ctx ParserRuleContext)
}
type BaseParseTreeListener struct{}
var _ ParseTreeListener = &BaseParseTreeListener{}
func (*BaseParseTreeListener) VisitTerminal(node TerminalNode) {}
func (*BaseParseTreeListener) VisitErrorNode(node ErrorNode) {}
func (*BaseParseTreeListener) EnterEveryRule(ctx ParserRuleContext) {}
func (*BaseParseTreeListener) ExitEveryRule(ctx ParserRuleContext) {}
type TerminalNodeImpl struct {
parentCtx RuleContext
symbol Token
}
var _ TerminalNode = &TerminalNodeImpl{}
func NewTerminalNodeImpl(symbol Token) *TerminalNodeImpl {
tn := new(TerminalNodeImpl)
tn.parentCtx = nil
tn.symbol = symbol
return tn
}
func (t *TerminalNodeImpl) GetChild(i int) Tree {
return nil
}
func (t *TerminalNodeImpl) GetChildren() []Tree {
return nil
}
func (t *TerminalNodeImpl) SetChildren(tree []Tree) {
panic("Cannot set children on terminal node")
}
func (t *TerminalNodeImpl) GetSymbol() Token {
return t.symbol
}
func (t *TerminalNodeImpl) GetParent() Tree {
return t.parentCtx
}
func (t *TerminalNodeImpl) SetParent(tree Tree) {
t.parentCtx = tree.(RuleContext)
}
func (t *TerminalNodeImpl) GetPayload() interface{} {
return t.symbol
}
func (t *TerminalNodeImpl) GetSourceInterval() *Interval {
if t.symbol == nil {
return TreeInvalidInterval
}
tokenIndex := t.symbol.GetTokenIndex()
return NewInterval(tokenIndex, tokenIndex)
}
func (t *TerminalNodeImpl) GetChildCount() int {
return 0
}
func (t *TerminalNodeImpl) Visit(v ParseTreeVisitor, args ...interface{}) interface{} {
return v.VisitTerminal(t, args)
}
func (t *TerminalNodeImpl) GetText() string {
return t.symbol.GetText()
}
func (t *TerminalNodeImpl) String() string {
if t.symbol.GetTokenType() == TokenEOF {
return "<EOF>"
}
return t.symbol.GetText()
}
func (t *TerminalNodeImpl) ToStringTree(s []string, r Recognizer) string {
return t.String()
}
// Represents a token that was consumed during reSynchronization
// rather than during a valid Match operation. For example,
// we will create this kind of a node during single token insertion
// and deletion as well as during "consume until error recovery set"
// upon no viable alternative exceptions.
type ErrorNodeImpl struct {
*TerminalNodeImpl
}
var _ ErrorNode = &ErrorNodeImpl{}
func NewErrorNodeImpl(token Token) *ErrorNodeImpl {
en := new(ErrorNodeImpl)
en.TerminalNodeImpl = NewTerminalNodeImpl(token)
return en
}
func (e *ErrorNodeImpl) errorNode() {}
func (e *ErrorNodeImpl) Visit(v ParseTreeVisitor, args ...interface{}) interface{} {
return v.VisitErrorNode(e, args)
}
type ParseTreeWalker struct {
}
func NewParseTreeWalker() *ParseTreeWalker {
return new(ParseTreeWalker)
}
func (p *ParseTreeWalker) Walk(listener ParseTreeListener, t Tree) {
switch tt := t.(type) {
case ErrorNode:
listener.VisitErrorNode(tt)
case TerminalNode:
listener.VisitTerminal(tt)
default:
p.EnterRule(listener, t.(RuleNode))
for i := 0; i < t.GetChildCount(); i++ {
child := t.GetChild(i)
p.Walk(listener, child)
}
p.ExitRule(listener, t.(RuleNode))
}
}
//
// The discovery of a rule node, involves sending two events: the generic
// {@link ParseTreeListener//EnterEveryRule} and a
// {@link RuleContext}-specific event. First we trigger the generic and then
// the rule specific. We to them in reverse order upon finishing the node.
//
func (p *ParseTreeWalker) EnterRule(listener ParseTreeListener, r RuleNode) {
ctx := r.GetRuleContext().(ParserRuleContext)
listener.EnterEveryRule(ctx)
ctx.EnterRule(listener)
}
func (p *ParseTreeWalker) ExitRule(listener ParseTreeListener, r RuleNode) {
ctx := r.GetRuleContext().(ParserRuleContext)
ctx.ExitRule(listener)
listener.ExitEveryRule(ctx)
}
var ParseTreeWalkerDefault = NewParseTreeWalker()