parser

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2021 License: ISC Imports: 15 Imported by: 0

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
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertToUnexported

func ConvertToUnexported(name string) string

ConvertToUnexported converts an exported identifier to an unexported one.

func InferName

func InferName(expr ast.Expr) string

InferName infers an identifier name from a type expression.

func IsExported

func IsExported(name string) bool

IsExported determines whether or not a given name is exported.

func WriteFile

func WriteFile(path string, fset *token.FileSet, file *ast.File) error

WriteFile formats and writes a Go source code file to disk.

Types

type Compiler

type Compiler struct {
	// contains filtered or unexported fields
}

Compiler is used for parsing Go source code files and compiling new source code files.

func NewCompiler

func NewCompiler(ui ui.UI, consumers ...*Consumer) *Compiler

NewCompiler creates a new compiler. This is meant to be used by downstream packages that provide consumers.

func (*Compiler) Compile

func (c *Compiler) Compile(packages string, opts ParseOptions) error

Compile parses all Go source code files in the given packages and generates new artifacts (source codes).

type Consumer

type Consumer struct {
	Name      string
	Package   func(*Package, *goast.Package) bool
	FilePre   func(*File, *goast.File) bool
	Import    func(*File, *goast.ImportSpec)
	Struct    func(*Type, *goast.StructType)
	Interface func(*Type, *goast.InterfaceType)
	FuncType  func(*Type, *goast.FuncType)
	FuncDecl  func(*Func, *goast.FuncType, *goast.BlockStmt)
	FilePost  func(*File, *goast.File) error
}

Consumer is used for processing AST nodes. This is meant to be provided by downstream packages.

type File

type File struct {
	Package
	*gotoken.FileSet
	Name string
}

File contains information about a parsed file.

type Func

type Func struct {
	File
	Name     string
	RecvName string
	RecvType goast.Expr
}

Func contains information about a parsed function.

func (*Func) IsExported

func (f *Func) IsExported() bool

IsExported determines whether or not a function is exported.

func (*Func) IsMethod

func (f *Func) IsMethod() bool

IsMethod determines if a function is a method of a struct.

type Module

type Module struct {
	Name string
}

Module contains information about a Go module.

type Package

type Package struct {
	Module
	Name        string
	ImportPath  string
	BaseDir     string
	RelativeDir string
}

Package contains information about a parsed package.

type ParseOptions

type ParseOptions struct {
	MergePackageFiles bool
	SkipTestFiles     bool
	TypeFilter        TypeFilter
}

ParseOptions configure how Go source code files should be parsed.

type Type

type Type struct {
	File
	Name string
}

Type contains information about a parsed type.

func (*Type) IsExported

func (t *Type) IsExported() bool

IsExported determines whether or not a type is exported.

type TypeFilter

type TypeFilter struct {
	// Exported filters unexported types.
	Exported bool
	// Names filters types based on their names.
	Names []string
	// Regexp filters types based on a regular expression.
	Regexp *regexp.Regexp
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL