token.go 2.4 KB

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