Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: invalid parsing of traitalias #402

Open
wants to merge 1 commit into
base: prettier
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/ast/traitalias.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,26 @@ module.exports = Node.extends(KIND, function TraitAlias(
trait,
method,
as,
flags,
visibility,
docs,
location
) {
Node.apply(this, [KIND, docs, location]);
this.trait = trait;
this.method = method;
this.as = as;
this.visibility = IS_UNDEFINED;
if (flags) {
if (flags[0] === 0) {

switch (visibility) {
case 0:
this.visibility = IS_PUBLIC;
} else if (flags[0] === 1) {
break;
case 1:
this.visibility = IS_PROTECTED;
} else if (flags[0] === 2) {
break;
case 2:
this.visibility = IS_PRIVATE;
}
break;
default:
this.visibility = IS_UNDEFINED;
}
});
107 changes: 81 additions & 26 deletions src/parser/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,40 @@ module.exports = {

return result(null, items, flags);
},

read_member_modifier: function() {
let modifier;

switch (this.token) {
case this.tok.T_PUBLIC:
modifier = 0;
break;
case this.tok.T_PROTECTED:
modifier = 1;
break;
case this.tok.T_PRIVATE:
modifier = 2;
break;
case this.tok.T_STATIC:
modifier = 3;
break;
case this.tok.T_ABSTRACT:
modifier = 4;
break;
case this.tok.T_FINAL:
modifier = 5;
break;
default: {
const err = this.error("T_MEMBER_FLAGS");
this.next();
return err;
}
}

this.next();
return modifier;
},

/**
* Read member flags
* @return array
Expand All @@ -213,31 +247,34 @@ module.exports = {
read_member_flags: function(asInterface) {
const result = [-1, -1, -1];
if (this.is("T_MEMBER_FLAGS")) {
let idx = 0,
val = 0;
do {
switch (this.token) {
case this.tok.T_PUBLIC:
let idx = 0;
let val = 0;

const visibility = this.read_member_modifier();

switch (visibility) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need refactor too, let's do it in next PRs

case 0:
idx = 0;
val = 0;
break;
case this.tok.T_PROTECTED:
case 1:
idx = 0;
val = 1;
break;
case this.tok.T_PRIVATE:
case 2:
idx = 0;
val = 2;
break;
case this.tok.T_STATIC:
case 3:
idx = 1;
val = 1;
break;
case this.tok.T_ABSTRACT:
case 4:
idx = 2;
val = 1;
break;
case this.tok.T_FINAL:
case 5:
idx = 2;
val = 2;
break;
Expand All @@ -259,7 +296,7 @@ module.exports = {
} else if (val !== -1) {
result[idx] = val;
}
} while (this.next().is("T_MEMBER_FLAGS"));
} while (this.is("T_MEMBER_FLAGS"));
}

if (result[1] == -1) result[1] = 0;
Expand Down Expand Up @@ -372,18 +409,18 @@ module.exports = {
const node = this.node("traituse");
this.expect(this.tok.T_USE) && this.next();
const traits = [this.read_namespace_name()];
let adaptations = null;
while (this.token === ",") {
traits.push(this.next().read_namespace_name());
}
const adaptations = this.read_trait_adaptations();
return node(traits, adaptations);
},

read_trait_adaptations: function() {
let adaptations = null;

if (this.token === "{") {
adaptations = [];
// defines alias statements
while (this.next().token !== this.EOF) {
if (this.token === "}") break;
adaptations.push(this.read_trait_use_alias());
this.expect(";");
}
adaptations = this.read_trait_adaptation_list();
if (this.expect("}")) {
this.next();
}
Expand All @@ -392,17 +429,34 @@ module.exports = {
this.next();
}
}
return node(traits, adaptations);

return adaptations;
},

/*
* Reads trait adaptation list
*/
read_trait_adaptation_list: function() {
let adaptations = [];
// defines alias statements
while (this.next().token !== this.EOF) {
if (this.token === "}") break;
adaptations.push(this.read_trait_adaptation());
this.expect(";");
}

return adaptations;
},

/**
* Reading trait alias
* Reading trait adaptation
* ```ebnf
* trait_use_alias ::= namespace_name ( T_DOUBLE_COLON T_STRING )? (T_INSTEADOF namespace_name) | (T_AS member_flags? T_STRING)
* ```
* name list : https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L303
* trait adaptation : https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L742
*/
read_trait_use_alias: function() {
read_trait_adaptation: function() {
const node = this.node();
let trait = null;
let method;
Expand Down Expand Up @@ -445,10 +499,11 @@ module.exports = {
);
} else if (this.token === this.tok.T_AS) {
// handle trait alias
let flags = null;
let visibility = null;
let alias = null;
if (this.next().is("T_MEMBER_FLAGS")) {
flags = this.read_member_flags();
this.next();
if (this.is("T_MEMBER_FLAGS")) {
visibility = this.read_member_modifier();
}

if (
Expand All @@ -459,12 +514,12 @@ module.exports = {
const name = this.text();
this.next();
alias = alias(name);
} else if (flags === false) {
} else if (visibility === false) {
// no visibility flags and no name => too bad
this.expect(this.tok.T_STRING);
}

return node("traitalias", trait, method, alias, flags);
return node("traitalias", trait, method, alias, visibility);
}

// handle errors
Expand Down
4 changes: 2 additions & 2 deletions test/snapshot/__snapshots__/graceful.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,8 @@ Program {
"expected": undefined,
"kind": "error",
"line": 3,
"message": "Parse Error : syntax error, unexpected 'abstract' (T_ABSTRACT) on line 3",
"token": "'abstract' (T_ABSTRACT)",
"message": "Parse Error : syntax error, unexpected 'function' (T_FUNCTION) on line 3",
"token": "'function' (T_FUNCTION)",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected because it is not grammatical problem, i think we should remove this check in future, we are parser not virtual machine for execution, so all runtime errors should be parsable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @evilebottnawi,

I'm not agree with you - with this change you validate every invalid PHP token that is checked on runtime level over the AST.

Take this example : interface foo { abstract function bar(); } - in PHP it will allways trigger an fatal error.

I thing we should consider the 2 approaches :

1- first would be to parse the grammar as you indicate and forget runtime checks. That would imply you can not rely on the library to check the PHP syntax, and we would need an external tool for linting the resulting AST (including PHP runtime checks)

2- or handle that kind of problem directly into the parser as it was done since, and use a gracefull mode to ignore any syntax error

As nobody wants to handle broken PHP syntax, and if you need to parse it anyway you have a graceful flag for this, I prefer to keep extra runtime checks on the parser level.

The reason for me it's about syntax rules - it has no meaning to have an interface with an abstract function, PHP does not permit it anyway.

I think they gave an incomplete or simplified version of the grammar (due to simplifications or technical issues) so PHP needs extra check on runtime to filter syntax errors.

By not filtering every syntax errors (based on php documentation and not only on grammar file) you have a sort of graceful mode on various grammar problems, only justified by implementation choices of Zend team...

Lets do it like before, it's not about VM, it's about valid syntax or not, if it's not, you need to trigger an error

},
Error {
"expected": ";",
Expand Down
Loading