mirror of
https://github.com/kataras/iris.git
synced 2026-07-31 08:29:50 +00:00
feat(token): add String() method to token Type
Add String() method to the token Type that returns the human-readable name
of the token (e.g., "LBRACE" for '{'), with fallback to numeric value for
unknown types. Also add typeNames map for token type name lookups.
This commit is contained in:
@@ -1,8 +1,20 @@
|
||||
package token
|
||||
|
||||
import "strconv"
|
||||
|
||||
// Type is a specific type of int which describes the symbols.
|
||||
type Type int
|
||||
|
||||
// String returns the name of the token type, e.g. "LBRACE" for the '{' symbol.
|
||||
// It returns the numeric value of an unknown type as it is.
|
||||
func (t Type) String() string {
|
||||
if name, ok := typeNames[t]; ok {
|
||||
return name
|
||||
}
|
||||
|
||||
return strconv.Itoa(int(t))
|
||||
}
|
||||
|
||||
// Token describes the letter(s) or symbol, is a result of the lexer.
|
||||
type Token struct {
|
||||
Type Type
|
||||
@@ -37,6 +49,20 @@ const (
|
||||
INT // 42
|
||||
)
|
||||
|
||||
var typeNames = map[Type]string{
|
||||
EOF: "EOF",
|
||||
ILLEGAL: "ILLEGAL",
|
||||
LBRACE: "LBRACE",
|
||||
RBRACE: "RBRACE",
|
||||
COLON: "COLON",
|
||||
LPAREN: "LPAREN",
|
||||
RPAREN: "RPAREN",
|
||||
COMMA: "COMMA",
|
||||
IDENT: "IDENT",
|
||||
ELSE: "ELSE",
|
||||
INT: "INT",
|
||||
}
|
||||
|
||||
var keywords = map[string]Type{
|
||||
"else": ELSE,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user