Golang 1.18 parser written in PHP 8.1

Overview

GoParser

Golang (1.18) parser written in PHP 8.1

Installation

To install this package, run:

composer require tuqqu/go-parser

Example

$parser = new \GoParser\Parser('
package main

import "fmt"

func main() {
    res := add(1, 2)
    fmt.Println("1+2 =", res)
}
');

$ast = $parser->parse();
$errs = $parser->getErrors();

Supported language level: Go 1.18 (generics).

Parsing single declarations

If you want, you can parse only a single declaration (e.g. a single function), instead of a fully defined Go program:

$parser = new \GoParser\Parser(
    'func add(x, y int) int { return x + y }', 
    mode: \GoParser\ParseMode::SingleDecl
);
$decl = $parser->parseSingleDecl();

Parser is able to recover itself if a parse error occurs, in this case it will continue parsing at the closest node it is able to recognise.

The resulting AST will be as full as possible, and you have to check getErrors() to see errors.

Abstract Syntax Tree

Parsing results in an Abstract Syntax Tree result. See src/Ast.

For the most part the AST nodes structure follows closely the official Golang specification.

Some Nodes may also have a bit different name (e.g. ExpressionList vs ExprList), but mostly the names are either 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.

You might also like...
A proof-of-concept parser for the SMART Health Cards format.

SMART Health Cards parser A proof-of-concept parser for the SMART Health Cards format. This is not intended for production use. I just hacked this tog

uaDetect – A multi-language port of Browserscope's user agent parser

uaDetect is a lightweight for detecting mobile devices. It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.

JsonCollectionParser - Event-based parser for large JSON collections (consumes small amount of memory)

Event-based parser for large JSON collections (consumes small amount of memory). Built on top of JSON Streaming Parser This packa

MySQL parser for free-id/core package

MySQL parser Installation You can install the package via composer: composer require free-id/mysql Usage use FreeId\Mysql\Parser; $parser = new Parse

A sampling profiler for PHP written in PHP, which reads information about running PHP VM from outside of the process.

Reli Reli is a sampling profiler (or a VM state inspector) written in PHP. It can read information about running PHP script from outside of the proces

A sampling profiler for PHP written in PHP, which reads information about running PHP VM from outside of the process.

Reli Reli is a sampling profiler (or a VM state inspector) written in PHP. It can read information about running PHP script from outside of the proces

A multithreaded application server for PHP, written in PHP.

appserver.io, a PHP application server This is the main repository for the appserver.io project. What is appserver.io appserver.io is a multithreaded

A status monitor for Elite Dangerous, written in PHP. Designed for 1080p screens in the four-panel-view in panel.php, and for 7 inch screens with a resolution of 1024x600 connected to a Raspberry Pi.

EDStatusPanel A status monitor for Elite Dangerous, written in PHP. Designed for 1080p screens in the four-panel-view in panel.php, and for 7 inch scr

PHP generics written in PHP

PHP generics written in PHP Require PHP = 7.4 Composer (PSR-4 Autoload) Table of contents How it works Quick start Example Features Tests How it work

Comments
  • Golang 1.18 new syntax

    Golang 1.18 new syntax

    The newest (1.18) release along with new features has brought new syntax, which must be implemented:

    • [x] Function with type parameters (generic function)
    • [x] Type with type parameters (generic type)
    • [x] Underlying type operator (~)
    • [x] Union of type sets
    • [x] Extending interface type to accept type unions
    opened by tuqqu 1
Releases(v0.4.0)
  • v0.4.0(Aug 29, 2022)

    • New error messages, closer in text to the original go parser.
    • New error output rendering
    • --json option to print AST as JSON in bin command
    • SyntaxErrors instead of ParseErrors
    • Improved parameter parsing
    • Fixed bugs with variadic parameter parsing
    • Fixed bugs with lexing bitwise operators (LeftShift & RightShift)
    • Code refactoring & improvements
    Source code(tar.gz)
    Source code(zip)
  • v0.3.0(Apr 3, 2022)

    • Go 1.18 syntax support
      • generic functions
      • generic types
      • underlying types
      • type unions in interfaces
    • More tests
    • New ast nodes
    • Composite type accepts type
    • Cli command option validation
    Source code(tar.gz)
    Source code(zip)
Owner
Arthur Kurbidaev
Arthur Kurbidaev
Ip2region is a offline IP location library with accuracy rate of 99.9% and 0.0x millseconds searching performance. DB file is ONLY a few megabytes with all IP address stored. binding for Java,PHP,C,Python,Nodejs,Golang,C#,lua. Binary,B-tree,Memory searching algorithm

Ip2region是什么? ip2region - 准确率99.9%的离线IP地址定位库,0.0x毫秒级查询,ip2region.db数据库只有数MB,提供了java,php,c,python,nodejs,golang,c#等查询绑定和Binary,B树,内存三种查询算法。 Ip2region特性

Lion 12.6k Dec 30, 2022
Deeper is a easy way to compare if 2 objects is equal based on values in these objects. This library is heavily inspired in Golang's reflect.DeepEqual().

Deeper Deeper is a easy way to compare if 2 objects is equal based on values in these objects. This library is heavily inspired in Golang's reflect.De

Joubert RedRat 4 Feb 12, 2022
DBML parser for PHP8. It's a PHP parser for DBML syntax.

DBML parser written on PHP8 DBML (database markup language) is a simple, readable DSL language designed to define database structures. This page outli

Pavel Buchnev 32 Dec 29, 2022
A PHP parser written in PHP

PHP Parser This is a PHP 5.2 to PHP 8.1 parser written in PHP. Its purpose is to simplify static code analysis and manipulation. Documentation for ver

Nikita Popov 15.9k Jan 8, 2023
Parsica - PHP Parser Combinators - The easiest way to build robust parsers.

Parsica The easiest way to build robust parsers in PHP. composer require parsica-php/parsica Documentation & API: parsica-php.github.io <?php $parser

null 350 Dec 27, 2022
PHP Simple M3U Parser, it clean the playlist and remove duplicate

SimpleM3UParser PHP Simple M3U Playlist Parser, it clean, remove duplicate, and group the playlist. Usage see example.php <?php require_once "M3UPars

erwin solihin 3 May 30, 2022
Referer-parser PHP library

referer-parser PHP library This is the PHP implementation of referer-parser, the library for extracting search marketing data from referer (sic) URLs.

null 4 Sep 3, 2022
Lightning Fast, Minimalist PHP User Agent String Parser.

Lightning Fast, Minimalist PHP User Agent String Parser.

Jesse Donat 523 Dec 21, 2022
A PHP package for MRZ (Machine Readable Zones) code parser for Passport, Visa & Travel Document (TD1 & TD2).

MRZ (Machine Readable Zones) Parser for PHP A PHP package for MRZ (Machine Readable Zones) code parser for Passport, Visa & Travel Document (TD1 & TD2

Md. Rakibul Islam 25 Aug 24, 2022
Annotations Docblock Parser

Doctrine Annotations Docblock Annotations Parser library (extracted from Doctrine Common). Documentation See the doctrine-project website. Contributin

Doctrine 6.6k Dec 29, 2022