Browse Source

basic: refactor error handling

Signed-off-by: Alejandro Mery <amery@jpi.io>
dev-amery-decoder
Alejandro Mery 1 year ago
parent
commit
46ba96d6b4
  1. 22
      basic/error.go
  2. 4
      basic/token.go

22
basic/error.go

@ -10,24 +10,22 @@ var (
errInvalidToken = errors.New("invalid token")
)
func newErrInvalidToken(t *token) *lexer.Error {
err := &lexer.Error{
Line: t.pos.Line,
Column: t.pos.Column,
Content: t.value,
Err: errInvalidToken,
}
return err
}
func (dec *decoder) OnError(pos lexer.Position, content string, err error) error {
err = &lexer.Error{
func newError(pos lexer.Position, content, hint string, err error) *lexer.Error {
return &lexer.Error{
Line: pos.Line,
Column: pos.Column,
Content: content,
Hint: hint,
Err: err,
}
}
func newErrInvalidToken(t *token) *lexer.Error {
return newError(t.pos, t.value, "", errInvalidToken)
}
func (dec *decoder) OnError(pos lexer.Position, content string, err error) error {
err = newError(pos, content, "", err)
dec.executeFinal()
return err
}

4
basic/token.go

@ -149,6 +149,8 @@ func (dec *decoder) OnToken(pos lexer.Position, typ parser.TokenType, value stri
return nil
default:
// unacceptable
return newErrInvalidToken(t)
err := newErrInvalidToken(t)
dec.executeFinal()
return err
}
}

Loading…
Cancel
Save