https://github.com/tuqqu/go-parser
Golang parser written in PHP
https://github.com/tuqqu/go-parser
ast golang parser php-parser
Last synced: 14 days ago
JSON representation
Golang parser written in PHP
- Host: GitHub
- URL: https://github.com/tuqqu/go-parser
- Owner: tuqqu
- License: mit
- Created: 2022-01-12T11:48:43.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-09-09T23:42:56.000Z (over 2 years ago)
- Last Synced: 2025-10-19T10:52:22.614Z (3 months ago)
- Topics: ast, golang, parser, php-parser
- Language: PHP
- Homepage:
- Size: 270 KB
- Stars: 42
- Watchers: 4
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-compilers - tuqqu/go-parser - Golang parser written in PHP. (Golang)
README
# GoParser
Golang parser written in PHP 8.1
## Installation
To install this package, run:
```
composer require tuqqu/go-parser
```
## Example
```php
use GoParser\Parser;
$program = <<parse();
$errs = $parser->getErrors();
```
The parser is capable of recovering itself if a parse error occurs. In such cases, it will continue parsing at the closest node it can recognise.
The resulting Abstract Syntax Tree (AST) will be as complete as possible. You need to check `getErrors()` to identify any errors.
## Single declaration parsing
The parser can also handle a single declaration (e.g., a single function) instead of an entire Go program:
```php
use GoParser\{Parser, ParseMode};
$func = <<parseSingleDecl();
```
## Abstract Syntax Tree
Parsing results in an Abstract Syntax Tree. Refer to `src/Ast` for details.
For the most part, the structure of AST nodes closely follows the official Golang [specification][1].
Some nodes may have slightly different names (e.g., `ExpressionList` instead of `ExprList`), but in most cases, the names are the same or easily recognisable.
## CLI
Package comes with a CLI command:
```
./vendor/bin/go-parser main.go [--flags]
```
By default, it uses a simple `NodeDumper` to print AST.
Use `--help` to see other options.
[1]: https://go.dev/ref/spec