aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/hil/scanner/token.go
blob: b6c82ae9b08a8ee9cf5465b7504d6174c3f822a9 (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
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
package scanner

import (
	"fmt"

	"github.com/hashicorp/hil/ast"
)

type Token struct {
	Type    TokenType
	Content string
	Pos     ast.Pos
}

//go:generate stringer -type=TokenType
type TokenType rune

const (
	// Raw string data outside of ${ .. } sequences
	LITERAL TokenType = 'o'

	// STRING is like a LITERAL but it's inside a quoted string
	// within a ${ ... } sequence, and so it can contain backslash
	// escaping.
	STRING TokenType = 'S'

	// Other Literals
	INTEGER TokenType = 'I'
	FLOAT   TokenType = 'F'
	BOOL    TokenType = 'B'

	BEGIN    TokenType = '$' // actually "${"
	END      TokenType = '}'
	OQUOTE   TokenType = '“' // Opening quote of a nested quoted sequence
	CQUOTE   TokenType = '”' // Closing quote of a nested quoted sequence
	OPAREN   TokenType = '('
	CPAREN   TokenType = ')'
	OBRACKET TokenType = '['
	CBRACKET TokenType = ']'
	COMMA    TokenType = ','

	IDENTIFIER TokenType = 'i'

	PERIOD  TokenType = '.'
	PLUS    TokenType = '+'
	MINUS   TokenType = '-'
	STAR    TokenType = '*'
	SLASH   TokenType = '/'
	PERCENT TokenType = '%'

	AND  TokenType = '∧'
	OR   TokenType = '∨'
	BANG TokenType = '!'

	EQUAL    TokenType = '='
	NOTEQUAL TokenType = '≠'
	GT       TokenType = '>'
	LT       TokenType = '<'
	GTE      TokenType = '≥'
	LTE      TokenType = '≤'

	QUESTION TokenType = '?'
	COLON    TokenType = ':'

	EOF TokenType = '␄'

	// Produced for sequences that cannot be understood as valid tokens
	// e.g. due to use of unrecognized punctuation.
	INVALID TokenType = '�'
)

func (t *Token) String() string {
	switch t.Type {
	case EOF:
		return "end of string"
	case INVALID:
		return fmt.Sprintf("invalid sequence %q", t.Content)
	case INTEGER:
		return fmt.Sprintf("integer %s", t.Content)
	case FLOAT:
		return fmt.Sprintf("float %s", t.Content)
	case STRING:
		return fmt.Sprintf("string %q", t.Content)
	case LITERAL:
		return fmt.Sprintf("literal %q", t.Content)
	case OQUOTE:
		return fmt.Sprintf("opening quote")
	case CQUOTE:
		return fmt.Sprintf("closing quote")
	case AND:
		return "&&"
	case OR:
		return "||"
	case NOTEQUAL:
		return "!="
	case GTE:
		return ">="
	case LTE:
		return "<="
	default:
		// The remaining token types have content that
		// speaks for itself.
		return fmt.Sprintf("%q", t.Content)
	}
}