Documentation ¶
Overview ¶
Package earley contains an implementation of glean.Grammar using a simple Earley-style parser.
Example ¶
Example uses a parser to add two integers
package main import ( "fmt" "os" "os/exec" "path/filepath" "runtime" "github.com/pat42smith/glean/earley" ) // Example uses a parser to add two integers func main() { gocmd := runtime.GOROOT() + "/bin/go" if runtime.GOOS == "windows" { gocmd += ".exe" } tmp, e := os.MkdirTemp("", "") if e != nil { panic(e) } defer func() { os.Remove(tmp) }() mainGo := filepath.Join(tmp, "main.go") e = os.WriteFile(mainGo, []byte(mainText), 0444) if e != nil { panic(e) } defer func() { os.Remove(mainGo) }() var g earley.Grammar e = g.AddRule("RuleAdd", "Sum", []string{"int", "int"}) if e != nil { panic(e) } parserText, e := g.WriteParser("Sum", "main", "_") if e != nil { panic(e) } parserGo := filepath.Join(tmp, "parser.go") e = os.WriteFile(parserGo, []byte(parserText), 0444) if e != nil { panic(e) } defer func() { os.Remove(parserGo) }() out, _ := exec.Command(gocmd, "run", mainGo, parserGo).CombinedOutput() fmt.Printf("%s", out) } var mainText = ` package main import "fmt" type Sum int func RuleAdd(i, j int) Sum { return Sum(i + j) } func main() { sum, e := _Parse([]interface{}{ 2, 5 }) if e != nil { panic(e) } fmt.Println(sum) } `
Output: 7
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
Click to show internal directories.
Click to hide internal directories.