error.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package source
  2. import (
  3. "strings"
  4. "math/big"
  5. "kumachan/standalone/util/richtext"
  6. )
  7. type Errors ([] *Error)
  8. type Error struct {
  9. Location Location
  10. Content ErrorContent
  11. Depth uint
  12. }
  13. type ErrorContent interface {
  14. DescribeError() richtext.Block
  15. }
  16. type LocatableErrorMessage struct {
  17. Location Location
  18. Content richtext.Block
  19. }
  20. type SerializableErrorMessage struct {
  21. Content richtext.Block
  22. FilePath string
  23. PosText string
  24. PosIndex1 *big.Int
  25. PosIndex2 *big.Int
  26. }
  27. // ----------------------
  28. func ErrorsFrom(err *Error) Errors {
  29. if err != nil {
  30. return Errors([] *Error { err })
  31. } else {
  32. return nil
  33. }
  34. }
  35. func ErrorsJoin(errs *Errors, err *Error) {
  36. if err != nil {
  37. *errs = append(*errs, err)
  38. }
  39. }
  40. func ErrorsJoinAll(errs *Errors, another Errors) {
  41. for _, err := range another {
  42. *errs = append(*errs, err)
  43. }
  44. }
  45. func (errs Errors) Message() richtext.Block {
  46. if len(errs) == 0 {
  47. panic("invalid operation")
  48. }
  49. var b richtext.Block
  50. for _, item := range errs {
  51. b.Append(item.Message())
  52. }
  53. return b
  54. }
  55. func (errs Errors) Error() string {
  56. if len(errs) == 0 {
  57. panic("invalid operation")
  58. }
  59. var all = make([] string, len(errs))
  60. for i, item := range errs {
  61. all[i] = item.Error()
  62. }
  63. return strings.Join(all, "\n")
  64. }
  65. // ----------------------
  66. func MakeError(loc Location, content ErrorContent) *Error {
  67. return &Error {
  68. Location: loc,
  69. Content: content,
  70. }
  71. }
  72. func (e *Error) IncreaseDepth() *Error {
  73. e.Depth += 1
  74. return e
  75. }
  76. func (e *Error) ToLocatable() LocatableErrorMessage {
  77. return LocatableErrorMessage {
  78. Location: e.Location,
  79. Content: e.Content.DescribeError(),
  80. }
  81. }
  82. func (e *Error) Message() richtext.Block {
  83. var m = e.ToLocatable()
  84. return m.ToBlock()
  85. }
  86. func (e *Error) Error() string {
  87. return e.Message().RenderPlainText()
  88. }
  89. // ----------------------
  90. func (msg *LocatableErrorMessage) ToBlock() richtext.Block {
  91. return msg.Location.FormatMessage(msg.Content)
  92. }
  93. func (msg *LocatableErrorMessage) ToSerializable() *SerializableErrorMessage {
  94. return &SerializableErrorMessage {
  95. Content: msg.Content,
  96. FilePath: msg.Location.File.GetPath(),
  97. PosText: msg.Location.File.DescribePosition(msg.Location.Pos),
  98. PosIndex1: big.NewInt(int64(msg.Location.Pos.Index1)),
  99. PosIndex2: big.NewInt(int64(msg.Location.Pos.Index2)),
  100. }
  101. }