diff options
author | Ryo Nihei <nihei.dev@gmail.com> | 2021-07-17 13:41:44 +0900 |
---|---|---|
committer | Ryo Nihei <nihei.dev@gmail.com> | 2021-07-17 13:41:44 +0900 |
commit | 2d3fed9f2992a11ab76601484a735d5500d84dba (patch) | |
tree | 643e1995e7b012af1fc0ff48e3bcb1b0ec95489b /cmd | |
parent | Add a line number to token error messages (diff) | |
download | urubu-2d3fed9f2992a11ab76601484a735d5500d84dba.tar.gz urubu-2d3fed9f2992a11ab76601484a735d5500d84dba.tar.xz |
Improve syntax error messages
- Add a source file name to error messages.
- Add a line that an error occurred at to error messages.
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/vartan/compile.go | 84 | ||||
-rw-r--r-- | cmd/vartan/parse.go | 18 | ||||
-rw-r--r-- | cmd/vartan/root.go | 10 |
3 files changed, 93 insertions, 19 deletions
diff --git a/cmd/vartan/compile.go b/cmd/vartan/compile.go index 769f143..3601f00 100644 --- a/cmd/vartan/compile.go +++ b/cmd/vartan/compile.go @@ -3,8 +3,11 @@ package main import ( "encoding/json" "fmt" + "io/ioutil" "os" + "path/filepath" + verr "github.com/nihei9/vartan/error" "github.com/nihei9/vartan/grammar" "github.com/nihei9/vartan/spec" "github.com/spf13/cobra" @@ -28,7 +31,63 @@ func init() { } func runCompile(cmd *cobra.Command, args []string) (retErr error) { - gram, err := readGrammar(*compileFlags.grammar) + var tmpDirPath string + defer func() { + if tmpDirPath == "" { + return + } + os.RemoveAll(tmpDirPath) + }() + + grmPath := *compileFlags.grammar + defer func() { + v := recover() + if v != nil { + err, ok := v.(error) + if !ok { + retErr = fmt.Errorf("an unexpected error occurred: %v\n", v) + fmt.Fprintln(os.Stderr, retErr) + return + } + + retErr = err + } + + if retErr != nil { + specErr, ok := retErr.(*verr.SpecError) + if ok { + if *compileFlags.grammar != "" { + specErr.FilePath = grmPath + specErr.SourceName = grmPath + } else { + specErr.FilePath = grmPath + specErr.SourceName = "stdin" + } + } + fmt.Fprintln(os.Stderr, retErr) + } + }() + + if grmPath == "" { + var err error + tmpDirPath, err = os.MkdirTemp("", "vartan-compile-*") + if err != nil { + return err + } + + src, err := ioutil.ReadAll(os.Stdin) + if err != nil { + return err + } + + grmPath = filepath.Join(tmpDirPath, "stdin.vr") + err = ioutil.WriteFile(grmPath, src, 0600) + if err != nil { + return err + } + } + + gram, err := readGrammar(grmPath) if err != nil { return err } @@ -46,17 +105,22 @@ func runCompile(cmd *cobra.Command, args []string) (retErr error) { return nil } -func readGrammar(path string) (*grammar.Grammar, error) { - r := os.Stdin - if path != "" { - f, err := os.Open(path) - if err != nil { - return nil, fmt.Errorf("Cannot open the grammar file %s: %w", path, err) +func readGrammar(path string) (grm *grammar.Grammar, retErr error) { + defer func() { + err := recover() + specErr, ok := err.(*verr.SpecError) + if ok { + specErr.FilePath = path + retErr = specErr } - defer f.Close() - r = f + }() + + f, err := os.Open(path) + if err != nil { + return nil, fmt.Errorf("Cannot open the grammar file %s: %w", path, err) } - ast, err := spec.Parse(r) + defer f.Close() + ast, err := spec.Parse(f) if err != nil { return nil, err } diff --git a/cmd/vartan/parse.go b/cmd/vartan/parse.go index 8089c5e..ef22ff3 100644 --- a/cmd/vartan/parse.go +++ b/cmd/vartan/parse.go @@ -28,6 +28,24 @@ func init() { } func runParse(cmd *cobra.Command, args []string) (retErr error) { + defer func() { + v := recover() + if v != nil { + err, ok := v.(error) + if !ok { + retErr = fmt.Errorf("an unexpected error occurred: %v\n", v) + fmt.Fprintln(os.Stderr, retErr) + return + } + + retErr = err + } + + if retErr != nil { + fmt.Fprintln(os.Stderr, retErr) + } + }() + cgram, err := readCompiledGrammar(args[0]) if err != nil { return fmt.Errorf("Cannot read a compiled grammar: %w", err) diff --git a/cmd/vartan/root.go b/cmd/vartan/root.go index ad163a6..a193577 100644 --- a/cmd/vartan/root.go +++ b/cmd/vartan/root.go @@ -1,9 +1,6 @@ package main import ( - "fmt" - "os" - "github.com/spf13/cobra" ) @@ -19,10 +16,5 @@ var rootCmd = &cobra.Command{ } func Execute() error { - err := rootCmd.Execute() - if err != nil { - fmt.Fprintf(os.Stderr, "%v\n", err) - return err - } - return nil + return rootCmd.Execute() } |