Module refinery.lib.scripts.js.token

Expand source code Browse git
from __future__ import annotations

import enum

from dataclasses import dataclass


class JsTokenKind(enum.Enum):
    INTEGER         = 'integer'          # noqa
    FLOAT           = 'float'            # noqa
    BIGINT          = 'bigint'           # noqa
    STRING_SINGLE   = 'sq-string'        # noqa
    STRING_DOUBLE   = 'dq-string'        # noqa
    TEMPLATE_FULL   = 'template-full'    # noqa
    TEMPLATE_HEAD   = 'template-head'    # noqa
    TEMPLATE_MIDDLE = 'template-middle'  # noqa
    TEMPLATE_TAIL   = 'template-tail'    # noqa
    REGEXP          = 'regexp'           # noqa

    IDENTIFIER      = 'identifier'       # noqa

    VAR             = 'var'              # noqa
    LET             = 'let'              # noqa
    CONST           = 'const'            # noqa
    FUNCTION        = 'function'         # noqa
    CLASS           = 'class'            # noqa
    IF              = 'if'               # noqa
    ELSE            = 'else'             # noqa
    FOR             = 'for'              # noqa
    WHILE           = 'while'            # noqa
    DO              = 'do'               # noqa
    SWITCH          = 'switch'           # noqa
    CASE            = 'case'             # noqa
    DEFAULT         = 'default'          # noqa
    BREAK           = 'break'            # noqa
    CONTINUE        = 'continue'         # noqa
    RETURN          = 'return'           # noqa
    THROW           = 'throw'            # noqa
    TRY             = 'try'              # noqa
    CATCH           = 'catch'            # noqa
    FINALLY         = 'finally'          # noqa
    NEW             = 'new'              # noqa
    DELETE          = 'delete'           # noqa
    TYPEOF          = 'typeof'           # noqa
    VOID            = 'void'             # noqa
    INSTANCEOF      = 'instanceof'       # noqa
    IN              = 'in'               # noqa
    OF              = 'of'               # noqa
    IMPORT          = 'import'           # noqa
    EXPORT          = 'export'           # noqa
    FROM            = 'from'             # noqa
    AS              = 'as'               # noqa
    YIELD           = 'yield'            # noqa
    AWAIT           = 'await'            # noqa
    ASYNC           = 'async'            # noqa
    EXTENDS         = 'extends'          # noqa
    SUPER           = 'super'            # noqa
    THIS            = 'this'             # noqa
    NULL            = 'null'             # noqa
    TRUE            = 'true'             # noqa
    FALSE           = 'false'            # noqa
    DEBUGGER        = 'debugger'         # noqa
    WITH            = 'with'             # noqa

    PLUS            = '+'                # noqa
    MINUS           = '-'                # noqa
    STAR            = '*'                # noqa
    SLASH           = '/'                # noqa
    PERCENT         = '%'                # noqa
    STAR2           = '**'               # noqa
    EQUALS          = '='                # noqa
    PLUS_ASSIGN     = '+='               # noqa
    MINUS_ASSIGN    = '-='               # noqa
    STAR_ASSIGN     = '*='               # noqa
    SLASH_ASSIGN    = '/='               # noqa
    PERCENT_ASSIGN  = '%='               # noqa
    STAR2_ASSIGN    = '**='              # noqa
    AMP_ASSIGN      = '&='               # noqa
    PIPE_ASSIGN     = '|='               # noqa
    CARET_ASSIGN    = '^='               # noqa
    LT2_ASSIGN      = '<<='              # noqa
    GT2_ASSIGN      = '>>='              # noqa
    GT3_ASSIGN      = '>>>='             # noqa
    AND_ASSIGN      = '&&='              # noqa
    OR_ASSIGN       = '||='              # noqa
    NULLISH_ASSIGN  = '??='              # noqa
    EQ2             = '=='               # noqa
    EQ3             = '==='              # noqa
    BANG_EQ         = '!='               # noqa
    BANG_EQ2        = '!=='              # noqa
    LT              = '<'                # noqa
    GT              = '>'                # noqa
    LT_EQ           = '<='               # noqa
    GT_EQ           = '>='               # noqa
    AND             = '&&'               # noqa
    OR              = '||'               # noqa
    QQ              = '??'               # noqa
    BANG            = '!'                # noqa
    AMP             = '&'                # noqa
    PIPE            = '|'                # noqa
    CARET           = '^'                # noqa
    TILDE           = '~'                # noqa
    LT2             = '<<'               # noqa
    GT2             = '>>'               # noqa
    GT3             = '>>>'              # noqa
    INC             = '++'               # noqa
    DEC             = '--'               # noqa
    DOT             = '.'                # noqa
    ELLIPSIS        = '...'              # noqa
    QUESTION_DOT    = '?.'               # noqa
    ARROW           = '=>'               # noqa
    QUESTION        = '?'                # noqa
    COLON           = ':'                # noqa

    LPAREN          = '('                # noqa
    RPAREN          = ')'                # noqa
    LBRACE          = '{'                # noqa
    RBRACE          = '}'                # noqa
    LBRACKET        = '['                # noqa
    RBRACKET        = ']'                # noqa
    SEMICOLON       = ';'                # noqa
    COMMA           = ','                # noqa

    NEWLINE         = 'newline'          # noqa
    COMMENT         = 'comment'          # noqa
    ERROR           = 'error'            # noqa
    EOF             = 'eof'              # noqa

    @property
    def is_keyword(self):
        return self in _KEYWORDS_SET

    @property
    def is_assignment(self):
        return self in _ASSIGNMENT_SET


