From 6fb7de55d35581daa04aa9a31eeee698152f252e Mon Sep 17 00:00:00 2001 From: Fatih Arslan Date: Sat, 3 Oct 2015 15:34:06 +0300 Subject: [PATCH] token: add token representations --- parser/token.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/parser/token.go b/parser/token.go index 5b969eb..3b6d615 100644 --- a/parser/token.go +++ b/parser/token.go @@ -1,5 +1,7 @@ package parser +import "strconv" + // Token is the set of lexical tokens of the HCL (HashiCorp Configuration Language) type Token int @@ -36,6 +38,51 @@ const ( operator_end ) +var tokens = [...]string{ + ILLEGAL: "ILLEGAL", + + EOF: "EOF", + COMMENT: "COMMENT", + NEWLINE: "NEWLINE", + + IDENT: "IDENT", + NUMBER: "NUMBER", + FLOAT: "FLOAT", + BOOL: "BOOL", + STRING: "STRING", + + LBRACK: "[", + LBRACE: "{", + COMMA: ",", + PERIOD: ".", + + RBRACK: "]", + RBRACE: "}", + + ASSIGN: "=", + ADD: "+", + SUB: "-", + + EPLUS: "e", + EMINUS: "e-", +} + +// String returns the string corresponding to the token tok. +// For operators, delimiters, and keywords the string is the actual +// token character sequence (e.g., for the token ADD, the string is +// "+"). For all other tokens the string corresponds to the token +// constant name (e.g. for the token IDENT, the string is "IDENT"). +func (t Token) String() string { + s := "" + if 0 <= t && t < Token(len(tokens)) { + s = tokens[t] + } + if s == "" { + s = "token(" + strconv.Itoa(int(t)) + ")" + } + return s +} + // IsLiteral returns true for tokens corresponding to identifiers and basic // type literals; it returns false otherwise. func (t Token) IsLiteral() bool { return literal_beg < t && t < literal_end }