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

Any plans for an official API to convert from Microsoft\PHPParser\Node to \ast\Node (nikic/php-ast) and/or \PhpParser\Node (nikic/php-parser)? #113

Closed
TysonAndre opened this issue Feb 26, 2017 · 4 comments

Comments

@TysonAndre
Copy link
Contributor

Related to #47 and #36 , didn't see anything mentioning this when searching issues.

A lot of existing tooling uses one of these two parsers, and there are potential use cases for being able to parse syntactically invalid files.

Examples of potential use cases:

  • print code style issues in addition to syntax errors, without a developer having to fix the file and run a check again. (e.g. phpcs?)
  • other static analysis tools being able to check for issues such as undefined methods, calling functions with invalid param types, etc. in other parts of syntactically invalid files
  • other code for autocompletion based on nikic/php-parser

Or, do you recommend creating a separate project depending on tolerant-php-parser?


Aside: if existing tooling needs to know something about any incomplete expressions/statements, adding a property such as $node->tolerantAST to the derived data structures may help

@mousetraps
Copy link
Contributor

Good question. No official plans at the moment, but we're currently working with @felixfbecker to port php-language-server to this parser, and after that work is complete, it will be easier for us to understand the tradeoffs of such an approach.

@TysonAndre
Copy link
Contributor Author

but we're currently working with @felixfbecker to port php-language-server to this parser

php-language-server has been ported, it seems.

On an unrelated note, I've been working on https://github.com/TysonAndre/php-parser-to-php-ast/ for converting PHP-Parser to php-ast (tests pass, but there's probably some uncommon syntax I've missed), which would help with converting to php-ast if tolerant-php-parser to PHP-Parser conversion existed.

@TysonAndre
Copy link
Contributor Author

TysonAndre commented Sep 23, 2017

https://github.com/TysonAndre/php-parser-to-php-ast is 90% (Edit: 98%) done. Test cases aren't comprehensive, there's still bugs in what remains.

Two modes of operation are planned (Edit: and implemented):

  1. Omitting tokens (and acting as though semicolons, etc. existed) so that the result is a valid AST (E.g. converting $x = $a + to $x = $a;), so that code using it can analyze a substitute valid AST.

    This is my main use case.

  2. Adding placeholders (e.g. converting $x = $a + \__INCOMPLETE_EXPR__;), so that code using it could try to manually handle those placeholders.

Still haven't decided how/if features such as autocompletion would work if I proceeded (E.g. should there be an option to return an SplObjectStorageMap mapping an ast\Node to the corresponding Microsoft\PhpParser\Node/Tokens (or adding a property such as $node->tolerantAST to the derived data structures may help, but plugins and node dumpers may misbehave)? Should the ast\Node start including a column/byte offset?)

@TysonAndre
Copy link
Contributor Author

https://github.com/TysonAndre/php-parser-to-php-ast is in a usable state. The question asked in the original issue has been answered.

Converting to nikic/php-parser's nodes is feasible, but would be very time consuming to implement, and I don't have use cases for PhpParser\Node.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants