parsererrors.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. return fmt.Sprintf("%s (Line:%d Pos:%d)", ret, pe.Line, pe.Pos)
  41. }
  42. /*
  43. Parser related error types
  44. */
  45. var (
  46. ErrImpossibleLeftDenotation = errors.New("Term can only start an expression")
  47. ErrImpossibleNullDenotation = errors.New("Term cannot start an expression")
  48. ErrLexicalError = errors.New("Lexical error")
  49. ErrNameExpected = errors.New("Name expected")
  50. ErrOnExpected = errors.New("Type condition starting with 'on' expected")
  51. ErrSelectionSetExpected = errors.New("Selection Set expected")
  52. ErrMultipleShorthand = errors.New("Query shorthand only allowed for one query operation")
  53. ErrUnexpectedEnd = errors.New("Unexpected end")
  54. ErrUnexpectedToken = errors.New("Unexpected term")
  55. ErrUnknownToken = errors.New("Unknown term")
  56. ErrValueOrVariableExpected = errors.New("Value or variable expected")
  57. ErrVariableExpected = errors.New("Variable expected")
  58. )