ast.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package ast
  2. import (
  3. "strconv"
  4. "strings"
  5. "time"
  6. )
  7. type Position struct {
  8. Begin int
  9. End int
  10. }
  11. type Value interface {
  12. Pos() int
  13. End() int
  14. Source() string
  15. }
  16. type String struct {
  17. Position Position
  18. Value string
  19. Data []rune
  20. }
  21. func (s *String) Pos() int {
  22. return s.Position.Begin
  23. }
  24. func (s *String) End() int {
  25. return s.Position.End
  26. }
  27. func (s *String) Source() string {
  28. return string(s.Data)
  29. }
  30. type Integer struct {
  31. Position Position
  32. Value string
  33. Data []rune
  34. }
  35. func (i *Integer) Pos() int {
  36. return i.Position.Begin
  37. }
  38. func (i *Integer) End() int {
  39. return i.Position.End
  40. }
  41. func (i *Integer) Source() string {
  42. return string(i.Data)
  43. }
  44. func (i *Integer) Int() (int64, error) {
  45. return strconv.ParseInt(i.Value, 10, 64)
  46. }
  47. type Float struct {
  48. Position Position
  49. Value string
  50. Data []rune
  51. }
  52. func (f *Float) Pos() int {
  53. return f.Position.Begin
  54. }
  55. func (f *Float) End() int {
  56. return f.Position.End
  57. }
  58. func (f *Float) Source() string {
  59. return string(f.Data)
  60. }
  61. func (f *Float) Float() (float64, error) {
  62. return strconv.ParseFloat(f.Value, 64)
  63. }
  64. type Boolean struct {
  65. Position Position
  66. Value string
  67. Data []rune
  68. }
  69. func (b *Boolean) Pos() int {
  70. return b.Position.Begin
  71. }
  72. func (b *Boolean) End() int {
  73. return b.Position.End
  74. }
  75. func (b *Boolean) Source() string {
  76. return string(b.Data)
  77. }
  78. func (b *Boolean) Boolean() (bool, error) {
  79. return strconv.ParseBool(b.Value)
  80. }
  81. type Datetime struct {
  82. Position Position
  83. Value string
  84. Data []rune
  85. }
  86. func (d *Datetime) Pos() int {
  87. return d.Position.Begin
  88. }
  89. func (d *Datetime) End() int {
  90. return d.Position.End
  91. }
  92. func (d *Datetime) Source() string {
  93. return string(d.Data)
  94. }
  95. func (d *Datetime) Time() (time.Time, error) {
  96. switch {
  97. case !strings.Contains(d.Value, ":"):
  98. return time.Parse("2006-01-02", d.Value)
  99. case !strings.Contains(d.Value, "-"):
  100. return time.Parse("15:04:05.999999999", d.Value)
  101. default:
  102. return time.Parse(time.RFC3339Nano, d.Value)
  103. }
  104. }
  105. type Array struct {
  106. Position Position
  107. Value []Value
  108. Data []rune
  109. }
  110. func (a *Array) Pos() int {
  111. return a.Position.Begin
  112. }
  113. func (a *Array) End() int {
  114. return a.Position.End
  115. }
  116. func (a *Array) Source() string {
  117. return string(a.Data)
  118. }
  119. type TableType uint8
  120. const (
  121. TableTypeNormal TableType = iota
  122. TableTypeArray
  123. )
  124. var tableTypes = [...]string{
  125. "normal",
  126. "array",
  127. }
  128. func (t TableType) String() string {
  129. return tableTypes[t]
  130. }
  131. type Table struct {
  132. Position Position
  133. Line int
  134. Name string
  135. Fields map[string]interface{}
  136. Type TableType
  137. Data []rune
  138. }
  139. func (t *Table) Pos() int {
  140. return t.Position.Begin
  141. }
  142. func (t *Table) End() int {
  143. return t.Position.End
  144. }
  145. func (t *Table) Source() string {
  146. return string(t.Data)
  147. }
  148. type KeyValue struct {
  149. Key string
  150. Value Value
  151. Line int
  152. }