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") errInvalidToken = errors.New("invalid token")
) )
func newErrInvalidToken(t *token) *lexer.Error { func newError(pos lexer.Position, content, hint string, err error) *lexer.Error {
err := &lexer.Error{ return &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{
Line: pos.Line, Line: pos.Line,
Column: pos.Column, Column: pos.Column,
Content: content, Content: content,
Hint: hint,
Err: err, 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() dec.executeFinal()
return err return err
} }

4
basic/token.go

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

Loading…
Cancel
Save