2015-10-25 15:14:16 +00:00
|
|
|
// Package token defines constants representing the lexical tokens for HCL
|
|
|
|
// (HashiCorp Configuration Language)
|
2015-10-16 20:12:26 +00:00
|
|
|
package token
|
2015-10-07 09:20:35 +00:00
|
|
|
|
2015-10-12 19:53:40 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
)
|
2015-10-07 09:20:35 +00:00
|
|
|
|
|
|
|
// Token defines a single HCL token which can be obtained via the Scanner
|
|
|
|
type Token struct {
|
2015-10-16 20:23:23 +00:00
|
|
|
Type Type
|
2015-10-11 23:27:43 +00:00
|
|
|
Pos Pos
|
|
|
|
Text string
|
2015-10-07 09:20:35 +00:00
|
|
|
}
|
|
|
|
|
2015-10-16 20:23:23 +00:00
|
|
|
// Type is the set of lexical tokens of the HCL (HashiCorp Configuration Language)
|
|
|
|
type Type int
|
2015-10-07 09:20:35 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
// Special tokens
|
2015-10-16 20:23:23 +00:00
|
|
|
ILLEGAL Type = iota
|
2015-10-07 09:20:35 +00:00
|
|
|
EOF
|
|
|
|
COMMENT
|
|
|
|
|
2015-10-14 22:27:35 +00:00
|
|
|
identifier_beg
|
|
|
|
IDENT // literals
|
2015-10-07 09:20:35 +00:00
|
|
|
literal_beg
|
|
|
|
NUMBER // 12345
|
|
|
|
FLOAT // 123.45
|
|
|
|
BOOL // true,false
|
|
|
|
STRING // "abc"
|
|
|
|
literal_end
|
2015-10-14 22:27:35 +00:00
|
|
|
identifier_end
|
2015-10-07 09:20:35 +00:00
|
|
|
|
|
|
|
operator_beg
|
|
|
|
LBRACK // [
|
|
|
|
LBRACE // {
|
|
|
|
COMMA // ,
|
|
|
|
PERIOD // .
|
|
|
|
|
|
|
|
RBRACK // ]
|
|
|
|
RBRACE // }
|
|
|
|
|
|
|
|
ASSIGN // =
|
|
|
|
ADD // +
|
|
|
|
SUB // -
|
|
|
|
operator_end
|
|
|
|
)
|
|
|
|
|
|
|
|
var tokens = [...]string{
|
|
|
|
ILLEGAL: "ILLEGAL",
|
|
|
|
|
|
|
|
EOF: "EOF",
|
|
|
|
COMMENT: "COMMENT",
|
|
|
|
|
|
|
|
IDENT: "IDENT",
|
|
|
|
NUMBER: "NUMBER",
|
|
|
|
FLOAT: "FLOAT",
|
|
|
|
BOOL: "BOOL",
|
|
|
|
STRING: "STRING",
|
|
|
|
|
|
|
|
LBRACK: "LBRACK",
|
|
|
|
LBRACE: "LBRACE",
|
|
|
|
COMMA: "COMMA",
|
|
|
|
PERIOD: "PERIOD",
|
|
|
|
|
|
|
|
RBRACK: "RBRACK",
|
|
|
|
RBRACE: "RBRACE",
|
|
|
|
|
|
|
|
ASSIGN: "ASSIGN",
|
|
|
|
ADD: "ADD",
|
|
|
|
SUB: "SUB",
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the string corresponding to the token tok.
|
2015-10-16 20:23:23 +00:00
|
|
|
func (t Type) String() string {
|
2015-10-07 09:20:35 +00:00
|
|
|
s := ""
|
2015-10-16 20:23:23 +00:00
|
|
|
if 0 <= t && t < Type(len(tokens)) {
|
2015-10-07 09:20:35 +00:00
|
|
|
s = tokens[t]
|
|
|
|
}
|
|
|
|
if s == "" {
|
|
|
|
s = "token(" + strconv.Itoa(int(t)) + ")"
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2015-10-14 22:27:35 +00:00
|
|
|
// IsIdentifier returns true for tokens corresponding to identifiers and basic
|
2015-10-07 09:20:35 +00:00
|
|
|
// type literals; it returns false otherwise.
|
2015-10-16 20:23:23 +00:00
|
|
|
func (t Type) IsIdentifier() bool { return identifier_beg < t && t < identifier_end }
|
2015-10-14 22:27:35 +00:00
|
|
|
|
|
|
|
// IsLiteral returns true for tokens corresponding to basic type literals; it
|
|
|
|
// returns false otherwise.
|
2015-10-16 20:23:23 +00:00
|
|
|
func (t Type) IsLiteral() bool { return literal_beg < t && t < literal_end }
|
2015-10-07 09:20:35 +00:00
|
|
|
|
|
|
|
// IsOperator returns true for tokens corresponding to operators and
|
|
|
|
// delimiters; it returns false otherwise.
|
2015-10-16 20:23:23 +00:00
|
|
|
func (t Type) IsOperator() bool { return operator_beg < t && t < operator_end }
|
2015-10-07 09:20:35 +00:00
|
|
|
|
2015-10-07 09:24:03 +00:00
|
|
|
// String returns the token's literal text. Note that this is only
|
2015-10-07 09:20:35 +00:00
|
|
|
// applicable for certain token types, such as token.IDENT,
|
|
|
|
// token.STRING, etc..
|
2015-10-07 09:24:03 +00:00
|
|
|
func (t Token) String() string {
|
2015-10-12 19:53:40 +00:00
|
|
|
return fmt.Sprintf("%s %s %s", t.Pos.String(), t.Type.String(), t.Text)
|
2015-10-07 09:20:35 +00:00
|
|
|
}
|