https://github.com/gardenbed/go-parser
A Parser for Building Go Compilers (Interpreters, Converters, Code Generators, etc.)
https://github.com/gardenbed/go-parser
compiler go golang lexer library parser scanner
Last synced: 21 days ago
JSON representation
A Parser for Building Go Compilers (Interpreters, Converters, Code Generators, etc.)
- Host: GitHub
- URL: https://github.com/gardenbed/go-parser
- Owner: gardenbed
- License: isc
- Created: 2021-11-22T08:38:48.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2025-11-16T06:15:53.000Z (3 months ago)
- Last Synced: 2025-11-16T08:18:07.497Z (3 months ago)
- Topics: compiler, go, golang, lexer, library, parser, scanner
- Language: Go
- Homepage:
- Size: 2.17 MB
- Stars: 4
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
[![Go Doc][godoc-image]][godoc-url]
[![CodeQL][codeql-image]][codeql-url]
[![Build Status][workflow-image]][workflow-url]
[![Go Report Card][goreport-image]][goreport-url]
[![Test Coverage][codecov-image]][codecov-url]
# 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
```go
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, string) 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
}
```
[godoc-url]: https://pkg.go.dev/github.com/gardenbed/go-parser
[godoc-image]: https://pkg.go.dev/badge/github.com/gardenbed/go-parser
[codeql-url]: https://github.com/gardenbed/go-parser/actions/workflows/github-code-scanning/codeql
[codeql-image]: https://github.com/gardenbed/go-parser/workflows/CodeQL/badge.svg
[workflow-url]: https://github.com/gardenbed/go-parser/actions
[workflow-image]: https://github.com/gardenbed/go-parser/workflows/Go/badge.svg
[goreport-url]: https://goreportcard.com/report/github.com/gardenbed/go-parser
[goreport-image]: https://goreportcard.com/badge/github.com/gardenbed/go-parser
[codecov-url]: https://codecov.io/gh/gardenbed/go-parser
[codecov-image]: https://codecov.io/gh/gardenbed/go-parser/branch/main/graph/badge.svg