KEYWORDS: dict[str, JsTokenKind] = {
    tok.value: tok for tok in [
        JsTokenKind.VAR,
        JsTokenKind.LET,
        JsTokenKind.CONST,
        JsTokenKind.FUNCTION,
        JsTokenKind.CLASS,
        JsTokenKind.IF,
        JsTokenKind.ELSE,
        JsTokenKind.FOR,
        JsTokenKind.WHILE,
        JsTokenKind.DO,
        JsTokenKind.SWITCH,
        JsTokenKind.CASE,
        JsTokenKind.DEFAULT,
        JsTokenKind.BREAK,
        JsTokenKind.CONTINUE,
        JsTokenKind.RETURN,
        JsTokenKind.THROW,
        JsTokenKind.TRY,
        JsTokenKind.CATCH,
        JsTokenKind.FINALLY,
        JsTokenKind.NEW,
        JsTokenKind.DELETE,
        JsTokenKind.TYPEOF,
        JsTokenKind.VOID,
        JsTokenKind.INSTANCEOF,
        JsTokenKind.IN,
        JsTokenKind.OF,
        JsTokenKind.IMPORT,
        JsTokenKind.EXPORT,
        JsTokenKind.FROM,
        JsTokenKind.AS,
        JsTokenKind.YIELD,
        JsTokenKind.AWAIT,
        JsTokenKind.ASYNC,
        JsTokenKind.EXTENDS,
        JsTokenKind.SUPER,
        JsTokenKind.THIS,
        JsTokenKind.NULL,
        JsTokenKind.TRUE,
        JsTokenKind.FALSE,
        JsTokenKind.DEBUGGER,
        JsTokenKind.WITH,
    ]
}

_KEYWORDS_SET = frozenset(KEYWORDS.values())

FUTURE_RESERVED: frozenset[str] = frozenset({
    'enum',
    'implements',
    'interface',
    'package',
    'private',
    'protected',
    'public',
    'static',
})

_ASSIGNMENT_SET = frozenset({
    JsTokenKind.EQUALS,
    JsTokenKind.PLUS_ASSIGN,
    JsTokenKind.MINUS_ASSIGN,
    JsTokenKind.STAR_ASSIGN,
    JsTokenKind.SLASH_ASSIGN,
    JsTokenKind.PERCENT_ASSIGN,
    JsTokenKind.STAR2_ASSIGN,
    JsTokenKind.AMP_ASSIGN,
    JsTokenKind.PIPE_ASSIGN,
    JsTokenKind.CARET_ASSIGN,
    JsTokenKind.LT2_ASSIGN,
    JsTokenKind.GT2_ASSIGN,
    JsTokenKind.GT3_ASSIGN,
    JsTokenKind.AND_ASSIGN,
    JsTokenKind.OR_ASSIGN,
    JsTokenKind.NULLISH_ASSIGN,
})


