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
120 lines (113 loc) · 3.03 KB
/
utils.js
File metadata and controls
120 lines (113 loc) · 3.03 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
/*!
* Defines a list of helper functions for parsing
* Copyright (C) 2017 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) {
var body = this.node('block'), 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);
}
/**
* Helper : reads a list of tokens / sample : T_STRING ',' T_STRING ...
* ```ebnf
* list ::= separator? ( item separator )* item
* ```
*/
,read_list: function(item, separator, preserveFirstSeparator) {
var result = [];
if (this.token == separator) {
if (preserveFirstSeparator) result.push('');
this.next();
}
if (typeof (item) === "function") {
do {
result.push(item.apply(this, []));
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 {Identifier[]}
*/
,read_name_list: function() {
return this.read_list(
this.read_namespace_name, ',', false
);
}
/**
* Reads a list of variables declarations
*
* ```ebnf
* variable_declaration ::= T_VARIABLE ('=' expr)?*
* variable_declarations ::= variable_declaration (',' variable_declaration)*
* ```
*
* Sample code :
* ```php
* <?php class foo extends bar, baz { }
* ```
* @return {Variable[]|Assign[]} Returns an array composed by a list of variables, or
* assign values
*/
,read_variable_declarations: function() {
return this.read_list(function() {
var node = this.node('assign'),
value = null,
variable = this.node('variable');
// plain variable name
if (this.expect(this.tok.T_VARIABLE)) {
var name = this.text().substring(1);
this.next();
variable = variable(name, false, false);
} else {
variable = variable('#ERR', false, false);
}
if (this.token === '=') {
return node(variable, this.next().read_expr());
} else {
return variable;
}
}, ',');
}
};