Skip to content

Commit

Permalink
Merge pull request #1 from Jintumoni/feature/better-syntax-error
Browse files Browse the repository at this point in the history
Show better error messages
  • Loading branch information
Jintumoni authored Sep 28, 2024
2 parents 214d7d7 + d868945 commit bf792bc
Show file tree
Hide file tree
Showing 11 changed files with 503 additions and 82 deletions.
136 changes: 136 additions & 0 deletions errors/parser_errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package errors

import (
"bytes"
"fmt"
"strings"

"github.com/Jintumoni/vortex/lexer"
"github.com/Jintumoni/vortex/nodes"
"github.com/fatih/color"
)

type UnexpectedToken struct {
SourceContext string
ActualToken *lexer.Token
ExpectedToken lexer.TokenType
SuggestedTokens []lexer.TokenType
}

func (e *UnexpectedToken) Error() string {
buffer := new(bytes.Buffer)
buffer.WriteString(color.RedString(fmt.Sprintf("Error: Unexpected \"%s\" found\n", e.ActualToken.Value)))

buffer.WriteString(e.SourceContext)

buffer.WriteString(strings.Repeat("\t", 2))
buffer.WriteString(strings.Repeat(" ", e.ActualToken.Col))
// buffer.WriteString("\033[31m")
buffer.WriteString(color.BlueString(strings.Repeat("^", e.ActualToken.Span)))
buffer.WriteString(color.BlueString("--"))

if e.SuggestedTokens != nil {
buffer.WriteString(color.BlueString(fmt.Sprintf("Expected one of: ")))
for i, t := range e.SuggestedTokens {
if i > 0 {
buffer.WriteString(color.BlueString(", "))
}
buffer.WriteString(color.BlueString(fmt.Sprintf("\"%s\"", t)))
}
} else if e.ExpectedToken != 0 {
buffer.WriteString(color.BlueString(fmt.Sprintf("Did you mean \"%s\"?", e.ExpectedToken)))
} else {
buffer.WriteString(color.BlueString("here"))
}
// buffer.WriteString("\033[0m")
buffer.WriteString("\n")

return buffer.String()
}

type UnknownEdgeType struct {
SourceContext string
ActualToken *lexer.Token
}

func (e *UnknownEdgeType) Error() string {
buffer := new(bytes.Buffer)
buffer.WriteString(color.RedString(fmt.Sprintf("Error: Unknown \"%s\" found\n", e.ActualToken.Value)))

buffer.WriteString(e.SourceContext)

buffer.WriteString(strings.Repeat("\t", 2))
buffer.WriteString(strings.Repeat(" ", e.ActualToken.Col))

buffer.WriteString(color.BlueString(strings.Repeat("^", e.ActualToken.Span)))
buffer.WriteString(color.BlueString("--"))

buffer.WriteString(color.BlueString(fmt.Sprintf("Expected one of: ")))
for i, t := range nodes.GetAllEdgeTypes() {
if i > 0 {
buffer.WriteString(color.BlueString(", "))
}
buffer.WriteString(color.BlueString(fmt.Sprintf("\"%s\"", t)))
}
buffer.WriteString("\n")

return buffer.String()
}

type UnknownStatement struct {
SourceContext string
ActualToken *lexer.Token
}

func (e *UnknownStatement) Error() string {
buffer := new(bytes.Buffer)
buffer.WriteString(color.RedString(fmt.Sprintf("Error: Unknown \"%s\" found\n", e.ActualToken.Value)))

buffer.WriteString(e.SourceContext)

buffer.WriteString(strings.Repeat("\t", 2))
buffer.WriteString(strings.Repeat(" ", e.ActualToken.Col))

buffer.WriteString(color.BlueString(strings.Repeat("^", e.ActualToken.Span)))
buffer.WriteString(color.BlueString("--"))

buffer.WriteString(color.BlueString(fmt.Sprintf("Expected one of: ")))
for i, t := range lexer.GetAllStatementTypes() {
if i > 0 {
buffer.WriteString(color.BlueString(", "))
}
buffer.WriteString(color.BlueString(fmt.Sprintf("\"%s\"", t)))
}
buffer.WriteString("\n")

return buffer.String()
}

type UnknownBuiltinFunc struct {
SourceContext string
ActualToken *lexer.Token
}

func (e *UnknownBuiltinFunc) Error() string {
buffer := new(bytes.Buffer)
buffer.WriteString(color.RedString(fmt.Sprintf("Error: Unknown \"%s\" found\n", e.ActualToken.Value)))

buffer.WriteString(e.SourceContext)

buffer.WriteString(strings.Repeat("\t", 2))
buffer.WriteString(strings.Repeat(" ", e.ActualToken.Col))

buffer.WriteString(color.BlueString(strings.Repeat("^", e.ActualToken.Span)))
buffer.WriteString(color.BlueString("--"))

buffer.WriteString(color.BlueString(fmt.Sprintf("Expected one of: ")))
for i, t := range nodes.GetAllFuncTypes() {
if i > 0 {
buffer.WriteString(color.BlueString(", "))
}
buffer.WriteString(color.BlueString(fmt.Sprintf("\"%s\"", t)))
}
buffer.WriteString("\n")

return buffer.String()
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/sys v0.18.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
Expand All @@ -10,6 +17,10 @@ github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IU
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
Loading

0 comments on commit bf792bc

Please sign in to comment.