-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.go
131 lines (112 loc) · 2.54 KB
/
token.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package validator
import (
"fmt"
)
// tokenType is the type of type emitted by the lexer
type tokenType int8
// MarshalText implements encoding.TextMarshaler
func (t *tokenType) MarshalText() ([]byte, error) {
switch *t {
case typeError:
return []byte("typeError"), nil
case typeEOF:
return []byte("typeEOF"), nil
case typeAnd:
return []byte("typeAnd"), nil
case typeOr:
return []byte("typeOr"), nil
case typeFunction:
return []byte("typeFunction"), nil
case typeColon:
return []byte("typeColon"), nil
case typeComma:
return []byte("typeComma"), nil
case typeOpenParen:
return []byte("typeOpenParen"), nil
case typeCloseParen:
return []byte("typeCloseParen"), nil
case typeBool:
return []byte("typeBool"), nil
case typeNumber:
return []byte("typeNumber"), nil
case typeString:
return []byte("typeString"), nil
case typeSpace:
return []byte("typeSpace"), nil
}
return nil, fmt.Errorf("not a valid type")
}
func (t tokenType) String() string {
bs, err := t.MarshalText()
if err != nil {
return err.Error()
}
return string(bs)
}
const (
// typeError represents an error
typeError = tokenType(iota)
// typeEOF is the end of the file
typeEOF
// typeAnd is `&&`
typeAnd
// typeOr is `||`
typeOr
// typeFunction is a method signature
typeFunction
// typeColon is `:`
typeColon
// typeComma is `,`
typeComma
// typeOpenParen is `(`
typeOpenParen
// typeCloseParen is `)`
typeCloseParen
// typeBool is a boolean
typeBool
// typeNumber is a number
typeNumber
// typeString is a string surrounded by a `"` or `'`
typeString
// typeSpace is white space
typeSpace
)
// type is a type emitted by the lexer
type token struct {
typ tokenType
val string
}
func (t token) String() string {
switch t.typ {
case typeEOF:
return "EOF"
case typeError:
return t.val
case typeAnd:
return fmt.Sprintf("and: %s", t.val)
case typeOr:
return fmt.Sprintf("or: %s", t.val)
case typeFunction:
return fmt.Sprintf("function: %s", t.val)
case typeColon:
return fmt.Sprintf("colon: %s", t.val)
case typeComma:
return fmt.Sprintf("comma: %s", t.val)
case typeOpenParen:
return fmt.Sprintf("open paren: %s", t.val)
case typeCloseParen:
return fmt.Sprintf("close paren: %s", t.val)
case typeBool:
return fmt.Sprintf("bool: %s", t.val)
case typeNumber:
return fmt.Sprintf("number: %s", t.val)
case typeString:
return fmt.Sprintf("string: %s", t.val)
case typeSpace:
return fmt.Sprintf("space: %s", t.val)
}
if len(t.val) > 10 {
return fmt.Sprintf("%.10s...", t.val)
}
return fmt.Sprintf("%s", t.val)
}