Skip to content

synt.tokens.lit ¤

litstr module-attribute ¤

litstr = str_

Alias for str_.

litint module-attribute ¤

litint = int_

Alias for int_.

litfloat module-attribute ¤

litfloat = float_

Alias for float_.

litbool module-attribute ¤

litbool = bool_

Alias for bool_.

TRUE module-attribute ¤

TRUE = bool_(True)

Alias for bool_(True).

FALSE module-attribute ¤

FALSE = bool_(False)

Alias for bool_(False).

NONE module-attribute ¤

NONE = Literal('None')

Alias for a literal None.

ELLIPSIS module-attribute ¤

ELLIPSIS = Literal('...')

Alias for a literal ellipsis ....

UNDERSCORE module-attribute ¤

UNDERSCORE = Literal('_')

Alias for a literal underscore _.

ELIDE module-attribute ¤

ELIDE = Literal('_')

Alias for a literal underscore _.

Literal ¤

Bases: Expression

Literal Python expression.

Source code in synt/tokens/lit.py
class Literal(Expression):
    r"""Literal Python expression."""

    lit: str
    """Source code of the literal."""

    precedence = ExprPrecedence.Atom
    expr_type = ExprType.Literal

    def __init__(self, src: str):
        """Initialize a Literal value.

        **DO NOT USE THIS IN YOUR CODE!** Use other entry points instead.

        Args:
            src: Source code of the literal.
        """
        self.lit = src

    @staticmethod
    def bool_(b: bool) -> Literal:
        """Initialize a literal boolean.

        Notes:
            `bool` is a built-in type, so this function is suffixed with a `_`.

        Args:
            b: Original boolean.

        Examples:
            ```python
            a = litbool(True)
            assert a.into_code() == "True"
            ```
        """
        if b:
            return Literal("True")
        else:
            return Literal("False")

    @staticmethod
    def str_(s: str) -> Literal:
        """Initialize a literal string.

        Notes:
            `str` is a built-in type, so this function is suffixed with a `_`.

        Args:
            s: Original string.

        Examples:
            ```python
            a = litstr("abc")
            assert a.into_code() == "'abc'"
            ```
        """
        return Literal._repr(s)

    @staticmethod
    def int_(s: int) -> Literal:
        """Initialize a literal integer.

        Notes:
            `int` is a built-in type, so this function is suffixed with a `_`.

        Args:
            s: Original integer.

        Examples:
            ```python
            a = litint(1)
            assert a.into_code() == "1"
            ```
        """
        return Literal._repr(s)

    @staticmethod
    def float_(s: float) -> Literal:
        """Initialize a literal float.

        Args:
            s: Original float.

        Notes:
            `float` is a built-in type, so this function is suffixed with a `_`.

        Examples:
            ```python
            a = litfloat(0.24)
            assert a.into_code() == "0.24"
            ```
        """
        return Literal._repr(s)

    @staticmethod
    def _repr(mr: Any) -> Literal:
        """Initialize a literal value with the `__repr__` method of the given value.

        Args:
            mr: Value to be represented.

        Raises:
            AttributeError: `__repr__` is not found for the given value.
        """
        return Literal(repr(mr))

    @staticmethod
    def _str(s: Any) -> Literal:
        """Initialize a literal value with the `__str__` method of the given value.

        Args:
            s: Value to be represented.

        Raises:
            AttributeError: `__str__` is not found for the given value.
        """
        return Literal(str(s))

    def into_code(self) -> str:
        return self.lit

precedence class-attribute instance-attribute ¤

precedence = Atom

expr_type class-attribute instance-attribute ¤

expr_type = Literal

lit instance-attribute ¤

lit: str = src

Source code of the literal.

__init__ ¤

__init__(src: str)

Initialize a Literal value.

DO NOT USE THIS IN YOUR CODE! Use other entry points instead.

Parameters:

Name Type Description Default
src str

Source code of the literal.

required
Source code in synt/tokens/lit.py
def __init__(self, src: str):
    """Initialize a Literal value.

    **DO NOT USE THIS IN YOUR CODE!** Use other entry points instead.

    Args:
        src: Source code of the literal.
    """
    self.lit = src

bool_ staticmethod ¤

bool_(b: bool) -> Literal

Initialize a literal boolean.

Notes

bool is a built-in type, so this function is suffixed with a _.

Parameters:

Name Type Description Default
b bool

Original boolean.

required

Examples:

a = litbool(True)
assert a.into_code() == "True"
Source code in synt/tokens/lit.py
@staticmethod
def bool_(b: bool) -> Literal:
    """Initialize a literal boolean.

    Notes:
        `bool` is a built-in type, so this function is suffixed with a `_`.

    Args:
        b: Original boolean.

    Examples:
        ```python
        a = litbool(True)
        assert a.into_code() == "True"
        ```
    """
    if b:
        return Literal("True")
    else:
        return Literal("False")

str_ staticmethod ¤

str_(s: str) -> Literal

Initialize a literal string.

Notes

str is a built-in type, so this function is suffixed with a _.

Parameters:

Name Type Description Default
s str

Original string.

required

Examples:

a = litstr("abc")
assert a.into_code() == "'abc'"
Source code in synt/tokens/lit.py
@staticmethod
def str_(s: str) -> Literal:
    """Initialize a literal string.

    Notes:
        `str` is a built-in type, so this function is suffixed with a `_`.

    Args:
        s: Original string.

    Examples:
        ```python
        a = litstr("abc")
        assert a.into_code() == "'abc'"
        ```
    """
    return Literal._repr(s)

int_ staticmethod ¤

int_(s: int) -> Literal

Initialize a literal integer.

Notes

int is a built-in type, so this function is suffixed with a _.

Parameters:

Name Type Description Default
s int

Original integer.

required

Examples:

a = litint(1)
assert a.into_code() == "1"
Source code in synt/tokens/lit.py
@staticmethod
def int_(s: int) -> Literal:
    """Initialize a literal integer.

    Notes:
        `int` is a built-in type, so this function is suffixed with a `_`.

    Args:
        s: Original integer.

    Examples:
        ```python
        a = litint(1)
        assert a.into_code() == "1"
        ```
    """
    return Literal._repr(s)

float_ staticmethod ¤

float_(s: float) -> Literal

Initialize a literal float.

Parameters:

Name Type Description Default
s float

Original float.

required
Notes

float is a built-in type, so this function is suffixed with a _.

Examples:

a = litfloat(0.24)
assert a.into_code() == "0.24"
Source code in synt/tokens/lit.py
@staticmethod
def float_(s: float) -> Literal:
    """Initialize a literal float.

    Args:
        s: Original float.

    Notes:
        `float` is a built-in type, so this function is suffixed with a `_`.

    Examples:
        ```python
        a = litfloat(0.24)
        assert a.into_code() == "0.24"
        ```
    """
    return Literal._repr(s)

into_code ¤

into_code() -> str
Source code in synt/tokens/lit.py
def into_code(self) -> str:
    return self.lit