Skip to content

Commit 95a9627

Browse files
lvan100lianghuan
authored andcommitted
111
1 parent cd11901 commit 95a9627

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

lib/vidl/parser.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,22 @@ func (e InnerExpr) Text() string {
107107
}
108108

109109
// Parse parses the input string and returns an Expr AST.
110-
func Parse(s string) (expr Expr, err error) {
111-
e := &ErrorListener{}
110+
func Parse(data string) (expr Expr, err error) {
111+
e := &ErrorListener{Data: data}
112112

113-
// Recover from panics to return a proper error
113+
// Recover from parser panics to provide better error reporting
114114
defer func() {
115115
if r := recover(); r != nil {
116116
expr = nil
117117
err = fmt.Errorf("[PANIC]: %v\n%s", r, debug.Stack())
118-
if e.err != nil {
119-
err = fmt.Errorf("%w\n%w", e.err, err)
118+
if e.Error != nil {
119+
err = fmt.Errorf("%w\n%w", e.Error, err)
120120
}
121121
}
122122
}()
123123

124124
// Step 1: Create lexer and token stream
125-
input := antlr.NewInputStream(s)
125+
input := antlr.NewInputStream(data)
126126
lexer := NewVLexer(input)
127127
lexer.RemoveErrorListeners()
128128
lexer.AddErrorListener(e)
@@ -141,25 +141,26 @@ func Parse(s string) (expr Expr, err error) {
141141
antlr.ParseTreeWalkerDefault.Walk(l, p.ValidateExpr())
142142

143143
// Step 4: Return parsed expression or error
144-
if e.err != nil {
145-
return nil, e.err
144+
if e.Error != nil {
145+
return nil, e.Error
146146
}
147147
return l.Expr, nil
148148
}
149149

150150
// ErrorListener implements a custom ANTLR error listener that records syntax errors.
151151
type ErrorListener struct {
152152
*antlr.DefaultErrorListener
153-
err error
153+
Error error
154+
Data string
154155
}
155156

156157
// SyntaxError is called by ANTLR when a syntax error occurs.
157158
func (l *ErrorListener) SyntaxError(_ antlr.Recognizer, _ any, line, column int, msg string, e antlr.RecognitionException) {
158-
if l.err == nil {
159-
l.err = fmt.Errorf("line %d:%d %s", line, column, msg)
159+
if l.Error == nil {
160+
l.Error = fmt.Errorf("line %d:%d %s << text: %q", line, column, msg, l.Data)
160161
return
161162
}
162-
l.err = fmt.Errorf("%w\nline %d:%d %s", l.err, line, column, msg)
163+
l.Error = fmt.Errorf("%w\nline %d:%d %s << text: %q", l.Error, line, column, msg, l.Data)
163164
}
164165

165166
// ParseTreeListener walks the parse tree and constructs the expression AST.

lib/vidl/parser_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,22 +280,22 @@ func TestParse(t *testing.T) {
280280
{
281281
name: "syntax_error_missing_paren",
282282
input: "(a",
283-
expectError: errors.New("line 1:2 missing ')' at '<EOF>'"),
283+
expectError: errors.New("line 1:2 missing ')' at '<EOF>' << text: \"(a\""),
284284
},
285285
{
286286
name: "syntax_error_unexpected_token",
287287
input: "a + b",
288-
expectError: errors.New("line 1:2 token recognition error at: '+ '"),
288+
expectError: errors.New("line 1:2 token recognition error at: '+ ' << text: \"a + b\""),
289289
},
290290
{
291291
name: "syntax_error_empty",
292292
input: "",
293-
expectError: errors.New("line 1:0 mismatched input '<EOF>' expecting {'$', 'nil', '!', '(', STRING, INTEGER, FLOAT, IDENTIFIER}"),
293+
expectError: errors.New("line 1:0 mismatched input '<EOF>' expecting {'$', 'nil', '!', '(', STRING, INTEGER, FLOAT, IDENTIFIER} << text: \"\""),
294294
},
295295
{
296296
name: "syntax_error_missing_comma_in_function",
297297
input: "func(a b)",
298-
expectError: errors.New("line 1:7 extraneous input 'b' expecting {')', ','}"),
298+
expectError: errors.New("line 1:7 extraneous input 'b' expecting {')', ','} << text: \"func(a b)\""),
299299
},
300300
}
301301

0 commit comments

Comments
 (0)