I can now use keyword in concept definition and parsing
This commit is contained in:
@@ -154,7 +154,7 @@ def get_node(
|
||||
if concept_found:
|
||||
concept_found = Concept().update_from(concept_found) # make a copy when massively used in tests
|
||||
if sya and len(concept_found.metadata.variables) > 0 and not is_bnf:
|
||||
return SyaConceptParserHelper(concept_found, start)
|
||||
return SyaConceptParserHelper(concept_found, start, start + length - 1)
|
||||
elif init_empty_body:
|
||||
node = CNC(concept_found, start, start + length - 1, source=sub_expr, exclude_body=exclude_body)
|
||||
init_body(node, concept_found, sub_expr)
|
||||
|
||||
@@ -215,6 +215,24 @@ class TestAtomsParser(TestUsingMemoryBasedSheerka):
|
||||
expected_array = compute_expected_array(concepts_map, "one", ["one"])
|
||||
assert lexer_nodes == expected_array
|
||||
|
||||
def test_i_can_parse_concepts_with_keyword(self):
|
||||
concepts_map = {
|
||||
"a special concept": Concept("a special concept"),
|
||||
"isa": Concept("isa"),
|
||||
}
|
||||
|
||||
sheerka, context, parser = self.init_parser(concepts_map)
|
||||
|
||||
res = parser.parse(context, "a special concept")
|
||||
lexer_nodes = res.body.body
|
||||
expected_array = compute_expected_array(concepts_map, "a special concept", ["a special concept"])
|
||||
assert lexer_nodes == expected_array
|
||||
|
||||
res = parser.parse(context, "isa")
|
||||
lexer_nodes = res.body.body
|
||||
expected_array = compute_expected_array(concepts_map, "isa", ["isa"])
|
||||
assert lexer_nodes == expected_array
|
||||
|
||||
@pytest.mark.parametrize("text", [
|
||||
"foo",
|
||||
f"foo one",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import pytest
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from core.concept import Concept, ConceptParts, DoNotResolve
|
||||
from core.concept import Concept, ConceptParts, DoNotResolve, DEFINITION_TYPE_DEF, CC
|
||||
from parsers.BaseNodeParser import CNC, UTN, CN
|
||||
from parsers.BnfNodeParser import BnfNodeParser, StrMatch, TerminalNode, NonTerminalNode, Sequence, OrderedChoice, \
|
||||
Optional, ZeroOrMore, OneOrMore, ConceptExpression
|
||||
@@ -18,7 +18,17 @@ cmap = {
|
||||
'one or more three': Concept("one or more three", definition="three+"),
|
||||
'two or four': Concept("two or four", definition="two | 'four'"),
|
||||
"twenties": Concept("twenties", definition="'twenty' c:two or four:=unit"),
|
||||
"one or more plus": Concept("one or more plus", definition="c:a plus b:+"),
|
||||
"one or more plus": Concept("one or more plus", definition="c:a plus b:+"), # TODO
|
||||
|
||||
# testing keywords
|
||||
"def_only": Concept("def"),
|
||||
"def number": Concept("def number", definition="def (one|two)=number"),
|
||||
# sequence of keywords using bnf definition
|
||||
# "def_concept_bnf": Concept("def_concept_bnf", definition="'def' 'concept'"),
|
||||
# "def concept_bnf number": Concept("def number", definition="def_concept_bnf (one|two)=number"),
|
||||
# sequence of keywords using def definition
|
||||
# "def_concept_def": Concept("def_concept_def", definition="def concept", definition_type=DEFINITION_TYPE_DEF),
|
||||
# "def concept_def number": Concept("def number", definition="def_concept_def (one|two|three)=number"),
|
||||
}
|
||||
|
||||
|
||||
@@ -40,12 +50,13 @@ def u(parsing_expression, start, end, children=None):
|
||||
return NonTerminalNode(parsing_expression, start, end, [], children)
|
||||
|
||||
|
||||
def compute_expected_array(my_concepts_map, expression, expected):
|
||||
def compute_expected_array(my_concepts_map, expression, expected, exclude_body=False):
|
||||
return tests.parsers.parsers_utils.compute_expected_array(
|
||||
my_concepts_map,
|
||||
expression,
|
||||
expected,
|
||||
init_empty_body=True)
|
||||
init_empty_body=True,
|
||||
exclude_body=exclude_body)
|
||||
|
||||
|
||||
class TestBnfNodeParser(TestUsingMemoryBasedSheerka):
|
||||
@@ -136,7 +147,7 @@ class TestBnfNodeParser(TestUsingMemoryBasedSheerka):
|
||||
assert res.body.reason == BuiltinConcepts.IS_EMPTY
|
||||
|
||||
@pytest.mark.parametrize("expr, text", [
|
||||
(StrMatch("foo"), "foo"),
|
||||
# (StrMatch("foo"), "foo"),
|
||||
(StrMatch("'foo'"), "'foo'"),
|
||||
(StrMatch("1"), "1"),
|
||||
(StrMatch("3.14"), "3.14"),
|
||||
@@ -741,7 +752,6 @@ class TestBnfNodeParser(TestUsingMemoryBasedSheerka):
|
||||
("twenty four", True, [CN("twenties", source="twenty four")]),
|
||||
("twenty one", False, [UTN("twenty "), CN("bnf one", source="one")]),
|
||||
("twenty two + 1", True, [CN("twenties", source="twenty two"), " + 1"]),
|
||||
|
||||
])
|
||||
def test_i_can_parse(self, parser_input, expected_status, expected):
|
||||
sheerka, context, parser = self.init_parser(init_from_sheerka=True)
|
||||
@@ -755,6 +765,24 @@ class TestBnfNodeParser(TestUsingMemoryBasedSheerka):
|
||||
assert sheerka.isinstance(parser_result, BuiltinConcepts.PARSER_RESULT)
|
||||
assert concepts_nodes == expected_array
|
||||
|
||||
def test_i_can_parse_when_keyword(self):
|
||||
sheerka, context, parser = self.init_parser(init_from_sheerka=True)
|
||||
|
||||
parser_input = "def one"
|
||||
expected = [CNC("def number", source="def one", number="one", one="one")]
|
||||
|
||||
res = parser.parse(context, parser_input)
|
||||
expected_array = compute_expected_array(cmap, parser_input, expected)
|
||||
expected_array[0].compiled["def"] = cmap["def_only"]
|
||||
|
||||
parser_result = res.value
|
||||
concepts_nodes = res.value.value
|
||||
|
||||
assert res.status
|
||||
assert sheerka.isinstance(parser_result, BuiltinConcepts.PARSER_RESULT)
|
||||
assert concepts_nodes == expected_array
|
||||
|
||||
|
||||
# @pytest.mark.parametrize("parser_input, expected", [
|
||||
# ("one", [
|
||||
# (True, [CNC("bnf_one", source="one", one="one", body="one")]),
|
||||
|
||||
@@ -103,9 +103,10 @@ class TestBnfParser(TestUsingMemoryBasedSheerka):
|
||||
("'str''='var", Sequence(StrMatch("str"), StrMatch("="), c("var"))),
|
||||
("foo=f", c("foo", "f")),
|
||||
("foo=f 'constant'", Sequence(c("foo", "f"), StrMatch("constant"))),
|
||||
("def 'concept'", Sequence(c("def"), StrMatch("concept"))),
|
||||
])
|
||||
def test_i_can_parse_regex_with_concept(self, expression, expected):
|
||||
sheerka, context, parser, foo, bar, var = self.init_parser("foo", "bar", "var")
|
||||
sheerka, context, parser, foo, bar, var, _def = self.init_parser("foo", "bar", "var", "def")
|
||||
|
||||
res = parser.parse(context, Tokenizer(expression))
|
||||
|
||||
|
||||
@@ -161,6 +161,32 @@ class TestExactConceptParser(TestUsingMemoryBasedSheerka):
|
||||
assert concept_found.metadata.need_validation
|
||||
assert not concept_found.metadata.is_evaluated
|
||||
|
||||
def test_i_can_parse_when_expression_contains_keyword(self):
|
||||
sheerka, context, isa, def_concept = self.init_concepts(
|
||||
Concept("c is a concept").def_var("c"),
|
||||
Concept("def concept a").def_var("a"),
|
||||
)
|
||||
|
||||
source = "z is a concept"
|
||||
results = ExactConceptParser().parse(context, source)
|
||||
concept_found = results[0].value.value
|
||||
|
||||
assert len(results) == 1
|
||||
assert results[0].status
|
||||
assert concept_found == CMV(isa, c="z")
|
||||
assert concept_found.metadata.need_validation
|
||||
assert not concept_found.metadata.is_evaluated
|
||||
|
||||
source = "def concept z"
|
||||
results = ExactConceptParser().parse(context, source)
|
||||
concept_found = results[0].value.value
|
||||
|
||||
assert len(results) == 1
|
||||
assert results[0].status
|
||||
assert concept_found == CMV(def_concept, a="z")
|
||||
assert concept_found.metadata.need_validation
|
||||
assert not concept_found.metadata.is_evaluated
|
||||
|
||||
def test_i_can_manage_unknown_concept(self):
|
||||
context = self.get_context(self.get_sheerka(singleton=True))
|
||||
source = "def concept hello" # this is not a concept by itself
|
||||
|
||||
@@ -34,6 +34,7 @@ cmap = {
|
||||
"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"),
|
||||
"is a concept": Concept("c is a concept").def_var("c"),
|
||||
}
|
||||
|
||||
|
||||
@@ -1013,6 +1014,8 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
|
||||
]),
|
||||
("function(suffixed x$!#)", False, [
|
||||
SCWC("function(", ")", CNC("suffixed", 2, 7, a="x$!#"))]),
|
||||
("one is a concept", True, [CNC("is a concept", c="one")]),
|
||||
("a is a concept", False, [CNC("is a concept", c=UTN("a"))]),
|
||||
])
|
||||
def test_i_can_parse_when_one_result(self, text, expected_status, expected_result):
|
||||
sheerka, context, parser = self.init_parser()
|
||||
@@ -1142,3 +1145,4 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
|
||||
|
||||
assert not res.status
|
||||
assert sheerka.isinstance(res.body, BuiltinConcepts.IS_EMPTY)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user