-
Notifications
You must be signed in to change notification settings - Fork 200
/
tree_builder.ts
278 lines (250 loc) · 8.69 KB
/
tree_builder.ts
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
266
267
268
269
270
271
272
273
274
275
276
277
278
import {
addNoneTerminalToCst,
addTerminalToCst,
setNodeLocationFull,
setNodeLocationOnlyOffset
} from "../../cst/cst"
import noop from "lodash/noop"
import has from "lodash/has"
import keys from "lodash/keys"
import isUndefined from "lodash/isUndefined"
import {
createBaseSemanticVisitorConstructor,
createBaseVisitorConstructorWithDefaults
} from "../../cst/cst_visitor"
import {
CstNode,
CstNodeLocation,
ICstVisitor,
IParserConfig,
IToken,
nodeLocationTrackingOptions
} from "@chevrotain/types"
import { MixedInParser } from "./parser_traits"
import { DEFAULT_PARSER_CONFIG } from "../parser"
/**
* This trait is responsible for the CST building logic.
*/
export class TreeBuilder {
outputCst: boolean
CST_STACK: CstNode[]
baseCstVisitorConstructor: Function
baseCstVisitorWithDefaultsConstructor: Function
// dynamically assigned Methods
setNodeLocationFromNode: (
nodeLocation: CstNodeLocation,
locationInformation: CstNodeLocation
) => void
setNodeLocationFromToken: (
nodeLocation: CstNodeLocation,
locationInformation: CstNodeLocation
) => void
cstPostRule: (this: MixedInParser, ruleCstNode: CstNode) => void
setInitialNodeLocation: (cstNode: CstNode) => void
nodeLocationTracking: nodeLocationTrackingOptions
initTreeBuilder(this: MixedInParser, config: IParserConfig) {
this.CST_STACK = []
// outputCst is no longer exposed/defined in the pubic API
this.outputCst = (config as any).outputCst
this.nodeLocationTracking = has(config, "nodeLocationTracking")
? (config.nodeLocationTracking as nodeLocationTrackingOptions) // assumes end user provides the correct config value/type
: DEFAULT_PARSER_CONFIG.nodeLocationTracking
if (!this.outputCst) {
this.cstInvocationStateUpdate = noop
this.cstFinallyStateUpdate = noop
this.cstPostTerminal = noop
this.cstPostNonTerminal = noop
this.cstPostRule = noop
} else {
if (/full/i.test(this.nodeLocationTracking)) {
if (this.recoveryEnabled) {
this.setNodeLocationFromToken = setNodeLocationFull
this.setNodeLocationFromNode = setNodeLocationFull
this.cstPostRule = noop
this.setInitialNodeLocation = this.setInitialNodeLocationFullRecovery
} else {
this.setNodeLocationFromToken = noop
this.setNodeLocationFromNode = noop
this.cstPostRule = this.cstPostRuleFull
this.setInitialNodeLocation = this.setInitialNodeLocationFullRegular
}
} else if (/onlyOffset/i.test(this.nodeLocationTracking)) {
if (this.recoveryEnabled) {
this.setNodeLocationFromToken = <any>setNodeLocationOnlyOffset
this.setNodeLocationFromNode = <any>setNodeLocationOnlyOffset
this.cstPostRule = noop
this.setInitialNodeLocation =
this.setInitialNodeLocationOnlyOffsetRecovery
} else {
this.setNodeLocationFromToken = noop
this.setNodeLocationFromNode = noop
this.cstPostRule = this.cstPostRuleOnlyOffset
this.setInitialNodeLocation =
this.setInitialNodeLocationOnlyOffsetRegular
}
} else if (/none/i.test(this.nodeLocationTracking)) {
this.setNodeLocationFromToken = noop
this.setNodeLocationFromNode = noop
this.cstPostRule = noop
this.setInitialNodeLocation = noop
} else {
throw Error(
`Invalid <nodeLocationTracking> config option: "${config.nodeLocationTracking}"`
)
}
}
}
setInitialNodeLocationOnlyOffsetRecovery(
this: MixedInParser,
cstNode: any
): void {
cstNode.location = {
startOffset: NaN,
endOffset: NaN
}
}
setInitialNodeLocationOnlyOffsetRegular(
this: MixedInParser,
cstNode: any
): void {
cstNode.location = {
// without error recovery the starting Location of a new CstNode is guaranteed
// To be the next Token's startOffset (for valid inputs).
// For invalid inputs there won't be any CSTOutput so this potential
// inaccuracy does not matter
startOffset: this.LA(1).startOffset,
endOffset: NaN
}
}
setInitialNodeLocationFullRecovery(this: MixedInParser, cstNode: any): void {
cstNode.location = {
startOffset: NaN,
startLine: NaN,
startColumn: NaN,
endOffset: NaN,
endLine: NaN,
endColumn: NaN
}
}
/**
* @see setInitialNodeLocationOnlyOffsetRegular for explanation why this work
* @param cstNode
*/
setInitialNodeLocationFullRegular(this: MixedInParser, cstNode: any): void {
const nextToken = this.LA(1)
cstNode.location = {
startOffset: nextToken.startOffset,
startLine: nextToken.startLine,
startColumn: nextToken.startColumn,
endOffset: NaN,
endLine: NaN,
endColumn: NaN
}
}
cstInvocationStateUpdate(this: MixedInParser, fullRuleName: string): void {
const cstNode: CstNode = {
name: fullRuleName,
children: Object.create(null)
}
this.setInitialNodeLocation(cstNode)
this.CST_STACK.push(cstNode)
}
cstFinallyStateUpdate(this: MixedInParser): void {
this.CST_STACK.pop()
}
cstPostRuleFull(this: MixedInParser, ruleCstNode: CstNode): void {
// casts to `required<CstNodeLocation>` are safe because `cstPostRuleFull` should only be invoked when full location is enabled
const prevToken = this.LA(0) as Required<CstNodeLocation>
const loc = ruleCstNode.location as Required<CstNodeLocation>
// If this condition is true it means we consumed at least one Token
// In this CstNode.
if (loc.startOffset <= prevToken.startOffset === true) {
loc.endOffset = prevToken.endOffset
loc.endLine = prevToken.endLine
loc.endColumn = prevToken.endColumn
}
// "empty" CstNode edge case
else {
loc.startOffset = NaN
loc.startLine = NaN
loc.startColumn = NaN
}
}
cstPostRuleOnlyOffset(this: MixedInParser, ruleCstNode: CstNode): void {
const prevToken = this.LA(0)
// `location' is not null because `cstPostRuleOnlyOffset` will only be invoked when location tracking is enabled.
const loc = ruleCstNode.location!
// If this condition is true it means we consumed at least one Token
// In this CstNode.
if (loc.startOffset <= prevToken.startOffset === true) {
loc.endOffset = prevToken.endOffset
}
// "empty" CstNode edge case
else {
loc.startOffset = NaN
}
}
cstPostTerminal(
this: MixedInParser,
key: string,
consumedToken: IToken
): void {
const rootCst = this.CST_STACK[this.CST_STACK.length - 1]
addTerminalToCst(rootCst, consumedToken, key)
// This is only used when **both** error recovery and CST Output are enabled.
this.setNodeLocationFromToken(rootCst.location!, <any>consumedToken)
}
cstPostNonTerminal(
this: MixedInParser,
ruleCstResult: CstNode,
ruleName: string
): void {
const preCstNode = this.CST_STACK[this.CST_STACK.length - 1]
addNoneTerminalToCst(preCstNode, ruleName, ruleCstResult)
// This is only used when **both** error recovery and CST Output are enabled.
this.setNodeLocationFromNode(preCstNode.location!, ruleCstResult.location!)
}
getBaseCstVisitorConstructor<IN = any, OUT = any>(
this: MixedInParser
): {
new (...args: any[]): ICstVisitor<IN, OUT>
} {
if (isUndefined(this.baseCstVisitorConstructor)) {
const newBaseCstVisitorConstructor = createBaseSemanticVisitorConstructor(
this.className,
keys(this.gastProductionsCache)
)
this.baseCstVisitorConstructor = newBaseCstVisitorConstructor
return newBaseCstVisitorConstructor
}
return <any>this.baseCstVisitorConstructor
}
getBaseCstVisitorConstructorWithDefaults<IN = any, OUT = any>(
this: MixedInParser
): {
new (...args: any[]): ICstVisitor<IN, OUT>
} {
if (isUndefined(this.baseCstVisitorWithDefaultsConstructor)) {
const newConstructor = createBaseVisitorConstructorWithDefaults(
this.className,
keys(this.gastProductionsCache),
this.getBaseCstVisitorConstructor()
)
this.baseCstVisitorWithDefaultsConstructor = newConstructor
return newConstructor
}
return <any>this.baseCstVisitorWithDefaultsConstructor
}
getLastExplicitRuleShortName(this: MixedInParser): number {
const ruleStack = this.RULE_STACK
return ruleStack[ruleStack.length - 1]
}
getPreviousExplicitRuleShortName(this: MixedInParser): number {
const ruleStack = this.RULE_STACK
return ruleStack[ruleStack.length - 2]
}
getLastExplicitRuleOccurrenceIndex(this: MixedInParser): number {
const occurrenceStack = this.RULE_OCCURRENCE_STACK
return occurrenceStack[occurrenceStack.length - 1]
}
}