Module refinery.lib.scripts.au3.token

Expand source code Browse git
from __future__ import annotations

import enum

from dataclasses import dataclass


class Au3TokenKind(enum.Enum):
    INTEGER             = 'integer'           # noqa
    FLOAT               = 'float'             # noqa
    STRING              = 'string'            # noqa
    VARIABLE            = 'variable'          # noqa
    MACRO               = 'macro'             # noqa
    IDENTIFIER          = 'identifier'        # noqa

    AND                 = 'and'               # noqa
    OR                  = 'or'                # noqa
    NOT                 = 'not'               # noqa
    IF                  = 'if'                # noqa
    THEN                = 'then'              # noqa
    ELSE                = 'else'              # noqa
    ELSEIF              = 'elseif'             # noqa
    ENDIF               = 'endif'             # noqa
    WHILE               = 'while'             # noqa
    WEND                = 'wend'              # noqa
    DO                  = 'do'                # noqa
    UNTIL               = 'until'             # noqa
    FOR                 = 'for'               # noqa
    NEXT                = 'next'              # noqa
    TO                  = 'to'                # noqa
    STEP                = 'step'              # noqa
    IN                  = 'in'                # noqa
    EXITLOOP            = 'exitloop'          # noqa
    CONTINUELOOP        = 'continueloop'      # noqa
    SELECT              = 'select'            # noqa
    CASE                = 'case'              # noqa
    ENDSELECT           = 'endselect'         # noqa
    SWITCH              = 'switch'            # noqa
    ENDSWITCH           = 'endswitch'         # noqa
    CONTINUECASE        = 'continuecase'       # noqa
    DIM                 = 'dim'               # noqa
    REDIM               = 'redim'             # noqa
    LOCAL               = 'local'             # noqa
    GLOBAL              = 'global'            # noqa
    CONST               = 'const'             # noqa
    STATIC              = 'static'            # noqa
    FUNC                = 'func'              # noqa
    ENDFUNC             = 'endfunc'           # noqa
    RETURN              = 'return'            # noqa
    EXIT                = 'exit'              # noqa
    BYREF               = 'byref'             # noqa
    WITH                = 'with'              # noqa
    ENDWITH             = 'endwith'           # noqa
    TRUE                = 'true'              # noqa
    FALSE               = 'false'             # noqa
    DEFAULT             = 'default'           # noqa
    NULL                = 'null'              # noqa
    VOLATILE            = 'volatile'          # noqa
    ENUM                = 'enum'              # noqa

    ASSIGN_OR_EQ        = '='                 # noqa
    EQ_STRICT           = '=='                # noqa
    NE                  = '<>'                # noqa
    LE                  = '<='                # noqa
    GE                  = '>='                # noqa
    PLUS_ASSIGN         = '+='                # noqa
    MINUS_ASSIGN        = '-='                # noqa
    MUL_ASSIGN          = '*='                # noqa
    DIV_ASSIGN          = '/='                # noqa
    CONCAT_ASSIGN       = '&='                # noqa
    PLUS                = '+'                 # noqa
    MINUS               = '-'                 # noqa
    STAR                = '*'                 # noqa
    SLASH               = '/'                 # noqa
    CARET               = '^'                 # noqa
    AMP                 = '&'                 # noqa
    LT                  = '<'                 # noqa
    GT                  = '>'                 # noqa
    QUESTION            = '?'                 # noqa
    COLON               = ':'                 # noqa
    LPAREN              = '('                 # noqa
    RPAREN              = ')'                 # noqa
    LBRACKET            = '['                 # noqa
    RBRACKET            = ']'                 # noqa
    COMMA               = ','                 # noqa
    DOT                 = '.'                 # noqa

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

    @property
    def is_keyword(self) -> bool:
        return self in _KEYWORDS_SET


