Fixed #49 : ExpressionParser: Implement ExpressionParser
This commit is contained in:
@@ -8,13 +8,14 @@ from core.concept import Concept, ConceptParts, DoNotResolve, AllConceptParts
|
||||
from core.rule import Rule
|
||||
from core.tokenizer import Tokenizer, TokenKind, Token
|
||||
from core.utils import get_text_from_tokens, tokens_index, str_concept
|
||||
from parsers.BaseExpressionParser import NameExprNode, AndNode, OrNode, NotNode, VariableNode, ComparisonNode, \
|
||||
ComparisonType, \
|
||||
FunctionParameter
|
||||
from parsers.BaseNodeParser import UnrecognizedTokensNode, SourceCodeNode, RuleNode, ConceptNode, \
|
||||
SourceCodeWithConceptNode
|
||||
from parsers.FunctionParser import FunctionNode
|
||||
from parsers.PythonParser import PythonNode
|
||||
from parsers.SyaNodeParser import SyaConceptParserHelper
|
||||
from parsers.BaseExpressionParser import NameExprNode, AndNode, OrNode, NotNode, VariableNode, ComparisonNode, ComparisonType, \
|
||||
FunctionParameter
|
||||
from sheerkarete.common import V
|
||||
from sheerkarete.conditions import Condition, AndConditions
|
||||
|
||||
@@ -1304,17 +1305,22 @@ def get_rete_conditions(*conditions_as_string):
|
||||
"identifier|__name__|'True'" -> Condition(identifier, '__name__', 'True') # the string 'True'
|
||||
"identifier|__name__|True" -> Condition(identifier, '__name__', True) # the bool True
|
||||
"""
|
||||
|
||||
def get_value(obj):
|
||||
if obj.startswith("#"):
|
||||
return V(obj[1:])
|
||||
if obj.startswith("'"):
|
||||
return obj[1:-1]
|
||||
if obj in ("True", "False"):
|
||||
return obj == "True"
|
||||
return int(obj)
|
||||
|
||||
res = []
|
||||
for as_string in conditions_as_string:
|
||||
identifier, attribute, value = as_string.split("|")
|
||||
if identifier.startswith("#"):
|
||||
identifier = V(identifier[1:])
|
||||
if value.startswith("'"):
|
||||
value = value[1:-1]
|
||||
elif value in ("True", "False"):
|
||||
value = (value == "True")
|
||||
else:
|
||||
value = int(value)
|
||||
parts = as_string.split("|")
|
||||
identifier = get_value(parts[0])
|
||||
attribute = parts[1]
|
||||
value = get_value(parts[2])
|
||||
|
||||
res.append(Condition(identifier, attribute, value))
|
||||
return AndConditions(res)
|
||||
|
||||
@@ -56,6 +56,7 @@ class TestExpressionParser(TestUsingMemoryBasedSheerka):
|
||||
("func(var1.attr1 > var2.attr2)", FN("func(", ")", [GT(VAR("var1.attr1"), VAR("var2.attr2"))])),
|
||||
("func1(var1) and func2(var2)", AND(FN("func1(", ")", [VAR("var1")]), FN("func2(", (")", 1), [VAR("var2")]))),
|
||||
("__ret", VAR("__ret")),
|
||||
#("func1().func2()", [])
|
||||
])
|
||||
def test_i_can_parse_input(self, expression, expected):
|
||||
sheerka, context, parser, parser_input, error_sink = self.init_parser_with_source(expression)
|
||||
|
||||
Reference in New Issue
Block a user