OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / hashicorp / hcl / json / token / token.go
1 package token
2
3 import (
4         "fmt"
5         "strconv"
6
7         hcltoken "github.com/hashicorp/hcl/hcl/token"
8 )
9
10 // Token defines a single HCL token which can be obtained via the Scanner
11 type Token struct {
12         Type Type
13         Pos  Pos
14         Text string
15 }
16
17 // Type is the set of lexical tokens of the HCL (HashiCorp Configuration Language)
18 type Type int
19
20 const (
21         // Special tokens
22         ILLEGAL Type = iota
23         EOF
24
25         identifier_beg
26         literal_beg
27         NUMBER // 12345
28         FLOAT  // 123.45
29         BOOL   // true,false
30         STRING // "abc"
31         NULL   // 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 // String returns the string corresponding to the token tok.
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 // IsIdentifier returns true for tokens corresponding to identifiers and basic
82 // type literals; it returns false otherwise.
83 func (t Type) IsIdentifier() bool { return identifier_beg < t && t < identifier_end }
84
85 // IsLiteral returns true for tokens corresponding to basic type literals; it
86 // returns false otherwise.
87 func (t Type) IsLiteral() bool { return literal_beg < t && t < literal_end }
88
89 // IsOperator returns true for tokens corresponding to operators and
90 // delimiters; it returns false otherwise.
91 func (t Type) IsOperator() bool { return operator_beg < t && t < operator_end }
92
93 // String returns the token's literal text. Note that this is only
94 // applicable for certain token types, such as token.IDENT,
95 // token.STRING, etc..
96 func (t Token) String() string {
97         return fmt.Sprintf("%s %s %s", t.Pos.String(), t.Type.String(), t.Text)
98 }
99
100 // HCLToken converts this token to an HCL token.
101 //
102 // The token type must be a literal type or this will panic.
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 }