Fixed SyaNodeParser false positive recognition issue

This commit is contained in:
2020-05-15 10:36:05 +02:00
parent 6e343ba996
commit 5489ef00b9
24 changed files with 484 additions and 5741 deletions
+204 -327
View File
@@ -31,6 +31,7 @@ cmap = {
"if": Concept("if a then b else c end").def_var("a").def_var("b").def_var("c"),
"square": Concept("square(a)").def_var("a"),
"foo bar": Concept("foo bar(a)").def_var("a"),
"long infixed": Concept("a long infixed b").def_var("a").def_var("b"),
"twenties": Concept("twenties", definition="'twenty' (one|two)=unit").def_var("unit"),
}
@@ -50,8 +51,7 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
TestSyaNodeParser.sheerka.force_sya_def(context, [
(cmap["plus"].id, 5, SyaAssociativity.Right),
(cmap["mult"].id, 10, SyaAssociativity.Right),
(cmap["minus"].id, 10, SyaAssociativity.Right),
(cmap["square"].id, None, SyaAssociativity.No)])
(cmap["minus"].id, 10, SyaAssociativity.Right)])
def init_parser(self,
my_concepts_map=None,
@@ -98,99 +98,92 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
@pytest.mark.parametrize("expression, expected_sequences", [
("one plus two", [["one", "two", "plus"]]),
("1 + 1 plus two", [["1 + 1 ", "two", "plus"]]),
("1 + 1 plus two", [["1 + 1", "two", "plus"]]),
("one + two plus three", [
["one", " + ", "two", "three", "plus"],
["one + two ", "three", "plus"]]),
["one + two", "three", "plus"]]),
("twenty one plus two", [
["twenty ", "one", "two", "plus"],
[short_cnode("twenties", "twenty one"), "two", "plus"]
]),
("x$!# plus two", [["x$!# ", "two", "plus"]]),
("x$!# plus two", [["x$!#", "two", "plus"]]),
("one plus 1 + 1", [["one", " 1 + 1", "plus"]]),
("1 + 1 plus 2 + 2", [["1 + 1 ", " 2 + 2", "plus"]]),
("one plus 1 + 1", [["one", "1 + 1", "plus"]]),
("1 + 1 plus 2 + 2", [["1 + 1", "2 + 2", "plus"]]),
("one + two plus 1 + 1", [
["one", " + ", "two", " 1 + 1", "plus"],
["one + two ", " 1 + 1", "plus"]
["one", " + ", "two", "1 + 1", "plus"],
["one + two", "1 + 1", "plus"]
]),
("twenty one plus 1 + 1", [
["twenty ", "one", " 1 + 1", "plus"],
[cnode("twenties", 0, 2, "twenty one"), " 1 + 1", "plus"]
["twenty ", "one", "1 + 1", "plus"],
[cnode("twenties", 0, 2, "twenty one"), "1 + 1", "plus"]
]),
("x$!# plus 1 + 1", [["x$!# ", " 1 + 1", "plus"]]),
("x$!# plus 1 + 1", [["x$!#", "1 + 1", "plus"]]),
("one plus two + three", [
["one", "two", "plus", " + ", "three"],
["one", " two + three", "plus"],
["one", "two + three", "plus"],
]),
("1 + 1 plus two + three", [
["1 + 1 ", "two", "plus", (" + ", 1), "three"],
["1 + 1 ", " two + three", "plus"],
["1 + 1", "two", "plus", (" + ", 1), "three"],
["1 + 1", "two + three", "plus"],
]),
("one + two plus two + three", [
["one", " + ", "two", ("two", 1), "plus", (" + ", 1), "three"],
["one + two ", ("two", 1), "plus", (" + ", 1), "three"],
["one", " + ", "two", " two + three", "plus"],
["one + two ", " two + three", "plus"],
["one + two", ("two", 1), "plus", (" + ", 1), "three"],
["one", " + ", "two", "two + three", "plus"],
["one + two", "two + three", "plus"],
]),
("twenty one plus two + three", [
["twenty ", "one", "two", "plus", " + ", "three"],
[cnode("twenties", 0, 2, "twenty one"), "two", "plus", " + ", "three"],
["twenty ", "one", " two + three", "plus"],
[cnode("twenties", 0, 2, "twenty one"), " two + three", "plus"],
["twenty ", "one", "two + three", "plus"],
[cnode("twenties", 0, 2, "twenty one"), "two + three", "plus"],
]),
("x$!# plus two + three", [
["x$!# ", "two", "plus", " + ", "three"],
["x$!# ", " two + three", "plus"],
["x$!#", "two", "plus", " + ", "three"],
["x$!#", "two + three", "plus"],
]),
("one plus twenty two", [
["one", " twenty ", "plus", "two"],
["one", "twenty ", "plus", "two"],
["one", cnode("twenties", 4, 6, "twenty two"), "plus"],
]),
("1 + 1 plus twenty one", [
["1 + 1 ", " twenty ", "plus", "one"],
["1 + 1 ", cnode("twenties", 8, 10, "twenty one"), "plus"],
["1 + 1", "twenty ", "plus", "one"],
["1 + 1", cnode("twenties", 8, 10, "twenty one"), "plus"],
]),
("one + two plus twenty one", [
["one", " + ", "two", " twenty ", "plus", ("one", 1)],
["one + two ", " twenty ", "plus", ("one", 1)],
["one", " + ", "two", "twenty ", "plus", ("one", 1)],
["one + two", "twenty ", "plus", ("one", 1)],
["one", " + ", "two", cnode("twenties", 8, 10, "twenty one"), "plus"],
["one + two ", cnode("twenties", 8, 10, "twenty one"), "plus"],
["one + two", cnode("twenties", 8, 10, "twenty one"), "plus"],
]),
("twenty one plus twenty two",
[
["twenty ", "one", " twenty ", "plus", "two"],
[cnode("twenties", 0, 2, "twenty one"), " twenty ", "plus", "two"],
["twenty ", "one", ("twenty ", 1), "plus", "two"],
[cnode("twenties", 0, 2, "twenty one"), ("twenty ", 1), "plus", "two"],
["twenty ", "one", cnode("twenties", 6, 8, "twenty two"), "plus"],
[cnode("twenties", 0, 2, "twenty one"), cnode("twenties", 6, 8, "twenty two"), "plus"],
]),
("x$!# plus twenty two", [
["x$!# ", " twenty ", "plus", "two"],
["x$!# ", cnode("twenties", 7, 9, "twenty two"), "plus"]
["x$!#", "twenty ", "plus", "two"],
["x$!#", cnode("twenties", 7, 9, "twenty two"), "plus"]
]),
("one plus z$!#", [["one", " z$!#", "plus"]]),
("1 + 1 plus z$!#", [["1 + 1 ", " z$!#", "plus"]]),
("one plus z$!#", [["one", "z$!#", "plus"]]),
("1 + 1 plus z$!#", [["1 + 1", "z$!#", "plus"]]),
("one + two plus z$!#", [
["one", " + ", "two", " z$!#", "plus"],
["one + two ", " z$!#", "plus"],
["one", " + ", "two", "z$!#", "plus"],
["one + two", "z$!#", "plus"],
]),
("twenty one plus z$!#", [
["twenty ", "one", " z$!#", "plus"],
[cnode("twenties", 0, 2, "twenty one"), " z$!#", "plus"],
["twenty ", "one", "z$!#", "plus"],
[cnode("twenties", 0, 2, "twenty one"), "z$!#", "plus"],
]),
("x$!# plus z$!#", [["x$!# ", " z$!#", "plus"]]),
("x$!# plus z$!#", [["x$!#", "z$!#", "plus"]]),
])
def test_i_can_post_fix_simple_infix_concepts(self, expression, expected_sequences):
# concepts_map = {
# "plus": Concept("a plus b").def_var("a").def_var("b"),
# "one": Concept("one"),
# "two": Concept("two"),
# "three": Concept("three"),
# "twenties": Concept("twenties", definition="'twenty' (one|two)=unit").def_var("unit"),
# }
sheerka, context, parser = self.init_parser()
res = parser.infix_to_postfix(context, expression)
@@ -202,10 +195,10 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
assert res_i.out == expected_array
@pytest.mark.parametrize("expression, expected_sequences", [
("one plus plus plus 1 + 1", [["one", " 1 + 1", "plus plus plus"]]),
("one plus plus plus 1 + 1", [["one", "1 + 1", "plus plus plus"]]),
("x$!# another long name infix twenty two", [
["x$!# ", " twenty ", "another long name infix", "two"],
["x$!# ", cnode("twenties", 13, 15, "twenty two"), "another long name infix"],
["x$!#", "twenty ", "another long name infix", "two"],
["x$!#", cnode("twenties", 13, 15, "twenty two"), "another long name infix"],
]),
])
def test_i_can_post_fix_infix_concepts_with_long_name(self, expression, expected_sequences):
@@ -229,24 +222,18 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
@pytest.mark.parametrize("expression, expected_sequences", [
("one prefixed", [["one", "prefixed"]]),
("1 + 1 prefixed", [["1 + 1 ", "prefixed"]]),
("1 + 1 prefixed", [["1 + 1", "prefixed"]]),
("one + two prefixed", [
["one", " + ", "two", "prefixed"],
["one + two ", "prefixed"],
["one + two", "prefixed"],
]),
("twenty one prefixed", [
["twenty ", "one", "prefixed"],
[cnode("twenties", 0, 2, "twenty one"), "prefixed"],
]),
("x$!# prefixed", [["x$!# ", "prefixed"]]),
("x$!# prefixed", [["x$!#", "prefixed"]]),
])
def test_i_can_post_fix_simple_prefixed_concepts(self, expression, expected_sequences):
# concepts_map = {
# "prefixed": Concept("a prefixed").def_var("a"),
# "one": Concept("one"),
# "two": Concept("two"),
# "twenties": Concept("twenties", definition="'twenty' (one|two)=unit").def_var("unit"),
# }
sheerka, context, parser = self.init_parser()
res = parser.infix_to_postfix(context, expression)
@@ -259,28 +246,28 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
@pytest.mark.parametrize("expression, expected_sequences", [
("one prefixed prefixed", [["one", "prefixed prefixed"]]),
("1 + 1 prefixed prefixed", [["1 + 1 ", "prefixed prefixed"]]),
("1 + 1 prefixed prefixed", [["1 + 1", "prefixed prefixed"]]),
("one + two prefixed prefixed", [
["one", " + ", "two", "prefixed prefixed"],
["one + two ", "prefixed prefixed"],
["one + two", "prefixed prefixed"],
]),
("twenty one prefixed prefixed", [
["twenty ", "one", "prefixed prefixed"],
[cnode("twenties", 0, 2, "twenty one"), "prefixed prefixed"],
]),
("x$!# prefixed prefixed", [["x$!# ", "prefixed prefixed"]]),
("x$!# prefixed prefixed", [["x$!#", "prefixed prefixed"]]),
("one long name prefixed", [["one", "long name prefixed"]]),
("1 + 1 long name prefixed", [["1 + 1 ", "long name prefixed"]]),
("1 + 1 long name prefixed", [["1 + 1", "long name prefixed"]]),
("one + two long name prefixed", [
["one", " + ", "two", "long name prefixed"],
["one + two ", "long name prefixed"],
["one + two", "long name prefixed"],
]),
("twenty one long name prefixed", [
["twenty ", "one", "long name prefixed"],
[cnode("twenties", 0, 2, "twenty one"), "long name prefixed"],
]),
("x$!# long name prefixed", [["x$!# ", "long name prefixed"]]),
("x$!# long name prefixed", [["x$!#", "long name prefixed"]]),
])
def test_i_can_post_fix_prefixed_concepts_with_long_names(self, expression, expected_sequences):
concepts_map = {
@@ -302,24 +289,18 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
@pytest.mark.parametrize("expression, expected_sequences", [
("suffixed one", [["one", "suffixed"]]),
("suffixed 1 + 1", [[" 1 + 1", "suffixed"]]),
("suffixed 1 + 1", [["1 + 1", "suffixed"]]),
("suffixed one + two", [
["one", "suffixed", " + ", "two"],
[" one + two", "suffixed"],
["one + two", "suffixed"],
]),
("suffixed twenty one", [
[" twenty ", "suffixed", "one"],
["twenty ", "suffixed", "one"],
[cnode("twenties", 2, 4, "twenty one"), "suffixed"],
]),
("suffixed x$!#", [[" x$!#", "suffixed"]]),
("suffixed x$!#", [["x$!#", "suffixed"]]),
])
def test_i_can_post_fix_simple_suffixed_concepts(self, expression, expected_sequences):
# concepts_map = {
# "suffixed": Concept("suffixed a").def_var("a"),
# "one": Concept("one"),
# "two": Concept("two"),
# "twenties": Concept("twenties", definition="'twenty' (one|two)=unit").def_var("unit"),
# }
sheerka, context, parser = self.init_parser()
res = parser.infix_to_postfix(context, expression)
@@ -351,26 +332,27 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
@pytest.mark.parametrize("expression, expected_sequences", [
("one ? two : three", [["one", "two", "three", "?"]]),
("one ? baz qux : two", [["one", "baz qux", "two", "?"]]),
("1+1 ? one + two : twenty one", [
["1+1 ", "one", " + ", "two"], # an error is detected
["1+1 ", " one + two ", " twenty ", "?", ("one", 1)],
["1+1 ", " one + two ", short_cnode("twenties", "twenty one"), "?"],
["1+1", "one", " + ", "two"], # error is detected so the parsing has stopped
["1+1", "one + two", "twenty ", "?", ("one", 1)],
["1+1", "one + two", short_cnode("twenties", "twenty one"), "?"],
]),
("x$!# ? y$!# : z$!#", [["x$!# ", " y$!# ", " z$!#", "?"]]),
("x$!# ? y$!# : z$!#", [["x$!#", "y$!#", "z$!#", "?"]]),
("if one then two else three end", [["one", "two", "three", "if"]]),
("if 1+1 then x$!# else twenty one end", [
[" 1+1 ", " x$!# ", " twenty ", "one"], # an error is detected
[" 1+1 ", " x$!# ", short_cnode("twenties", "twenty one"), "if"],
["1+1", "x$!#", "twenty ", "one"], # an error is detected
["1+1", "x$!#", short_cnode("twenties", "twenty one"), "if"],
]),
("if x$!# then one + two else z$!# end", [
[" x$!# ", "one", " + ", "two"], # an error is detected
[" x$!# ", " one + two ", " z$!# ", "if"],
["x$!#", "one", " + ", "two"], # error is detected so the parsing has stopped
["x$!#", "one + two", "z$!#", "if"],
]),
])
def test_i_can_post_fix_ternary_concepts(self, expression, expected_sequences):
"""
The purpose of this test is to validate concepts like
The purpose of this test is to validate concepts
that have at least 3 parameters separated by tokens
Example :
var_0 token var_1 token var_2
@@ -381,14 +363,6 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
:return:
"""
# concepts_map = {
# "?": Concept("a ? b : c").def_var("a").def_var("b").def_var("c"),
# "if": Concept("if a then b else c end").def_var("a").def_var("b").def_var("c"),
# "one": Concept("one"),
# "two": Concept("two"),
# "three": Concept("three"),
# "twenties": Concept("twenties", definition="'twenty' (one|two)=unit").def_var("unit"),
# }
sheerka, context, parser = self.init_parser()
res = parser.infix_to_postfix(context, expression)
@@ -402,15 +376,15 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
@pytest.mark.parametrize("expression, expected_sequences", [
("one ? ? two : : three", [["one", "two", "three", "? ?"]]),
("1+1 ? ? one + two : : twenty one", [
["1+1 ", "one", " + ", "two"], # error
["1+1 ", " one + two ", " twenty ", "? ?", ("one", 1)],
["1+1 ", " one + two ", short_cnode("twenties", "twenty one"), "? ?"],
["1+1", "one", " + ", "two"], # error
["1+1", "one + two", "twenty ", "? ?", ("one", 1)],
["1+1", "one + two", short_cnode("twenties", "twenty one"), "? ?"],
]),
("if if one then then two else else three end end ", [["one", "two", "three", "if if"]]),
("if if 1+1 then then x$!# else else twenty one end end ", [
[" 1+1 ", " x$!# ", " twenty ", "one"], # error
[" 1+1 ", " x$!# ", short_cnode("twenties", "twenty one"), "if if"]]),
["1+1", "x$!#", "twenty ", "one"], # error
["1+1", "x$!#", short_cnode("twenties", "twenty one"), "if if"]]),
])
def test_i_can_post_fix_ternary_concept_with_long_names(self, expression, expected_sequences):
concepts_map = {
@@ -433,8 +407,8 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
@pytest.mark.parametrize("expression, expected", [
("foo bar baz", ["baz", "bar", "foo"]),
("foo bar x$!#", [" x$!#", "bar", "foo"]),
("foo bar 1 + 1", [" 1 + 1", "bar", "foo"]),
("foo bar x$!#", ["x$!#", "bar", "foo"]),
("foo bar 1 + 1", ["1 + 1", "bar", "foo"]),
])
def test_i_can_post_fix_suffixed_unary_composition(self, expression, expected):
concepts_map = {
@@ -452,8 +426,8 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
@pytest.mark.parametrize("expression, expected", [
("baz bar foo", ["baz", "bar", "foo"]),
("x$!# bar foo", ["x$!# ", "bar", "foo"]),
("1 + 1 bar foo", ["1 + 1 ", "bar", "foo"]),
("x$!# bar foo", ["x$!#", "bar", "foo"]),
("1 + 1 bar foo", ["1 + 1", "bar", "foo"]),
])
def test_i_can_post_fix_prefixed_unary_composition(self, expression, expected):
concepts_map = {
@@ -480,17 +454,6 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
("one mult (two plus three)", ["one", "two", "three", "plus", "mult"]),
])
def test_i_can_post_fix_binary_with_precedence(self, expression, expected):
# concepts_map = {
# "plus": Concept("a plus b").def_var("a").def_var("b"),
# "mult": Concept("a mult b").def_var("a").def_var("b"),
# "one": Concept("one"),
# "two": Concept("two"),
# "three": Concept("three"),
# }
# sya_def = {
# concepts_map["plus"]: (5, SyaAssociativity.Right),
# concepts_map["mult"]: (10, SyaAssociativity.Right), # precedence greater than plus
# }
sheerka, context, parser = self.init_parser()
res = parser.infix_to_postfix(context, expression)
@@ -566,7 +529,7 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
}
sya_def = {
concepts_map["plus"]: (None, SyaAssociativity.Left),
concepts_map["plus"]: (1, SyaAssociativity.Left),
}
sheerka, context, parser = self.init_parser(concepts_map, sya_def)
@@ -580,14 +543,14 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
assert res[0].out == expected_array
@pytest.mark.parametrize("expression, expected", [
("x$!# ? y$!# : z$!# ? two : three", ["x$!# ", " y$!# ", " z$!# ", "two", "three", ("?", 1), "?"]),
("x$!# ? y$!# : (z$!# ? two : three)", ["x$!# ", " y$!# ", "z$!# ", "two", "three", ("?", 1), "?"]),
("x$!# ? y$!# : z$!# ? two : three", ["x$!#", "y$!#", "z$!#", "two", "three", ("?", 1), "?"]),
("x$!# ? y$!# : (z$!# ? two : three)", ["x$!#", "y$!#", "z$!#", "two", "three", ("?", 1), "?"]),
("one ? x$!# ? y$!# : z$!# : three", ["one", " x$!# ", " y$!# ", " z$!# ", ("?", 1), "three", "?"]),
("one ? (x$!# ? y$!# : z$!#) : three", ["one", "x$!# ", " y$!# ", " z$!#", ("?", 1), "three", "?"]),
("one ? x$!# ? y$!# : z$!# : three", ["one", "x$!#", "y$!#", "z$!#", ("?", 1), "three", "?"]),
("one ? (x$!# ? y$!# : z$!#) : three", ["one", "x$!#", "y$!#", "z$!#", ("?", 1), "three", "?"]),
("one ? two : x$!# ? y$!# : z$!#", ["one", "two", " x$!# ", " y$!# ", " z$!#", ("?", 1), "?"]),
("one ? two : (x$!# ? y$!# : z$!#)", ["one", "two", "x$!# ", " y$!# ", " z$!#", ("?", 1), "?"]),
("one ? two : x$!# ? y$!# : z$!#", ["one", "two", "x$!#", "y$!#", "z$!#", ("?", 1), "?"]),
("one ? two : (x$!# ? y$!# : z$!#)", ["one", "two", "x$!#", "y$!#", "z$!#", ("?", 1), "?"]),
])
def test_i_can_post_fix_right_associated_ternary(self, expression, expected):
concepts_map = {
@@ -607,14 +570,14 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
assert res[0].out == expected_array
@pytest.mark.parametrize("expression, expected", [
("x$!# ? y$!# : z$!# ? two : three", ["x$!# ", " y$!# ", " z$!# ", "?", "two", "three", ("?", 1)]),
("(x$!# ? y$!# : z$!#) ? two : three", ["x$!# ", " y$!# ", " z$!#", "?", "two", "three", ("?", 1)]),
("x$!# ? y$!# : z$!# ? two : three", ["x$!#", "y$!#", "z$!#", "?", "two", "three", ("?", 1)]),
("(x$!# ? y$!# : z$!#) ? two : three", ["x$!#", "y$!#", "z$!#", "?", "two", "three", ("?", 1)]),
# the following one is not possible when Left association
# ("one ? x$!# ? y$!# : z$!# : three", ["one", " x$!# ", " y$!# ", " z$!# ", ("?", 1), "three", "?"]),
# ("one ? x$!# ? y$!# : z$!# : three", ["one", "x$!#", "y$!#", "z$!#", ("?", 1), "three", "?"]),
("one ? two : x$!# ? y$!# : z$!#", ["one", "two", " x$!# ", "?", " y$!# ", " z$!#", ("?", 1)]),
("(one ? two : x$!#) ? y$!# : z$!#", ["one", "two", " x$!#", "?", " y$!# ", " z$!#", ("?", 1)]),
("one ? two : x$!# ? y$!# : z$!#", ["one", "two", "x$!#", "?", "y$!#", "z$!#", ("?", 1)]),
("(one ? two : x$!#) ? y$!# : z$!#", ["one", "two", "x$!#", "?", "y$!#", "z$!#", ("?", 1)]),
])
def test_i_can_post_fix_left_associated_ternary(self, expression, expected):
concepts_map = {
@@ -644,7 +607,7 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
expression = "foo bar baz"
res = parser.infix_to_postfix(context, expression)
expected_sequences = [
[UTN(" bar "), "foo", "baz"],
[UTN("bar "), "foo", "baz"],
["baz", "foo bar"]
]
@@ -669,9 +632,9 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
("square( one plus three ) minus two", ["one", "three", "plus", "square", "two", "minus"]),
("one minus square( two plus three ) ", ["one", "two", "three", "plus", "square", "minus"]),
("(one prefixed) prefixed)", ["one", "prefixed", ("prefixed", 1)]),
("( one prefixed ) prefixed)", ["one", "prefixed", ("prefixed", 1)]),
("( square( one ) prefixed ) prefixed)", ["one", "square", "prefixed", ("prefixed", 1)]),
("((one prefixed) prefixed)", ["one", "prefixed", ("prefixed", 1)]),
("( ( one prefixed ) prefixed)", ["one", "prefixed", ("prefixed", 1)]),
("( ( square( one ) prefixed ) prefixed)", ["one", "square", "prefixed", ("prefixed", 1)]),
("suffixed (suffixed one)", ["one", ("suffixed", 1), "suffixed"]),
("suffixed ( suffixed one) ", ["one", ("suffixed", 1), "suffixed"]),
@@ -681,32 +644,12 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
("one plus (two minus three)", ["one", "two", "three", "minus", "plus"]),
("one plus ( two minus three )", ["one", "two", "three", "minus", "plus"]),
("(one plus two) minus three", ["one", "two", "plus", "three", "minus"]),
("( one plus two ) minus three )", ["one", "two", "plus", "three", "minus"]),
("(( one plus two ) minus three )", ["one", "two", "plus", "three", "minus"]),
("foo bar (one)", ["one", "foo bar"]),
("foo bar(one)", ["one", "foo bar"]),
("foo bar ( one )", ["one", "foo bar"]),
])
def test_i_can_pos_fix_when_parenthesis(self, expression, expected):
# concepts_map = {
# "prefixed": Concept("a prefixed").def_var("a"),
# "suffixed": Concept("suffixed a").def_var("a"),
# "square": Concept("square(a)").def_var("a"),
# "foo bar": Concept("foo bar(a)").def_var("a"),
# "plus": Concept("a plus b").def_var("a").def_var("b"),
# "minus": Concept("a minus b").def_var("a").def_var("b"),
# "if": Concept("if a then b else c end").def_var("a").def_var("b").def_var("c"),
# "?": Concept("a ? b : c").def_var("a").def_var("b").def_var("c"),
# "one": Concept("one"),
# "two": Concept("two"),
# "three": Concept("three"),
# }
#
# sya_def = {
# concepts_map["square"]: (None, SyaAssociativity.No),
# concepts_map["plus"]: (10, SyaAssociativity.Right),
# concepts_map["minus"]: (10, SyaAssociativity.Right),
# }
sheerka, context, parser = self.init_parser()
res = parser.infix_to_postfix(context, expression)
@@ -721,14 +664,14 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
("function(one prefixed)", [[SCWC("function(", ")", "one", "prefixed")]]),
("function(if one then two else three end)", [[SCWC("function(", ")", "one", "two", "three", "if")]]),
("function(suffixed twenty two)", [
[SCWC("function(", ")", " twenty ", "suffixed", "two")],
[SCWC("function(", ")", "twenty ", "suffixed", "two")],
[SCWC("function(", ")", short_cnode("twenties", "twenty two"), "suffixed")]]),
("function(twenty two prefixed)", [
[SCWC("function(", ")", "twenty ", "two", "prefixed")],
[SCWC("function(", ")", short_cnode("twenties", "twenty two"), "prefixed")],
]),
("function(if one then twenty two else three end)", [
["')'", "one", " twenty ", "two"], # error
["')'", "one", "twenty ", "two"], # error
[SCWC("function(", ")", "one", short_cnode("twenties", "twenty two"), "three", "if")]
]),
("func1(func2(one two) three)", [
@@ -744,16 +687,16 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
]),
("f1(one plus two mult three) plus f2(suffixed x$!# prefixed)", [
[SCWC("f1(", ")", "one", "two", "three", "mult", "plus"),
SCWC(" f2(", (")", 1), " x$!# ", "prefixed", "suffixed"),
SCWC("f2(", (")", 1), "x$!#", "prefixed", "suffixed"),
("plus", 1)]
]),
# plus, suffixed, prefixed, ternary
("func1(one) plus func2(two)", [[SCWC("func1(", ")", "one"), SCWC(" func2(", (")", 1), "two"), "plus"]]),
("suffixed function(one)", [[SCWC(" function(", ")", "one"), "suffixed"]]),
("func1(one) plus func2(two)", [[SCWC("func1(", ")", "one"), SCWC("func2(", (")", 1), "two"), "plus"]]),
("suffixed function(one)", [[SCWC("function(", ")", "one"), "suffixed"]]),
("function(one) prefixed", [[SCWC("function(", ")", "one"), "prefixed"]]),
("if f1(one) then f2(two) else f3(three) end", [
[SCWC(" f1(", ")", "one"), SCWC(" f2(", (")", 1), "two"), SCWC(" f3(", (")", 2), "three"), "if"]]),
[SCWC("f1(", ")", "one"), SCWC("f2(", (")", 1), "two"), SCWC("f3(", (")", 2), "three"), "if"]]),
# Sequence
("if one then two else three end function(x$!#)", [
@@ -762,21 +705,6 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
("suffixed one function(two)", [["one", "suffixed", SCWC(" function(", ")", "two")]]),
])
def test_i_can_post_fix_when_parenthesis_and_unknown(self, expression, expected_sequences):
# concepts_map = {
# "prefixed": Concept("a prefixed").def_var("a"),
# "suffixed": Concept("suffixed a").def_var("a"),
# "plus": Concept("a plus b").def_var("a").def_var("b"),
# "mult": Concept("a mult b").def_var("a").def_var("b"),
# "if": Concept("if a then b else c end").def_var("a").def_var("b").def_var("c"),
# "one": Concept("one"),
# "two": Concept("two"),
# "three": Concept("three"),
# "twenties": Concept("twenties", definition="'twenty' (one|two)=unit").def_var("unit"),
# }
# sya_def = {
# concepts_map["plus"]: (5, SyaAssociativity.Right),
# concepts_map["mult"]: (10, SyaAssociativity.Right), # precedence greater than plus
# }
sheerka, context, parser = self.init_parser()
res = parser.infix_to_postfix(context, expression)
@@ -787,28 +715,22 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
assert res_i.out == expected_array
@pytest.mark.parametrize("expression, expected", [
("(", ("(", 0)),
("one plus ( 1 + ", ("(", 4)),
("one( 1 + ", ("(", 1)),
("one ( 1 + ", ("(", 2)),
("function( 1 + ", ("(", 1)),
("function ( 1 + ", ("(", 2)),
("one plus ) 1 + ", (")", 4)),
("one ) 1 + ", (")", 2)),
("function ) 1 + ", (")", 2)),
("one ? ( : two", ("(", 4)),
("one ? one plus ( : two", ("(", 8)),
("one ? ) : two", (")", 4)),
("one ? one plus ) : two", (")", 8)),
# ("(", ("(", 0)),
# ("one plus ( 1 + ", ("(", 4)),
# ("one( 1 + ", ("(", 1)),
# ("one ( 1 + ", ("(", 2)),
# ("function( 1 + ", ("(", 1)),
# ("function ( 1 + ", ("(", 2)),
# ("one plus ) 1 + ", (")", 4)),
# ("one ) 1 + ", (")", 2)),
# ("function ) 1 + ", (")", 2)),
# ("one ? ( : two", ("(", 4)),
# ("one ? one plus ( : two", ("(", 8)),
# ("one ? ) : two", (")", 4)),
# ("one ? one plus ) : two", (")", 8)),
("(one plus ( 1 + )", ("(", 0)),
])
def test_i_can_detect_parenthesis_mismatch_error_when_post_fixing(self, expression, expected):
# concepts_map = {
# "one": Concept("one"),
# "two": Concept("two"),
# "plus": Concept("a plus b").def_var("a").def_var("b"),
# "?": Concept("a ? b : c").def_var("a").def_var("b").def_var("c"),
# }
sheerka, context, parser = self.init_parser()
res = parser.infix_to_postfix(context, expression)
@@ -820,12 +742,6 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
("one ? one two : three", ("?", ":")),
])
def test_i_can_detected_when_too_many_parameters(self, expression, expected):
# concepts_map = {
# "one": Concept("one"),
# "two": Concept("two"),
# "plus": Concept("a plus b").def_var("a").def_var("b"),
# "?": Concept("a ? b : c").def_var("a").def_var("b").def_var("c"),
# }
sheerka, context, parser = self.init_parser(cmap, None)
res = parser.infix_to_postfix(context, expression)
@@ -850,27 +766,16 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
("one infix two three infix four", ["one", "two", "infix", "three", "four", ("infix", 1)]),
("one infix two three prefixed", ["one", "two", "infix", "three", "prefixed"]),
("one infix two suffixed three", ["one", "two", "infix", "three", "suffixed"]),
("one infix two x$!# ? y$!# : z$!#", ["one", "two", "infix", " x$!# ", " y$!# ", " z$!#", "?"]),
("one infix two x$!# ? y$!# : z$!#", ["one", "two", "infix", " x$!#", "y$!#", "z$!#", "?"]),
("one prefixed two infix three", ["one", "prefixed", "two", "three", "infix"]),
("one prefixed two prefixed", ["one", "prefixed", "two", ("prefixed", 1)]),
("one prefixed suffixed two", ["one", "prefixed", "two", "suffixed"]),
("one prefixed x$!# ? y$!# : z$!#", ["one", "prefixed", " x$!# ", " y$!# ", " z$!#", "?"]),
("one prefixed x$!# ? y$!# : z$!#", ["one", "prefixed", " x$!#", "y$!#", "z$!#", "?"]),
("(one infix two) (three prefixed)", ["one", "two", "infix", "three", "prefixed"]),
])
def test_i_can_post_fix_sequences(self, expression, expected):
# concepts_map = {
# "prefixed": Concept("a prefixed").def_var("a"),
# "suffixed": Concept("suffixed a").def_var("a"),
# "infix": Concept("a infix b").def_var("a").def_var("b"),
# "?": Concept("a ? b : c").def_var("a").def_var("b").def_var("c"),
# "one": Concept("one"),
# "two": Concept("two"),
# "three": Concept("three"),
# "four": Concept("four"),
# }
sheerka, context, parser = self.init_parser(cmap, None)
res = parser.infix_to_postfix(context, expression)
@@ -886,23 +791,49 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
"plus equals": Concept("a plus equals b").def_var("a").def_var("b"),
}
sheerka, context, parser = self.init_parser(concepts_map, None)
sya_def = {
concepts_map["plus"]: (1, SyaAssociativity.Right),
concepts_map["plus plus"]: (1, SyaAssociativity.Right),
concepts_map["plus equals"]: (1, SyaAssociativity.Right),
}
sheerka, context, parser = self.init_parser(concepts_map, sya_def)
expression = "a plus plus equals b"
res = parser.infix_to_postfix(context, expression)
expected_array = tests.parsers.parsers_utils.compute_debug_array(res)
assert expected_array == [
["a", "a plus b", "a plus b", "equals", "b"],
["a", "a plus b", "a plus plus", "equals", "b"],
["a", "a plus b", "a plus equals b", "equals", "b"],
["a", "a plus plus", "plus", "equals", "b"],
["a", "a plus plus", "plus", "equals", "b"],
["a", "a plus plus", "plus", "equals", "b"],
["a", "a plus equals b", "a plus b", "equals", "b"],
["a", "a plus equals b", "a plus plus", "equals", "b"],
["a", "a plus equals b", "a plus equals b", "equals", "b"],
["T(a)", "C(a plus b)", "C(a plus b)", "T(equals)", "T(b)"],
["T(a)", "C(a plus b)", "C(a plus plus)", "T(equals)", "T(b)"],
["T(a)", "C(a plus b)", "C(a plus equals b)", "T(equals)", "T(b)"],
["T(a)", "C(a plus plus)", "T(plus)", "T(equals)", "T(b)"],
["T(a)", "C(a plus plus)", "T(plus)", "T(equals)", "T(b)"],
["T(a)", "C(a plus plus)", "T(plus)", "T(equals)", "T(b)"],
["T(a)", "C(a plus equals b)", "C(a plus b)", "T(equals)", "T(b)"],
["T(a)", "C(a plus equals b)", "C(a plus plus)", "T(equals)", "T(b)"],
["T(a)", "C(a plus equals b)", "C(a plus equals b)", "T(equals)", "T(b)"],
]
def test_non_reg(self):
concepts_map = {
"plus": Concept("a plus b").def_var("a").def_var("b"),
"complex infix": Concept("a complex infix b ").def_var("a").def_var("b"),
}
sya_def = {
# concepts_map["plus"]: (1, SyaAssociativity.Right),
# concepts_map["plus plus"]: (1, SyaAssociativity.Right),
# concepts_map["plus equals"]: (1, SyaAssociativity.Right),
}
sheerka, context, parser = self.init_parser(concepts_map, sya_def)
expression = "a plus complex infix b"
res = parser.infix_to_postfix(context, expression)
res = parser.parse(context, expression)
pass
def test_i_can_use_string_instead_of_identifier(self):
concepts_map = {
"ternary": Concept("a ? ? b '::' c").def_var("a").def_var("b").def_var("c"),
@@ -945,13 +876,7 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
Not quite sure why this test is here
:return:
"""
# concepts_map = {
# "foo": Concept("foo a").def_var("a"),
# "one": Concept("one"),
# "two": Concept("two"),
# "twenties": Concept("twenties", definition="'twenty' (one|two)=unit").def_var("unit"),
# }
sheerka, context, parser = self.init_parser(cmap, None)
sheerka, context, parser = self.init_parser()
expression = "suffixed twenties"
res = parser.infix_to_postfix(context, expression)
@@ -962,17 +887,6 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
assert res[0].out == expected_array
def test_i_can_parse_when_concept_atom_only(self):
# concepts_map = {
# "plus": Concept("a plus b").def_var("a").def_var("b"),
# "mult": Concept("a mult b").def_var("a").def_var("b"),
# "one": Concept("one"),
# "two": Concept("two"),
# "three": Concept("three"),
# }
# sya_def = {
# concepts_map["plus"]: (5, SyaAssociativity.Right),
# concepts_map["mult"]: (10, SyaAssociativity.Right), # precedence greater than plus
# }
sheerka, context, parser = self.init_parser()
text = "one plus two mult three"
@@ -992,10 +906,7 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
assert expected_concept.compiled["b"].compiled["b"] == cmap["three"]
def test_i_can_parse_when_python_code(self):
# concepts_map = {
# "foo": Concept("foo a").def_var("a")
# }
sheerka, context, parser = self.init_parser(cmap, None)
sheerka, context, parser = self.init_parser()
text = "suffixed 1 + 1"
res = parser.parse(context, text)
@@ -1014,16 +925,10 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
assert sheerka.isinstance(return_value_a, BuiltinConcepts.RETURN_VALUE)
assert return_value_a.status
assert sheerka.isinstance(return_value_a.body, BuiltinConcepts.PARSER_RESULT)
assert return_value_a.body.source == " 1 + 1"
assert return_value_a.body.source == "1 + 1"
assert isinstance(return_value_a.body.body, PythonNode)
def test_i_can_parse_when_bnf_concept(self):
# concepts_map = {
# "foo": Concept("foo a").def_var("a"),
# "one": Concept("one"),
# "two": Concept("two"),
# "twenties": Concept("twenties", definition="'twenty' (one|two)=unit").def_var("unit"),
# }
sheerka, context, parser = self.init_parser()
text = "suffixed twenty one"
@@ -1043,13 +948,7 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
assert expected_concept.compiled["a"].compiled["unit"] == cmap["one"]
def test_i_can_parse_sequences(self):
# concepts_map = {
# "plus": Concept("a plus b").def_var("a").def_var("b"),
# "foo": Concept("foo a").def_var("a"),
# "one": Concept("one"),
# "two": Concept("two"),
# }
sheerka, context, parser = self.init_parser(cmap, None)
sheerka, context, parser = self.init_parser()
text = "one plus 1 + 1 suffixed two"
res = parser.parse(context, text)
@@ -1081,27 +980,12 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
("f1(one prefixed) plus f2(suffixed two)", True, [
CNC("plus",
a=SCWC("f1(", ")", CNC("prefixed", a="one")),
b=SCWC(" f2(", (")", 1), CNC("suffixed", a="two")))
b=SCWC("f2(", (")", 1), CNC("suffixed", a="two")))
]),
("function(suffixed x$!#)", False, [
SCWC("function(", ")", CNC("suffixed", 2, 7, a=" x$!#"))]),
SCWC("function(", ")", CNC("suffixed", 2, 7, a="x$!#"))]),
])
def test_i_can_parse_when_one_result(self, text, expected_status, expected_result):
# concepts_map = {
# "prefixed": Concept("a prefixed").def_var("a"),
# "suffixed": Concept("suffixed a").def_var("a"),
# "mult": Concept("a mult b").def_var("a").def_var("b"),
# "plus": Concept("a plus b").def_var("a").def_var("b"),
# "if": Concept("if a then b else c end").def_var("a").def_var("b").def_var("c"),
# "one": Concept("one"),
# "two": Concept("two"),
# "three": Concept("three"),
# "twenties": Concept("twenties", definition="'twenty' (one|two)=unit").def_var("unit"),
# }
# sya_def = {
# concepts_map["plus"]: (5, SyaAssociativity.Right),
# concepts_map["mult"]: (10, SyaAssociativity.Right), # precedence greater than plus
# }
sheerka, context, parser = self.init_parser()
res = parser.parse(context, text)
@@ -1113,41 +997,54 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
assert lexer_nodes == expected_array
# @pytest.mark.parametrize("text, list_of_expected", [
# ("1 plus twenty one", [
# (False, [CNC("plus", a=scnode(0, 0, "1"), b=UTN(" twenty ")), CN("one")]),
# (True, [CNC("plus", a=scnode(0, 0, "1"), b=CN("twenties", source="twenty one"))])
# ])
# ])
# def test_i_can_parse_when_multiple_results(self, text, list_of_expected):
# concepts_map = {
# "prefixed": Concept("a prefixed").def_var("a"),
# "suffixed": Concept("suffixed a").def_var("a"),
# "mult": Concept("a mult b").def_var("a").def_var("b"),
# "plus": Concept("a plus b").def_var("a").def_var("b"),
# "if": Concept("if a then b else c end").def_var("a").def_var("b").def_var("c"),
# "one": Concept("one"),
# "two": Concept("two"),
# "three": Concept("three"),
# "twenties": Concept("twenties", definition="'twenty' (one|two)=unit").def_var("unit"),
# }
# sya_def = {
# concepts_map["plus"]: (5, SyaAssociativity.Right),
# concepts_map["mult"]: (10, SyaAssociativity.Right), # precedence greater than plus
# }
# sheerka, context, parser = self.init_parser(concepts_map, sya_def)
#
# list_of_res = parser.parse(context, text)
# assert len(list_of_res) == len(list_of_expected)
#
# for res, expected in zip(list_of_res, list_of_expected):
# wrapper = res.body
# lexer_nodes = res.body.body
# assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
#
# expected_array = compute_expected_array(concepts_map, text, expected[1])
# assert res.status == expected[0]
# assert lexer_nodes == expected_array
@pytest.mark.parametrize("text", [
"foo bar (one",
"foo bar one",
"foo one two",
"foo x$!# one",
])
def test_i_cannot_parse_when_concept_almost_found(self, text):
"""
We test that the parsed concept seems like a known one, but it was not.
The parser has to detected that the predication was incorrect
:return:
"""
sheerka, context, parser = self.init_parser()
res = parser.parse(context, text)
assert not res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.NOT_FOR_ME)
assert res.body.body == text
@pytest.mark.parametrize("text, expected_result", [
("one plus two foo bar baz", [CNC("plus", a="one", b="two"), UTN(" foo bar baz")]),
("one plus two foo bar", [CNC("plus", a="one", b="two"), UTN(" foo bar")]),
("foo bar one plus two", [UTN("foo bar "), CNC("plus", a="one", b="two")]),
("foo bar (one plus two", [UTN("foo bar ("), CNC("plus", a="one", b="two")]),
("one plus two a long other b", [CNC("plus", a="one", b="two"), UTN(" a long other b")]),
("one plus two a long infixed", [CNC("plus", a="one", b="two"), UTN(" a long infixed")]),
("one plus two a long", [CNC("plus", a="one", b="two"), UTN(" a long")]),
("one ? a long infixed : two", [CNC("?", a="one", b=UTN("a long infixed"), c="two")]),
("one ? a long infix : two", [CNC("?", a="one", b=UTN("a long infix"), c="two")]),
])
def test_i_cannot_parse_when_one_part_is_recognized_but_not_the_rest(self, text, expected_result):
"""
We test that the parsed concept seems like a known one, but it was not.
The parser has to detected that the predication was incorrect
:return:
"""
sheerka, context, parser = self.init_parser()
res = parser.parse(context, text)
wrapper = res.body
lexer_nodes = res.body.body
expected_array = compute_expected_array(cmap, text, expected_result)
assert not res.status
assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
assert lexer_nodes == expected_array
@pytest.mark.parametrize("text, expected_concept, expected_unrecognized", [
("x$!# prefixed", "prefixed", ["a"]),
@@ -1157,12 +1054,6 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
("x$!# infix z$!#", "infix", ["a", "b"]),
])
def test_i_cannot_parse_when_unrecognized(self, text, expected_concept, expected_unrecognized):
# concepts_map = {
# "suffixed": Concept("suffixed a").def_var("a"),
# "prefixed": Concept("a prefixed").def_var("a"),
# "infix": Concept("a infix b").def_var("a").def_var("b"),
# "one": Concept("one")
# }
sheerka, context, parser = self.init_parser()
res = parser.parse(context, text)
@@ -1183,13 +1074,6 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
("one prefixed x$!#", [cnode("__var__0 prefixed", 0, 2, "one prefixed"), utnode(3, 7, " x$!#")]),
])
def test_i_cannot_parse_when_part_of_the_sequence_is_not_recognized(self, text, expected):
# concepts_map = {
# "suffixed": Concept("suffixed a").def_var("a"),
# "prefixed": Concept("a prefixed").def_var("a"),
# "infix": Concept("a infix b").def_var("a").def_var("b"),
# "one": Concept("one"),
# "two": Concept("two"),
# }
sheerka, context, parser = self.init_parser()
res = parser.parse(context, text)
@@ -1203,7 +1087,7 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
@pytest.mark.parametrize("text", [
"one",
"1 + 1",
"x$!# ",
"x$!#",
"twenty one"
"",
"function(not an sya concept)",
@@ -1214,13 +1098,6 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
Atoms concepts, source code or BNF concepts alone are discarded by the lexer
:return:
"""
# concepts_map = {
# "plus": Concept("a plus b").def_var("a").def_var("b"),
# "one": Concept("one"),
# "two": Concept("two"),
# "three": Concept("three"),
# "twenties": Concept("twenties", definition="'twenty' (one|two)=unit").def_var("unit"),
# }
sheerka, context, parser = self.init_parser()
res = parser.parse(context, text)