go-dirty.git

ref: master

./const.go


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package dirty

import (
	"fmt"
)

type Const int

const (
	TRUE Const = iota
	FALSE
	NULL
)

func NewConst(s string) Const {
	if s == "true" {
		return TRUE
	}
	if s == "false" {
		return FALSE
	}
	if s == "null" {
		return NULL
	}
	panic("invalid const " + s)
}

func (Const) isElement() {}
func (Const) getType() ElementType {
	return ElemConst
}
func (c Const) String() string {
	if c == TRUE {
		return "true"
	}
	if c == FALSE {
		return "false"
	}
	if c == NULL {
		return "null"
	}
	panic(fmt.Sprintf("invalid const %d", c))
}
func (c Const) Bool() bool {
	if c == TRUE {
		return true
	} else if c == FALSE {
		return false
	} else {
		panic("Const is not bool")
	}
}

func parseConst(t token) (token, error) {
	if t.t != "true" && t.t != "false" && t.t != "null" {
		return token{}, NewConstError(t.t)
	}
	return t, nil
}