Skip to content

Commit a6519fd

Browse files
committed
glayzzle#83 : fixed list & short arrays syntax resolution
1 parent 41dee02 commit a6519fd

File tree

10 files changed

+966
-598
lines changed

10 files changed

+966
-598
lines changed

src/parser/array.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ module.exports = {
3333
items = this.read_array_pair_list(shortForm);
3434
}
3535
// check non empty entries
36-
for(let i = 0, size = items.length - 1; i < size; i++) {
36+
/*for(let i = 0, size = items.length - 1; i < size; i++) {
3737
if (items[i] === null) {
3838
this.raiseError(
3939
"Cannot use empty array elements in arrays"
4040
);
4141
}
42-
}
42+
}*/
4343
this.expect(expect);
4444
this.next();
4545
return result(shortForm, items);

src/parser/expr.js

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -129,71 +129,55 @@ module.exports = {
129129
return this.next().read_encapsed_string("`");
130130
}
131131

132-
if (this.token === this.tok.T_LIST || this.token === "[") {
132+
if (this.token === this.tok.T_LIST) {
133133
let assign = null;
134-
const isShort = this.token === "[";
135134
const isInner = this.innerList;
136135
result = this.node("list");
137136
if (!isInner) {
138-
this.innerListForm = isShort;
139137
assign = this.node("assign");
140-
} else if (this.innerListForm !== isShort) {
141-
// Both due to implementation issues,
142-
// and for consistency's sake, list()
143-
// cannot be nested inside [], nor vice-versa
144-
this.expect(isShort ? "list" : "[");
145138
}
146-
this.next();
147-
if (!isShort && this.expect("(")) {
139+
if (this.next().expect("(")) {
148140
this.next();
149141
}
150142

151143
if (!this.innerList) this.innerList = true;
152144

153145
// reads inner items
154-
const assignList = this.read_array_pair_list(isShort);
155-
if (this.expect(isShort ? "]" : ")")) {
146+
const assignList = this.read_array_pair_list(false);
147+
if (this.expect(")")) {
156148
this.next();
157149
}
158150

159151
// check if contains at least one assignment statement
160-
if (!isShort) {
161-
let hasItem = false;
162-
for (let i = 0; i < assignList.length; i++) {
163-
if (assignList[i] !== null) {
164-
hasItem = true;
165-
break;
166-
}
167-
}
168-
if (!hasItem) {
169-
this.raiseError(
170-
"Fatal Error : Cannot use empty list on line " +
171-
this.lexer.yylloc.first_line
172-
);
152+
let hasItem = false;
153+
for (let i = 0; i < assignList.length; i++) {
154+
if (assignList[i] !== null) {
155+
hasItem = true;
156+
break;
173157
}
174158
}
159+
if (!hasItem) {
160+
this.raiseError(
161+
"Fatal Error : Cannot use empty list on line " +
162+
this.lexer.yylloc.first_line
163+
);
164+
}
175165

176166
// handles the node resolution
177167
if (!isInner) {
178168
this.innerList = false;
179-
if (isShort) {
180-
if (this.token === '=') {
181-
return assign(result(assignList, isShort), this.next().read_expr(), "=");
182-
} else {
183-
// handles as an array declaration
184-
result.setKind('array');
185-
return this.handleDereferencable(result(isShort, assignList));
186-
}
169+
if (this.expect("=")) {
170+
return assign(
171+
result(assignList, false),
172+
this.next().read_expr(),
173+
"="
174+
);
187175
} else {
188-
if (this.expect("=")) {
189-
return assign(result(assignList, isShort), this.next().read_expr(), "=");
190-
} else {
191-
// error fallback : list($a, $b);
192-
return result(assignList, isShort);
193-
}
176+
// error fallback : list($a, $b);
177+
return result(assignList, false);
194178
}
195179
} else {
196-
return result(assignList, isShort);
180+
return result(assignList, false);
197181
}
198182
}
199183

@@ -428,7 +412,16 @@ module.exports = {
428412
return result("post", "-", expr);
429413
}
430414
} else if (this.is("SCALAR")) {
415+
result = this.node();
431416
expr = this.read_scalar();
417+
if (expr.kind === 'array' && expr.shortForm && this.token === '=') {
418+
// list assign
419+
let list = this.node('list')(expr.items, true);
420+
if (expr.loc) list.loc = expr.loc;
421+
let right = this.next().read_expr();
422+
return result("assign", list, right, "=");
423+
}
424+
// classic array
432425
return this.handleDereferencable(expr);
433426
} else {
434427
this.error("EXPR");

src/parser/scalar.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ module.exports = {
133133

134134
// ARRAYS
135135
case this.tok.T_ARRAY: // array parser
136+
return this.read_array();
136137
case "[": // short array format
137138
return this.read_array();
138139
default: {

test/snapshot/__snapshots__/acid.test.js.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6364,8 +6364,8 @@ next:
63646364
},
63656365
"value": "1",
63666366
},
6367-
List {
6368-
"arguments": Array [
6367+
Array {
6368+
"items": Array [
63696369
Number {
63706370
"kind": "number",
63716371
"loc": Location {
@@ -6401,7 +6401,7 @@ next:
64016401
"value": "3",
64026402
},
64036403
],
6404-
"kind": "list",
6404+
"kind": "array",
64056405
"loc": Location {
64066406
"end": Position {
64076407
"column": 38,
@@ -6459,8 +6459,8 @@ next:
64596459
},
64606460
"name": "a",
64616461
},
6462-
List {
6463-
"arguments": Array [
6462+
Array {
6463+
"items": Array [
64646464
Variable {
64656465
"byref": false,
64666466
"curly": false,
@@ -6501,7 +6501,7 @@ next:
65016501
"name": "c",
65026502
},
65036503
],
6504-
"kind": "list",
6504+
"kind": "array",
65056505
"loc": Location {
65066506
"end": Position {
65076507
"column": 16,

test/snapshot/__snapshots__/array.test.js.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ Program {
9999
},
100100
"operator": "=",
101101
"right": Array {
102-
"items": Array [
103-
null,
104-
],
102+
"items": Array [],
105103
"kind": "array",
106104
"shortForm": true,
107105
},

0 commit comments

Comments
 (0)