token.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package muesli
  2. import (
  3. "fmt"
  4. )
  5. /* Position of a token in an input stream */
  6. type Position struct {
  7. FileName string
  8. Line int
  9. Column int
  10. }
  11. /* Token Kind. Uses a rune to easily handle single character tokens. */
  12. type TokenKind rune
  13. const (
  14. TokenKindNone = TokenKind(0)
  15. TokenKindInteger = TokenKind('i')
  16. TokenKindFloat = TokenKind('f')
  17. TokenKindString = TokenKind('s')
  18. TokenKindSymbol = TokenKind('S')
  19. TokenKindBoolean = TokenKind('b')
  20. TokenKindNil = TokenKind('N')
  21. TokenKindWord = TokenKind('w')
  22. TokenKindType = TokenKind('t')
  23. TokenKindGet = TokenKind('$')
  24. TokenKindSet = TokenKind('=')
  25. TokenKindOpenBlock = TokenKind('{')
  26. TokenKindCloseBlock = TokenKind('}')
  27. TokenKindOpenList = TokenKind('[')
  28. TokenKindCloseList = TokenKind(']')
  29. TokenKindOpenParen = TokenKind('(')
  30. TokenKindCloseParen = TokenKind(')')
  31. TokenKindError = TokenKind('!')
  32. TokenKindEOX = TokenKind('\n')
  33. TokenKindEOF = TokenKind(0x255)
  34. )
  35. /* Names of the different token types. */
  36. var TokenKindNames map[TokenKind]string = map[TokenKind]string{
  37. TokenKindNone: "None",
  38. TokenKindInteger: "Integer",
  39. TokenKindFloat: "Float",
  40. TokenKindSymbol: "Symbol",
  41. TokenKindString: "String",
  42. TokenKindBoolean: "Boolean",
  43. TokenKindNil: "?il",
  44. TokenKindWord: "Word",
  45. TokenKindType: "Type",
  46. TokenKindGet: "Get",
  47. TokenKindSet: "Set",
  48. TokenKindOpenBlock: "OpenBlock",
  49. TokenKindCloseBlock: "CloseBlock",
  50. TokenKindOpenList: "OpenList",
  51. TokenKindCloseList: "CloseList",
  52. TokenKindOpenParen: "OpenParen",
  53. TokenKindCloseParen: "CloseParen",
  54. TokenKindError: "Error",
  55. TokenKindEOX: "EOX",
  56. TokenKindEOF: "EOF",
  57. }
  58. /* Transforms a token kid to a String */
  59. func (kind TokenKind) String() string {
  60. name, ok := TokenKindNames[kind]
  61. if !ok {
  62. return "Unknown TokenKind!"
  63. }
  64. return name
  65. }
  66. type Token struct {
  67. TokenKind
  68. Value
  69. Position
  70. }
  71. func (token Token) String() string {
  72. if token.Value == nil {
  73. return fmt.Sprintf("<%s:nil:%v>", token.TokenKind.String(), token.Position)
  74. }
  75. return fmt.Sprintf("<%s:%q:%v>", token.TokenKind.String(),
  76. token.Value.String(), token.Position)
  77. }
  78. func (token Token) Error() string {
  79. if token.TokenKind == TokenKindError {
  80. return token.Value.String()
  81. }
  82. return "No error"
  83. }
  84. /* Returns whether or not the token is the last to be expected,
  85. * that is either an error or EOF. */
  86. func (token Token) IsLast() bool {
  87. switch token.TokenKind {
  88. case TokenKindError, TokenKindEOF, TokenKindNone:
  89. return true
  90. default:
  91. return false
  92. }
  93. }
  94. /* Creates a new token. */
  95. func NewToken(kind TokenKind, val Value, pos Position) Token {
  96. return Token{kind, val, pos}
  97. }
  98. /* Creates a token with type TokenTypeNone */
  99. func NoToken() Token {
  100. return NewToken(TokenKindNone, NilValue, Position{})
  101. }
  102. func (token Token) IsNone() bool {
  103. return token.TokenKind == TokenKindNone
  104. }