ast
¶
This module contains the nodes which comprise the abstract syntax tree generated from parsed grammar text.
Functions¶
-
is_natural_number
(value)[source]¶ Check whether value is a natural number (i.e. a whole, non-negative number). This can, for example, be used to check if a floating point number such as
3.0
can safely be converted to an integer without loss of information.- Parameters
value – The value to check. This value is a native Python type.
- Returns
Whether or not the value is a natural number.
- Return type
-
is_numeric
(value)[source]¶ Check whether value is a numeric value (i.e. capable of being represented as a floating point value without loss of information).
- Parameters
value – The value to check. This value is a native Python type.
- Returns
Whether or not the value is numeric.
- Return type
-
is_real_number
(value)[source]¶ Check whether value is a real number (i.e. capable of being represented as a floating point value without loss of information as well as being finite). Despite being able to be represented as a float,
NaN
is not considered a real number for the purposes of this function.- Parameters
value – The value to check. This value is a native Python type.
- Returns
Whether or not the value is a natural number.
- Return type
Classes¶
-
class
DataType
(value)[source]¶ Bases:
enum.Enum
A collection of constants representing the different supported data types.
-
BOOLEAN
¶
-
DATETIME
¶
-
FLOAT
¶
-
NULL
¶
-
STRING
¶
-
classmethod
from_type
(python_type)[source]¶ Get the supported data type constant for the specified Python type. If the type can not be mapped to a supported data type, then a
ValueError
exception will be raised. This function will not returnUNDEFINED
.- Parameters
python_type (type) – The native Python type to retrieve the corresponding type constant for.
- Returns
One of the constants.
-
classmethod
from_value
(python_value)[source]¶ Get the supported data type constant for the specified Python value. If the value can not be mapped to a supported data type, then a
TypeError
exception will be raised. This function will not returnUNDEFINED
.- Parameters
python_value – The native Python value to retrieve the corresponding data type constant for.
- Returns
One of the constants.
-
UNDEFINED
= None¶ Undefined values. This constant can be used to indicate that a particular symbol is valid, but it’s data type is currently unknown.
-
classmethod
from_type
(python_type)[source]¶ Get the supported data type constant for the specified Python type. If the type can not be mapped to a supported data type, then a
ValueError
exception will be raised. This function will not returnUNDEFINED
.- Parameters
python_type (type) – The native Python type to retrieve the corresponding type constant for.
- Returns
One of the constants.
-
classmethod
from_value
(python_value)[source]¶ Get the supported data type constant for the specified Python value. If the value can not be mapped to a supported data type, then a
TypeError
exception will be raised. This function will not returnUNDEFINED
.- Parameters
python_value – The native Python value to retrieve the corresponding data type constant for.
- Returns
One of the constants.
-
-
class
Statement
(context, expression)[source]¶ Bases:
rule_engine.ast.ASTNodeBase
A class representing the top level statement of the grammar text.
Base Classes¶
-
class
ExpressionBase
[source]¶ Bases:
rule_engine.ast.ASTNodeBase
-
result_type
= UNDEFINED¶ The data type of the result of successful evaluation.
-
evaluate
(thing)[source]¶ Evaluate this AST node and all applicable children nodes.
- Parameters
thing – The object to use for symbol resolution.
- Returns
The result of the evaluation as a native Python type.
-
reduce
()[source]¶ Reduce this expression into a smaller subset of nodes. If the expression can not be reduced, then return an instance of itself, otherwise return a reduced
ExpressionBase
to replace it.- Returns
Either a reduced version of this node or itself.
- Return type
-
result_type
= None¶ The data type of the result of successful evaluation.
-
-
class
LeftOperatorRightExpressionBase
(context, type_, left, right)[source]¶ Bases:
rule_engine.ast.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 notSTRING
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:
rule_engine.ast.ExpressionBase
A base class for representing literal values from the grammar text.
Left-Operator-Right Expressions¶
-
class
ArithmeticExpression
(context, type_, left, right)[source]¶ Bases:
rule_engine.ast.LeftOperatorRightExpressionBase
A class for representing arithmetic expressions from the grammar text such as addition and subtraction.
-
result_type
= FLOAT¶
-
-
class
ArithmeticComparisonExpression
(context, type_, left, right)[source]¶ Bases:
rule_engine.ast.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¶
-
-
class
BitwiseExpression
(*args, **kwargs)[source]¶ Bases:
rule_engine.ast.LeftOperatorRightExpressionBase
A class for representing bitwise arithmetic expressions from the grammar text such as XOR and shifting operations.
-
result_type
= FLOAT¶
-
-
class
ComparisonExpression
(context, type_, left, right)[source]¶ Bases:
rule_engine.ast.LeftOperatorRightExpressionBase
A class for representing comparison expressions from the grammar text such as equality checks.
-
result_type
= BOOLEAN¶
-
-
class
LogicExpression
(context, type_, left, right)[source]¶ Bases:
rule_engine.ast.LeftOperatorRightExpressionBase
A class for representing logical expressions from the grammar text such as as “and” and “or”.
-
result_type
= BOOLEAN¶
-
-
class
FuzzyComparisonExpression
(*args, **kwargs)[source]¶ Bases:
rule_engine.ast.ComparisonExpression
A class for representing regular expression comparison expressions from the grammar text such as search and does not match.
-
result_type
= BOOLEAN¶
-
Literal Expressions¶
-
class
BooleanExpression
(context, value)[source]¶ Bases:
rule_engine.ast.LiteralExpressionBase
Literal boolean expressions representing True or False.
-
result_type
= BOOLEAN¶
-
-
class
FloatExpression
(context, value)[source]¶ Bases:
rule_engine.ast.LiteralExpressionBase
Literal float expressions representing numerical values.
-
result_type
= FLOAT¶
-
-
class
NullExpression
(context)[source]¶ Bases:
rule_engine.ast.LiteralExpressionBase
Literal null expressions representing null values. This expression type always evaluates to false.
-
result_type
= NULL¶
-
-
class
StringExpression
(context, value)[source]¶ Bases:
rule_engine.ast.LiteralExpressionBase
Literal string expressions representing an array of characters.
-
result_type
= STRING¶
-
Miscellaneous Expressions¶
-
class
SymbolExpression
(context, name, scope=None)[source]¶ Bases:
rule_engine.ast.ExpressionBase
A class representing a symbol name to be resolved at evaluation time with the help of a
Context
object.-
result_type
¶
-
-
class
TernaryExpression
(context, condition, case_true, case_false)[source]¶ Bases:
rule_engine.ast.ExpressionBase
A class for representing ternary expressions from the grammar text. These involve evaluating
condition
before evaluating eithercase_true
orcase_false
based on the results.-
result_type
= UNDEFINED¶
-
-
class
UnaryExpression
(context, type_, right)[source]¶ Bases:
rule_engine.ast.ExpressionBase
-
result_type
= UNDEFINED¶
-