package saccharine import "fmt" // All tokens in the pseudo-lambda language. type Type int const ( TokenOpenParen Type = iota // Denotes the '(' token. TokenCloseParen // Denotes the ')' token. TokenOpenBrace // Denotes the '{' token. TokenCloseBrace // Denotes the '}' token. TokenHardBreak // Denotes the ';' token. TokenAssign // Denotes the ':=' token. TokenAtom // Denotes an alpha-numeric variable. TokenSlash // Denotes the '/' token. TokenDot // Denotes the '.' token. TokenSoftBreak // Denotes a new-line. ) // A representation of a token in source code. type Token struct { Column int // Where the token begins in the source text. Type Type // What type the token is. Value string // The value of the token. } func NewOpenParen(column int) *Token { return &Token{Type: TokenOpenParen, Column: column, Value: "("} } func NewCloseParen(column int) *Token { return &Token{Type: TokenCloseParen, Column: column, Value: ")"} } func NewOpenBrace(column int) *Token { return &Token{Type: TokenOpenBrace, Column: column, Value: "{"} } func NewCloseBrace(column int) *Token { return &Token{Type: TokenCloseBrace, Column: column, Value: "}"} } func NewDot(column int) *Token { return &Token{Type: TokenDot, Column: column, Value: "."} } func NewHardBreak(column int) *Token { return &Token{Type: TokenHardBreak, Column: column, Value: ";"} } func NewAssign(column int) *Token { return &Token{Type: TokenAssign, Column: column, Value: ":="} } func NewSlash(column int) *Token { return &Token{Type: TokenSlash, Column: column, Value: "\\"} } func NewAtom(name string, column int) *Token { return &Token{Type: TokenAtom, Column: column, Value: name} } func NewSoftBreak(column int) *Token { return &Token{Type: TokenSoftBreak, Column: column, Value: "\\n"} } func Name(typ Type) string { switch typ { case TokenOpenParen: return "(" case TokenCloseParen: return ")" case TokenSlash: return "\\" case TokenDot: return "." case TokenAtom: return "ATOM" case TokenSoftBreak: return "\\n" case TokenHardBreak: return ";" default: panic(fmt.Errorf("unknown token type %v", typ)) } } func (t Token) Name() string { return Name(t.Type) }