diff options
Diffstat (limited to 'vendor/github.com/hashicorp/hcl/json/token')
-rw-r--r-- | vendor/github.com/hashicorp/hcl/json/token/position.go | 46 | ||||
-rw-r--r-- | vendor/github.com/hashicorp/hcl/json/token/token.go | 118 |
2 files changed, 164 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/hcl/json/token/position.go b/vendor/github.com/hashicorp/hcl/json/token/position.go new file mode 100644 index 0000000..59c1bb7 --- /dev/null +++ b/vendor/github.com/hashicorp/hcl/json/token/position.go | |||
@@ -0,0 +1,46 @@ | |||
1 | package token | ||
2 | |||
3 | import "fmt" | ||
4 | |||
5 | // Pos describes an arbitrary source position | ||
6 | // including the file, line, and column location. | ||
7 | // A Position is valid if the line number is > 0. | ||
8 | type Pos struct { | ||
9 | Filename string // filename, if any | ||
10 | Offset int // offset, starting at 0 | ||
11 | Line int // line number, starting at 1 | ||
12 | Column int // column number, starting at 1 (character count) | ||
13 | } | ||
14 | |||
15 | // IsValid returns true if the position is valid. | ||
16 | func (p *Pos) IsValid() bool { return p.Line > 0 } | ||
17 | |||
18 | // String returns a string in one of several forms: | ||
19 | // | ||
20 | // file:line:column valid position with file name | ||
21 | // line:column valid position without file name | ||
22 | // file invalid position with file name | ||
23 | // - invalid position without file name | ||
24 | func (p Pos) String() string { | ||
25 | s := p.Filename | ||
26 | if p.IsValid() { | ||
27 | if s != "" { | ||
28 | s += ":" | ||
29 | } | ||
30 | s += fmt.Sprintf("%d:%d", p.Line, p.Column) | ||
31 | } | ||
32 | if s == "" { | ||
33 | s = "-" | ||
34 | } | ||
35 | return s | ||
36 | } | ||
37 | |||
38 | // Before reports whether the position p is before u. | ||
39 | func (p Pos) Before(u Pos) bool { | ||
40 | return u.Offset > p.Offset || u.Line > p.Line | ||
41 | } | ||
42 | |||
43 | // After reports whether the position p is after u. | ||
44 | func (p Pos) After(u Pos) bool { | ||
45 | return u.Offset < p.Offset || u.Line < p.Line | ||
46 | } | ||
diff --git a/vendor/github.com/hashicorp/hcl/json/token/token.go b/vendor/github.com/hashicorp/hcl/json/token/token.go new file mode 100644 index 0000000..95a0c3e --- /dev/null +++ b/vendor/github.com/hashicorp/hcl/json/token/token.go | |||
@@ -0,0 +1,118 @@ | |||
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 | } | ||