Module refinery.lib.scripts.ps1.token

Expand source code Browse git
from __future__ import annotations

import enum

from dataclasses import dataclass


class Ps1TokenKind(enum.Enum):
    INTEGER          = 'integer'         # noqa
    REAL             = 'real'            # noqa
    STRING_VERBATIM  = 'sq-string'       # noqa
    STRING_EXPAND    = 'dq-string'       # noqa
    HSTRING_VERBATIM = 'sq-hstring'      # noqa
    HSTRING_EXPAND   = 'dq-hstring'      # noqa

    VARIABLE         = 'variable'        # noqa
    SPLAT_VARIABLE   = 'splat-variable'  # noqa
    LABEL            = 'label'           # noqa

    PLUS             = '+'               # noqa
    DASH             = '-'               # noqa
    STAR             = '*'               # noqa
    SLASH            = '/'               # noqa
    PERCENT          = '%'               # noqa
    DOT              = '.'               # noqa
    DOTDOT           = '..'              # noqa
    COMMA            = ','               # noqa
    SEMICOLON        = ';'               # noqa
    INCREMENT        = '++'              # noqa
    DECREMENT        = '--'              # noqa
    EXCLAIM          = '!'               # noqa
    DOUBLE_COLON     = '::'              # noqa
    DOUBLE_AMPERSAND = '&&'              # noqa
    DOUBLE_PIPE      = '||'              # noqa

    EQUALS           = '='               # noqa
    PLUS_ASSIGN      = '+='              # noqa
    DASH_ASSIGN      = '-='              # noqa
    STAR_ASSIGN      = '*='              # noqa
    SLASH_ASSIGN     = '/='              # noqa
    PERCENT_ASSIGN   = '%='              # noqa

    OPERATOR         = 'operator'        # noqa
    PARAMETER        = 'parameter'       # noqa
    GENERIC_TOKEN    = 'generic-token'   # noqa
    GENERIC_EXPAND   = 'generic-expand'  # noqa

    LPAREN           = '('               # noqa
    RPAREN           = ')'               # noqa
    LBRACE           = '{'               # noqa
    RBRACE           = '}'               # noqa
    LBRACKET         = '['               # noqa
    RBRACKET         = ']'               # noqa
    AT_LPAREN        = '@('              # noqa
    AT_LBRACE        = '@{'              # noqa
    DOLLAR_LPAREN    = '$('              # noqa

    PIPE             = '|'               # noqa
    AMPERSAND        = '&'               # noqa
    REDIRECTION      = 'redirection'     # noqa

    IF               = 'if'              # noqa
    ELSEIF           = 'elseif'          # noqa
    ELSE             = 'else'            # noqa
    SWITCH           = 'switch'          # noqa
    WHILE            = 'while'           # noqa
    FOR              = 'for'             # noqa
    FOREACH          = 'foreach'         # noqa
    DO               = 'do'              # noqa
    UNTIL            = 'until'           # noqa
    FUNCTION         = 'function'        # noqa
    FILTER           = 'filter'          # noqa
    RETURN           = 'return'          # noqa
    BREAK            = 'break'           # noqa
    CONTINUE         = 'continue'        # noqa
    THROW            = 'throw'           # noqa
    EXIT             = 'exit'            # noqa
    TRY              = 'try'             # noqa
    CATCH            = 'catch'           # noqa
    FINALLY          = 'finally'         # noqa
    TRAP             = 'trap'            # noqa
    DATA             = 'data'            # noqa
    BEGIN            = 'begin'           # noqa
    PROCESS          = 'process'         # noqa
    END              = 'end'             # noqa
    PARAM            = 'param'           # noqa
    IN               = 'in'              # noqa
    CLASS            = 'class'           # noqa
    USING            = 'using'           # noqa
    ENUM             = 'enum'            # noqa
    DYNAMICPARAM     = 'dynamicparam'    # noqa

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

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

    @property
    def is_assignment(self):
        return self in (
            Ps1TokenKind.EQUALS,
            Ps1TokenKind.PLUS_ASSIGN,
            Ps1TokenKind.DASH_ASSIGN,
            Ps1TokenKind.STAR_ASSIGN,
            Ps1TokenKind.SLASH_ASSIGN,
            Ps1TokenKind.PERCENT_ASSIGN,
        )