KEYWORD_SPELLINGS: dict[Au3TokenKind, str] = {
    Au3TokenKind.AND           : 'And',
    Au3TokenKind.OR            : 'Or',
    Au3TokenKind.NOT           : 'Not',
    Au3TokenKind.IF            : 'If',
    Au3TokenKind.THEN          : 'Then',
    Au3TokenKind.ELSE          : 'Else',
    Au3TokenKind.ELSEIF        : 'ElseIf',
    Au3TokenKind.ENDIF         : 'EndIf',
    Au3TokenKind.WHILE         : 'While',
    Au3TokenKind.WEND          : 'Wend',
    Au3TokenKind.DO            : 'Do',
    Au3TokenKind.UNTIL         : 'Until',
    Au3TokenKind.FOR           : 'For',
    Au3TokenKind.NEXT          : 'Next',
    Au3TokenKind.TO            : 'To',
    Au3TokenKind.STEP          : 'Step',
    Au3TokenKind.IN            : 'In',
    Au3TokenKind.EXITLOOP      : 'ExitLoop',
    Au3TokenKind.CONTINUELOOP  : 'ContinueLoop',
    Au3TokenKind.SELECT        : 'Select',
    Au3TokenKind.CASE          : 'Case',
    Au3TokenKind.ENDSELECT     : 'EndSelect',
    Au3TokenKind.SWITCH        : 'Switch',
    Au3TokenKind.ENDSWITCH     : 'EndSwitch',
    Au3TokenKind.CONTINUECASE  : 'ContinueCase',
    Au3TokenKind.DIM           : 'Dim',
    Au3TokenKind.REDIM         : 'ReDim',
    Au3TokenKind.LOCAL         : 'Local',
    Au3TokenKind.GLOBAL        : 'Global',
    Au3TokenKind.CONST         : 'Const',
    Au3TokenKind.STATIC        : 'Static',
    Au3TokenKind.FUNC          : 'Func',
    Au3TokenKind.ENDFUNC       : 'EndFunc',
    Au3TokenKind.RETURN        : 'Return',
    Au3TokenKind.EXIT          : 'Exit',
    Au3TokenKind.BYREF         : 'ByRef',
    Au3TokenKind.WITH          : 'With',
    Au3TokenKind.ENDWITH       : 'EndWith',
    Au3TokenKind.TRUE          : 'True',
    Au3TokenKind.FALSE         : 'False',
    Au3TokenKind.DEFAULT       : 'Default',
    Au3TokenKind.NULL          : 'Null',
    Au3TokenKind.VOLATILE      : 'Volatile',
    Au3TokenKind.ENUM          : 'Enum',
}

KEYWORDS: dict[str, Au3TokenKind] = {
    spelling.lower(): kind for kind, spelling in KEYWORD_SPELLINGS.items()
}

_KEYWORDS_SET = frozenset(KEYWORDS.values())


