123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- package ast
- import (
- "strconv"
- "strings"
- "time"
- )
- type Position struct {
- Begin int
- End int
- }
- type Value interface {
- Pos() int
- End() int
- Source() string
- }
- type String struct {
- Position Position
- Value string
- Data []rune
- }
- func (s *String) Pos() int {
- return s.Position.Begin
- }
- func (s *String) End() int {
- return s.Position.End
- }
- func (s *String) Source() string {
- return string(s.Data)
- }
- type Integer struct {
- Position Position
- Value string
- Data []rune
- }
- func (i *Integer) Pos() int {
- return i.Position.Begin
- }
- func (i *Integer) End() int {
- return i.Position.End
- }
- func (i *Integer) Source() string {
- return string(i.Data)
- }
- func (i *Integer) Int() (int64, error) {
- return strconv.ParseInt(i.Value, 10, 64)
- }
- type Float struct {
- Position Position
- Value string
- Data []rune
- }
- func (f *Float) Pos() int {
- return f.Position.Begin
- }
- func (f *Float) End() int {
- return f.Position.End
- }
- func (f *Float) Source() string {
- return string(f.Data)
- }
- func (f *Float) Float() (float64, error) {
- return strconv.ParseFloat(f.Value, 64)
- }
- type Boolean struct {
- Position Position
- Value string
- Data []rune
- }
- func (b *Boolean) Pos() int {
- return b.Position.Begin
- }
- func (b *Boolean) End() int {
- return b.Position.End
- }
- func (b *Boolean) Source() string {
- return string(b.Data)
- }
- func (b *Boolean) Boolean() (bool, error) {
- return strconv.ParseBool(b.Value)
- }
- type Datetime struct {
- Position Position
- Value string
- Data []rune
- }
- func (d *Datetime) Pos() int {
- return d.Position.Begin
- }
- func (d *Datetime) End() int {
- return d.Position.End
- }
- func (d *Datetime) Source() string {
- return string(d.Data)
- }
- var timeFormats = [...]string{
- "2006-01-02T15:04:05.999999999Z07:00",
- "2006-01-02T15:04:05.999999999",
- }
- func (d *Datetime) Time() (time.Time, error) {
- switch {
- case !strings.Contains(d.Value, ":"):
- return time.Parse("2006-01-02", d.Value)
- case !strings.Contains(d.Value, "-"):
- return time.Parse("15:04:05.999999999", d.Value)
- default:
- var t time.Time
- var err error
- for _, format := range timeFormats {
- if t, err = time.Parse(format, d.Value); err == nil {
- return t, nil
- }
- }
- return t, err
- }
- }
- type Array struct {
- Position Position
- Value []Value
- Data []rune
- }
- func (a *Array) Pos() int {
- return a.Position.Begin
- }
- func (a *Array) End() int {
- return a.Position.End
- }
- func (a *Array) Source() string {
- return string(a.Data)
- }
- type TableType uint8
- const (
- TableTypeNormal TableType = iota
- TableTypeArray
- )
- var tableTypes = [...]string{
- "normal",
- "array",
- }
- func (t TableType) String() string {
- return tableTypes[t]
- }
- type Table struct {
- Position Position
- Line int
- Name string
- Fields map[string]interface{}
- Type TableType
- Data []rune
- }
- func (t *Table) Pos() int {
- return t.Position.Begin
- }
- func (t *Table) End() int {
- return t.Position.End
- }
- func (t *Table) Source() string {
- return string(t.Data)
- }
- type KeyValue struct {
- Key string
- Value Value
- Line int
- }
|