Skip to content

Commit cdb9f42

Browse files
committed
fix & test simple_variable parsing
1 parent a874732 commit cdb9f42

File tree

6 files changed

+92
-16
lines changed

6 files changed

+92
-16
lines changed

src/parser/variable.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,30 +233,34 @@ module.exports = {
233233
var result = this.node('variable');
234234
if (this.expect([this.tok.T_VARIABLE, '$']) && this.token === this.tok.T_VARIABLE) {
235235
// plain variable name
236-
var name = this.text();
236+
var name = this.text().substring(1);
237237
this.next();
238238
result = result(name, byref);
239239
} else {
240240
if (this.token === '$') this.next();
241241
// dynamic variable name
242242
switch(this.token) {
243243
case '{':
244-
result = this.next().read_expr();
244+
var expr = this.next().read_expr();
245245
this.expect('}') && this.next();
246+
result = result(expr, byref);
246247
break;
247248
case '$': // $$$var
248-
result = ['lookup', 'var', this.read_simple_variable(false)];
249+
result = result(this.read_simple_variable(false), byref);
249250
break;
250251
case this.tok.T_VARIABLE: // $$var
251-
result = ['var', this.text()];
252+
var name = this.text().substring(1);
253+
var node = this.node('variable');
252254
this.next();
255+
result = result(node(name, false), byref);
253256
break;
254257
default:
255-
result = this.error(['{', '$', this.tok.T_VARIABLE]);
258+
this.error(['{', '$', this.tok.T_VARIABLE]);
256259
// graceful mode
260+
var name = this.text();
257261
this.next();
262+
result = result(name, byref);
258263
}
259-
result = ['lookup', 'var', result];
260264
}
261265
return result;
262266
}

test/arrayTests.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ describe('Array without keys', function() {
9292

9393
it('should have correct item types and values', function () {
9494
ast.children[0].items[0].value.kind.should.be.exactly("variable");
95-
ast.children[0].items[0].value.name.should.be.exactly("$obj1");
95+
ast.children[0].items[0].value.name.should.be.exactly("obj1");
9696

9797
ast.children[0].items[1].value.kind.should.be.exactly("variable");
98-
ast.children[0].items[1].value.name.should.be.exactly("$obj2");
98+
ast.children[0].items[1].value.name.should.be.exactly("obj2");
9999

100100
ast.children[0].items[2].value.kind.should.be.exactly("variable");
101-
ast.children[0].items[2].value.name.should.be.exactly("$obj3");
101+
ast.children[0].items[2].value.name.should.be.exactly("obj3");
102102
});
103103
});
104104

@@ -171,10 +171,10 @@ describe('Array without keys', function() {
171171
ast.children[0].items[0].key.kind.should.be.exactly('number');
172172
ast.children[0].items[0].value.kind.should.be.exactly('variable');
173173
ast.children[0].items[0].value.byref.should.be.exactly(true);
174-
ast.children[0].items[0].value.name.should.be.exactly('$foo');
174+
ast.children[0].items[0].value.name.should.be.exactly('foo');
175175
ast.children[0].items[0].value.byref.should.be.exactly(true);
176176
ast.children[0].items[1].key.kind.should.be.exactly('variable');
177-
ast.children[0].items[1].key.name.should.be.exactly('$bar');
177+
ast.children[0].items[1].key.name.should.be.exactly('bar');
178178
ast.children[0].items[1].key.byref.should.be.exactly(false);
179179
});
180180

