Skip to content

Commit

Permalink
syntax errors with proper line and column
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Oct 20, 2016
1 parent ae299ef commit f1bc9b2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
4 changes: 2 additions & 2 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ type Location struct {

func Errorf(format string, a ...interface{}) *GraphQLError {
return &GraphQLError{
Message: fmt.Sprintf(format, a),
Message: fmt.Sprintf(format, a...),
}
}

func ErrorfWithLoc(line int, column int, format string, a ...interface{}) *GraphQLError {
return &GraphQLError{
Message: fmt.Sprintf(format, a),
Message: fmt.Sprintf(format, a...),
Locations: []*Location{{
Line: line,
Column: column,
Expand Down
21 changes: 19 additions & 2 deletions internal/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"fmt"
"strconv"
"text/scanner"

"github.com/neelance/graphql-go/errors"
)

type SyntaxError string
type syntaxError string

type Lexer struct {
sc *scanner.Scanner
Expand All @@ -19,6 +21,21 @@ func New(sc *scanner.Scanner) *Lexer {
return l
}

func (l *Lexer) CatchSyntaxError(f func()) (errRes *errors.GraphQLError) {
defer func() {
if err := recover(); err != nil {
if err, ok := err.(syntaxError); ok {
errRes = errors.ErrorfWithLoc(l.sc.Line, l.sc.Column, "syntax error: %s", err)
return
}
panic(err)
}
}()

f()
return
}

func (l *Lexer) Peek() rune {
return l.next
}
Expand Down Expand Up @@ -72,5 +89,5 @@ func (l *Lexer) ConsumeToken(expected rune) {
}

func (l *Lexer) SyntaxError(message string) {
panic(SyntaxError(fmt.Sprintf("%s:%d: syntax error: %s", l.sc.Filename, l.sc.Line, message)))
panic(syntaxError(message))
}
18 changes: 6 additions & 12 deletions internal/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,17 @@ type Literal struct {
func (Variable) isValue() {}
func (Literal) isValue() {}

func Parse(queryString string) (res *Document, errRes *errors.GraphQLError) {
func Parse(queryString string) (doc *Document, err *errors.GraphQLError) {
sc := &scanner.Scanner{
Mode: scanner.ScanIdents | scanner.ScanFloats | scanner.ScanStrings,
}
sc.Init(strings.NewReader(queryString))

defer func() {
if err := recover(); err != nil {
if err, ok := err.(lexer.SyntaxError); ok {
errRes = errors.Errorf("%s", string(err))
return
}
panic(err)
}
}()

return parseDocument(lexer.New(sc)), nil
l := lexer.New(sc)
err = l.CatchSyntaxError(func() {
doc = parseDocument(l)
})
return
}

func parseDocument(l *lexer.Lexer) *Document {
Expand Down
20 changes: 8 additions & 12 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,19 @@ type Parameter struct {
Default string
}

func Parse(schemaString string) (res *Schema, errRes *errors.GraphQLError) {
func Parse(schemaString string) (s *Schema, err *errors.GraphQLError) {
sc := &scanner.Scanner{
Mode: scanner.ScanIdents | scanner.ScanFloats | scanner.ScanStrings,
}
sc.Init(strings.NewReader(schemaString))

defer func() {
if err := recover(); err != nil {
if err, ok := err.(lexer.SyntaxError); ok {
errRes = errors.Errorf("%s", string(err))
return
}
panic(err)
}
}()

s := parseSchema(lexer.New(sc))
l := lexer.New(sc)
err = l.CatchSyntaxError(func() {
s = parseSchema(l)
})
if err != nil {
return nil, err
}

for _, obj := range s.Objects {
if obj.Implements != "" {
Expand Down

0 comments on commit f1bc9b2

Please sign in to comment.