Categorygithub.com/gardenbed/go-parser
modulepackage
0.1.0
Repository: https://github.com/gardenbed/go-parser.git
Documentation: pkg.go.dev

# README

Go Doc Build Status Go Report Card Test Coverage

go-parser

This repo provides an abstraction and an implementation for a Go parser. It can be used for building all sorts of Go compilers such as interpreters, converters, code generators, and so forth.

Quick Start

package main

import (
  "go/ast"

  "github.com/gardenbed/charm/ui"
  "github.com/gardenbed/go-parser"
)

func main() {
  compiler := parser.NewCompiler(
    ui.New(ui.Debug),
    &parser.Consumer{
      Name:      "compiler",
      Package:   Package,
      FilePre:   FilePre,
      Import:    Import,
      Struct:    Struct,
      Interface: Interface,
      FuncType:  FuncType,
      FuncDecl:  FuncDecl,
      FilePost:  FilePost,
    },
  )

  if err := compiler.Compile("...", parser.ParseOptions{}); err != nil {
    panic(err)
  }
}

func Package(*parser.Package, *ast.Package) bool {
  return true
}

func FilePre(*parser.File, *ast.File) bool {
  return true
}

func Import(*parser.File, *ast.ImportSpec)                 {}
func Struct(*parser.Type, *ast.StructType)                 {}
func Interface(*parser.Type, *ast.InterfaceType)           {}
func FuncType(*parser.Type, *ast.FuncType)                 {}
func FuncDecl(*parser.Func, *ast.FuncType, *ast.BlockStmt) {}

func FilePost(*parser.File, *ast.File) error {
  return nil
}

# Functions

ConvertToUnexported converts an exported identifier to an unexported one.
InferName infers an identifier name from a type expression.
IsExported determines whether or not a given name is exported.
NewCompiler creates a new compiler.
WriteFile formats and writes a Go source code file to disk.

# Structs

Compiler is used for parsing Go source code files and compiling new source code files.
Consumer is used for processing AST nodes.
File contains information about a parsed file.
Func contains information about a parsed function.
Module contains information about a Go module.
Package contains information about a parsed package.
ParseOptions configure how Go source code files should be parsed.
Type contains information about a parsed type.
No description provided by the author