token.go 3.1 KB

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