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
89 lines (86 loc) · 1.95 KB
/
array.js
File metadata and controls
89 lines (86 loc) · 1.95 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
/*!
* Copyright (C) 2017 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
var ArrayExpr = 'array';
var ArrayEntry = 'entry';
module.exports = {
/**
* Parse an array
* ```ebnf
* array ::= T_ARRAY '(' array_pair_list ')' |
* '[' array_pair_list ']'
* ```
*/
read_array: function() {
var expect = null;
var shortForm = false;
var items = [];
var result = this.node(ArrayExpr);
if (this.token === this.tok.T_ARRAY) {
this.next().expect('(');
expect = ')';
} else {
shortForm = true;
expect = ']';
}
if (this.next().token != expect) {
while(this.token != this.EOF) {
items.push(this.read_array_pair_list());
if (this.token == ',') {
this.next();
if (this.token === expect) {
break;
}
} else break;
}
}
this.expect(expect);
this.next();
return result(shortForm, items);
},
/**
* Reads an array entry item
* ```ebnf
* array_pair_list ::= '&' w_variable |
* (
* expr (
* T_DOUBLE_ARROW (
* expr | '&' w_variable
* )
* )?
* )
* ```
*/
read_array_pair_list: function() {
var result = this.node(ArrayEntry);
var key = null;
var value = null;
if (this.token === '&') {
value = this.next().read_variable(true, false, true);
} else {
var expr = this.read_expr();
if (this.token === this.tok.T_DOUBLE_ARROW) {
key = expr;
if (this.next().token === '&') {
value = this.next().read_variable(true, false, true);
} else {
value = this.read_expr();
}
} else {
value = expr;
}
}
return result(key, value);
},
/**
* ```ebnf
* dim_offset ::= expr?
* ```
*/
read_dim_offset: function() {
if (this.token == ']') return false;
return this.read_expr();
}
};