Upgrade to https://github.com/acornjs/acorn.git commit 84eda6bf
[jst.git] / src / node.js
1 import {Parser} from "./state.js"
2 import {SourceLocation} from "./locutil.js"
3
4 export class Node {
5   constructor(parser, pos, loc) {
6     this.type = ""
7     this.start = pos
8     this.end = 0
9     if (parser.options.locations)
10       this.loc = new SourceLocation(parser, loc)
11     if (parser.options.directSourceFile)
12       this.sourceFile = parser.options.directSourceFile
13     if (parser.options.ranges)
14       this.range = [pos, 0]
15   }
16 }
17
18 // Start an AST node, attaching a start offset.
19
20 const pp = Parser.prototype
21
22 pp.startNode = function() {
23   return new Node(this, this.start, this.startLoc)
24 }
25
26 pp.startNodeAt = function(pos, loc) {
27   return new Node(this, pos, loc)
28 }
29
30 // Finish an AST node, adding `type` and `end` properties.
31
32 function finishNodeAt(node, type, pos, loc) {
33   node.type = type
34   node.end = pos
35   if (this.options.locations)
36     node.loc.end = loc
37   if (this.options.ranges)
38     node.range[1] = pos
39   return node
40 }
41
42 pp.finishNode = function(node, type) {
43   return finishNodeAt.call(this, node, type, this.lastTokEnd, this.lastTokEndLoc)
44 }
45
46 // Finish node at given position
47
48 pp.finishNodeAt = function(node, type, pos, loc) {
49   return finishNodeAt.call(this, node, type, pos, loc)
50 }
51
52 pp.copyNode = function(node) {
53   let newNode = new Node(this, node.start, this.startLoc)
54   for (let prop in node) newNode[prop] = node[prop]
55   return newNode
56 }