ast.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. var timeFormats = [...]string{
  96. "2006-01-02T15:04:05.999999999Z07:00",
  97. "2006-01-02T15:04:05.999999999",
  98. }
  99. func (d *Datetime) Time() (time.Time, error) {
  100. switch {
  101. case !strings.Contains(d.Value, ":"):
  102. return time.Parse("2006-01-02", d.Value)
  103. case !strings.Contains(d.Value, "-"):
  104. return time.Parse("15:04:05.999999999", d.Value)
  105. default:
  106. var t time.Time
  107. var err error
  108. for _, format := range timeFormats {
  109. if t, err = time.Parse(format, d.Value); err == nil {
  110. return t, nil
  111. }
  112. }
  113. return t, err
  114. }
  115. }
  116. type Array struct {
  117. Position Position
  118. Value []Value
  119. Data []rune
  120. }
  121. func (a *Array) Pos() int {
  122. return a.Position.Begin
  123. }
  124. func (a *Array) End() int {
  125. return a.Position.End
  126. }
  127. func (a *Array) Source() string {
  128. return string(a.Data)
  129. }
  130. type TableType uint8
  131. const (
  132. TableTypeNormal TableType = iota
  133. TableTypeArray
  134. )
  135. var tableTypes = [...]string{
  136. "normal",
  137. "array",
  138. }
  139. func (t TableType) String() string {
  140. return tableTypes[t]
  141. }
  142. type Table struct {
  143. Position Position
  144. Line int
  145. Name string
  146. Fields map[string]interface{}
  147. Type TableType
  148. Data []rune
  149. }
  150. func (t *Table) Pos() int {
  151. return t.Position.Begin
  152. }
  153. func (t *Table) End() int {
  154. return t.Position.End
  155. }
  156. func (t *Table) Source() string {
  157. return string(t.Data)
  158. }
  159. type KeyValue struct {
  160. Key string
  161. Value Value
  162. Line int
  163. }