...
1 package token
2
3 import (
4 "fmt"
5 "strconv"
6
7 hcltoken "github.com/hashicorp/hcl/hcl/token"
8 )
9
10
11 type Token struct {
12 Type Type
13 Pos Pos
14 Text string
15 }
16
17
18 type Type int
19
20 const (
21
22 ILLEGAL Type = iota
23 EOF
24
25 identifier_beg
26 literal_beg
27 NUMBER
28 FLOAT
29 BOOL
30 STRING
31 NULL
32 literal_end
33 identifier_end
34
35 operator_beg
36 LBRACK
37 LBRACE
38 COMMA
39 PERIOD
40 COLON
41
42 RBRACK
43 RBRACE
44
45 operator_end
46 )
47
48 var tokens = [...]string{
49 ILLEGAL: "ILLEGAL",
50
51 EOF: "EOF",
52
53 NUMBER: "NUMBER",
54 FLOAT: "FLOAT",
55 BOOL: "BOOL",
56 STRING: "STRING",
57 NULL: "NULL",
58
59 LBRACK: "LBRACK",
60 LBRACE: "LBRACE",
61 COMMA: "COMMA",
62 PERIOD: "PERIOD",
63 COLON: "COLON",
64
65 RBRACK: "RBRACK",
66 RBRACE: "RBRACE",
67 }
68
69
70 func (t Type) String() string {
71 s := ""
72 if 0 <= t && t < Type(len(tokens)) {
73 s = tokens[t]
74 }
75 if s == "" {
76 s = "token(" + strconv.Itoa(int(t)) + ")"
77 }
78 return s
79 }
80
81
82
83 func (t Type) IsIdentifier() bool { return identifier_beg < t && t < identifier_end }
84
85
86
87 func (t Type) IsLiteral() bool { return literal_beg < t && t < literal_end }
88
89
90
91 func (t Type) IsOperator() bool { return operator_beg < t && t < operator_end }
92
93
94
95
96 func (t Token) String() string {
97 return fmt.Sprintf("%s %s %s", t.Pos.String(), t.Type.String(), t.Text)
98 }
99
100
101
102
103 func (t Token) HCLToken() hcltoken.Token {
104 switch t.Type {
105 case BOOL:
106 return hcltoken.Token{Type: hcltoken.BOOL, Text: t.Text}
107 case FLOAT:
108 return hcltoken.Token{Type: hcltoken.FLOAT, Text: t.Text}
109 case NULL:
110 return hcltoken.Token{Type: hcltoken.STRING, Text: ""}
111 case NUMBER:
112 return hcltoken.Token{Type: hcltoken.NUMBER, Text: t.Text}
113 case STRING:
114 return hcltoken.Token{Type: hcltoken.STRING, Text: t.Text, JSON: true}
115 default:
116 panic(fmt.Sprintf("unimplemented HCLToken for type: %s", t.Type))
117 }
118 }
119
View as plain text