token.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. TokenKindOperator = TokenKind('o')
  27. TokenKindRedirect = TokenKind('R')
  28. TokenKindMethod = TokenKind('M')
  29. TokenKindGet = TokenKind('$')
  30. TokenKindSet = TokenKind('=')
  31. TokenKindOpenBlock = TokenKind('{')
  32. TokenKindCloseBlock = TokenKind('}')
  33. TokenKindOpenList = TokenKind('[')
  34. TokenKindCloseList = TokenKind(']')
  35. TokenKindOpenParen = TokenKind('(')
  36. TokenKindCloseParen = TokenKind(')')
  37. TokenKindError = TokenKind('!')
  38. TokenKindEOX = TokenKind('\n')
  39. TokenKindEOF = TokenKind(0x255)
  40. )
  41. /* Names of the different token types. */
  42. var TokenKindNames map[TokenKind]string = map[TokenKind]string{
  43. TokenKindNone: "None",
  44. TokenKindInteger: "Integer",
  45. TokenKindFloat: "Float",
  46. TokenKindSymbol: "Symbol",
  47. TokenKindString: "String",
  48. TokenKindBoolean: "Boolean",
  49. TokenKindNil: "Nil",
  50. TokenKindWord: "Word",
  51. TokenKindType: "Type",
  52. TokenKindOperator: "Operator",
  53. TokenKindRedirect: "Redirect",
  54. TokenKindMethod: "Method",
  55. TokenKindGet: "Get",
  56. TokenKindSet: "Set",
  57. TokenKindOpenBlock: "OpenBlock",
  58. TokenKindCloseBlock: "CloseBlock",
  59. TokenKindOpenList: "OpenList",
  60. TokenKindCloseList: "CloseList",
  61. TokenKindOpenParen: "OpenParen",
  62. TokenKindCloseParen: "CloseParen",
  63. TokenKindError: "Error",
  64. TokenKindEOX: "EOX",
  65. TokenKindEOF: "EOF",
  66. }
  67. /* Reverse map of the names of the different token types. */
  68. var NamesTokenKind map[string] TokenKind = map[string]TokenKind{}
  69. func init () {
  70. for k, v := range TokenKindNames {
  71. NamesTokenKind[v] = k
  72. }
  73. }
  74. /* Transforms a token kind to a String */
  75. func (kind TokenKind) String() string {
  76. name, ok := TokenKindNames[kind]
  77. if !ok {
  78. return "Unknown TokenKind!"
  79. }
  80. return name
  81. }
  82. type Token struct {
  83. TokenKind
  84. Value
  85. Position
  86. }
  87. func (token Token) String() string {
  88. if token.Value == nil {
  89. return fmt.Sprintf("%s:%s:nil", token.Position.String(), token.TokenKind.String())
  90. }
  91. return fmt.Sprintf("%s:%s:%v", token.Position.String(), token.TokenKind.String(), token.Value)
  92. }
  93. func (token Token) Error() string {
  94. if token.TokenKind == TokenKindError {
  95. return token.Value.String()
  96. }
  97. return "No error"
  98. }
  99. /* Returns whether or not the token is the last to be expected,
  100. * that is either an error or EOF. */
  101. func (token Token) IsLast() bool {
  102. switch token.TokenKind {
  103. case TokenKindError, TokenKindEOF, TokenKindNone:
  104. return true
  105. default:
  106. return false
  107. }
  108. }
  109. /* Creates a new token. */
  110. func NewToken(kind TokenKind, val Value, pos Position) Token {
  111. return Token{kind, val, pos}
  112. }
  113. /* Creates a token with type TokenTypeNone */
  114. func NoToken() Token {
  115. return NewToken(TokenKindNone, NilValue, Position{})
  116. }
  117. func (token Token) IsNone() bool {
  118. return token.TokenKind == TokenKindNone
  119. }
  120. func (token Token) IsError() bool {
  121. return token.TokenKind == TokenKindError
  122. }