88 lines
2.0 KiB
Go
88 lines
2.0 KiB
Go
package token
|
|
|
|
// All tokens in the pseudo-lambda language.
|
|
type Type int
|
|
|
|
const (
|
|
OpenParen Type = iota // Denotes the '(' token.
|
|
CloseParen // Denotes the ')' token.
|
|
OpenBrace // Denotes the '{' token.
|
|
CloseBrace // Denotes the '}' token.
|
|
End // Denotes the ';' token.
|
|
Assign // Denotes the ':=' token.
|
|
Atom // Denotes an alpha-numeric variable.
|
|
Slash // Denotes the '/' token.
|
|
Dot // Denotes the '.' token.
|
|
Newline // 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: OpenParen, Column: column, Value: "("}
|
|
}
|
|
|
|
func NewCloseParen(column int) *Token {
|
|
return &Token{Type: CloseParen, Column: column, Value: ")"}
|
|
}
|
|
|
|
func NewOpenBrace(column int) *Token {
|
|
return &Token{Type: OpenBrace, Column: column, Value: "{"}
|
|
}
|
|
|
|
func NewCloseBrace(column int) *Token {
|
|
return &Token{Type: CloseBrace, Column: column, Value: "}"}
|
|
}
|
|
|
|
func NewDot(column int) *Token {
|
|
return &Token{Type: Dot, Column: column, Value: "."}
|
|
}
|
|
|
|
func NewEnd(column int) *Token {
|
|
return &Token{Type: End, Column: column, Value: ";"}
|
|
}
|
|
|
|
func NewAssign(column int) *Token {
|
|
return &Token{Type: Assign, Column: column, Value: ":="}
|
|
}
|
|
|
|
func NewSlash(column int) *Token {
|
|
return &Token{Type: Slash, Column: column, Value: "\\"}
|
|
}
|
|
|
|
func NewAtom(name string, column int) *Token {
|
|
return &Token{Type: Atom, Column: column, Value: name}
|
|
}
|
|
|
|
func NewNewline(column int) *Token {
|
|
return &Token{Type: Newline, Column: column, Value: "\\n"}
|
|
}
|
|
|
|
func Name(typ Type) string {
|
|
switch typ {
|
|
case OpenParen:
|
|
return "("
|
|
case CloseParen:
|
|
return ")"
|
|
case Slash:
|
|
return "\\"
|
|
case Dot:
|
|
return "."
|
|
case Atom:
|
|
return "ATOM"
|
|
case Newline:
|
|
return "\\n"
|
|
default:
|
|
return "?"
|
|
}
|
|
}
|
|
|
|
func (t Token) Name() string {
|
|
return Name(t.Type)
|
|
}
|