123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- package muesli
- import (
- "fmt"
- )
- /* Position of a token in an input stream */
- type Position struct {
- FileName string
- Line int
- Column int
- }
- /* Token Kind. Uses a rune to easily handle single character tokens. */
- type TokenKind rune
- const (
- TokenKindNone = TokenKind(0)
- TokenKindInteger = TokenKind('i')
- TokenKindFloat = TokenKind('f')
- TokenKindString = TokenKind('s')
- TokenKindSymbol = TokenKind('S')
- TokenKindBoolean = TokenKind('b')
- TokenKindNil = TokenKind('N')
- TokenKindWord = TokenKind('w')
- TokenKindType = TokenKind('t')
- TokenKindGet = TokenKind('$')
- TokenKindSet = TokenKind('=')
- TokenKindOpenBlock = TokenKind('{')
- TokenKindCloseBlock = TokenKind('}')
- TokenKindOpenList = TokenKind('[')
- TokenKindCloseList = TokenKind(']')
- TokenKindOpenParen = TokenKind('(')
- TokenKindCloseParen = TokenKind(')')
- TokenKindError = TokenKind('!')
- TokenKindEOX = TokenKind('\n')
- TokenKindEOF = TokenKind(0x255)
- )
- /* Names of the different token types. */
- var TokenKindNames map[TokenKind]string = map[TokenKind]string{
- TokenKindNone: "None",
- TokenKindInteger: "Integer",
- TokenKindFloat: "Float",
- TokenKindSymbol: "Symbol",
- TokenKindString: "String",
- TokenKindBoolean: "Boolean",
- TokenKindNil: "?il",
- TokenKindWord: "Word",
- TokenKindType: "Type",
- TokenKindGet: "Get",
- TokenKindSet: "Set",
- TokenKindOpenBlock: "OpenBlock",
- TokenKindCloseBlock: "CloseBlock",
- TokenKindOpenList: "OpenList",
- TokenKindCloseList: "CloseList",
- TokenKindOpenParen: "OpenParen",
- TokenKindCloseParen: "CloseParen",
- TokenKindError: "Error",
- TokenKindEOX: "EOX",
- TokenKindEOF: "EOF",
- }
- /* Transforms a token kid to a String */
- func (kind TokenKind) String() string {
- name, ok := TokenKindNames[kind]
- if !ok {
- return "Unknown TokenKind!"
- }
- return name
- }
- type Token struct {
- TokenKind
- Value
- Position
- }
- func (token Token) String() string {
- if token.Value == nil {
- return fmt.Sprintf("<%s:nil:%v>", token.TokenKind.String(), token.Position)
- }
- return fmt.Sprintf("<%s:%q:%v>", token.TokenKind.String(),
- token.Value.String(), token.Position)
- }
- func (token Token) Error() string {
- if token.TokenKind == TokenKindError {
- return token.Value.String()
- }
- return "No error"
- }
- /* Returns whether or not the token is the last to be expected,
- * that is either an error or EOF. */
- func (token Token) IsLast() bool {
- switch token.TokenKind {
- case TokenKindError, TokenKindEOF, TokenKindNone:
- return true
- default:
- return false
- }
- }
- /* Creates a new token. */
- func NewToken(kind TokenKind, val Value, pos Position) Token {
- return Token{kind, val, pos}
- }
- /* Creates a token with type TokenTypeNone */
- func NoToken() Token {
- return NewToken(TokenKindNone, NilValue, Position{})
- }
- func (token Token) IsNone() bool {
- return token.TokenKind == TokenKindNone
- }
|