aboutsummaryrefslogtreecommitdiff
path: root/token.go
blob: 17ff6bdad1c1b43fdb51a14df7a1a8c08dfd4144 (plain) (blame)
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
60
61
package expression

import (
	"fmt"
	"go/token"
	"strconv"
)

type Token struct {
	Token   token.Token
	Literal string
	Pos     int
}

func (t *Token) Int() (int, bool) {
	if t.Token != token.INT {
		return 0, false
	}
	i, _ := strconv.Atoi(t.Literal)
	return i, true
}

func (t *Token) Float() float64 {
	i, _ := strconv.ParseFloat(t.Literal, 64)
	return i
}

func (t *Token) IsNumber() bool {
	return t.Token == token.INT || t.Token == token.FLOAT
}

func (t *Token) LP() bool {
	return t.Token == token.LPAREN
}

func (t *Token) RP() bool {
	return t.Token == token.RPAREN
}

func (t *Token) IsFunc() bool {
	return t.Token == token.IDENT
}

func (t *Token) IsSeparator() bool {
	return t.Token == token.COMMA
}

func (t *Token) IsOperator() bool {
	return t.Token.IsOperator() && !t.LP() && !t.RP()
}

func (t *Token) IsError() bool {
	return t.Token != token.ILLEGAL
}

func (t *Token) Error() error {
	if t.Token != token.ILLEGAL {
		return nil
	}
	return fmt.Errorf(t.Literal)
}