-
Notifications
You must be signed in to change notification settings - Fork 10
/
syntaxerror.go
49 lines (41 loc) · 1.01 KB
/
syntaxerror.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
package runtime
import (
"errors"
"fmt"
"github.com/arnodel/golua/parsing"
"github.com/arnodel/golua/token"
)
// A SyntaxError is a lua syntax error.
type SyntaxError struct {
File string
Err parsing.Error
}
// NewSyntaxError returns a pointer to a SyntaxError for the error err in file.
func NewSyntaxError(file string, err parsing.Error) *SyntaxError {
return &SyntaxError{
File: file,
Err: err,
}
}
// Error implements the error interface.
func (e *SyntaxError) Error() string {
return fmt.Sprintf("%s:%s", e.File, e.Err)
}
// IsUnexpectedEOF returns true if the error signals that EOF was encountered
// when further tokens were required.
func (e *SyntaxError) IsUnexpectedEOF() bool {
switch e.Err.Got.Type {
case token.EOF, token.UNFINISHED:
return true
default:
return false
}
}
func ErrorIsUnexpectedEOF(err error) bool {
snErr, ok := AsSyntaxError(err)
return ok && snErr.IsUnexpectedEOF()
}
func AsSyntaxError(err error) (snErr *SyntaxError, ok bool) {
ok = errors.As(err, &snErr)
return
}