Added ZeroAndMore and OneAndMore to BNF. BNF expressions can now be captured

This commit is contained in:
2019-12-18 12:01:51 +01:00
parent 88cd3162be
commit 8dbe2e1b20
9 changed files with 425 additions and 91 deletions
+52 -9
View File
@@ -5,7 +5,7 @@ from core.concept import Concept
from core.sheerka import Sheerka, ExecutionContext
from evaluators.ConceptNodeEvaluator import ConceptNodeEvaluator
from parsers.ConceptLexerParser import ConceptNode, ConceptLexerParser, NonTerminalNode, Sequence, TerminalNode, \
StrMatch, Optional, OrderedChoice
StrMatch, Optional, OrderedChoice, ZeroOrMore
def get_context():
@@ -61,7 +61,7 @@ def test_concept_is_returned_when_list_of_one_concept_node():
assert result.who == evaluator.name
assert result.status
assert result.value == node.concept
assert result.value == Concept("foo", body="foo").init_key()
assert result.parents == [ret_val]
@@ -119,6 +119,22 @@ def test_concept_property_is_correctly_updated_for_optional():
assert updated.props["o"].value == "two"
def test_concept_property_is_correctly_updated_for_zero_or_more():
context = get_context()
foo = Concept("foo")
grammar = {foo: ZeroOrMore("one", rule_name="variable")}
concept_node = get_concept_node(context, grammar, "one one one")
updated = ConceptNodeEvaluator().update_concept(
context.sheerka,
context.sheerka.new(concept_node.concept.key),
concept_node.underlying)
assert "variable" in updated.props
assert updated.props["variable"].value == "one one one"
def test_concept_property_is_correctly_updated_when_list_of_properties():
context = get_context()
@@ -141,9 +157,10 @@ def test_concept_property_is_correctly_updated_when_another_concept():
foo = Concept("foo")
bar = Concept("bar")
context.sheerka.add_in_cache(foo)
context.sheerka.add_in_cache(bar)
grammar = {
foo: Sequence("one", "two", rule_name="variable"),
bar: Sequence(foo, "three", rule_name="variable")}
foo: Sequence("one", "two", rule_name="var"),
bar: Sequence(foo, "three", rule_name="var")}
concept_node = get_concept_node(context, grammar, "one two three")
updated = ConceptNodeEvaluator().update_concept(
@@ -151,11 +168,11 @@ def test_concept_property_is_correctly_updated_when_another_concept():
context.sheerka.new(concept_node.concept.key),
concept_node.underlying)
assert updated.props["variable"].value == "one two three"
assert updated.props["foo"].value == Concept("foo").set_prop("variable", "one two").init_key()
assert updated.props["var"].value == "one two three"
assert updated.props["foo"].value == Concept("foo", body="one two").set_prop("var", "one two").init_key()
def test_concept_property_is_correctly_updated_when_concept_recursion():
def test_concept_property_is_correctly_updated_when_concept_recursion_using_optional():
context = get_context()
number = Concept("number")
@@ -173,6 +190,32 @@ def test_concept_property_is_correctly_updated_when_concept_recursion():
context.sheerka.new(concept_node.concept.key),
concept_node.underlying)
assert updated.props["number"].value == Concept("number").init_key()
assert updated.props["number"].value == Concept("number", body="one").init_key()
assert updated.props["op"].value == "plus"
assert updated.props["add"].value == Concept("add").set_prop("number", Concept("number").init_key()).init_key()
expected_add = Concept("add", body="two").set_prop("number", Concept("number", body="two").init_key()).init_key()
assert updated.props["add"].value == expected_add
def test_concept_property_is_correctly_updated_when_concept_recursion_using_zero_or_more():
context = get_context()
number = Concept("number")
add = Concept("add")
context.sheerka.add_in_cache(number)
context.sheerka.add_in_cache(add)
grammar = {
number: OrderedChoice("one", "two", 'three'),
add: Sequence(number, ZeroOrMore(Sequence(OrderedChoice("plus", "minus", rule_name="op"), number)))
}
concept_node = get_concept_node(context, grammar, "one plus two minus three")
updated = ConceptNodeEvaluator().update_concept(
context.sheerka,
context.sheerka.new(concept_node.concept.key),
concept_node.underlying,
init_empty_body=True)
assert updated.props["number"].value == [Concept("number", body="one").init_key(),
Concept("number", body="two").init_key(),
Concept("number", body="three").init_key()]
assert updated.props["op"].value == ["plus", "minus"]