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.0can 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: bool
-
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: bool
-
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,
NaNis 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: bool
Classes¶
-
class
DataType[source]¶ Bases:
enum.EnumA 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
ValueErrorexception 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
TypeErrorexception 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.
-
-
class
Statement(context, expression)[source]¶ Bases:
rule_engine.ast.ASTNodeBaseA 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
ExpressionBaseto replace it.Returns: Either a reduced version of this node or itself. Return type: ExpressionBase
-
result_type= None The data type of the result of successful evaluation.
-
-
class
LeftOperatorRightExpressionBase(context, type_, left, right)[source]¶ Bases:
rule_engine.ast.ExpressionBaseA 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
FLOATbut notSTRINGvalues.
-
__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.
- context (
-
-
class
LiteralExpressionBase(context, value)[source]¶ Bases:
rule_engine.ast.ExpressionBaseA 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.LeftOperatorRightExpressionBaseA 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.ComparisonExpressionA 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.LeftOperatorRightExpressionBaseA 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.LeftOperatorRightExpressionBaseA 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.LeftOperatorRightExpressionBaseA 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.ComparisonExpressionA 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.LiteralExpressionBaseLiteral boolean expressions representing True or False.
-
result_type= BOOLEAN¶
-
-
class
FloatExpression(context, value)[source]¶ Bases:
rule_engine.ast.LiteralExpressionBaseLiteral float expressions representing numerical values.
-
result_type= FLOAT¶
-
-
class
NullExpression(context)[source]¶ Bases:
rule_engine.ast.LiteralExpressionBaseLiteral null expressions representing null values. This expression type always evaluates to false.
-
result_type= NULL¶
-
-
class
StringExpression(context, value)[source]¶ Bases:
rule_engine.ast.LiteralExpressionBaseLiteral string expressions representing an array of characters.
-
result_type= STRING¶
-
Miscellaneous Expressions¶
-
class
SymbolExpression(context, name, scope=None)[source]¶ Bases:
rule_engine.ast.ExpressionBaseA class representing a symbol name to be resolved at evaluation time with the help of a
Contextobject.-
result_type= UNDEFINED¶ A collection of constants representing the different supported data types.
-
-
class
TernaryExpression(context, condition, case_true, case_false)[source]¶ Bases:
rule_engine.ast.ExpressionBaseA class for representing ternary expressions from the grammar text. These involve evaluating
conditionbefore evaluating eithercase_trueorcase_falsebased on the results.-
result_type= UNDEFINED¶
-
-
class
UnaryExpression(context, type_, right)[source]¶ Bases:
rule_engine.ast.ExpressionBase-
result_type= UNDEFINED¶
-