@dataclass
class JsToken:
    kind: JsTokenKind
    value: str
    offset: int

    def __repr__(self):
        v = self.value
        if len(v) > 15:
            v = F'{v[:8]}..{v[-4:]}'
        return F'Token({self.kind.name}, {v!r}, {self.offset})'

Classes

class JsTokenKind (*args, **kwds)

Create a collection of name/value pairs.

Example enumeration:

>>> class Color(Enum):
...     RED = 1
...     BLUE = 2
...     GREEN = 3

Access them by:

  • attribute access:

Color.RED

  • value lookup:

Color(1)

  • name lookup:

Color['RED']

Enumerations can be iterated over, and know how many members they have:

>>> len(Color)
3
>>> list(Color)
[<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]

Methods can be added to enumerations, and members can have their own attributes – see the documentation for details.

Expand source code Browse git
class JsTokenKind(enum.Enum):
    INTEGER         = 'integer'          # noqa
    FLOAT           = 'float'            # noqa
    BIGINT          = 'bigint'           # noqa
    STRING_SINGLE   = 'sq-string'        # noqa
    STRING_DOUBLE   = 'dq-string'        # noqa
    TEMPLATE_FULL   = 'template-full'    # noqa
    TEMPLATE_HEAD   = 'template-head'    # noqa
    TEMPLATE_MIDDLE = 'template-middle'  # noqa
    TEMPLATE_TAIL   = 'template-tail'    # noqa
    REGEXP          = 'regexp'           # noqa

    IDENTIFIER      = 'identifier'       # noqa

    VAR             = 'var'              # noqa
    LET             = 'let'              # noqa
    CONST           = 'const'            # noqa
    FUNCTION        = 'function'         # noqa
    CLASS           = 'class'            # noqa
    IF              = 'if'               # noqa
    ELSE            = 'else'             # noqa
    FOR             = 'for'              # noqa
    WHILE           = 'while'            # noqa
    DO              = 'do'               # noqa
    SWITCH          = 'switch'           # noqa
    CASE            = 'case'             # noqa
    DEFAULT         = 'default'          # noqa
    BREAK           = 'break'            # noqa
    CONTINUE        = 'continue'         # noqa
    RETURN          = 'return'           # noqa
    THROW           = 'throw'            # noqa
    TRY             = 'try'              # noqa
    CATCH           = 'catch'            # noqa
    FINALLY         = 'finally'          # noqa
    NEW             = 'new'              # noqa
    DELETE          = 'delete'           # noqa
    TYPEOF          = 'typeof'           # noqa
    VOID            = 'void'             # noqa
    INSTANCEOF      = 'instanceof'       # noqa
    IN              = 'in'               # noqa
    OF              = 'of'               # noqa
    IMPORT          = 'import'           # noqa
    EXPORT          = 'export'           # noqa
    FROM            = 'from'             # noqa
    AS              = 'as'               # noqa
    YIELD           = 'yield'            # noqa
    AWAIT           = 'await'            # noqa
    ASYNC           = 'async'            # noqa
    EXTENDS         = 'extends'          # noqa
    SUPER           = 'super'            # noqa
    THIS            = 'this'             # noqa
    NULL            = 'null'             # noqa
    TRUE            = 'true'             # noqa
    FALSE           = 'false'            # noqa
    DEBUGGER        = 'debugger'         # noqa
    WITH            = 'with'             # noqa

    PLUS            = '+'                # noqa
    MINUS           = '-'                # noqa
    STAR            = '*'                # noqa
    SLASH           = '/'                # noqa
    PERCENT         = '%'                # noqa
    STAR2           = '**'               # noqa
    EQUALS          = '='                # noqa
    PLUS_ASSIGN     = '+='               # noqa
    MINUS_ASSIGN    = '-='               # noqa
    STAR_ASSIGN     = '*='               # noqa
    SLASH_ASSIGN    = '/='               # noqa
    PERCENT_ASSIGN  = '%='               # noqa
    STAR2_ASSIGN    = '**='              # noqa
    AMP_ASSIGN      = '&='               # noqa
    PIPE_ASSIGN     = '|='               # noqa
    CARET_ASSIGN    = '^='               # noqa
    LT2_ASSIGN      = '<<='              # noqa
    GT2_ASSIGN      = '>>='              # noqa
    GT3_ASSIGN      = '>>>='             # noqa
    AND_ASSIGN      = '&&='              # noqa
    OR_ASSIGN       = '||='              # noqa
    NULLISH_ASSIGN  = '??='              # noqa
    EQ2             = '=='               # noqa
    EQ3             = '==='              # noqa
    BANG_EQ         = '!='               # noqa
    BANG_EQ2        = '!=='              # noqa
    LT              = '<'                # noqa
    GT              = '>'                # noqa
    LT_EQ           = '<='               # noqa
    GT_EQ           = '>='               # noqa
    AND             = '&&'               # noqa
    OR              = '||'               # noqa
    QQ              = '??'               # noqa
    BANG            = '!'                # noqa
    AMP             = '&'                # noqa
    PIPE            = '|'                # noqa
    CARET           = '^'                # noqa
    TILDE           = '~'                # noqa
    LT2             = '<<'               # noqa
    GT2             = '>>'               # noqa
    GT3             = '>>>'              # noqa
    INC             = '++'               # noqa
    DEC             = '--'               # noqa
    DOT             = '.'                # noqa
    ELLIPSIS        = '...'              # noqa
    QUESTION_DOT    = '?.'               # noqa
    ARROW           = '=>'               # noqa
    QUESTION        = '?'                # noqa
    COLON           = ':'                # noqa

    LPAREN          = '('                # noqa
    RPAREN          = ')'                # noqa
    LBRACE          = '{'                # noqa
    RBRACE          = '}'                # noqa
    LBRACKET        = '['                # noqa
    RBRACKET        = ']'                # noqa
    SEMICOLON       = ';'                # noqa
    COMMA           = ','                # noqa

    NEWLINE         = 'newline'          # noqa
    COMMENT         = 'comment'          # noqa
    ERROR           = 'error'            # noqa
    EOF             = 'eof'              # noqa

    @property
    def is_keyword(self):
        return self in _KEYWORDS_SET

    @property
    def is_assignment(self):
        return self in _ASSIGNMENT_SET

