forked from glayzzle/php-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.js
More file actions
105 lines (102 loc) · 2.43 KB
/
array.js
File metadata and controls
105 lines (102 loc) · 2.43 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
/**
* Copyright (C) 2018 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
"use strict";
const ArrayExpr = "array";
const ArrayEntry = "entry";
module.exports = {
/**
* Parse an array
* ```ebnf
* array ::= T_ARRAY '(' array_pair_list ')' |
* '[' array_pair_list ']'
* ```
*/
read_array: function() {
let expect = null;
let shortForm = false;
const result = this.node(ArrayExpr);
if (this.token === this.tok.T_ARRAY) {
this.next().expect("(");
expect = ")";
} else {
shortForm = true;
expect = "]";
}
let items = [];
if (this.next().token !== expect) {
items = this.read_array_pair_list(shortForm);
}
// check non empty entries
/*for(let i = 0, size = items.length - 1; i < size; i++) {
if (items[i] === null) {
this.raiseError(
"Cannot use empty array elements in arrays"
);
}
}*/
this.expect(expect);
this.next();
return result(shortForm, items);
},
/**
* Reads an array of items
* ```ebnf
* array_pair_list ::= array_pair (',' array_pair?)*
* ```
*/
read_array_pair_list: function(shortForm) {
const self = this;
return this.read_list(
function() {
return self.read_array_pair(shortForm);
},
",",
true
);
},
/**
* Reads an entry
* array_pair:
* expr T_DOUBLE_ARROW expr
* | expr
* | expr T_DOUBLE_ARROW '&' variable
* | '&' variable
* | expr T_DOUBLE_ARROW T_LIST '(' array_pair_list ')'
* | T_LIST '(' array_pair_list ')'
*/
read_array_pair: function(shortForm) {
if (
this.token === "," ||
(!shortForm && this.token === ")") ||
(shortForm && this.token === "]")
) {
return null;
}
if (this.token === "&") {
return this.next().read_variable(true, false, true);
} else {
const entry = this.node(ArrayEntry);
const expr = this.read_expr();
if (this.token === this.tok.T_DOUBLE_ARROW) {
if (this.next().token === "&") {
return entry(expr, this.next().read_variable(true, false, true));
} else {
return entry(expr, this.read_expr());
}
}
return expr;
}
},
/**
* ```ebnf
* dim_offset ::= expr?
* ```
*/
read_dim_offset: function() {
if (this.token == "]") return false;
return this.read_expr();
}
};