-
Notifications
You must be signed in to change notification settings - Fork 19
/
errors.go
77 lines (62 loc) · 2.18 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package excellent
import (
"fmt"
"strings"
"github.com/antlr/antlr4/runtime/Go/antlr/v4"
"github.com/nyaruka/goflow/utils"
"github.com/pkg/errors"
)
// TemplateError is an error which occurs during evaluation of an expression
type TemplateError struct {
expression string
message string
}
func (e TemplateError) Error() string {
return fmt.Sprintf("error evaluating %s: %s", e.expression, e.message)
}
// TemplateErrors represents the list of all errors encountered during evaluation of a template
type TemplateErrors struct {
errors []*TemplateError
}
// NewTemplateErrors creates a new empty lsit of template errors
func NewTemplateErrors() *TemplateErrors {
return &TemplateErrors{}
}
// Add adds an error for the given expression
func (e *TemplateErrors) Add(expression, message string) {
e.errors = append(e.errors, &TemplateError{expression: expression, message: message})
}
// HasErrors returns whether there are errors
func (e *TemplateErrors) HasErrors() bool {
return len(e.errors) > 0
}
// Error returns a single string describing all the errors encountered
func (e *TemplateErrors) Error() string {
messages := make([]string, len(e.errors))
for i, err := range e.errors {
messages[i] = err.Error()
}
return strings.Join(messages, ", ")
}
// ErrorListener records syntax errors
type ErrorListener struct {
*antlr.DefaultErrorListener
expression string
errors []error
}
// NewErrorListener creates a new error listener
func NewErrorListener(expression string) *ErrorListener {
return &ErrorListener{expression: expression}
}
// Errors returns the errors encountered so far
func (l *ErrorListener) Errors() []error {
return l.errors
}
// SyntaxError handles a new syntax error encountered by the recognizer
func (l *ErrorListener) SyntaxError(recognizer antlr.Recognizer, offendingSymbol interface{}, line, column int, msg string, e antlr.RecognitionException) {
// extract the part of the original expression where this error has occurred
lines := strings.Split(l.expression, "\n")
lineOfError := lines[line-1]
contextOfError := lineOfError[column:utils.Min(column+10, len(lineOfError))]
l.errors = append(l.errors, errors.Errorf("syntax error at %s", contextOfError))
}