question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using the standard format for the AST, used by PHP 7

See original GitHub issue

Hi!

I read your comment on #41, and I was thinking about how the resulting AST is going to be.

After checking this repository: https://github.com/nikic/php-ast, it seems there is an official format for a PHP AST, that is generated by PHP 7. (I haven’t tested it, but according to it’s readme, this project is just exposing the AST that PHP7 generates on it’s own)

One of the things I noticed there is that it seems the PHP 7 AST is formed by assoc arrays, which are like JavaScript object literals, instead of using numeric arrays.

Now, I do like how your AST looks like (since it reminds me to Lisp, and IMO, Lisp looks very elegant), but I do think an AST based on objects literals would be more practical.

Right now, in certain cases, I’m checking for the number of elements an array has, but that number will change as soon the AST requires new elements. For instance, on php-unparser class translator, I have an if (method.length === 7) to check if the method has a body (otherwise is an abstract method). But that will break if later becomes necessary to add more elements to the methods and then the number of elements a methods goes to 10 or something. On an object-based AST, I could check for something like if (!method.body) instead, and the amount of properties that the method has would be irrelevant.

But more important than just being easier and more stable, it seems to me that is really important to use and follow one standard AST.

Like in the case of esprima, they don’t use their own AST, instead, they use an standard JavaScript AST spec called estree, that was originally created by mozilla.

Since the AST of PHP 7 could be considered as the official format of the AST, I think it would be better to follow that format. Perhaps even would be a good idea to create a project like estree to document the PHP7 AST and create a more formal spec.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ichiriaccommented, Jan 3, 2017

I close this issue as the new version is released.

You can play with the new output online : http://glayzzle.com/php-parser/#demo

1reaction
ichiriaccommented, Dec 23, 2016

+1 by using the same structure as https://github.com/nikic/php-ast it will be more easy to automate tests and check if AST nodes are OK, similar to how the lexer is checked with the php lexer.

Read more comments on GitHub >

github_iconTop Results From Across the Web

nikic/php-ast: Extension exposing PHP 7 abstract syntax tree
The tolerant-php-parser-to-php-ast project can convert the AST produced by tolerant-php-parser (Another pure PHP parser library) into the format used by the php ......
Read more >
How to change PHP code with Abstract Syntax Tree
use PhpParser\PrettyPrinter\Standard; $standardPrinter = new Standard(); // here we parse the file to $astNodes // and traverse it with node ...
Read more >
PHP RFC: Abstract syntax tree
This RFC proposes the introduction of an Abstract Syntax Tree (AST) as an intermediary structure in our compilation process.
Read more >
for - Manual - PHP
The first expression ( expr1 ) is evaluated (executed) once unconditionally at the beginning of the loop. In the beginning of each iteration, ......
Read more >
sprintf - Manual - PHP
Returns a string produced according to the formatting string format . ... Attempting to use a combination of the string and width specifiers...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found