error.go 589 B

1234567891011121314151617181920212223242526272829303132333435
  1. package parse
  2. import (
  3. "fmt"
  4. "io"
  5. "github.com/tdewolff/parse/buffer"
  6. )
  7. type Error struct {
  8. Message string
  9. Line int
  10. Col int
  11. Context string
  12. }
  13. func NewError(msg string, r io.Reader, offset int) *Error {
  14. line, col, context, _ := Position(r, offset)
  15. return &Error{
  16. msg,
  17. line,
  18. col,
  19. context,
  20. }
  21. }
  22. func NewErrorLexer(msg string, l *buffer.Lexer) *Error {
  23. r := buffer.NewReader(l.Bytes())
  24. offset := l.Offset()
  25. return NewError(msg, r, offset)
  26. }
  27. func (e *Error) Error() string {
  28. return fmt.Sprintf("parse error:%d:%d: %s\n%s", e.Line, e.Col, e.Message, e.Context)
  29. }