Skip to content

Commit

Permalink
cmd/genlex: Fix import path of token package in lexer.go.tmpl.
Browse files Browse the repository at this point in the history
  • Loading branch information
mewmew committed Feb 13, 2017
1 parent 1479d13 commit 41a3902
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cmd/genlex/lexer.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"io/ioutil"
"regexp"

"github.com/mewmew/speak/cmd/genlex/token"
"{{ .ImportPath }}"
"github.com/pkg/errors"
)

// regstr specifies a regular expression for identifying the tokens of the input
// grammar.
const regstr = `{{ . }}`
const regstr = `{{ .Regexp }}`

// reg is a compiled version of regstr with leftmost-longest matching enabled.
var reg *regexp.Regexp
Expand Down
14 changes: 13 additions & 1 deletion cmd/genlex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func genLexer(ids []string, reg string) error {
}

// Generate token/token.go.
log.Println(`Creating "token/token.go"`)
t1 := t.Lookup("token.go.tmpl")
if err := os.MkdirAll("token", 0755); err != nil {
return errors.WithStack(err)
Expand All @@ -85,7 +86,14 @@ func genLexer(ids []string, reg string) error {
return errors.WithStack(err)
}

// Locate import path of the token package.
tokenImportPath, err := goutil.RelImpPath("token")
if err != nil {
return errors.WithStack(err)
}

// Generate lexer/lexer.go.
log.Println(`Creating "lexer/lexer.go"`)
t2 := t.Lookup("lexer.go.tmpl")
if err := os.MkdirAll("lexer", 0755); err != nil {
return errors.WithStack(err)
Expand All @@ -95,7 +103,11 @@ func genLexer(ids []string, reg string) error {
return errors.WithStack(err)
}
defer f2.Close()
if err := t2.Execute(f2, reg); err != nil {
m := map[string]string{
"ImportPath": tokenImportPath,
"Regexp": reg,
}
if err := t2.Execute(f2, m); err != nil {
return errors.WithStack(err)
}
return nil
Expand Down
1 change: 1 addition & 0 deletions cmd/terms/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func outputTerms(grammarPath, start, output string, indent bool, skip []string)
// Print the JSON output to stdout or the path specified by the "-o" flag.
w := os.Stdout
if len(output) > 0 {
log.Printf("Creating %q", output)
f, err := os.Create(output)
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit 41a3902

Please sign in to comment.