ast

This module contains the nodes which comprise the abstract syntax tree generated from parsed grammar text.

Warning

The content of this module should be treated as private.

While the code within this module is documented, it is not meant to be used by consumers of the package. Directly accessing and using any object or function within this module should be done with care. Breaking API changes within this module may not always cause a major version bump. The reason for this is that it is often necessary to update the AST in an API breaking way in order to add new features.

Classes

class Assignment(name, *, value=UNDEFINED, value_type=None)[source]

Bases: object

An internal assignment whereby a symbol is populated with a value of the specified type.

__init__(name, *, value=UNDEFINED, value_type=None)[source]
Parameters:
  • name (str) – The symbol name that the assignment is defining.

  • value – The value of the assignment.

  • value_type (DataType) – The data type of the assignment.

name
value
value_type
class Statement(context, expression, comment=None)[source]

Bases: ASTNodeBase

A class representing the top level statement of the grammar text.

Base Classes

class ExpressionBase[source]

Bases: ASTNodeBase

result_type = UNDEFINED

The data type of the result of successful evaluation.

class LeftOperatorRightExpressionBase(context, type_, left, right)[source]

Bases: ExpressionBase

A base class for representing complex expressions composed of a left side and a right side, separated by an operator.

compatible_types

A tuple containing the compatible data types that the left and right expressions must return. This can for example be used to indicate that arithmetic operations are compatible with FLOAT but not STRING values.

__init__(context, type_, left, right)[source]
Parameters:
  • context (Context) – The context to use for evaluating the expression.

  • type (str) – The grammar type of operator at the center of the expression. Subclasses must define operator methods to handle evaluation based on this value.

  • left (ExpressionBase) – The expression to the left of the operator.

  • right (ExpressionBase) – The expression to the right of the operator.

class LiteralExpressionBase(context, value)[source]

Bases: ExpressionBase

A base class for representing literal values from the grammar text.

__init__(context, value)[source]
Parameters:
  • context (Context) – The context to use for evaluating the expression.

  • value – The native Python value.

Left-Operator-Right Expressions

class AddExpression(*args, **kwargs)[source]

Bases: LeftOperatorRightExpressionBase

A class for representing addition expressions from the grammar text.

result_type = UNDEFINED

The data type of the result of successful evaluation.

class SubtractExpression(*args, **kwargs)[source]

Bases: LeftOperatorRightExpressionBase

A class for representing subtraction expressions from the grammar text.

Added in version 3.5.0.

result_type = UNDEFINED

The data type of the result of successful evaluation.

class ArithmeticExpression(context, type_, left, right)[source]

Bases: LeftOperatorRightExpressionBase

A class for representing arithmetic expressions from the grammar text such as multiplication and division.

result_type = FLOAT

The data type of the result of successful evaluation.

class ArithmeticComparisonExpression(*args, **kwargs)[source]

Bases: ComparisonExpression

A class for representing arithmetic comparison expressions from the grammar text such as less-than-or-equal-to and greater-than.

result_type = BOOLEAN

The data type of the result of successful evaluation.

class BitwiseExpression(*args, **kwargs)[source]

Bases: LeftOperatorRightExpressionBase

A class for representing bitwise arithmetic expressions from the grammar text such as XOR and shifting operations.

result_type = UNDEFINED

The data type of the result of successful evaluation.

class BitwiseShiftExpression(*args, **kwargs)[source]

Bases: BitwiseExpression

result_type = FLOAT

The data type of the result of successful evaluation.

class ComparisonExpression(context, type_, left, right)[source]

Bases: LeftOperatorRightExpressionBase

A class for representing comparison expressions from the grammar text such as equality checks.

result_type = BOOLEAN

The data type of the result of successful evaluation.

class LogicExpression(context, type_, left, right)[source]

Bases: LeftOperatorRightExpressionBase

A class for representing logical expressions from the grammar text such as “and” and “or”.

result_type = BOOLEAN

The data type of the result of successful evaluation.

class FuzzyComparisonExpression(*args, **kwargs)[source]

Bases: ComparisonExpression

A class for representing regular expression comparison expressions from the grammar text such as search and does not match.

result_type = BOOLEAN