test/loopTests.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ describe('Test loops statements (for, while)', function() {
6767
});
6868
it('test init args', function() {
6969
ast.children[0].init.length.should.be.exactly(2);
70-
ast.children[0].init[0].left.name.should.be.exactly('$i');
71-
ast.children[0].init[1].left.name.should.be.exactly('$b');
70+
ast.children[0].init[0].left.name.should.be.exactly('i');
71+
ast.children[0].init[1].left.name.should.be.exactly('b');
7272
ast.children[1].init.length.should.be.exactly(0);
7373
});
7474
it('check test args', function() {
@@ -105,7 +105,7 @@ describe('Test loops statements (for, while)', function() {
105105
it('test source', function() {
106106
ast.children[0].source.kind.should.be.exactly('variable');
107107
ast.children[0].source.byref.should.be.exactly(true);
108-
ast.children[0].source.name.should.be.exactly('$foo');
108+
ast.children[0].source.name.should.be.exactly('foo');
109109
ast.children[1].source.kind.should.be.exactly('array');
110110
ast.children[1].source.items.length.should.be.exactly(2);
111111
});
@@ -116,7 +116,7 @@ describe('Test loops statements (for, while)', function() {
116116
});
117117
it('test value', function() {
118118
ast.children[0].value.kind.should.be.exactly('variable');
119-
ast.children[0].value.name.should.be.exactly('$v');
119+
ast.children[0].value.name.should.be.exactly('v');
120120
ast.children[1].value.kind.should.be.exactly('array');
121121
ast.children[1].value.shortForm.should.be.exactly(true);
122122
});

test/main.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*!
2+
* Defines a list of helpers for tests
3+
* Copyright (C) 2017 Glayzzle (BSD3 License)
4+
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
5+
* @url http://glayzzle.com
6+
*/
7+
8+
var parser = require('../src/index');
9+
// @todo : move here the debug code
10+
// @todo : add an automated token check (with php7)
11+
module.exports = parser;

test/statementTests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('Test statements', function() {
3737
ast.children[0].catches[0].what[0].name.should.be.exactly('FooError');
3838
ast.children[0].catches[0].what[1].name.should.be.exactly('BarError');
3939
ast.children[0].catches[0].variable.kind.should.be.exactly('variable');
40-
ast.children[0].catches[0].variable.name.should.be.exactly('$err');
40+
ast.children[0].catches[0].variable.name.should.be.exactly('err');
4141
ast.children[0].always.kind.should.be.exactly('block');
4242
});
4343
});

test/variableTests.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
var should = require("should");
2+
var parser = require('./main');
3+
4+
describe('Test variables', function() {
5+
describe('Default variables', function() {
6+
var ast = parser.parseEval([
7+
'$a = "foo";',
8+
'$b = &$c;'
9+
].join('\n'));
10+
it('should be $a', function() {
11+
ast.children[0].left.kind.should.be.exactly('variable');
12+
ast.children[0].left.name.should.be.exactly('a');
13+
});
14+
it('should be $c byref', function() {
15+
ast.children[1].right.kind.should.be.exactly('variable');
16+
ast.children[1].right.name.should.be.exactly('c');
17+
ast.children[1].right.byref.should.be.exactly(true);
18+
});
19+
});
20+
21+
describe('Dynamic variables', function() {
22+
var ast = parser.parseEval([
23+
'$$a = "bar";',
24+
'$$$a = "bar";',
25+
'${$a."bar"} = "bar";'
26+
].join('\n'));
27+
it('should be $$a', function() {
28+
ast.children[0].left.kind.should.be.exactly('variable');
29+
ast.children[0].left.name.kind.should.be.exactly('variable');
30+
ast.children[0].left.name.name.should.be.exactly('a');
31+
});
32+
it('should be $$$a', function() {
33+
ast.children[1].left.kind.should.be.exactly('variable');
34+
ast.children[1].left.name.kind.should.be.exactly('variable');
35+
ast.children[1].left.name.name.kind.should.be.exactly('variable');
36+
});
37+
it('should be ${$a."bar"}', function() {
38+
ast.children[2].left.kind.should.be.exactly('variable');
39+
ast.children[2].left.name.kind.should.be.exactly('bin');
40+
ast.children[2].left.name.type.should.be.exactly('.');
41+
ast.children[2].left.name.left.kind.should.be.exactly('variable');
42+
ast.children[2].left.name.left.name.should.be.exactly('a');
43+
ast.children[2].left.name.right.kind.should.be.exactly('string');
44+
ast.children[2].left.name.right.value.should.be.exactly('bar');
45+
});
46+
});
47+
48+
describe('Check errors', function() {
49+
var ast = parser.parseEval([
50+
'$? = true;'
51+
].join('\n'), {
52+
parser: {
53+
suppressErrors: true
54+
}
55+
});
56+
it('should be ?', function() {
57+
ast.children[0].left.kind.should.be.exactly('variable');
58+
ast.children[0].left.name.should.be.exactly('?');
59+
});
60+
});
61+
});

0 commit comments

Comments
 (0)