_KEYWORDS: dict[str, Ps1TokenKind] = {
    'if'           : Ps1TokenKind.IF,
    'elseif'       : Ps1TokenKind.ELSEIF,
    'else'         : Ps1TokenKind.ELSE,
    'switch'       : Ps1TokenKind.SWITCH,
    'while'        : Ps1TokenKind.WHILE,
    'for'          : Ps1TokenKind.FOR,
    'foreach'      : Ps1TokenKind.FOREACH,
    'do'           : Ps1TokenKind.DO,
    'until'        : Ps1TokenKind.UNTIL,
    'function'     : Ps1TokenKind.FUNCTION,
    'filter'       : Ps1TokenKind.FILTER,
    'return'       : Ps1TokenKind.RETURN,
    'break'        : Ps1TokenKind.BREAK,
    'continue'     : Ps1TokenKind.CONTINUE,
    'throw'        : Ps1TokenKind.THROW,
    'exit'         : Ps1TokenKind.EXIT,
    'try'          : Ps1TokenKind.TRY,
    'catch'        : Ps1TokenKind.CATCH,
    'finally'      : Ps1TokenKind.FINALLY,
    'trap'         : Ps1TokenKind.TRAP,
    'data'         : Ps1TokenKind.DATA,
    'begin'        : Ps1TokenKind.BEGIN,
    'process'      : Ps1TokenKind.PROCESS,
    'end'          : Ps1TokenKind.END,
    'param'        : Ps1TokenKind.PARAM,
    'in'           : Ps1TokenKind.IN,
    'class'        : Ps1TokenKind.CLASS,
    'using'        : Ps1TokenKind.USING,
    'enum'         : Ps1TokenKind.ENUM,
    'dynamicparam' : Ps1TokenKind.DYNAMICPARAM,
}

_KEYWORDS_SET = frozenset(_KEYWORDS.values())


@dataclass
class Ps1Token:
    kind: Ps1TokenKind
    value: str
    offset: int

    def __repr__(self):
        v = self.value
        if len(v) > 40:
            v = v[:37] + '...'
        return F'Token({self.kind.name}, {v!r}, @{self.offset})'

Classes

