r/PHP•
helloworder

Golang Parser Written in PHP 8.1

Hi everyone!

I wanted to play with hot new PHP features, but am hardstuck with 7.4 version on current job, so it made me decide to create a small project to test waters with new features.

So here it is, it is a hand written parser with zero dependencies, which translates Golang code to an AST, written in PHP 8.1.

https://github.com/tuqqu/go-parser

Feel free to provide feedback!

Posted Feb 5, 2022, 12:40 PM UTC · edited

4712
jenshaase•

What was the most useful PHP 8.1 feature that helped you to build the parser?

6
helloworderOP•

I loved all of them, but the most useful were Enums paired with match expression, definitely. A very very useful combo.

11
[deleted]•

[deleted]

2
helloworderOP•

match is nice, but cannot substitute switch completely (yet at least).

It is limited to a single expression only, hence we still need to use switch in cases where we have more than one statement to execute.

7
__north__•

Amazing! Can I generate Go code with this lib? Would be cool to translate PHP code to Go.

3
helloworderOP•

Thanks! More likely the library would be useful if you want to write a Go to PHP transpiler, not the other way around, I think

7
Sgt_H4rtman•

Why on gods sacred earth should anyone want to do this?

4
ivain•

Because we can !

6
[deleted]•

[deleted]

1
zamzungzam•

Do you have any recomendations for starting with implementing parsers?

3
helloworderOP•

Sure! I would suggest looking at the first section of "Crafting Interpreters". And section 6 of "The Awk Programming language". Both books aren't strictly about parsers, but nonetheless they cover this topic very well and in a beginner friendly manner.

7
zamzungzam•

Thank you, will look into it.

1
celsowm•

Wow! Amazing! Does even things like go() concurrence works on it?

1
helloworderOP•

I think you got an impression this is some kind of a runtime. It’s not, it’s a parser only. But yeah, all language features are supported.

11