parsererror.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package parser
  11. import (
  12. "errors"
  13. "fmt"
  14. )
  15. /*
  16. newParserError creates a new ParserError object.
  17. */
  18. func (p *parser) newParserError(t error, d string, token LexToken) error {
  19. return &Error{p.name, t, d, token.Lline, token.Lpos}
  20. }
  21. /*
  22. Error models a parser related error.
  23. */
  24. type Error struct {
  25. Source string // Name of the source which was given to the parser
  26. Type error // Error type (to be used for equal checks)
  27. Detail string // Details of this error
  28. Line int // Line of the error
  29. Pos int // Position of the error
  30. }
  31. /*
  32. Error returns a human-readable string representation of this error.
  33. */
  34. func (pe *Error) Error() string {
  35. var ret string
  36. if pe.Detail != "" {
  37. ret = fmt.Sprintf("Parse error in %s: %v (%v)", pe.Source, pe.Type, pe.Detail)
  38. } else {
  39. ret = fmt.Sprintf("Parse error in %s: %v", pe.Source, pe.Type)
  40. }
  41. if pe.Line != 0 {
  42. return fmt.Sprintf("%s (Line:%d Pos:%d)", ret, pe.Line, pe.Pos)
  43. }
  44. return ret
  45. }
  46. /*
  47. Parser related error types
  48. */
  49. var (
  50. ErrUnexpectedEnd = errors.New("Unexpected end")
  51. ErrLexicalError = errors.New("Lexical error")
  52. ErrUnknownToken = errors.New("Unknown term")
  53. ErrImpossibleNullDenotation = errors.New("Term cannot start an expression")
  54. ErrImpossibleLeftDenotation = errors.New("Term can only start an expression")
  55. ErrUnexpectedToken = errors.New("Unexpected term")
  56. )