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
+2 -2
View File
@@ -12,10 +12,10 @@ from core.concept import Concept, ConceptParts, DEFINITION_TYPE_DEF
("foo", ["foo"], "foo"),
("foo a", ["foo"], "__var__0 a"),
("foo a b", ["a"], "foo __var__0 b"),
("'foo'", [], "foo"),
("'foo'", [], "'foo'"),
("my name is a", ["a"], "my name is __var__0"),
("a b c d", ["b", "c"], "a __var__0 __var__1 d"),
("a 'b c' d", ["b", "c"], "a b c d"),
("a 'b c' d", ["b", "c"], "a 'b c' d"),
("a | b", ["a", "b"], "__var__0 | __var__1"),
("a b a c", ["a", "b"], "__var__0 __var__1 __var__0 c"),
("a b a c", ["b", "a"], "__var__1 __var__0 __var__1 c"),
+16 -2
View File
@@ -4,7 +4,7 @@ from core.tokenizer import Tokenizer, Token, TokenKind, LexerError, Keywords
def test_i_can_tokenize():
source = "+*-/{}[]() ,;:.?\n\n\r\r\r\nidentifier_0\t \t10.15 10 'string\n' \"another string\"=|&<>c:name:"
source += "$£€!_identifier°~_^\\`==#"
source += "$£€!_identifier°~_^\\`==#__var__10"
tokens = list(Tokenizer(source))
assert tokens[0] == Token(TokenKind.PLUS, "+", 0, 1, 1)
assert tokens[1] == Token(TokenKind.STAR, "*", 1, 1, 2)
@@ -54,8 +54,9 @@ def test_i_can_tokenize():
assert tokens[45] == Token(TokenKind.BACK_QUOTE, '`', 108, 6, 50)
assert tokens[46] == Token(TokenKind.EQUALSEQUALS, '==', 109, 6, 51)
assert tokens[47] == Token(TokenKind.HASH, '#', 111, 6, 53)
assert tokens[48] == Token(TokenKind.VAR_DEF, '__var__10', 112, 6, 54)
assert tokens[48] == Token(TokenKind.EOF, '', 112, 6, 54)
assert tokens[49] == Token(TokenKind.EOF, '', 121, 6, 63)
@pytest.mark.parametrize("text, expected", [
@@ -88,6 +89,19 @@ def test_i_can_parse_word(text):
assert tokens[1].index == len(text)
@pytest.mark.parametrize("text", [
"__var__0",
"__var__1",
"__var__10",
"__var__999",
])
def test_i_can_parse_var_def(text):
tokens = list(Tokenizer(text))
assert len(tokens) == 2
assert tokens[0].type == TokenKind.VAR_DEF
assert tokens[0].value == text
@pytest.mark.parametrize("text, message, error_text, index, line, column", [
("'string", "Missing Trailing quote", "'string", 7, 1, 8),
('"string', "Missing Trailing quote", '"string', 7, 1, 8),
+2 -2
View File
@@ -36,9 +36,9 @@ def compute_debug_array(res):
if token.type == TokenKind.WHITESPACE:
continue
else:
res_debug.append(token.value)
res_debug.append("T(" + token.value + ")")
else:
res_debug.append(token.concept.name)
res_debug.append("C(" + token.concept.name + ")")
to_compare.append(res_debug)
return to_compare
+2 -2
View File
@@ -218,8 +218,8 @@ class TestAtomsParser(TestUsingMemoryBasedSheerka):
@pytest.mark.parametrize("text, expected", [
("hello foo bar",
[
(True, [CNC("hello1", source="hello foo ", a=" foo "), "bar"]),
(True, [CNC("hello2", source="hello foo ", b=" foo "), "bar"]),
(True, [CNC("hello1", source="hello foo ", a="foo "), "bar"]),
(True, [CNC("hello2", source="hello foo ", b="foo "), "bar"]),
]),
])
def test_i_can_parse_when_unrecognized_yield_multiple_values(self, text, expected):
File diff suppressed because it is too large Load Diff
@@ -1,193 +0,0 @@
# import ast
#
# import pytest
#
# from core.builtin_concepts import ParserResultConcept, ReturnValueConcept, BuiltinConcepts
# from core.concept import Concept
# from core.tokenizer import Token, TokenKind, Tokenizer
# from parsers.BaseNodeParser import SourceCodeNode, ConceptNode, UnrecognizedTokensNode
# from parsers.ConceptsWithConceptsParser import ConceptsWithConceptsParser
# from parsers.MultipleConceptsParser import MultipleConceptsParser
# from parsers.PythonParser import PythonNode
#
# from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
#
# multiple_concepts_parser = MultipleConceptsParser()
#
#
# def ret_val(*args):
# result = []
# index = 0
# source = ""
# for item in args:
# if isinstance(item, Concept):
# tokens = [Token(TokenKind.IDENTIFIER, item.name, 0, 0, 0)]
# result.append(ConceptNode(item, index, index, tokens, item.name))
# index += 1
# source += item.name
# elif isinstance(item, PythonNode):
# tokens = list(Tokenizer(item.source))[:-1] # strip trailing EOF
# result.append(SourceCodeNode(item, index, index + len(tokens) - 1, tokens, item.source))
# index += len(tokens)
# source += item.source
# else:
# tokens = list(Tokenizer(item))[:-1] # strip trailing EOF
# result.append(UnrecognizedTokensNode(index, index + len(tokens) - 1, tokens))
# index += len(tokens)
# source += item
#
# return ReturnValueConcept(
# "who",
# False,
# ParserResultConcept(parser=multiple_concepts_parser, value=result, source=source))
#
#
# class TestConceptsWithConceptsParser(TestUsingMemoryBasedSheerka):
#
# def init(self, concepts, inputs):
# context = self.get_context()
# for concept in concepts:
# context.sheerka.create_new_concept(context, concept)
#
# return context, ret_val(*inputs)
#
# def execute(self, concepts, inputs):
# context, input_return_values = self.init(concepts, inputs)
#
# parser = ConceptsWithConceptsParser()
# result = parser.parse(context, input_return_values.body)
#
# wrapper = result.body
# return_value = result.body.body
#
# return context, parser, result, wrapper, return_value
#
# @pytest.mark.parametrize("text, interested", [
# ("not parser result", False),
# (ParserResultConcept(parser="not multiple_concepts_parser"), False),
# (ParserResultConcept(parser=multiple_concepts_parser, value=[UnrecognizedTokensNode(0, 0, [])]), True),
# ])
# def test_not_interested(self, text, interested):
# context = self.get_context()
#
# res = ConceptsWithConceptsParser().parse(context, text)
# if interested:
# assert res is not None
# else:
# assert res is None
#
# def test_i_can_parse_composition_of_concepts(self):
# foo = Concept("foo")
# bar = Concept("bar")
# plus = Concept("a plus b").def_var("a").def_var("b")
#
# context, parser, result, wrapper, return_value = self.execute([foo, bar, plus], [foo, " plus ", bar])
#
# assert result.status
# assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
# assert result.who == wrapper.parser.name
# assert wrapper.source == "foo plus bar"
# assert context.sheerka.isinstance(return_value, plus)
#
# assert return_value.compiled["a"] == foo
# assert return_value.compiled["b"] == bar
#
# # sanity check, I can evaluate the result
# evaluated = context.sheerka.evaluate_concept(self.get_context(context.sheerka, True), return_value)
# assert evaluated.key == return_value.key
# assert evaluated.get_prop("a") == foo.init_key()
# assert evaluated.get_prop("b") == bar.init_key()
#
# def test_i_can_parse_when_composition_of_source_code(self):
# plus = Concept("a plus b", body="a + b").def_var("a").def_var("b")
# left = PythonNode("1+1", ast.parse("1+1", mode="eval"))
# right = PythonNode("2+2", ast.parse("2+2", mode="eval"))
# context, parser, result, wrapper, return_value = self.execute([plus], [left, " plus ", right])
#
# assert result.status
# assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
# assert result.who == wrapper.parser.name
# assert wrapper.source == "1+1 plus 2+2"
# assert context.sheerka.isinstance(return_value, plus)
#
# left_parser_result = ParserResultConcept(parser=parser, source="1+1", value=left)
# right_parser_result = ParserResultConcept(parser=parser, source="2+2", value=right)
# assert return_value.compiled["a"] == [ReturnValueConcept(parser.name, True, left_parser_result)]
# assert return_value.compiled["b"] == [ReturnValueConcept(parser.name, True, right_parser_result)]
#
# # sanity check, I can evaluate the result
# evaluated = context.sheerka.evaluate_concept(self.get_context(context.sheerka, True), return_value)
# assert evaluated.key == return_value.key
# assert evaluated.get_prop("a") == 2
# assert evaluated.get_prop("b") == 4
# assert evaluated.body == 6
#
# def test_i_can_parse_when_mix_of_concept_and_code(self):
# plus = Concept("a plus b").def_var("a").def_var("b")
# code = PythonNode("1+1", ast.parse("1+1", mode="eval"))
# foo = Concept("foo")
# context, parser, result, wrapper, return_value = self.execute([plus, foo], [foo, " plus ", code])
#
# assert result.status
# assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
# assert result.who == wrapper.parser.name
# assert wrapper.source == "foo plus 1+1"
# assert context.sheerka.isinstance(return_value, plus)
#
# code_parser_result = ParserResultConcept(parser=parser, source="1+1", value=code)
# assert return_value.compiled["a"] == foo
# assert return_value.compiled["b"] == [ReturnValueConcept(parser.name, True, code_parser_result)]
#
# # sanity check, I can evaluate the result
# evaluated = context.sheerka.evaluate_concept(self.get_context(context.sheerka, True), return_value)
# assert evaluated.key == return_value.key
# assert evaluated.get_prop("a") == foo.init_key()
# assert evaluated.get_prop("b") == 2
#
# def test_i_can_parse_when_multiple_concepts_are_recognized(self):
# foo = Concept("foo")
# bar = Concept("bar")
# plus_1 = Concept("a plus b", body="body1").def_var("a").def_var("b")
# plus_2 = Concept("a plus b", body="body2").def_var("a").def_var("b")
#
# context, input_return_values = self.init([foo, bar, plus_1, plus_2], [foo, " plus ", bar])
# parser = ConceptsWithConceptsParser()
# result = parser.parse(context, input_return_values.body)
#
# assert len(result) == 2
#
# res = result[0]
# wrapper = res.value
# return_value = res.value.value
# assert res.status
# assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
# assert res.who == wrapper.parser.name
# assert wrapper.source == "foo plus bar"
# assert context.sheerka.isinstance(return_value, plus_1)
# assert return_value.compiled["a"] == foo
# assert return_value.compiled["b"] == bar
#
# res = result[1]
# wrapper = res.value
# return_value = res.value.value
# assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
# assert res.who == wrapper.parser.name
# assert wrapper.source == "foo plus bar"
# assert context.sheerka.isinstance(return_value, plus_2)
# assert return_value.compiled["a"] == foo
# assert return_value.compiled["b"] == bar
#
# def test_i_cannot_parse_when_unknown_concept(self):
# foo = Concept("foo")
# bar = Concept("bar")
#
# context, input_return_values = self.init([foo, bar], [foo, " plus ", bar])
# parser = ConceptsWithConceptsParser()
# result = parser.parse(context, input_return_values.body)
# wrapper = result.body
# return_value = result.body.body
#
# assert not result.status
# assert context.sheerka.isinstance(wrapper, BuiltinConcepts.NOT_FOR_ME)
# assert result.who == parser.name
# assert return_value == input_return_values.body.body
@@ -1,216 +0,0 @@
# import pytest
#
# from core.builtin_concepts import ParserResultConcept, BuiltinConcepts
# from core.concept import Concept
# from core.tokenizer import Tokenizer, TokenKind, Token
# from parsers.BaseNodeParser import cnode, scnode, utnode, SourceCodeNode, ConceptNode
# from parsers.BnfNodeParser import BnfNodeParser, Sequence
# from parsers.MultipleConceptsParser import MultipleConceptsParser
# from parsers.PythonParser import PythonNode
#
# from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
#
#
# def get_return_value(context, grammar, expression):
# parser = BnfNodeParser()
# parser.initialize(context, grammar)
#
# ret_val = parser.parse(context, expression)
# assert not ret_val.status
# return ret_val
#
#
# class TestMultipleConceptsParser(TestUsingMemoryBasedSheerka):
#
# def init(self, concepts, grammar, expression):
# context = self.get_context()
# for c in concepts:
# context.sheerka.create_new_concept(context, c)
# return_value = get_return_value(context, grammar, expression)
#
# return context, return_value
#
# def test_not_interested_if_not_parser_result(self):
# context = self.get_context()
# text = "not parser result"
#
# res = MultipleConceptsParser().parse(context, text)
# assert res is None
#
# def test_not_interested_if_not_from_concept_lexer_parser(self):
# context = self.get_context()
# text = ParserResultConcept(parser="not concept lexer", value="some value")
#
# res = MultipleConceptsParser().parse(context, text)
# assert res is None
#
# def test_i_can_parse_exact_concepts(self):
# foo = Concept("foo", body="'foo'")
# bar = Concept("bar", body="'bar'")
# baz = Concept("baz", body="'baz'")
# grammar = {}
# context, return_value = self.init([foo, bar, baz], grammar, "bar foo baz")
#
# parser = MultipleConceptsParser()
# ret_val = parser.parse(context, return_value.body)
#
# assert ret_val.status
# assert ret_val.who == parser.name
# assert context.sheerka.isinstance(ret_val.value, BuiltinConcepts.PARSER_RESULT)
# assert ret_val.value.value == [
# ConceptNode(bar, 0, 0, source="bar"),
# ConceptNode(foo, 2, 2, source="foo"),
# ConceptNode(baz, 4, 4, source="baz")]
# assert ret_val.value.source == "bar foo baz"
#
# def test_i_can_parse_when_ending_with_bnf(self):
# foo = Concept("foo", body="'foo'")
# bar = Concept("bar", body="'bar'")
# grammar = {foo: Sequence("foo1", "foo2", "foo3")}
# context, return_value = self.init([foo, bar], grammar, "bar foo1 foo2 foo3")
#
# parser = MultipleConceptsParser()
# ret_val = parser.parse(context, return_value.body)
#
# assert ret_val.status
# assert ret_val.who == parser.name
# assert context.sheerka.isinstance(ret_val.value, BuiltinConcepts.PARSER_RESULT)
# assert ret_val.value.value == [cnode("bar", 0, 0, "bar"), cnode("foo", 2, 6, "foo1 foo2 foo3")]
# assert ret_val.value.source == "bar foo1 foo2 foo3"
#
# def test_i_can_parse_when_starting_with_bnf(self):
# foo = Concept("foo", body="'foo'")
# bar = Concept("bar", body="'bar'")
# grammar = {foo: Sequence("foo1", "foo2", "foo3")}
# context, return_value = self.init([foo, bar], grammar, "foo1 foo2 foo3 bar")
#
# parser = MultipleConceptsParser()
# ret_val = parser.parse(context, return_value.body)
#
# assert ret_val.status
# assert ret_val.who == parser.name
# assert context.sheerka.isinstance(ret_val.value, BuiltinConcepts.PARSER_RESULT)
# assert ret_val.value.value == [cnode("foo", 0, 4, "foo1 foo2 foo3"), cnode("bar", 6, 6, "bar")]
# assert ret_val.value.source == "foo1 foo2 foo3 bar"
#
# def test_i_can_parse_when_concept_are_mixed(self):
# foo = Concept("foo")
# bar = Concept("bar")
# baz = Concept("baz")
# grammar = {foo: Sequence("foo1", "foo2", "foo3")}
# context, return_value = self.init([foo, bar, baz], grammar, "baz foo1 foo2 foo3 bar")
#
# parser = MultipleConceptsParser()
# ret_val = parser.parse(context, return_value.body)
#
# assert ret_val.status
# assert ret_val.who == parser.name
# assert context.sheerka.isinstance(ret_val.value, BuiltinConcepts.PARSER_RESULT)
# assert ret_val.value.value == [
# cnode("baz", 0, 0, "baz"),
# cnode("foo", 2, 6, "foo1 foo2 foo3"),
# cnode("bar", 8, 8, "bar")]
# assert ret_val.value.source == "baz foo1 foo2 foo3 bar"
#
# def test_i_can_parse_when_multiple_concepts_are_matching(self):
# foo = Concept("foo")
# bar = Concept("bar", body="bar1")
# baz = Concept("bar", body="bar2")
# grammar = {foo: "foo"}
# context, return_value = self.init([foo, bar, baz], grammar, "foo bar")
#
# parser = MultipleConceptsParser()
# ret_val = parser.parse(context, return_value.body)
#
# assert len(ret_val) == 2
# assert ret_val[0].status
# assert ret_val[0].value.value == [cnode("foo", 0, 0, "foo"), cnode("bar", 2, 2, "bar")]
# assert ret_val[0].value.source == "foo bar"
# assert ret_val[0].value.value[1].concept.metadata.body == "bar1"
#
# assert ret_val[1].status
# assert ret_val[1].value.value == [cnode("foo", 0, 0, "foo"), cnode("bar", 2, 2, "bar")]
# assert ret_val[1].value.source == "foo bar"
# assert ret_val[1].value.value[1].concept.metadata.body == "bar2"
#
# def test_i_can_parse_when_source_code(self):
# foo = Concept("foo")
# grammar = {foo: "foo"}
# context, return_value = self.init([foo], grammar, "1 foo")
#
# parser = MultipleConceptsParser()
# ret_val = parser.parse(context, return_value.body)
# wrapper = ret_val.value
# value = ret_val.value.value
#
# assert ret_val.status
# assert ret_val.who == parser.name
# assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
# assert wrapper.source == "1 foo"
# assert value == [
# scnode(0, 1, "1 "),
# cnode("foo", 2, 2, "foo")]
#
# def test_i_cannot_parse_when_unrecognized_token(self):
# twenty_two = Concept("twenty two")
# one = Concept("one")
# grammar = {twenty_two: Sequence("twenty", "two")}
# context, return_value = self.init([twenty_two, one], grammar, "twenty two + one")
#
# parser = MultipleConceptsParser()
# ret_val = parser.parse(context, return_value.body)
#
# assert not ret_val.status
# assert ret_val.who == parser.name
# assert context.sheerka.isinstance(ret_val.value, BuiltinConcepts.PARSER_RESULT)
# assert ret_val.value.value == [
# cnode("twenty two", 0, 2, "twenty two"),
# utnode(3, 5, " + "),
# cnode("one", 6, 6, "one")
# ]
# assert ret_val.value.source == "twenty two + one"
#
# def test_i_cannot_parse_when_unknown_concepts(self):
# twenty_two = Concept("twenty two")
# one = Concept("one")
# grammar = {twenty_two: Sequence("twenty", "two")}
# context, return_value = self.init([twenty_two, one], grammar, "twenty two plus one")
#
# parser = MultipleConceptsParser()
# ret_val = parser.parse(context, return_value.body)
#
# assert not ret_val.status
# assert ret_val.who == parser.name
# assert context.sheerka.isinstance(ret_val.value, BuiltinConcepts.PARSER_RESULT)
# assert ret_val.value.value == [
# cnode("twenty two", 0, 2, "twenty two"),
# utnode(3, 5, " plus "),
# cnode("one", 6, 6, "one")
# ]
# assert ret_val.value.source == "twenty two plus one"
#
# @pytest.mark.parametrize("text, expected_source, expected_end", [
# ("True", "True", 0),
# ("1 == 1", "1 == 1", 4),
# ("1!xdf", "1", 0),
# ("1", "1", 0),
# ])
# def test_i_can_get_source_code_node(self, text, expected_source, expected_end):
# tokens = list(Tokenizer(text))[:-1] # strip trailing EOF
#
# start_index = 5 # a random number different of zero
# res = MultipleConceptsParser().get_source_code_node(self.get_context(), start_index, tokens)
#
# assert isinstance(res, SourceCodeNode)
# assert isinstance(res.node, PythonNode)
# assert res.source == expected_source
# assert res.start == start_index
# assert res.end == start_index + expected_end
#
# def test_i_cannot_parse_null_text(self):
# res = MultipleConceptsParser().get_source_code_node(self.get_context(), 0, [])
# assert res is None
#
# eof = Token(TokenKind.EOF, "", 0, 0, 0)
# res = MultipleConceptsParser().get_source_code_node(self.get_context(), 0, [eof])
# assert res is None
+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)
File diff suppressed because it is too large Load Diff