token.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. switch {
  15. case tkn == 0:
  16. return "UNKNOWN"
  17. case tkn < Token(len(token2string)):
  18. return token2string[tkn]
  19. default:
  20. return "token(" + strconv.Itoa(int(tkn)) + ")"
  21. }
  22. }
  23. type keyword struct {
  24. token Token
  25. futureKeyword bool
  26. strict bool
  27. }
  28. // IsKeyword returns the keyword token if literal is a keyword, a KEYWORD token
  29. // if the literal is a future keyword (const, let, class, super, ...), or 0 if the literal is not a keyword.
  30. //
  31. // If the literal is a keyword, IsKeyword returns a second value indicating if the literal
  32. // is considered a future keyword in strict-mode only.
  33. //
  34. // 7.6.1.2 Future Reserved Words:
  35. //
  36. // const
  37. // class
  38. // enum
  39. // export
  40. // extends
  41. // import
  42. // super
  43. //
  44. // 7.6.1.2 Future Reserved Words (strict):
  45. //
  46. // implements
  47. // interface
  48. // let
  49. // package
  50. // private
  51. // protected
  52. // public
  53. // static
  54. func IsKeyword(literal string) (Token, bool) {
  55. if kw, exists := keywordTable[literal]; exists {
  56. if kw.futureKeyword {
  57. return KEYWORD, kw.strict
  58. }
  59. return kw.token, false
  60. }
  61. return 0, false
  62. }