token.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Package token defines constants representing the lexical tokens of JavaScript (ECMA5).
  2. package token
  3. import (
  4. "strconv"
  5. )
  6. // Token is the set of lexical tokens in JavaScript (ECMA5).
  7. type Token int
  8. // String returns the string corresponding to the token.
  9. // For operators, delimiters, and keywords the string is the actual
  10. // token string (e.g., for the token PLUS, the String() is
  11. // "+"). For all other tokens the string corresponds to the token
  12. // name (e.g. for the token IDENTIFIER, the string is "IDENTIFIER").
  13. func (tkn Token) String() string {
  14. if tkn == 0 {
  15. return "UNKNOWN"
  16. }
  17. if tkn < Token(len(token2string)) {
  18. return token2string[tkn]
  19. }
  20. return "token(" + strconv.Itoa(int(tkn)) + ")"
  21. }
  22. //lint:ignore U1000 This is not used for anything
  23. func (tkn Token) precedence(in bool) int {
  24. switch tkn {
  25. case LOGICAL_OR:
  26. return 1
  27. case LOGICAL_AND:
  28. return 2
  29. case OR, OR_ASSIGN:
  30. return 3
  31. case EXCLUSIVE_OR:
  32. return 4
  33. case AND, AND_ASSIGN:
  34. return 5
  35. case EQUAL,
  36. NOT_EQUAL,
  37. STRICT_EQUAL,
  38. STRICT_NOT_EQUAL:
  39. return 6
  40. case LESS, GREATER, LESS_OR_EQUAL, GREATER_OR_EQUAL, INSTANCEOF:
  41. return 7
  42. case IN:
  43. if in {
  44. return 7
  45. }
  46. return 0
  47. case SHIFT_LEFT, SHIFT_RIGHT, UNSIGNED_SHIFT_RIGHT:
  48. fallthrough
  49. case SHIFT_LEFT_ASSIGN, SHIFT_RIGHT_ASSIGN, UNSIGNED_SHIFT_RIGHT_ASSIGN:
  50. return 8
  51. case PLUS, MINUS, ADD_ASSIGN, SUBTRACT_ASSIGN:
  52. return 9
  53. case MULTIPLY, SLASH, REMAINDER, MULTIPLY_ASSIGN, QUOTIENT_ASSIGN, REMAINDER_ASSIGN:
  54. return 11
  55. }
  56. return 0
  57. }
  58. type _keyword struct {
  59. token Token
  60. futureKeyword bool
  61. strict bool
  62. }
  63. // IsKeyword returns the keyword token if literal is a keyword, a KEYWORD token
  64. // if the literal is a future keyword (const, let, class, super, ...), or 0 if the literal is not a keyword.
  65. //
  66. // If the literal is a keyword, IsKeyword returns a second value indicating if the literal
  67. // is considered a future keyword in strict-mode only.
  68. //
  69. // 7.6.1.2 Future Reserved Words:
  70. //
  71. // const
  72. // class
  73. // enum
  74. // export
  75. // extends
  76. // import
  77. // super
  78. //
  79. // 7.6.1.2 Future Reserved Words (strict):
  80. //
  81. // implements
  82. // interface
  83. // let
  84. // package
  85. // private
  86. // protected
  87. // public
  88. // static
  89. func IsKeyword(literal string) (Token, bool) {
  90. if keyword, exists := keywordTable[literal]; exists {
  91. if keyword.futureKeyword {
  92. return KEYWORD, keyword.strict
  93. }
  94. return keyword.token, false
  95. }
  96. return 0, false
  97. }
  98. func IsId(tkn Token) bool {
  99. return tkn >= IDENTIFIER
  100. }
  101. func IsUnreservedWord(tkn Token) bool {
  102. return tkn > ESCAPED_RESERVED_WORD
  103. }