53 lines
1.3 KiB
Go
53 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"code.squareroundforest.org/arpio/treerack"
|
|
"io"
|
|
)
|
|
|
|
type generateOptions struct {
|
|
|
|
// Syntax specifies the filename of the syntax definition file.
|
|
Syntax *string
|
|
|
|
// SyntaxString specifies the syntax as an inline string.
|
|
SyntaxString *string
|
|
|
|
// PackageName specifies the package name for the generated code. Defaults to main.
|
|
PackageName string
|
|
|
|
// Export determines whether the generated parse function is exported (visible outside its package).
|
|
Export bool
|
|
}
|
|
|
|
// generate generates Go code that can parse arbitrary input with the provided syntax, and can be used embedded
|
|
// in an application.
|
|
//
|
|
// The syntax may be provided via a file path (using an option or a positional argument), an
|
|
// inline string, or piped from standard input.
|
|
func generate(o generateOptions, stdin io.Reader, stdout io.Writer, args ...string) error {
|
|
syntax, finalizeSyntax, err := initInput(o.Syntax, o.SyntaxString, stdin, args)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer finalizeSyntax()
|
|
s := &treerack.Syntax{}
|
|
if err := s.ReadSyntax(syntax); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := s.Init(); err != nil {
|
|
return err
|
|
}
|
|
|
|
var genOpt treerack.GeneratorOptions
|
|
genOpt.PackageName = o.PackageName
|
|
genOpt.Export = o.Export
|
|
if err := s.Generate(genOpt, stdout); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|