Ancestors

  • enum.Enum

Class variables

var INTEGER

The type of the None singleton.

var FLOAT

The type of the None singleton.

var BIGINT

The type of the None singleton.

var STRING_SINGLE

The type of the None singleton.

var STRING_DOUBLE

The type of the None singleton.

var TEMPLATE_FULL

The type of the None singleton.

var TEMPLATE_HEAD

The type of the None singleton.

var TEMPLATE_MIDDLE

The type of the None singleton.

var TEMPLATE_TAIL

The type of the None singleton.

var REGEXP

The type of the None singleton.

var IDENTIFIER

The type of the None singleton.

var VAR

The type of the None singleton.

var LET

The type of the None singleton.

var CONST

The type of the None singleton.

var FUNCTION

The type of the None singleton.

var CLASS

The type of the None singleton.

var IF

The type of the None singleton.

var ELSE

The type of the None singleton.

var FOR

The type of the None singleton.

var WHILE

The type of the None singleton.

var DO

The type of the None singleton.

var SWITCH

The type of the None singleton.

var CASE

The type of the None singleton.

var DEFAULT

The type of the None singleton.

var BREAK

The type of the None singleton.

var CONTINUE

The type of the None singleton.

var RETURN

The type of the None singleton.

var THROW

The type of the None singleton.

var TRY

The type of the None singleton.

var CATCH

The type of the None singleton.

var FINALLY

The type of the None singleton.

var NEW

The type of the None singleton.

var DELETE

The type of the None singleton.

var TYPEOF

The type of the None singleton.

var VOID

The type of the None singleton.

var INSTANCEOF

The type of the None singleton.

var IN

The type of the None singleton.

var OF

The type of the None singleton.

var IMPORT

