package token // TokenType represents the type of a token. type TokenType int // Token types. const ( // Single-character tokens. LEFT_PAREN TokenType = iota RIGHT_PAREN LEFT_BRACE RIGHT_BRACE COMMA DOT MINUS PLUS SEMICOLON SLASH STAR // One or two character tokens. BANG BANG_EQUAL EQUAL EQUAL_EQUAL GREATER GREATER_EQUAL LESS LESS_EQUAL // Literals. IDENTIFIER STRING NUMBER // Keywords. AND CLASS ELSE FALSE FUN FOR IF NIL OR PRINT RETURN SUPER THIS TRUE VAR WHILE EOF ) // Token represents a token in the source code. type Token struct { Type TokenType Lexeme string Literal interface{} Line int } // New creates a new Token. func New(t TokenType, lexeme string, literal interface{}, line int) Token { return Token{t, lexeme, literal, line} } // String returns the string representation of the token. func (t Token) String() string { return t.Lexeme } // keywords maps keywords to their respective TokenType. var keywords = map[string]TokenType{ "and": AND, "class": CLASS, "else": ELSE, "false": FALSE, "for": FOR, "fun": FUN, "if": IF, "nil": NIL, "or": OR, "print": PRINT, "return": RETURN, "super": SUPER, "this": THIS, "true": TRUE, "var": VAR, "while": WHILE, } // LookupKeyword returns the TokenType for the given identifier. // If the identifier is not a keyword, it returns IDENTIFIER. func LookupKeyword(identifier string) TokenType { if t, ok := keywords[identifier]; ok { return t } return IDENTIFIER }