Skip to content

Commit

Permalink
Merge pull request #93 from bbtgp/static-lookup-property
Browse files Browse the repository at this point in the history
Added support for static lookup on property
  • Loading branch information
ichiriac authored Nov 1, 2017
2 parents c4df2b8 + 1368751 commit 20b8234
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/parser/variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,25 @@ module.exports = {
}
result = node(result, offset);
break;

case this.tok.T_DOUBLE_COLON:
var node = this.node('staticlookup'), offset;
this.next();

if(this.is('IDENTIFIER') || this.token === this.tok.T_STRING
|| this.token === this.tok.T_CLASS
) {
offset = this.node('constref');
var name = this.text();
this.next();
offset = offset(name);

if(this.token === this.tok.T_OBJECT_OPERATOR || this.token === this.tok.T_DOUBLE_COLON) {
this.error();
}
}

result = node(result, offset);
break;
case this.tok.T_OBJECT_OPERATOR:
var node = this.node('propertylookup');
var what = null;
Expand Down
58 changes: 57 additions & 1 deletion test/variableTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ describe('Test variables', function() {
'parent::foo();',
'foo::class;',
'$this->foo();',
'foo::$bar;'
'foo::$bar;',
'$this->foo::bar["baz"]::qux();'
].join('\n'), {
parser: {
// debug: true
Expand Down Expand Up @@ -104,6 +105,30 @@ describe('Test variables', function() {
expr.offset.kind.should.be.exactly('variable');
expr.offset.name.should.be.exactly('bar');
});
it('should be $this->foo::bar["baz"]::qux();', function() {
var expr = ast.children[6];

expr.kind.should.be.exactly("call");
expr.arguments.length.should.be.exactly(0);

expr.what.kind.should.be.exactly("staticlookup");
expr.what.offset.kind.should.be.exactly("constref");
expr.what.offset.name.should.be.exactly("qux");

expr.what.what.kind.should.be.exactly("offsetlookup");
expr.what.what.offset.kind.should.be.exactly("string");
expr.what.what.offset.value.should.be.exactly("baz");

expr.what.what.what.kind.should.be.exactly("staticlookup");
expr.what.what.what.offset.kind.should.be.exactly("constref");
expr.what.what.what.offset.name.should.be.exactly("bar");

expr.what.what.what.what.kind.should.be.exactly("propertylookup");
expr.what.what.what.what.what.kind.should.be.exactly("variable");
expr.what.what.what.what.what.name.should.be.exactly("this");
expr.what.what.what.what.offset.kind.should.be.exactly("constref");
expr.what.what.what.what.offset.name.should.be.exactly("foo");
});
});


Expand Down Expand Up @@ -163,5 +188,36 @@ describe('Test variables', function() {
ast.children[0].left.kind.should.be.exactly('variable');
ast.children[0].left.name.should.be.exactly('?');
});

it('should fail on double static lookup', function() {
var astErr = parser.parseEval([
'this->foo::bar::baz;'
].join('\n'), {
parser: {
suppressErrors: true
}
});

var msg = 'Parse Error : syntax error, unexpected \'::\' (T_DOUBLE_COLON) on line 1';
astErr.errors.length.should.be.exactly(1);
astErr.errors[0].line.should.be.exactly(1);
astErr.errors[0].message.should.be.exactly(msg);
});

it('should fail on property lookup on static lookup', function() {
var astErr = parser.parseEval([
'this->foo::bar->baz;'
].join('\n'), {
parser: {
suppressErrors: true
}
});

var msg = 'Parse Error : syntax error, unexpected \'->\' (T_OBJECT_OPERATOR) on line 1';
astErr.errors.length.should.be.exactly(1);
astErr.errors[0].line.should.be.exactly(1);
astErr.errors[0].message.should.be.exactly(msg);
});

});
});

0 comments on commit 20b8234

Please sign in to comment.