| 1 | // Package token defines constants representing the lexical tokens of JavaScript (ECMA5). |
| 2 | package token |
| 3 | |
| 4 | import ( |
| 5 | "strconv" |
| 6 | ) |
| 7 | |
| 8 | // Token is the set of lexical tokens in JavaScript (ECMA5). |
| 9 | type Token int |
| 10 | |
| 11 | // String returns the string corresponding to the token. |
| 12 | // For operators, delimiters, and keywords the string is the actual |
| 13 | // token string (e.g., for the token PLUS, the String() is |
| 14 | // "+"). For all other tokens the string corresponds to the token |
| 15 | // name (e.g. for the token IDENTIFIER, the string is "IDENTIFIER"). |
| 16 | func (tkn Token) String() string { |
| 17 | switch { |
| 18 | case tkn == 0: |
| 19 | return "UNKNOWN" |
| 20 | case tkn < Token(len(token2string)): |
| 21 | return token2string[tkn] |
| 22 | default: |
| 23 | return "token(" + strconv.Itoa(int(tkn)) + ")" |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | type keyword struct { |
| 28 | token Token |
| 29 | futureKeyword bool |
| 30 | strict bool |
| 31 | } |
| 32 | |
| 33 | // IsKeyword returns the keyword token if literal is a keyword, a KEYWORD token |
| 34 | // if the literal is a future keyword (const, let, class, super, ...), or 0 if the literal is not a keyword. |
| 35 | // |
| 36 | // If the literal is a keyword, IsKeyword returns a second value indicating if the literal |
| 37 | // is considered a future keyword in strict-mode only. |
| 38 | // |
| 39 | // 7.6.1.2 Future Reserved Words: |
| 40 | // |
| 41 | // const |
| 42 | // class |
| 43 | // enum |
| 44 | // export |
| 45 | // extends |
| 46 | // import |
| 47 | // super |
| 48 | // |
| 49 | // 7.6.1.2 Future Reserved Words (strict): |
| 50 | // |
| 51 | // implements |
| 52 | // interface |
| 53 | // let |
| 54 | // package |
| 55 | // private |
| 56 | // protected |
| 57 | // public |
| 58 | // static |
| 59 | func IsKeyword(literal string) (Token, bool) { |
| 60 | if kw, exists := keywordTable[literal]; exists { |
| 61 | if kw.futureKeyword { |
| 62 | return KEYWORD, kw.strict |
| 63 | } |
| 64 | return kw.token, false |
| 65 | } |
| 66 | return 0, false |
| 67 | } |