Mako/types/token.go

100 lines
1.7 KiB
Go

package types
// TokenType represents the type of a token
type TokenType int
// Token represents a lexical token
type Token struct {
Type TokenType
Lexeme string
Literal any
Line int
Column int
}
// All token types in the language
const (
// Special tokens
EOF TokenType = iota
ERROR
// Literals
IDENTIFIER
STRING
NUMBER
// Operators
PLUS // +
MINUS // -
STAR // *
SLASH // /
EQUAL // =
EQUAL_EQUAL // ==
BANG_EQUAL // !=
LESS //
LESS_EQUAL // <=
GREATER // >
GREATER_EQUAL // >=
// Keywords
AND
OR
IF
ELSEIF
ELSE
THEN
END
FN
RETURN
ECHO
TRUE
FALSE
NIL
// Punctuation
LEFT_PAREN // (
RIGHT_PAREN // )
COMMA // ,
)
// String returns the string representation of the token type
func (t TokenType) String() string {
return tokenNames[t]
}
// Token type string representations
var tokenNames = [...]string{
EOF: "EOF",
ERROR: "ERROR",
IDENTIFIER: "IDENTIFIER",
STRING: "STRING",
NUMBER: "NUMBER",
PLUS: "PLUS",
MINUS: "MINUS",
STAR: "STAR",
SLASH: "SLASH",
EQUAL: "EQUAL",
EQUAL_EQUAL: "EQUAL_EQUAL",
BANG_EQUAL: "BANG_EQUAL",
LESS: "LESS",
LESS_EQUAL: "LESS_EQUAL",
GREATER: "GREATER",
GREATER_EQUAL: "GREATER_EQUAL",
AND: "AND",
OR: "OR",
IF: "IF",
ELSEIF: "ELSEIF",
ELSE: "ELSE",
THEN: "THEN",
END: "END",
FN: "FN",
RETURN: "RETURN",
ECHO: "ECHO",
TRUE: "TRUE",
FALSE: "FALSE",
NIL: "NIL",
LEFT_PAREN: "LEFT_PAREN",
RIGHT_PAREN: "RIGHT_PAREN",
COMMA: "COMMA",
}