The data type of the result of successful evaluation.

Literal Expressions

class ArrayExpression(*args, **kwargs)[source]

Bases: _CollectionMixin, LiteralExpressionBase

Literal array expressions containing 0 or more sub-expressions.

result_type = ARRAY

The data type of the result of successful evaluation.

class BooleanExpression(context, value)[source]

Bases: LiteralExpressionBase

Literal boolean expressions representing True or False.

result_type = BOOLEAN

The data type of the result of successful evaluation.

class BytesExpression(context, value)[source]

Bases: LiteralExpressionBase

Literal bytes expressions representing a binary string. This expression type always evaluates to true when not empty.

result_type = BYTES

The data type of the result of successful evaluation.

class DatetimeExpression(context, value)[source]

Bases: LiteralExpressionBase

Literal datetime expressions representing a specific point in time. This expression type always evaluates to true.

result_type = DATETIME

The data type of the result of successful evaluation.

class FloatExpression(context, value, **kwargs)[source]

Bases: LiteralExpressionBase

Literal float expressions representing numerical values.

result_type = FLOAT

The data type of the result of successful evaluation.

class FunctionExpression(context, value)[source]

Bases: LiteralExpressionBase

Literal mapping expression representing a function.

result_type = FUNCTION

The data type of the result of successful evaluation.

class MappingExpression(context, value, **kwargs)[source]

Bases: LiteralExpressionBase

Literal mapping expression representing a set of associations between keys and values.

result_type = MAPPING

The data type of the result of successful evaluation.

class NullExpression(context, value=None)[source]

Bases: LiteralExpressionBase

Literal null expressions representing null values. This expression type always evaluates to false.

result_type = NULL

The data type of the result of successful evaluation.

class SetExpression(*args, **kwargs)[source]

Bases: _CollectionMixin, LiteralExpressionBase

Literal set expressions containing 0 or more sub-expressions.

result_type = SET

The data type of the result of successful evaluation.

class StringExpression(context, value)[source]

Bases: LiteralExpressionBase

Literal string expressions representing an array of characters.

result_type = STRING

The data type of the result of successful evaluation.

class TimedeltaExpression(context, value)[source]

Bases: LiteralExpressionBase

Literal timedelta expressions representing an offset from a specific point in time.

Added in version 3.5.0.

result_type = TIMEDELTA

The data type of the result of successful evaluation.

Miscellaneous Expressions

class ComprehensionExpression(context, result, variable, iterable, condition=None)[source]

Bases: ExpressionBase

result_type = ARRAY

The data type of the result of successful evaluation.

class ContainsExpression(context, container, member)[source]

Bases: ExpressionBase

An expression used to test whether an item exists within a container.

result_type = BOOLEAN

The data type of the result of successful evaluation.

class GetAttributeExpression(context, object_, name, safe=False)[source]

Bases: ExpressionBase

A class representing an expression in which name is retrieved as an attribute of object.

result_type = UNDEFINED

The data type of the result of successful evaluation.

class GetItemExpression(context, container, item, safe=False)[source]

Bases: ExpressionBase

A class representing an expression in which an item is retrieved from a container object.

result_type = UNDEFINED

The data type of the result of successful evaluation.

class GetSliceExpression(context, container, start=None, stop=None, safe=False)[source]

Bases: ExpressionBase

A class representing an expression in which a range of items is retrieved from a container object.

result_type = UNDEFINED

The data type of the result of successful evaluation.

class SymbolExpression(context, name, scope=None)[source]

Bases: ExpressionBase

A class representing a symbol name to be resolved at evaluation time with the help of a Context object.

result_type = UNDEFINED

The data type of the result of successful evaluation.

class TernaryExpression(context, condition, case_true, case_false)[source]

Bases: ExpressionBase

A class for representing ternary expressions from the grammar text. These involve evaluating condition before evaluating either case_true or case_false based on the results.

result_type = UNDEFINED

The data type of the result of successful evaluation.

class UnaryExpression(context, type_, right)[source]

Bases: ExpressionBase

A class for representing unary expressions from the grammar text. These involve a single operator on the left side.

result_type = UNDEFINED

The data type of the result of successful evaluation.