Php-parser written in golang v0.3 is released
github.com
github.com
Obviously not every project needs a "why?", and this is a technically cool endeavour that stands on its own..
A few ideas I can think of for "Why?":
* Static analysis of a codebase looking for bad practices or errors
* Feeding the structured output into code-gen for another language, to start a porting project
* //STEP MISSING//, Profit?
Anything else?So the answer might just be "because I can and I want to".
This is the talk, IIRC: "Yak shaving is best shaving" https://www.youtube.com/watch?v=nY_Pl2x7Fgs
But if you're developing a web application that you host and also want to offer a private version of for enterprise use, this is somewhat appealing because it's the first step of being able to package the PHP app within a Go binary that runs on a lot of platforms and not have to release your source code.
Sure you could write it in Go from the start, but if you already have a bunch written or want to use less expensive developers, this might work out one day.
Seems like part of a larger "I built X in Y" HN pattern.
You wouldn't be able to distribute PHP programs as go binaries; it would still be an interpreted language, so you'd still have to make sure your programs, and their dependencies, were correctly deployed to wherever you had installed the interpreter.
You can "solve" that by embedding an interpreter (or a copy of your compiler) into your runtime, but most interpreted platforms in that situation consider the interpreter "good enough" and work on making optimizations (e.g. JIT compilation to native code) on it rather than making a static compiler.
If I ever get around to playing with golang you can bet there's gonna be yet another earley parser implementation for whatever grammar I'm currently looking at.
If that is it, you'd probably be better off just using the PHP parser (https://github.com/nikic/PHP-Parser).
If you are writing analyzers for PHP, you must know it enough to right an analyzer in actual PHP. Why Go?
I can't see a lot of people using this.
I'm not sure about the "Just" here.
The opportunities for writing code transformation tools are abundant. E.g. a PHP IDE that isn't written in PHP, a linting tool, a preprocessor for an embedded DSL. It isn't obvious to me that the tooling for a language like PHP need to be written in PHP.
> Why Go?
Go isn't a bad choice for writing parts of a compiler. The parser itself was, however, written with the parser generator goyacc rather than native Go code:
https://github.com/z7zmey/php-parser/blob/master/php7/php7.y
Taking a goyacc file like this and porting it to another YACC is a lower effort than starting from scratch. In such a way, this project delivers not just "a PHP parser with xx% coverage in Go", but a shortcut to writing PHP parsers in any language with a decent YACC.
One way this project is structured that I don't particularly like is that each node in the AST corresponds to a file. For a programming language with algebraic types (ML, Haskell), having the AST definition in a single file is quite typical.
There's already a yacc file for PHP's parser, part of the PHP runtime: https://github.com/php/php-src/blob/master/Zend/zend_languag...
nikic/PHP-Parser is feature-complete/mature/stable and developed by one of the most knowledgeable members of the PHP internals core development team. It is the library powering many PHP dev tools; static analysis included.
I can see that creating a tool like this is an excellent learning experience, but I don't see why anyone else should care.