forked from glayzzle/php-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
193 lines (179 loc) · 4.62 KB
/
utils.js
File metadata and controls
193 lines (179 loc) · 4.62 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* Copyright (C) 2018 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
"use strict";
module.exports = {
/**
* Reads a short form of tokens
* @param {Number} token - The ending token
* @return {Block}
*/
read_short_form: function(token) {
const body = this.node("block");
const items = [];
if (this.expect(":")) this.next();
while (this.token != this.EOF && this.token !== token) {
items.push(this.read_inner_statement());
}
if (this.expect(token)) this.next();
this.expectEndOfStatement();
return body(null, items);
},
/**
* https://wiki.php.net/rfc/trailing-comma-function-calls
* @param {*} item
* @param {*} separator
*/
read_function_list: function(item, separator) {
const result = [];
do {
if (this.token == separator && this.version >= 703 && result.length > 0) {
result.push(this.node("noop")());
break;
}
result.push(item.apply(this, []));
if (this.token != separator) {
break;
}
if (this.next().token == ")" && this.version >= 703) {
break;
}
} while (this.token != this.EOF);
return result;
},
/**
* Helper : reads a list of tokens / sample : T_STRING ',' T_STRING ...
* ```ebnf
* list ::= separator? ( item separator )* item
* ```
*/
read_list: function(item, separator, preserveFirstSeparator) {
const result = [];
if (this.token == separator) {
if (preserveFirstSeparator) {
result.push(typeof item === "function" ? this.node("noop")() : null);
}
this.next();
}
if (typeof item === "function") {
do {
const itemResult = item.apply(this, []);
if (itemResult) {
result.push(itemResult);
}
if (this.token != separator) {
break;
}
} while (this.next().token != this.EOF);
} else {
if (this.expect(item)) {
result.push(this.text());
} else {
return [];
}
while (this.next().token != this.EOF) {
if (this.token != separator) break;
// trim current separator & check item
if (this.next().token != item) break;
result.push(this.text());
}
}
return result;
},
/**
* Reads a list of names separated by a comma
*
* ```ebnf
* name_list ::= namespace (',' namespace)*
* ```
*
* Sample code :
* ```php
* <?php class foo extends bar, baz { }
* ```
*
* @see https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L726
* @return {Reference[]}
*/
read_name_list: function() {
return this.read_list(this.read_namespace_name, ",", false);
},
/**
* Reads the byref token and assign it to the specified node
* @param {*} cb
*/
read_byref: function(cb) {
let byref = this.node("byref");
this.next();
byref = byref(null);
const result = cb();
if (result) {
this.ast.swapLocations(result, byref, result, this);
result.byref = true;
}
return result;
},
/**
* Reads a list of variables declarations
*
* ```ebnf
* variable_declaration ::= T_VARIABLE ('=' expr)?*
* variable_declarations ::= variable_declaration (',' variable_declaration)*
* ```
*
* Sample code :
* ```php
* <?php static $a = 'hello', $b = 'world';
* ```
* @return {StaticVariable[]} Returns an array composed by a list of variables, or
* assign values
*/
read_variable_declarations: function() {
return this.read_list(function() {
const node = this.node("staticvariable");
let variable = this.node("variable");
// plain variable name
if (this.expect(this.tok.T_VARIABLE)) {
const name = this.text().substring(1);
this.next();
variable = variable(name, false);
} else {
variable = variable("#ERR", false);
}
if (this.token === "=") {
return node(variable, this.next().read_expr());
} else {
return variable;
}
}, ",");
},
/*
* Reads class extends
*/
read_extends_from: function() {
if (this.token === this.tok.T_EXTENDS) {
return this.next().read_namespace_name();
}
return null;
},
/*
* Reads interface extends list
*/
read_interface_extends_list: function() {
if (this.token === this.tok.T_EXTENDS) {
return this.next().read_name_list();
}
return null;
},
/*
* Reads implements list
*/
read_implements_list: function() {
if (this.token === this.tok.T_IMPLEMENTS) {
return this.next().read_name_list();
}
return null;
}
};