errors.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package dirty
  2. import (
  3. "fmt"
  4. )
  5. type SyntaxError struct {
  6. found token
  7. expected []token
  8. }
  9. func NewSyntaxError(found token, expected []token) SyntaxError {
  10. return SyntaxError{found: found, expected: expected}
  11. }
  12. func (se SyntaxError) Error() string {
  13. return fmt.Sprintf("expected %v; got %v\n", se.expected, se.found)
  14. }
  15. type UnterminatedError struct {
  16. ttype string
  17. t string
  18. }
  19. func NewUnterminatedError(ttype string, t string) UnterminatedError {
  20. return UnterminatedError{ttype: ttype, t: t}
  21. }
  22. func (e UnterminatedError) Error() string {
  23. return fmt.Sprintf("unterminated %s ‘%s’\n", e.ttype, e.t)
  24. }
  25. type InvalidCharError struct {
  26. r rune
  27. }
  28. func NewInvalidCharError(r rune) InvalidCharError {
  29. return InvalidCharError{r: r}
  30. }
  31. func (e InvalidCharError) Error() string {
  32. return fmt.Sprintf("invalid character ‘%d’\n", e.r)
  33. }
  34. type CommaError struct {
  35. s string
  36. }
  37. func NewCommaError(s string) CommaError {
  38. return CommaError{s: s}
  39. }
  40. func (e CommaError) Error() string {
  41. return fmt.Sprintf("comma in wrong place in ‘%s’\n", e.s)
  42. }
  43. type InvalidCodepointError struct {
  44. c string
  45. }
  46. func NewInvalidCodepointError(c string) InvalidCodepointError {
  47. return InvalidCodepointError{c: c}
  48. }
  49. func (e InvalidCodepointError) Error() string {
  50. return fmt.Sprintf("invalid codepoint ‘%s’\n", e.c)
  51. }
  52. type ConstError struct {
  53. t string
  54. }
  55. func NewConstError(t string) ConstError {
  56. return ConstError{t: t}
  57. }
  58. func (e ConstError) Error() string {
  59. return fmt.Sprintf("malformed const ‘%s’\n", e.t)
  60. }
  61. type EscapeError struct {
  62. char rune
  63. }
  64. func NewEscapeError(char rune) EscapeError {
  65. return EscapeError{char: char}
  66. }
  67. func (e EscapeError) Error() string {
  68. return fmt.Sprintf("invalid escape sequence \\%v\n", e.char)
  69. }
  70. type RawStringError struct {
  71. s string
  72. }
  73. func NewRawStringError(s string) RawStringError {
  74. return RawStringError{s: s}
  75. }
  76. func (e RawStringError) Error() string {
  77. return e.s
  78. }