gitee.com/quant1x/pkg@v0.2.8/goja/token/token.go (about)

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