Introduced ParserInput
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import ast
|
||||
from dataclasses import dataclass
|
||||
|
||||
import pytest
|
||||
from core.builtin_concepts import ParserResultConcept, BuiltinConcepts, ReturnValueConcept
|
||||
from core.concept import DEFINITION_TYPE_BNF, DEFINITION_TYPE_DEF
|
||||
from core.sheerka.services.SheerkaExecute import ParserInput
|
||||
from core.tokenizer import Keywords, Tokenizer, LexerError
|
||||
from parsers.BnfNodeParser import OrderedChoice, ConceptExpression, StrMatch
|
||||
from parsers.BnfParser import BnfParser
|
||||
@@ -39,7 +41,7 @@ def get_def_concept(name, where=None, pre=None, post=None, body=None, definition
|
||||
|
||||
def get_concept_part(part):
|
||||
if isinstance(part, str):
|
||||
node = PythonNode(part, ast.parse(part, mode="eval"))
|
||||
node = PythonNode(part.strip(), ast.parse(part.strip(), mode="eval"))
|
||||
return ReturnValueConcept(
|
||||
who="parsers.Default",
|
||||
status=True,
|
||||
@@ -48,6 +50,16 @@ def get_concept_part(part):
|
||||
parser=PythonParser(),
|
||||
value=node))
|
||||
|
||||
if isinstance(part, PN):
|
||||
node = PythonNode(part.source.strip(), ast.parse(part.source.strip(), mode=part.mode))
|
||||
return ReturnValueConcept(
|
||||
who="parsers.Default",
|
||||
status=True,
|
||||
value=ParserResultConcept(
|
||||
source=part.source,
|
||||
parser=PythonParser(),
|
||||
value=node))
|
||||
|
||||
if isinstance(part, PythonNode):
|
||||
return ReturnValueConcept(
|
||||
who="parsers.Default",
|
||||
@@ -61,6 +73,12 @@ def get_concept_part(part):
|
||||
return part
|
||||
|
||||
|
||||
@dataclass
|
||||
class PN:
|
||||
source: str # parser result source
|
||||
mode: str # compilation mode
|
||||
|
||||
|
||||
class TestDefaultParser(TestUsingMemoryBasedSheerka):
|
||||
|
||||
def init_parser(self, *concepts):
|
||||
@@ -82,7 +100,7 @@ class TestDefaultParser(TestUsingMemoryBasedSheerka):
|
||||
])
|
||||
def test_i_can_parse_def_concept(self, text, expected):
|
||||
sheerka, context, parser = self.init_parser()
|
||||
res = parser.parse(context, text)
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
node = res.value.value
|
||||
|
||||
assert res.status
|
||||
@@ -99,14 +117,14 @@ post isinstance(res, int)
|
||||
as res = a + b
|
||||
"""
|
||||
sheerka, context, parser = self.init_parser()
|
||||
res = parser.parse(context, text)
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
return_value = res.value
|
||||
expected_concept = get_def_concept(
|
||||
name="a plus b",
|
||||
where="a,b",
|
||||
pre="isinstance(a, int) and isinstance(b, float)",
|
||||
post="isinstance(res, int)",
|
||||
body=PythonNode("res = a + b", ast.parse("res = a + b", mode="exec"))
|
||||
where="a,b\n",
|
||||
pre="isinstance(a, int) and isinstance(b, float)\n",
|
||||
post="isinstance(res, int)\n",
|
||||
body=PN("res = a + b\n ", "exec")
|
||||
)
|
||||
|
||||
assert res.status
|
||||
@@ -119,17 +137,15 @@ def concept add one to a as
|
||||
def func(x):
|
||||
return x+1
|
||||
func(a)
|
||||
"""
|
||||
"""
|
||||
|
||||
expected_concept = get_def_concept(
|
||||
name="add one to a ",
|
||||
body=PythonNode(
|
||||
"def func(x):\n return x+1\nfunc(a)",
|
||||
ast.parse("def func(x):\n return x+1\nfunc(a)", mode="exec"))
|
||||
body=PN("def func(x):\n return x+1\nfunc(a)\n", "exec")
|
||||
)
|
||||
|
||||
sheerka, context, parser = self.init_parser()
|
||||
res = parser.parse(context, text)
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
return_value = res.value
|
||||
|
||||
assert res.status
|
||||
@@ -141,8 +157,7 @@ func(a)
|
||||
def concept add one to a as:
|
||||
def func(x):
|
||||
return x+1
|
||||
func(a)
|
||||
"""
|
||||
func(a)"""
|
||||
|
||||
expected_concept = get_def_concept(
|
||||
name="add one to a ",
|
||||
@@ -152,7 +167,7 @@ def concept add one to a as:
|
||||
)
|
||||
|
||||
sheerka, context, parser = self.init_parser()
|
||||
res = parser.parse(context, text)
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
return_value = res.value
|
||||
|
||||
assert res.status
|
||||
@@ -168,7 +183,7 @@ func(a)
|
||||
"""
|
||||
|
||||
sheerka, context, parser = self.init_parser()
|
||||
res = parser.parse(context, text)
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
return_value = res.value
|
||||
|
||||
assert not res.status
|
||||
@@ -184,7 +199,7 @@ def concept add one to a as
|
||||
func(a)
|
||||
"""
|
||||
sheerka, context, parser = self.init_parser()
|
||||
res = parser.parse(context, text)
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
return_value = res.value
|
||||
|
||||
assert not res.status
|
||||
@@ -194,7 +209,7 @@ def concept add one to a as
|
||||
text = "def concept as 'hello'"
|
||||
|
||||
sheerka, context, parser = self.init_parser()
|
||||
res = parser.parse(context, text)
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
return_value = res.value
|
||||
|
||||
assert not res.status
|
||||
@@ -205,9 +220,9 @@ def concept add one to a as
|
||||
def test_concept_keyword_is_mandatory_but_the_concept_is_recognized(self):
|
||||
text = "def hello as a where b pre c post d"
|
||||
|
||||
expected_concept = get_def_concept(name="hello", body="a", where="b", pre="c", post="d")
|
||||
expected_concept = get_def_concept(name="hello", body="a ", where="b ", pre="c ", post="d")
|
||||
sheerka, context, parser = self.init_parser()
|
||||
res = parser.parse(context, text)
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
return_value = res.value
|
||||
|
||||
assert not res.status
|
||||
@@ -225,7 +240,7 @@ def concept add one to a as
|
||||
])
|
||||
def test_i_can_detect_error_in_declaration(self, text):
|
||||
sheerka, context, parser = self.init_parser()
|
||||
res = parser.parse(context, text)
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
return_value = res.value
|
||||
|
||||
assert not res.status
|
||||
@@ -235,7 +250,7 @@ def concept add one to a as
|
||||
text = "def concept hello \n my friend as 'hello'"
|
||||
|
||||
sheerka, context, parser = self.init_parser()
|
||||
res = parser.parse(context, text)
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
return_value = res.value
|
||||
|
||||
assert not res.status
|
||||
@@ -244,7 +259,7 @@ def concept add one to a as
|
||||
def test_i_can_parse_def_concept_from_bnf(self):
|
||||
text = "def concept name from bnf a_concept | 'a_string' as __definition[0]"
|
||||
sheerka, context, parser, a_concept = self.init_parser("a_concept")
|
||||
res = parser.parse(context, text)
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
|
||||
node = res.value.value
|
||||
definition = OrderedChoice(ConceptExpression(a_concept, rule_name="a_concept"), StrMatch("a_string"))
|
||||
@@ -260,7 +275,7 @@ def concept add one to a as
|
||||
def test_i_can_parse_def_concept_where_bnf_references_itself(self):
|
||||
text = "def concept name from bnf 'a' + name?"
|
||||
sheerka, context, parser, a_concept = self.init_parser("a_concept")
|
||||
parser.parse(context, text)
|
||||
parser.parse(context, ParserInput(text))
|
||||
|
||||
assert not parser.has_error
|
||||
|
||||
@@ -271,7 +286,7 @@ def concept add one to a as
|
||||
])
|
||||
def test_i_can_detect_empty_bnf_declaration(self, text):
|
||||
sheerka, context, parser = self.init_parser()
|
||||
res = parser.parse(context, text)
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
|
||||
assert not res.status
|
||||
assert res.value.value[0] == SyntaxErrorNode([], "Empty declaration")
|
||||
@@ -281,7 +296,7 @@ def concept add one to a as
|
||||
"def concept addition from def a plus b as a + b"])
|
||||
def test_i_can_def_concept_from_definition(self, text):
|
||||
sheerka, context, parser = self.init_parser()
|
||||
res = parser.parse(context, text)
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
expected = get_def_concept("addition", definition="a plus b", body="a + b")
|
||||
node = res.value.value
|
||||
|
||||
@@ -294,7 +309,7 @@ def concept add one to a as
|
||||
def test_i_can_detect_not_for_me(self):
|
||||
text = "hello world"
|
||||
sheerka, context, parser = self.init_parser()
|
||||
res = parser.parse(context, text)
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
|
||||
assert not res.status
|
||||
assert context.sheerka.isinstance(res.value, BuiltinConcepts.NOT_FOR_ME)
|
||||
@@ -303,7 +318,7 @@ def concept add one to a as
|
||||
def test_i_can_parse_is_a(self):
|
||||
text = "the name of my 'concept' isa the name of the set"
|
||||
sheerka, context, parser = self.init_parser()
|
||||
res = parser.parse(context, text)
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
expected = IsaConceptNode([],
|
||||
concept=NameNode(list(Tokenizer("the name of my 'concept'"))),
|
||||
set=NameNode(list(Tokenizer("the name of the set"))))
|
||||
@@ -323,7 +338,7 @@ def concept add one to a as
|
||||
])
|
||||
def test_i_cannot_parse_invalid_entries(self, text):
|
||||
sheerka, context, parser = self.init_parser()
|
||||
res = parser.parse(context, text)
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
|
||||
assert not res.status
|
||||
assert isinstance(res.body, ParserResultConcept)
|
||||
@@ -339,10 +354,10 @@ def concept add one to a as
|
||||
])
|
||||
def test_i_cannot_parse_when_tokenizer_fails(self, text, error_msg, error_text):
|
||||
sheerka, context, parser = self.init_parser()
|
||||
res = parser.parse(context, text)
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
|
||||
assert not res.status
|
||||
assert isinstance(res.body, ParserResultConcept)
|
||||
assert sheerka.isinstance(res.body, BuiltinConcepts.ERROR)
|
||||
assert isinstance(res.body.body[0], LexerError)
|
||||
assert res.body.body[0].message == error_msg
|
||||
assert res.body.body[0].text == error_text
|
||||
@@ -350,7 +365,7 @@ def concept add one to a as
|
||||
def test_i_cannot_parse_bnf_definition_referencing_unknown_concept(self):
|
||||
text = "def concept name from bnf unknown"
|
||||
sheerka, context, parser = self.init_parser()
|
||||
res = parser.parse(context, text)
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
|
||||
assert not res.status
|
||||
assert context.sheerka.isinstance(res.value, BuiltinConcepts.UNKNOWN_CONCEPT)
|
||||
|
||||
Reference in New Issue
Block a user