Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80559 views
1
import {Parser} from "./state"
2
import {SourceLocation} from "./location"
3
4
// Start an AST node, attaching a start offset.
5
6
const pp = Parser.prototype
7
8
export class Node {}
9
10
pp.startNode = function() {
11
let node = new Node
12
node.start = this.start
13
if (this.options.locations)
14
node.loc = new SourceLocation(this, this.startLoc)
15
if (this.options.directSourceFile)
16
node.sourceFile = this.options.directSourceFile
17
if (this.options.ranges)
18
node.range = [this.start, 0]
19
return node
20
}
21
22
pp.startNodeAt = function(pos, loc) {
23
let node = new Node
24
if (Array.isArray(pos)){
25
if (this.options.locations && loc === undefined) {
26
// flatten pos
27
loc = pos[1]
28
pos = pos[0]
29
}
30
}
31
node.start = pos
32
if (this.options.locations)
33
node.loc = new SourceLocation(this, loc)
34
if (this.options.directSourceFile)
35
node.sourceFile = this.options.directSourceFile
36
if (this.options.ranges)
37
node.range = [pos, 0]
38
return node
39
}
40
41
// Finish an AST node, adding `type` and `end` properties.
42
43
pp.finishNode = function(node, type) {
44
node.type = type
45
node.end = this.lastTokEnd
46
if (this.options.locations)
47
node.loc.end = this.lastTokEndLoc
48
if (this.options.ranges)
49
node.range[1] = this.lastTokEnd
50
return node
51
}
52
53
// Finish node at given position
54
55
pp.finishNodeAt = function(node, type, pos, loc) {
56
node.type = type
57
if (Array.isArray(pos)){
58
if (this.options.locations && loc === undefined) {
59
// flatten pos
60
loc = pos[1]
61
pos = pos[0]
62
}
63
}
64
node.end = pos
65
if (this.options.locations)
66
node.loc.end = loc
67
if (this.options.ranges)
68
node.range[1] = pos
69
return node
70
}
71
72