class Ps1TokenKind (*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 Ps1TokenKind(enum.Enum):
    INTEGER          = 'integer'         # noqa
    REAL             = 'real'            # noqa
    STRING_VERBATIM  = 'sq-string'       # noqa
    STRING_EXPAND    = 'dq-string'       # noqa
    HSTRING_VERBATIM = 'sq-hstring'      # noqa
    HSTRING_EXPAND   = 'dq-hstring'      # noqa

    VARIABLE         = 'variable'        # noqa
    SPLAT_VARIABLE   = 'splat-variable'  # noqa
    LABEL            = 'label'           # noqa

    PLUS             = '+'               # noqa
    DASH             = '-'               # noqa
    STAR             = '*'               # noqa
    SLASH            = '/'               # noqa
    PERCENT          = '%'               # noqa
    DOT              = '.'               # noqa
    DOTDOT           = '..'              # noqa
    COMMA            = ','               # noqa
    SEMICOLON        = ';'               # noqa
    INCREMENT        = '++'              # noqa
    DECREMENT        = '--'              # noqa
    EXCLAIM          = '!'               # noqa
    DOUBLE_COLON     = '::'              # noqa
    DOUBLE_AMPERSAND = '&&'              # noqa
    DOUBLE_PIPE      = '||'              # noqa

    EQUALS           = '='               # noqa
    PLUS_ASSIGN      = '+='              # noqa
    DASH_ASSIGN      = '-='              # noqa
    STAR_ASSIGN      = '*='              # noqa
    SLASH_ASSIGN     = '/='              # noqa
    PERCENT_ASSIGN   = '%='              # noqa

    OPERATOR         = 'operator'        # noqa
    PARAMETER        = 'parameter'       # noqa
    GENERIC_TOKEN    = 'generic-token'   # noqa
    GENERIC_EXPAND   = 'generic-expand'  # noqa

    LPAREN           = '('               # noqa
    RPAREN           = ')'               # noqa
    LBRACE           = '{'               # noqa
    RBRACE           = '}'               # noqa
    LBRACKET         = '['               # noqa
    RBRACKET         = ']'               # noqa
    AT_LPAREN        = '@('              # noqa
    AT_LBRACE        = '@{'              # noqa
    DOLLAR_LPAREN    = '$('              # noqa

    PIPE             = '|'               # noqa
    AMPERSAND        = '&'               # noqa
    REDIRECTION      = 'redirection'     # noqa

    IF               = 'if'              # noqa
    ELSEIF           = 'elseif'          # noqa
    ELSE             = 'else'            # noqa
    SWITCH           = 'switch'          # noqa
    WHILE            = 'while'           # noqa
    FOR              = 'for'             # noqa
    FOREACH          = 'foreach'         # noqa
    DO               = 'do'              # noqa
    UNTIL            = 'until'           # noqa
    FUNCTION         = 'function'        # noqa
    FILTER           = 'filter'          # noqa
    RETURN           = 'return'          # noqa
    BREAK            = 'break'           # noqa
    CONTINUE         = 'continue'        # noqa
    THROW            = 'throw'           # noqa
    EXIT             = 'exit'            # noqa
    TRY              = 'try'             # noqa
    CATCH            = 'catch'           # noqa
    FINALLY          = 'finally'         # noqa
    TRAP             = 'trap'            # noqa
    DATA             = 'data'            # noqa
    BEGIN            = 'begin'           # noqa
    PROCESS          = 'process'         # noqa
    END              = 'end'             # noqa
    PARAM            = 'param'           # noqa
    IN               = 'in'              # noqa
    CLASS            = 'class'           # noqa
    USING            = 'using'           # noqa
    ENUM             = 'enum'            # noqa
    DYNAMICPARAM     = 'dynamicparam'    # noqa

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

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

    @property
    def is_assignment(self):
        return self in (
            Ps1TokenKind.EQUALS,
            Ps1TokenKind.PLUS_ASSIGN,
            Ps1TokenKind.DASH_ASSIGN,
            Ps1TokenKind.STAR_ASSIGN,
            Ps1TokenKind.SLASH_ASSIGN,
            Ps1TokenKind.PERCENT_ASSIGN,
        )

Ancestors

  • enum.Enum

Class variables

var INTEGER

The type of the None singleton.

var REAL

The type of the None singleton.

var STRING_VERBATIM

The type of the None singleton.

var STRING_EXPAND

The type of the None singleton.

var HSTRING_VERBATIM

The type of the None singleton.

var HSTRING_EXPAND

The type of the None singleton.

var VARIABLE

The type of the None singleton.

var SPLAT_VARIABLE

The type of the None singleton.

var LABEL

The type of the None singleton.

var PLUS

The type of the None singleton.

var DASH

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 DOT

The type of the None singleton.

var DOTDOT

The type of the None singleton.

var COMMA

The type of the None singleton.

var SEMICOLON

The type of the None singleton.

var INCREMENT

The type of the None singleton.

var DECREMENT

The type of the None singleton.

var EXCLAIM

The type of the None singleton.

var DOUBLE_COLON

The type of the None singleton.

var DOUBLE_AMPERSAND

The type of the None singleton.

var DOUBLE_PIPE

The type of the None singleton.

var EQUALS

The type of the None singleton.

var PLUS_ASSIGN

The type of the None singleton.

var DASH_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 OPERATOR

The type of the None singleton.

var PARAMETER

The type of the None singleton.

var GENERIC_TOKEN

The type of the None singleton.

var GENERIC_EXPAND

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 AT_LPAREN

The type of the None singleton.

var AT_LBRACE

The type of the None singleton.

var DOLLAR_LPAREN

The type of the None singleton.

var PIPE

The type of the None singleton.

var AMPERSAND

The type of the None singleton.

var REDIRECTION

The type of the None singleton.

var IF

The type of the None singleton.

var ELSEIF

The type of the None singleton.

var ELSE

The type of the None singleton.

var SWITCH

The type of the None singleton.

var WHILE

The type of the None singleton.

var FOR

The type of the None singleton.

var FOREACH

The type of the None singleton.

var DO

The type of the None singleton.

var UNTIL

The type of the None singleton.

var FUNCTION

The type of the None singleton.

var FILTER

The type of the None singleton.

var RETURN

The type of the None singleton.

var BREAK

The type of the None singleton.

var CONTINUE

The type of the None singleton.

var THROW

The type of the None singleton.

var EXIT

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 TRAP

The type of the None singleton.

var DATA

The type of the None singleton.

var BEGIN

The type of the None singleton.

var PROCESS

The type of the None singleton.

var END

The type of the None singleton.

var PARAM

The type of the None singleton.

var IN

The type of the None singleton.

var CLASS

The type of the None singleton.

var USING

The type of the None singleton.

var ENUM

The type of the None singleton.

var DYNAMICPARAM

The type of the None singleton.

var NEWLINE

The type of the None singleton.

var COMMENT

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 (
        Ps1TokenKind.EQUALS,
        Ps1TokenKind.PLUS_ASSIGN,
        Ps1TokenKind.DASH_ASSIGN,
        Ps1TokenKind.STAR_ASSIGN,
        Ps1TokenKind.SLASH_ASSIGN,
        Ps1TokenKind.PERCENT_ASSIGN,
    )
class Ps1Token (kind, value, offset)

Ps1Token(kind: 'Ps1TokenKind', value: 'str', offset: 'int')

Expand source code Browse git
@dataclass
class Ps1Token:
    kind: Ps1TokenKind
    value: str
    offset: int

    def __repr__(self):
        v = self.value
        if len(v) > 40:
            v = v[:37] + '...'
        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.