@dataclass
class Au3Token:
    kind: Au3TokenKind
    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 Au3TokenKind (*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 Au3TokenKind(enum.Enum):
    INTEGER             = 'integer'           # noqa
    FLOAT               = 'float'             # noqa
    STRING              = 'string'            # noqa
    VARIABLE            = 'variable'          # noqa
    MACRO               = 'macro'             # noqa
    IDENTIFIER          = 'identifier'        # noqa

    AND                 = 'and'               # noqa
    OR                  = 'or'                # noqa
    NOT                 = 'not'               # noqa
    IF                  = 'if'                # noqa
    THEN                = 'then'              # noqa
    ELSE                = 'else'              # noqa
    ELSEIF              = 'elseif'             # noqa
    ENDIF               = 'endif'             # noqa
    WHILE               = 'while'             # noqa
    WEND                = 'wend'              # noqa
    DO                  = 'do'                # noqa
    UNTIL               = 'until'             # noqa
    FOR                 = 'for'               # noqa
    NEXT                = 'next'              # noqa
    TO                  = 'to'                # noqa
    STEP                = 'step'              # noqa
    IN                  = 'in'                # noqa
    EXITLOOP            = 'exitloop'          # noqa
    CONTINUELOOP        = 'continueloop'      # noqa
    SELECT              = 'select'            # noqa
    CASE                = 'case'              # noqa
    ENDSELECT           = 'endselect'         # noqa
    SWITCH              = 'switch'            # noqa
    ENDSWITCH           = 'endswitch'         # noqa
    CONTINUECASE        = 'continuecase'       # noqa
    DIM                 = 'dim'               # noqa
    REDIM               = 'redim'             # noqa
    LOCAL               = 'local'             # noqa
    GLOBAL              = 'global'            # noqa
    CONST               = 'const'             # noqa
    STATIC              = 'static'            # noqa
    FUNC                = 'func'              # noqa
    ENDFUNC             = 'endfunc'           # noqa
    RETURN              = 'return'            # noqa
    EXIT                = 'exit'              # noqa
    BYREF               = 'byref'             # noqa
    WITH                = 'with'              # noqa
    ENDWITH             = 'endwith'           # noqa
    TRUE                = 'true'              # noqa
    FALSE               = 'false'             # noqa
    DEFAULT             = 'default'           # noqa
    NULL                = 'null'              # noqa
    VOLATILE            = 'volatile'          # noqa
    ENUM                = 'enum'              # noqa

    ASSIGN_OR_EQ        = '='                 # noqa
    EQ_STRICT           = '=='                # noqa
    NE                  = '<>'                # noqa
    LE                  = '<='                # noqa
    GE                  = '>='                # noqa
    PLUS_ASSIGN         = '+='                # noqa
    MINUS_ASSIGN        = '-='                # noqa
    MUL_ASSIGN          = '*='                # noqa
    DIV_ASSIGN          = '/='                # noqa
    CONCAT_ASSIGN       = '&='                # noqa
    PLUS                = '+'                 # noqa
    MINUS               = '-'                 # noqa
    STAR                = '*'                 # noqa
    SLASH               = '/'                 # noqa
    CARET               = '^'                 # noqa
    AMP                 = '&'                 # noqa
    LT                  = '<'                 # noqa
    GT                  = '>'                 # noqa
    QUESTION            = '?'                 # noqa
    COLON               = ':'                 # noqa
    LPAREN              = '('                 # noqa
    RPAREN              = ')'                 # noqa
    LBRACKET            = '['                 # noqa
    RBRACKET            = ']'                 # noqa
    COMMA               = ','                 # noqa
    DOT                 = '.'                 # noqa

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

    @property
    def is_keyword(self) -> bool:
        return self in _KEYWORDS_SET

Ancestors

  • enum.Enum

Class variables

var INTEGER

The type of the None singleton.

var FLOAT

The type of the None singleton.

var STRING

The type of the None singleton.

var VARIABLE

The type of the None singleton.

var MACRO

The type of the None singleton.

var IDENTIFIER

The type of the None singleton.

var AND

The type of the None singleton.

var OR

The type of the None singleton.

var NOT

The type of the None singleton.

var IF

The type of the None singleton.

var THEN

The type of the None singleton.

var ELSE

The type of the None singleton.

var ELSEIF

The type of the None singleton.

var ENDIF

The type of the None singleton.

var WHILE

The type of the None singleton.

var WEND

The type of the None singleton.

var DO

The type of the None singleton.

var UNTIL

The type of the None singleton.

var FOR

The type of the None singleton.

var NEXT

The type of the None singleton.

var TO

The type of the None singleton.

var STEP

The type of the None singleton.

var IN

The type of the None singleton.

var EXITLOOP

The type of the None singleton.

var CONTINUELOOP

The type of the None singleton.

var SELECT

The type of the None singleton.

var CASE

The type of the None singleton.

var ENDSELECT

The type of the None singleton.

var SWITCH

The type of the None singleton.

var ENDSWITCH

The type of the None singleton.

var CONTINUECASE

The type of the None singleton.

var DIM

The type of the None singleton.

var REDIM

The type of the None singleton.

var LOCAL

The type of the None singleton.

var GLOBAL

The type of the None singleton.

var CONST

The type of the None singleton.

var STATIC

The type of the None singleton.

var FUNC

The type of the None singleton.

var ENDFUNC

The type of the None singleton.

var RETURN

The type of the None singleton.

var EXIT

The type of the None singleton.

var BYREF

The type of the None singleton.

var WITH

The type of the None singleton.

var ENDWITH

The type of the None singleton.

var TRUE

The type of the None singleton.

var FALSE

The type of the None singleton.

var DEFAULT

The type of the None singleton.

var NULL

The type of the None singleton.

var VOLATILE

The type of the None singleton.

var ENUM

The type of the None singleton.

var ASSIGN_OR_EQ

The type of the None singleton.

var EQ_STRICT

The type of the None singleton.

var NE

The type of the None singleton.

var LE

The type of the None singleton.

var GE

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 MUL_ASSIGN

The type of the None singleton.

var DIV_ASSIGN

The type of the None singleton.

var CONCAT_ASSIGN

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 CARET

The type of the None singleton.

var AMP

The type of the None singleton.

var LT

The type of the None singleton.

var GT

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 LBRACKET

The type of the None singleton.

var RBRACKET

The type of the None singleton.

var COMMA

The type of the None singleton.

var DOT

The type of the None singleton.

var DIRECTIVE

The type of the None singleton.

var COMMENT

The type of the None singleton.

var NEWLINE

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) -> bool:
    return self in _KEYWORDS_SET
class Au3Token (kind, value, offset)

Au3Token(kind: 'Au3TokenKind', value: 'str', offset: 'int')

Expand source code Browse git
@dataclass
class Au3Token:
    kind: Au3TokenKind
    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.