Skip to content

synt.tokens.ident ¤

id_ module-attribute ¤

Alias Identifier.

Notes

id is a built-in function in Python, so it's renamed to id_ with a suffix.

Identifier ¤

Bases: IntoExpression, IntoCode

Represents a valid Python identifier.

For more information, see the Identifier and Keywords section of the Python's standard documentation.

Source code in synt/tokens/ident.py
class Identifier(expr.IntoExpression, code.IntoCode):
    r"""Represents a valid Python identifier.

    For more information, see the [Identifier and Keywords][ident-and-keywords-python-docs]
    section of the Python's standard documentation.

    [ident-and-keywords-python-docs]: https://docs.python.org/3.12/reference/lexical_analysis.html#identifiers
    """

    raw: str
    """Raw identifier text."""

    def __init__(self, raw: str):
        """Initialize a new identifier.

        The raw content will be checked immediately when initializing the object.

        Args:
            raw: Raw identifier text.

        Raises:
            InvalidIdentifierException: If the raw identifier text is not a valid identifier.

        Examples:
            ```python
            id_foo = synt.tokens.ident.Identifier('foo')
            id_foo_alias = id_('foo') # with alias
            try:
                id_fail = id_('foo bar') # invalid identifier lit will fail
            except ValueError:
                pass
            ```
        """
        if not raw.isidentifier():
            raise ValueError(f"Invalid identifier: `{raw!r}`")
        self.raw = raw

    def into_expression(self) -> IdentifierExpr:
        return IdentifierExpr(self)

    def expr(self) -> IdentifierExpr:
        """Initialize a new expression with `self`.

        Alias for [`into_expression`][synt.tokens.ident.Identifier.into_expression].

        Examples:
            ```python
            id_foo = id_('foo')
            id_foo_expr = id_foo.expr()
            assert isinstance(id_foo_expr, synt.expr.expr.Expression)
            ```
        """
        return IdentifierExpr(self)

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

    def as_(self, alias: Identifier) -> synt.expr.alias.Alias:
        """Construct a new alias.

        Args:
            alias: The alias name.
        """
        return synt.expr.alias.Alias(self, alias)

    def __hash__(self) -> int:
        return hash(("Identifier", self.raw))

raw instance-attribute ¤

raw: str = raw

Raw identifier text.

__init__ ¤

__init__(raw: str)

Initialize a new identifier.

The raw content will be checked immediately when initializing the object.

Parameters:

Name Type Description Default
raw str

Raw identifier text.

required

Raises:

Type Description
InvalidIdentifierException

If the raw identifier text is not a valid identifier.

Examples:

1
2
3
4
5
6
id_foo = synt.tokens.ident.Identifier('foo')
id_foo_alias = id_('foo') # with alias
try:
    id_fail = id_('foo bar') # invalid identifier lit will fail
except ValueError:
    pass
Source code in synt/tokens/ident.py
def __init__(self, raw: str):
    """Initialize a new identifier.

    The raw content will be checked immediately when initializing the object.

    Args:
        raw: Raw identifier text.

    Raises:
        InvalidIdentifierException: If the raw identifier text is not a valid identifier.

    Examples:
        ```python
        id_foo = synt.tokens.ident.Identifier('foo')
        id_foo_alias = id_('foo') # with alias
        try:
            id_fail = id_('foo bar') # invalid identifier lit will fail
        except ValueError:
            pass
        ```
    """
    if not raw.isidentifier():
        raise ValueError(f"Invalid identifier: `{raw!r}`")
    self.raw = raw

into_expression ¤

into_expression() -> IdentifierExpr
Source code in synt/tokens/ident.py
def into_expression(self) -> IdentifierExpr:
    return IdentifierExpr(self)

expr ¤

expr() -> IdentifierExpr

Initialize a new expression with self.

Alias for into_expression.

Examples:

1
2
3
id_foo = id_('foo')
id_foo_expr = id_foo.expr()
assert isinstance(id_foo_expr, synt.expr.expr.Expression)
Source code in synt/tokens/ident.py
def expr(self) -> IdentifierExpr:
    """Initialize a new expression with `self`.

    Alias for [`into_expression`][synt.tokens.ident.Identifier.into_expression].

    Examples:
        ```python
        id_foo = id_('foo')
        id_foo_expr = id_foo.expr()
        assert isinstance(id_foo_expr, synt.expr.expr.Expression)
        ```
    """
    return IdentifierExpr(self)

into_code ¤

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

as_ ¤

as_(alias: Identifier) -> Alias

Construct a new alias.

Parameters:

Name Type Description Default
alias Identifier

The alias name.

required
Source code in synt/tokens/ident.py
def as_(self, alias: Identifier) -> synt.expr.alias.Alias:
    """Construct a new alias.

    Args:
        alias: The alias name.
    """
    return synt.expr.alias.Alias(self, alias)

__hash__ ¤

__hash__() -> int
Source code in synt/tokens/ident.py
def __hash__(self) -> int:
    return hash(("Identifier", self.raw))

IdentifierExpr ¤

Bases: Expression

An identifier as a Python expression.

See Identifier for more information.

Source code in synt/tokens/ident.py
class IdentifierExpr(expr.Expression):
    r"""An identifier as a Python expression.

    See [`Identifier`][synt.tokens.ident.Identifier] for more information.
    """

    precedence = expr.ExprPrecedence.Atom
    expr_type = expr.ExprType.Identifier

    ident: Identifier
    """Inner identifier."""

    def __init__(self, raw: Identifier):
        """Initialize a new identifier.

        Use [`Identifier`][synt.tokens.ident.Identifier.expr] instead and converts it into an expression.

        Args:
            raw: Identifier to be used as an expression.
        """
        self.ident = raw

    @staticmethod
    def from_str(s: str) -> IdentifierExpr:
        """Parse an identifier from a string.

        The raw content will be checked immediately when initializing the object.

        Args:
            s: Raw identifier text.

        Raises:
            InvalidIdentifierException: If the raw identifier text is not a valid identifier.
        """
        return IdentifierExpr(Identifier(s))

    def into_code(self) -> str:
        return self.ident.raw

precedence class-attribute instance-attribute ¤

precedence = Atom

expr_type class-attribute instance-attribute ¤

expr_type = Identifier

ident instance-attribute ¤

ident: Identifier = raw

Inner identifier.

__init__ ¤

__init__(raw: Identifier)

Initialize a new identifier.

Use Identifier instead and converts it into an expression.

Parameters:

Name Type Description Default
raw Identifier

Identifier to be used as an expression.

required
Source code in synt/tokens/ident.py
def __init__(self, raw: Identifier):
    """Initialize a new identifier.

    Use [`Identifier`][synt.tokens.ident.Identifier.expr] instead and converts it into an expression.

    Args:
        raw: Identifier to be used as an expression.
    """
    self.ident = raw

from_str staticmethod ¤

from_str(s: str) -> IdentifierExpr

Parse an identifier from a string.

The raw content will be checked immediately when initializing the object.

Parameters:

Name Type Description Default
s str

Raw identifier text.

required

Raises:

Type Description
InvalidIdentifierException

If the raw identifier text is not a valid identifier.

Source code in synt/tokens/ident.py
@staticmethod
def from_str(s: str) -> IdentifierExpr:
    """Parse an identifier from a string.

    The raw content will be checked immediately when initializing the object.

    Args:
        s: Raw identifier text.

    Raises:
        InvalidIdentifierException: If the raw identifier text is not a valid identifier.
    """
    return IdentifierExpr(Identifier(s))

into_code ¤

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