Skip to content

Commit

Permalink
Improve error position reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Aug 5, 2018
1 parent 3578406 commit 0947d4f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
18 changes: 16 additions & 2 deletions doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ func ExampleEval_error() {
//----------^
}

func ExampleEval_matches() {
output, err := expr.Eval(`"a" matches "a("`, nil)

if err != nil {
fmt.Printf("err: %v", err)
return
}

fmt.Printf("%v", output)
// Output: err: error parsing regexp: missing closing ): `a(`
//"a" matches "a("
//----------------^
}

func ExampleParse() {
env := map[string]interface{}{
"foo": 1,
Expand Down Expand Up @@ -128,7 +142,7 @@ func ExampleNames() {

// Output: err: unknown name baz
//foo + bar + baz
//---------------^
//------------^
}

func ExampleFuncs() {
Expand All @@ -141,7 +155,7 @@ func ExampleFuncs() {

// Output: err: unknown func baz
//foo(bar(baz()))
//-----------^
//--------^
}

func ExampleNode() {
Expand Down
5 changes: 5 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ func (e *syntaxError) Error() string {
}
return e.message + snippet
}

func (e *syntaxError) at(t token) error {
e.pos = t.pos
return e
}
10 changes: 5 additions & 5 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func Funcs(funcs ...string) OptionFn {
}
}

func (p *parser) errorf(format string, args ...interface{}) error {
func (p *parser) errorf(format string, args ...interface{}) *syntaxError {
return &syntaxError{
message: fmt.Sprintf(format, args...),
input: p.input,
Expand Down Expand Up @@ -309,7 +309,7 @@ func (p *parser) parsePrimaryExpression() (Node, error) {
} else {
if p.options.funcs != nil {
if _, ok := p.options.funcs[token.value]; !ok {
return nil, p.errorf("unknown func %v", token.value)
return nil, p.errorf("unknown func %v", token.value).at(token)
}
}
arguments, err := p.parseArguments()
Expand All @@ -321,7 +321,7 @@ func (p *parser) parsePrimaryExpression() (Node, error) {
} else {
if p.options.names != nil {
if _, ok := p.options.names[token.value]; !ok {
return nil, p.errorf("unknown name %v", token.value)
return nil, p.errorf("unknown name %v", token.value).at(token)
}
}
node = nameNode{name: token.value}
Expand Down Expand Up @@ -356,7 +356,7 @@ func (p *parser) parsePrimaryExpression() (Node, error) {
return nil, err
}
} else {
return nil, p.errorf("unexpected token %v", token)
return nil, p.errorf("unexpected token %v", token).at(token)
}
}

Expand Down Expand Up @@ -453,7 +453,7 @@ func (p *parser) parsePostfixExpression(node Node) (Node, error) {
// As a result, if token is NOT an operator OR token.value is NOT a valid property or method name,
// an error shall be returned.
(token.kind != operator || !isValidIdentifier(token.value)) {
return nil, p.errorf("expected name")
return nil, p.errorf("expected name").at(token)
}

property := identifierNode{value: token.value}
Expand Down

0 comments on commit 0947d4f

Please sign in to comment.