Skip to content

Commit

Permalink
Stop handling panic to print a stack trace
Browse files Browse the repository at this point in the history
  • Loading branch information
nihei9 committed May 15, 2022
1 parent 10ff05b commit acc2df9
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 135 deletions.
35 changes: 2 additions & 33 deletions cmd/vartan-go/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io/ioutil"
"os"
"runtime/debug"

mldriver "github.com/nihei9/maleeni/driver"
"github.com/nihei9/vartan/driver"
Expand All @@ -14,13 +13,7 @@ import (
)

func Execute() error {
err := generateCmd.Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return err
}

return nil
return generateCmd.Execute()
}

var generateFlags = struct {
Expand All @@ -42,31 +35,7 @@ func init() {
generateFlags.pkgName = generateCmd.Flags().StringP("package", "p", "main", "package name")
}

func runGenerate(cmd *cobra.Command, args []string) (retErr error) {
defer func() {
panicked := false
v := recover()
if v != nil {
err, ok := v.(error)
if !ok {
retErr = fmt.Errorf("an unexpected error occurred: %v", v)
fmt.Fprintf(os.Stderr, "%v:\n%v", retErr, string(debug.Stack()))
return
}

retErr = err
panicked = true
}

if retErr != nil {
if panicked {
fmt.Fprintf(os.Stderr, "%v:\n%v", retErr, string(debug.Stack()))
} else {
fmt.Fprintf(os.Stderr, "%v\n", retErr)
}
}
}()

func runGenerate(cmd *cobra.Command, args []string) error {
cgram, err := readCompiledGrammar(args[0])
if err != nil {
return fmt.Errorf("Cannot read a compiled grammar: %w", err)
Expand Down
2 changes: 2 additions & 0 deletions cmd/vartan-go/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package main

import (
"fmt"
"os"
)

func main() {
err := Execute()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
21 changes: 0 additions & 21 deletions cmd/vartan/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime/debug"
"strings"

verr "github.com/nihei9/vartan/error"
Expand Down Expand Up @@ -47,20 +46,6 @@ func runCompile(cmd *cobra.Command, args []string) (retErr error) {
grmPath = args[0]
}
defer func() {
panicked := false
v := recover()
if v != nil {
err, ok := v.(error)
if !ok {
retErr = fmt.Errorf("an unexpected error occurred: %v", v)
fmt.Fprintf(os.Stderr, "%v:\n%v", retErr, string(debug.Stack()))
return
}

retErr = err
panicked = true
}

if retErr != nil {
specErrs, ok := retErr.(verr.SpecErrors)
if ok {
Expand All @@ -74,12 +59,6 @@ func runCompile(cmd *cobra.Command, args []string) (retErr error) {
}
}
}

if panicked {
fmt.Fprintf(os.Stderr, "%v:\n%v", retErr, string(debug.Stack()))
} else {
fmt.Fprintf(os.Stderr, "%v\n", retErr)
}
}
}()

Expand Down
2 changes: 2 additions & 0 deletions cmd/vartan/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package main

import (
"fmt"
"os"
)

func main() {
err := Execute()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
89 changes: 34 additions & 55 deletions cmd/vartan/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"runtime/debug"
"strings"

"github.com/nihei9/vartan/driver"
"github.com/nihei9/vartan/spec"
Expand Down Expand Up @@ -36,31 +36,7 @@ func init() {
rootCmd.AddCommand(cmd)
}

func runParse(cmd *cobra.Command, args []string) (retErr error) {
defer func() {
panicked := false
v := recover()
if v != nil {
err, ok := v.(error)
if !ok {
retErr = fmt.Errorf("an unexpected error occurred: %v", v)
fmt.Fprintf(os.Stderr, "%v:\n%v", retErr, string(debug.Stack()))
return
}

retErr = err
panicked = true
}

if retErr != nil {
if panicked {
fmt.Fprintf(os.Stderr, "%v:\n%v", retErr, string(debug.Stack()))
} else {
fmt.Fprintf(os.Stderr, "%v\n", retErr)
}
}
}()

func runParse(cmd *cobra.Command, args []string) error {
if *parseFlags.onlyParse && *parseFlags.cst {
return fmt.Errorf("You cannot enable --only-parse and --cst at the same time")
}
Expand Down Expand Up @@ -121,31 +97,6 @@ func runParse(cmd *cobra.Command, args []string) (retErr error) {
return err
}

synErrs := p.SyntaxErrors()
for _, synErr := range synErrs {
tok := synErr.Token

var msg string
switch {
case tok.EOF():
msg = "<eof>"
case tok.Invalid():
msg = fmt.Sprintf("'%v' (<invalid>)", string(tok.Lexeme()))
default:
t := cg.ParsingTable.Terminals[tok.TerminalID()]
msg = fmt.Sprintf("'%v' (%v)", string(tok.Lexeme()), t)
}

fmt.Fprintf(os.Stderr, "%v:%v: %v: %v", synErr.Row+1, synErr.Col+1, synErr.Message, msg)
if len(synErrs) > 0 {
fmt.Fprintf(os.Stderr, "; expected: %v", synErr.ExpectedTerminals[0])
for _, t := range synErr.ExpectedTerminals[1:] {
fmt.Fprintf(os.Stderr, ", %v", t)
}
}
fmt.Fprintf(os.Stderr, "\n")
}

if !*parseFlags.onlyParse {
// A parser can construct a parse tree even if syntax errors occur.
// When therer is a parse tree, print it.
Expand All @@ -157,10 +108,6 @@ func runParse(cmd *cobra.Command, args []string) (retErr error) {
tree = tb.Tree()
}
if tree != nil {
if len(synErrs) > 0 {
fmt.Println("")
}

if *parseFlags.json {
b, err := json.Marshal(tree)
if err != nil {
Expand All @@ -173,6 +120,19 @@ func runParse(cmd *cobra.Command, args []string) (retErr error) {
}
}

if len(p.SyntaxErrors()) > 0 {
var b strings.Builder
synErrs := p.SyntaxErrors()
writeSyntaxErrorMessage(&b, cg, synErrs[0])
for _, synErr := range synErrs[1:] {
fmt.Fprintf(&b, "\n")
writeSyntaxErrorMessage(&b, cg, synErr)
}
if b.Len() > 0 {
return fmt.Errorf(b.String())
}
}

return nil
}

Expand All @@ -192,3 +152,22 @@ func readCompiledGrammar(path string) (*spec.CompiledGrammar, error) {
}
return cg, nil
}

func writeSyntaxErrorMessage(b *strings.Builder, cgram *spec.CompiledGrammar, synErr *driver.SyntaxError) {
fmt.Fprintf(b, "%v:%v: %v: ", synErr.Row+1, synErr.Col+1, synErr.Message)

tok := synErr.Token
switch {
case tok.EOF():
fmt.Fprintf(b, "<eof>")
case tok.Invalid():
fmt.Fprintf(b, "'%v' (<invalid>)", string(tok.Lexeme()))
default:
fmt.Fprintf(b, "'%v' (%v)", string(tok.Lexeme()), cgram.ParsingTable.Terminals[tok.TerminalID()])
}

fmt.Fprintf(b, "; expected: %v", synErr.ExpectedTerminals[0])
for _, t := range synErr.ExpectedTerminals[1:] {
fmt.Fprintf(b, ", %v", t)
}
}
27 changes: 1 addition & 26 deletions cmd/vartan/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"io/ioutil"
"os"
"runtime/debug"
"strings"
"text/template"

Expand All @@ -26,31 +25,7 @@ func init() {
rootCmd.AddCommand(cmd)
}

func runShow(cmd *cobra.Command, args []string) (retErr error) {
defer func() {
panicked := false
v := recover()
if v != nil {
err, ok := v.(error)
if !ok {
retErr = fmt.Errorf("an unexpected error occurred: %v", v)
fmt.Fprintf(os.Stderr, "%v:\n%v", retErr, string(debug.Stack()))
return
}

retErr = err
panicked = true
}

if retErr != nil {
if panicked {
fmt.Fprintf(os.Stderr, "%v:\n%v", retErr, string(debug.Stack()))
} else {
fmt.Fprintf(os.Stderr, "%v\n", retErr)
}
}
}()

func runShow(cmd *cobra.Command, args []string) error {
report, err := readReport(args[0])
if err != nil {
return err
Expand Down

0 comments on commit acc2df9

Please sign in to comment.