Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80744 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
node.start = pos
25
if (this.options.locations)
26
node.loc = new SourceLocation(this, loc)
27
if (this.options.directSourceFile)
28
node.sourceFile = this.options.directSourceFile
29
if (this.options.ranges)
30
node.range = [pos, 0]
31
return node
32
}
33
34
// Finish an AST node, adding `type` and `end` properties.
35
36
pp.finishNode = function(node, type) {
37
node.type = type
38
node.end = this.lastTokEnd
39
if (this.options.locations)
40
node.loc.end = this.lastTokEndLoc
41
if (this.options.ranges)
42
node.range[1] = this.lastTokEnd
43
return node
44
}
45
46
// Finish node at given position
47
48
pp.finishNodeAt = function(node, type, pos, loc) {
49
node.type = type
50
node.end = pos
51
if (this.options.locations)
52
node.loc.end = loc
53
if (this.options.ranges)
54
node.range[1] = pos
55
return node
56
}
57
58