GithubHelp home page GithubHelp logo

go-parser's Introduction

GoParser

Golang (1.18) parser written in PHP 8.1

Installation

To install this package, run:

composer require tuqqu/go-parser

Example

use GoParser\Parser;

$program = <<<GO
    package main
    
    import "fmt"
    
    func main() {
        res := add(1, 2)
        fmt.Println("1+2 =", res)
    }
GO;

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

Supported language level: Go 1.18 (generics).

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.

Single declaration parsing

Parser can also handle a single declaration only (e.g. a single function), instead of a fully defined Go program:

use GoParser\{Parser, ParseMode};

$func = <<<GO
    func add(x, y int) int { 
        return x + y
    }
GO;

$parser = new Parser($func, mode: ParseMode::SingleDecl);
$decl = $parser->parseSingleDecl();

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.

go-parser's People

Contributors

tuqqu avatar kawniy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.