1
0
Fork 0
penny/token/token.go

147 lines
2.0 KiB
Go
Executable File

package token
import (
"fmt"
)
type Token struct {
Type
Lit []byte
Pos
}
type Type int
const (
INVALID Type = iota
EOF
)
type Pos struct {
Offset int
Line int
Column int
}
func (this Pos) String() string {
return fmt.Sprintf("Pos(offset=%d, line=%d, column=%d)", this.Offset, this.Line, this.Column)
}
type TokenMap struct {
typeMap []string
idMap map[string]Type
}
func (this TokenMap) Id(tok Type) string {
if int(tok) < len(this.typeMap) {
return this.typeMap[tok]
}
return "unknown"
}
func (this TokenMap) Type(tok string) Type {
if typ, exist := this.idMap[tok]; exist {
return typ
}
return INVALID
}
func (this TokenMap) TokenString(tok *Token) string {
//TODO: refactor to print pos & token string properly
return fmt.Sprintf("%s(%d,%s)", this.Id(tok.Type), tok.Type, tok.Lit)
}
func (this TokenMap) StringType(typ Type) string {
return fmt.Sprintf("%s(%d)", this.Id(typ), typ)
}
var TokMap = TokenMap{
typeMap: []string{
"INVALID",
"$",
"word",
"number",
"=",
"&&",
"||",
"!",
"|",
"(",
")",
"for",
"in",
"name",
"case",
"esac",
";;",
"if",
"then",
"fi",
"elif",
"else",
"while",
"until",
"{",
"}",
"do",
"done",
"<",
"<&",
">",
">&",
">>",
"<>",
">|",
"<<",
"<<-",
"\n",
"nothing",
"&",
";",
},
idMap: map[string]Type{
"INVALID": 0,
"$": 1,
"word": 2,
"number": 3,
"=": 4,
"&&": 5,
"||": 6,
"!": 7,
"|": 8,
"(": 9,
")": 10,
"for": 11,
"in": 12,
"name": 13,
"case": 14,
"esac": 15,
";;": 16,
"if": 17,
"then": 18,
"fi": 19,
"elif": 20,
"else": 21,
"while": 22,
"until": 23,
"{": 24,
"}": 25,
"do": 26,
"done": 27,
"<": 28,
"<&": 29,
">": 30,
">&": 31,
">>": 32,
"<>": 33,
">|": 34,
"<<": 35,
"<<-": 36,
"\n": 37,
"nothing": 38,
"&": 39,
";": 40,
},
}