parsererror.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package parser
  10. import (
  11. "errors"
  12. "fmt"
  13. )
  14. /*
  15. newParserError creates a new ParserError object.
  16. */
  17. func (p *parser) newParserError(t error, d string, token LexToken) error {
  18. return &Error{p.name, t, d, token.Lline, token.Lpos}
  19. }
  20. /*
  21. Error models a parser related error.
  22. */
  23. type Error struct {
  24. Source string // Name of the source which was given to the parser
  25. Type error // Error type (to be used for equal checks)
  26. Detail string // Details of this error
  27. Line int // Line of the error
  28. Pos int // Position of the error
  29. }
  30. /*
  31. Error returns a human-readable string representation of this error.
  32. */
  33. func (pe *Error) Error() string {
  34. var ret string
  35. if pe.Detail != "" {
  36. ret = fmt.Sprintf("Parse error in %s: %v (%v)", pe.Source, pe.Type, pe.Detail)
  37. } else {
  38. ret = fmt.Sprintf("Parse error in %s: %v", pe.Source, pe.Type)
  39. }
  40. if pe.Line != 0 {
  41. return fmt.Sprintf("%s (Line:%d Pos:%d)", ret, pe.Line, pe.Pos)
  42. }
  43. return ret
  44. }
  45. /*
  46. Parser related error types
  47. */
  48. var (
  49. ErrUnexpectedEnd = errors.New("Unexpected end")
  50. ErrLexicalError = errors.New("Lexical error")
  51. ErrUnknownToken = errors.New("Unknown term")
  52. ErrImpossibleNullDenotation = errors.New("Term cannot start an expression")
  53. ErrImpossibleLeftDenotation = errors.New("Term can only start an expression")
  54. ErrUnexpectedToken = errors.New("Unexpected term")
  55. )