The type of the None singleton.

var EXPORT

The type of the None singleton.

var FROM

The type of the None singleton.

var AS

The type of the None singleton.

var YIELD

The type of the None singleton.

var AWAIT

The type of the None singleton.

var ASYNC

The type of the None singleton.

var EXTENDS

The type of the None singleton.

var SUPER

The type of the None singleton.

var THIS

The type of the None singleton.

var NULL

The type of the None singleton.

var TRUE

The type of the None singleton.

var FALSE

The type of the None singleton.

var DEBUGGER

The type of the None singleton.

var WITH

The type of the None singleton.

var PLUS

The type of the None singleton.

var MINUS

The type of the None singleton.

var STAR

The type of the None singleton.

var SLASH

The type of the None singleton.

var PERCENT

The type of the None singleton.

var STAR2

The type of the None singleton.

var EQUALS

The type of the None singleton.

var PLUS_ASSIGN

The type of the None singleton.

var MINUS_ASSIGN

The type of the None singleton.

var STAR_ASSIGN

The type of the None singleton.

var SLASH_ASSIGN

The type of the None singleton.

var PERCENT_ASSIGN

The type of the None singleton.

var STAR2_ASSIGN

The type of the None singleton.

var AMP_ASSIGN

The type of the None singleton.

var PIPE_ASSIGN

The type of the None singleton.

var CARET_ASSIGN

The type of the None singleton.

var LT2_ASSIGN

The type of the None singleton.

var GT2_ASSIGN

The type of the None singleton.

var GT3_ASSIGN

The type of the None singleton.

var AND_ASSIGN

The type of the None singleton.

var OR_ASSIGN

The type of the None singleton.

var NULLISH_ASSIGN

The type of the None singleton.

var EQ2

The type of the None singleton.

var EQ3

The type of the None singleton.

var BANG_EQ

The type of the None singleton.

var BANG_EQ2

The type of the None singleton.

var LT

The type of the None singleton.

var GT

The type of the None singleton.

var LT_EQ

The type of the None singleton.

var GT_EQ

The type of the None singleton.

var AND

The type of the None singleton.

var OR

The type of the None singleton.

var QQ

The type of the None singleton.

var BANG

The type of the None singleton.

var AMP

The type of the None singleton.

var PIPE

The type of the None singleton.

var CARET

The type of the None singleton.

var TILDE

The type of the None singleton.

var LT2

The type of the None singleton.

var GT2

The type of the None singleton.

var GT3

The type of the None singleton.

var INC

The type of the None singleton.

var DEC

The type of the None singleton.

var DOT

The type of the None singleton.

var ELLIPSIS

The type of the None singleton.

var QUESTION_DOT

The type of the None singleton.

var ARROW

The type of the None singleton.

var QUESTION

The type of the None singleton.

var COLON

The type of the None singleton.

var LPAREN

The type of the None singleton.

var RPAREN

The type of the None singleton.

var LBRACE

The type of the None singleton.

var RBRACE

The type of the None singleton.

var LBRACKET

The type of the None singleton.

var RBRACKET

The type of the None singleton.

var SEMICOLON

The type of the None singleton.

var COMMA

The type of the None singleton.

var NEWLINE

The type of the None singleton.

var COMMENT

The type of the None singleton.

var ERROR

The type of the None singleton.

var EOF

The type of the None singleton.

Instance variables

var is_keyword
Expand source code Browse git
@property
def is_keyword(self):
    return self in _KEYWORDS_SET
var is_assignment
Expand source code Browse git
@property
def is_assignment(self):
    return self in _ASSIGNMENT_SET
class JsToken (kind, value, offset)

JsToken(kind: 'JsTokenKind', value: 'str', offset: 'int')

Expand source code Browse git
@dataclass
class JsToken:
    kind: JsTokenKind
    value: str
    offset: int

    def __repr__(self):
        v = self.value
        if len(v) > 15:
            v = F'{v[:8]}..{v[-4:]}'
        return F'Token({self.kind.name}, {v!r}, {self.offset})'

Instance variables

var kind

The type of the None singleton.

var value

The type of the None singleton.

var offset

The type of the None singleton.