token.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package lexer
  2. import "fmt"
  3. const (
  4. // TokenError represents an error
  5. TokenError TokenKind = iota
  6. // TokenEOF represents an End Of File
  7. TokenEOF
  8. //
  9. // Mustache delimiters
  10. //
  11. // TokenOpen is the OPEN token
  12. TokenOpen
  13. // TokenClose is the CLOSE token
  14. TokenClose
  15. // TokenOpenRawBlock is the OPEN_RAW_BLOCK token
  16. TokenOpenRawBlock
  17. // TokenCloseRawBlock is the CLOSE_RAW_BLOCK token
  18. TokenCloseRawBlock
  19. // TokenOpenEndRawBlock is the END_RAW_BLOCK token
  20. TokenOpenEndRawBlock
  21. // TokenOpenUnescaped is the OPEN_UNESCAPED token
  22. TokenOpenUnescaped
  23. // TokenCloseUnescaped is the CLOSE_UNESCAPED token
  24. TokenCloseUnescaped
  25. // TokenOpenBlock is the OPEN_BLOCK token
  26. TokenOpenBlock
  27. // TokenOpenEndBlock is the OPEN_ENDBLOCK token
  28. TokenOpenEndBlock
  29. // TokenInverse is the INVERSE token
  30. TokenInverse
  31. // TokenOpenInverse is the OPEN_INVERSE token
  32. TokenOpenInverse
  33. // TokenOpenInverseChain is the OPEN_INVERSE_CHAIN token
  34. TokenOpenInverseChain
  35. // TokenOpenPartial is the OPEN_PARTIAL token
  36. TokenOpenPartial
  37. // TokenComment is the COMMENT token
  38. TokenComment
  39. //
  40. // Inside mustaches
  41. //
  42. // TokenOpenSexpr is the OPEN_SEXPR token
  43. TokenOpenSexpr
  44. // TokenCloseSexpr is the CLOSE_SEXPR token
  45. TokenCloseSexpr
  46. // TokenEquals is the EQUALS token
  47. TokenEquals
  48. // TokenData is the DATA token
  49. TokenData
  50. // TokenSep is the SEP token
  51. TokenSep
  52. // TokenOpenBlockParams is the OPEN_BLOCK_PARAMS token
  53. TokenOpenBlockParams
  54. // TokenCloseBlockParams is the CLOSE_BLOCK_PARAMS token
  55. TokenCloseBlockParams
  56. //
  57. // Tokens with content
  58. //
  59. // TokenContent is the CONTENT token
  60. TokenContent
  61. // TokenID is the ID token
  62. TokenID
  63. // TokenString is the STRING token
  64. TokenString
  65. // TokenNumber is the NUMBER token
  66. TokenNumber
  67. // TokenBoolean is the BOOLEAN token
  68. TokenBoolean
  69. )
  70. const (
  71. // Option to generate token position in its string representation
  72. dumpTokenPos = false
  73. // Option to generate values for all token kinds for their string representations
  74. dumpAllTokensVal = true
  75. )
  76. // TokenKind represents a Token type.
  77. type TokenKind int
  78. // Token represents a scanned token.
  79. type Token struct {
  80. Kind TokenKind // Token kind
  81. Val string // Token value
  82. Pos int // Byte position in input string
  83. Line int // Line number in input string
  84. }
  85. // tokenName permits to display token name given token type
  86. var tokenName = map[TokenKind]string{
  87. TokenError: "Error",
  88. TokenEOF: "EOF",
  89. TokenContent: "Content",
  90. TokenComment: "Comment",
  91. TokenOpen: "Open",
  92. TokenClose: "Close",
  93. TokenOpenUnescaped: "OpenUnescaped",
  94. TokenCloseUnescaped: "CloseUnescaped",
  95. TokenOpenBlock: "OpenBlock",
  96. TokenOpenEndBlock: "OpenEndBlock",
  97. TokenOpenRawBlock: "OpenRawBlock",
  98. TokenCloseRawBlock: "CloseRawBlock",
  99. TokenOpenEndRawBlock: "OpenEndRawBlock",
  100. TokenOpenBlockParams: "OpenBlockParams",
  101. TokenCloseBlockParams: "CloseBlockParams",
  102. TokenInverse: "Inverse",
  103. TokenOpenInverse: "OpenInverse",
  104. TokenOpenInverseChain: "OpenInverseChain",
  105. TokenOpenPartial: "OpenPartial",
  106. TokenOpenSexpr: "OpenSexpr",
  107. TokenCloseSexpr: "CloseSexpr",
  108. TokenID: "ID",
  109. TokenEquals: "Equals",
  110. TokenString: "String",
  111. TokenNumber: "Number",
  112. TokenBoolean: "Boolean",
  113. TokenData: "Data",
  114. TokenSep: "Sep",
  115. }
  116. // String returns the token kind string representation for debugging.
  117. func (k TokenKind) String() string {
  118. s := tokenName[k]
  119. if s == "" {
  120. return fmt.Sprintf("Token-%d", int(k))
  121. }
  122. return s
  123. }
  124. // String returns the token string representation for debugging.
  125. func (t Token) String() string {
  126. result := ""
  127. if dumpTokenPos {
  128. result += fmt.Sprintf("%d:", t.Pos)
  129. }
  130. result += fmt.Sprintf("%s", t.Kind)
  131. if (dumpAllTokensVal || (t.Kind >= TokenContent)) && len(t.Val) > 0 {
  132. if len(t.Val) > 100 {
  133. result += fmt.Sprintf("{%.20q...}", t.Val)
  134. } else {
  135. result += fmt.Sprintf("{%q}", t.Val)
  136. }
  137. }
  138. return result
  139. }