forked from glayzzle/php-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.js
More file actions
69 lines (64 loc) · 1.57 KB
/
node.js
File metadata and controls
69 lines (64 loc) · 1.57 KB
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
/**
* Copyright (C) 2018 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
"use strict";
/**
* A generic AST node
* @constructor Node
* @property {Location|null} loc
* @property {Comment[]} leadingComments
* @property {Comment[]?} trailingComments
* @property {String} kind
*/
const Node = function Node(kind, docs, location) {
this.kind = kind;
if (docs) {
this.leadingComments = docs;
}
if (location) {
this.loc = location;
}
};
/**
* Attach comments to current node
* @param {*} docs
*/
Node.prototype.setTrailingComments = function(docs) {
this.trailingComments = docs;
};
/**
* Includes current token position of the parser
* @param {*} parser
*/
Node.prototype.includeToken = function(parser) {
if (this.loc) {
if (this.loc.end) {
this.loc.end.line = parser.lexer.yylloc.last_line;
this.loc.end.column = parser.lexer.yylloc.last_column;
this.loc.end.offset = parser.lexer.offset;
}
if (parser.ast.withSource) {
this.loc.source = parser.lexer._input.substring(
this.loc.start.offset,
parser.lexer.offset
);
}
}
return this;
};
/**
* Helper for extending the Node class
* @param {String} type
* @param {Function} constructor
* @return {Function}
*/
Node.extends = function(type, constructor) {
constructor.prototype = Object.create(this.prototype);
constructor.extends = this.extends;
constructor.prototype.constructor = constructor;
constructor.kind = type;
return constructor;
};
module.exports = Node;