blob: 2c9f5d33f72af7de50f5d609181b9202f84bc708 [file] [log] [blame]
package token
import "fmt"
// Type represents the type of a lexical token.
type Type int
const (
Number Type = iota // numeric literal
Plus // +
Minus // -
Star // *
Slash // /
LParen // (
RParen // )
Ident // identifier (e.g. "f", "x")
Comma // ,
Equals // =
EOF // end of input
)
// String returns a human-readable name for the token type.
func (t Type) String() string {
switch t {
case Number:
return "Number"
case Plus:
return "+"
case Minus:
return "-"
case Star:
return "*"
case Slash:
return "/"
case LParen:
return "("
case RParen:
return ")"
case Ident:
return "Ident"
case Comma:
return ","
case Equals:
return "="
case EOF:
return "EOF"
default:
return fmt.Sprintf("Unknown(%d)", int(t))
}
}
// Token represents a single lexical token.
type Token struct {
Type Type // the kind of token
Literal string // raw text (e.g. "3.14", "+")
Pos int // byte offset in input string
}