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
110 lines (104 loc) · 2.58 KB
/
node.js
File metadata and controls
110 lines (104 loc) · 2.58 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
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
/**
* 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
* @memberOf module:php-parser
* @property {Location|null} loc
* @property {CommentBlock[]|Comment[]|null} leadingComments
* @property {CommentBlock[]|Comment[]|null} 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
* @function Node#setTrailingComments
* @memberOf module:php-parser
* @param {*} docs
*/
Node.prototype.setTrailingComments = function (docs) {
this.trailingComments = docs;
};
/**
* Destroying an unused node
* @function Node#destroy
* @memberOf module:php-parser
*/
Node.prototype.destroy = function (node) {
if (!node) {
throw new Error(
"Node already initialized, you must swap with another node"
);
}
if (this.leadingComments) {
if (node.leadingComments) {
node.leadingComments = Array.concat(
this.leadingComments,
node.leadingComments
);
} else {
node.leadingComments = this.leadingComments;
}
}
if (this.trailingComments) {
if (node.trailingComments) {
node.trailingComments = Array.concat(
this.trailingComments,
node.trailingComments
);
} else {
node.trailingComments = this.trailingComments;
}
}
return node;
};
/**
* Includes current token position of the parser
* @function Node#includeToken
* @memberOf module:php-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
* @function Node.extends
* @memberOf module:php-parser
* @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;