go-parsekit/parsehandler_error.go

38 lines
1.0 KiB
Go

package parsekit
import (
"fmt"
)
// Error is used as the error type when parsing errors occur.
// The error includes some context information to allow for useful
// error messages to the user.
type Error struct {
Message string
Line int
Column int
}
func (err *Error) Error() string {
return err.Message
}
// Full returns the current error message, including information about
// the position in the input where the error occurred.
func (err *Error) Full() string {
if err.Line == 0 {
return fmt.Sprintf("%s at start of file", err)
} else {
return fmt.Sprintf("%s at line %d, column %d", err, err.Line, err.Column)
}
}
// Error sets the error message in the parser API. This error message
// will eventually be returned by the Parser.Execute() method.
func (p *ParseAPI) Error(format string, args ...interface{}) {
// No call to p.panicWhenStoppedOrInError(), to allow a parser to
// set a different error message when needed.
message := fmt.Sprintf(format, args...)
p.err = &Error{message, p.tokenAPI.cursor.Line, p.tokenAPI.cursor.Column}
}