Skip to content

Commit 0947d4f

Browse files
committed
Improve error position reporting
1 parent 3578406 commit 0947d4f

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

Diff for: doc_test.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ func ExampleEval_error() {
7070
//----------^
7171
}
7272

73+
func ExampleEval_matches() {
74+
output, err := expr.Eval(`"a" matches "a("`, nil)
75+
76+
if err != nil {
77+
fmt.Printf("err: %v", err)
78+
return
79+
}
80+
81+
fmt.Printf("%v", output)
82+
// Output: err: error parsing regexp: missing closing ): `a(`
83+
//"a" matches "a("
84+
//----------------^
85+
}
86+
7387
func ExampleParse() {
7488
env := map[string]interface{}{
7589
"foo": 1,
@@ -128,7 +142,7 @@ func ExampleNames() {
128142

129143
// Output: err: unknown name baz
130144
//foo + bar + baz
131-
//---------------^
145+
//------------^
132146
}
133147

134148
func ExampleFuncs() {
@@ -141,7 +155,7 @@ func ExampleFuncs() {
141155

142156
// Output: err: unknown func baz
143157
//foo(bar(baz()))
144-
//-----------^
158+
//--------^
145159
}
146160

147161
func ExampleNode() {

Diff for: error.go

+5
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ func (e *syntaxError) Error() string {
1818
}
1919
return e.message + snippet
2020
}
21+
22+
func (e *syntaxError) at(t token) error {
23+
e.pos = t.pos
24+
return e
25+
}

Diff for: parser.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func Funcs(funcs ...string) OptionFn {
124124
}
125125
}
126126

127-
func (p *parser) errorf(format string, args ...interface{}) error {
127+
func (p *parser) errorf(format string, args ...interface{}) *syntaxError {
128128
return &syntaxError{
129129
message: fmt.Sprintf(format, args...),
130130
input: p.input,
@@ -309,7 +309,7 @@ func (p *parser) parsePrimaryExpression() (Node, error) {
309309
} else {
310310
if p.options.funcs != nil {
311311
if _, ok := p.options.funcs[token.value]; !ok {
312-
return nil, p.errorf("unknown func %v", token.value)
312+
return nil, p.errorf("unknown func %v", token.value).at(token)
313313
}
314314
}
315315
arguments, err := p.parseArguments()
@@ -321,7 +321,7 @@ func (p *parser) parsePrimaryExpression() (Node, error) {
321321
} else {
322322
if p.options.names != nil {
323323
if _, ok := p.options.names[token.value]; !ok {
324-
return nil, p.errorf("unknown name %v", token.value)
324+
return nil, p.errorf("unknown name %v", token.value).at(token)
325325
}
326326
}
327327
node = nameNode{name: token.value}
@@ -356,7 +356,7 @@ func (p *parser) parsePrimaryExpression() (Node, error) {
356356
return nil, err
357357
}
358358
} else {
359-
return nil, p.errorf("unexpected token %v", token)
359+
return nil, p.errorf("unexpected token %v", token).at(token)
360360
}
361361
}
362362

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

459459
property := identifierNode{value: token.value}

0 commit comments

Comments
 (0)