Improved PythonEvaluator when dealing with concept class

This commit is contained in:
2020-05-20 04:19:19 +02:00
parent 95dc147bbd
commit d357329f51
16 changed files with 288 additions and 89 deletions
+20 -4
View File
@@ -141,15 +141,22 @@ class BaseParser:
body=self.error_sink if self.has_error else tree,
try_parsed=try_parse)
def get_input_as_text(self, parser_input, custom_switcher=None):
def get_input_as_text(self, parser_input, custom_switcher=None, tracker=None):
"""
Recreate back the source code from parser_input
:param parser_input: list of Tokens
:param custom_switcher: map of [TokenKind, overridden values]
:param tracker: keep track of the value overridden by custom_switcher
:return:
"""
if isinstance(parser_input, list):
return self.get_text_from_tokens(parser_input, custom_switcher)
return self.get_text_from_tokens(parser_input, custom_switcher, tracker)
if isinstance(parser_input, ParserResultConcept):
parser_input = parser_input.source
if "c:" in parser_input:
return self.get_text_from_tokens(list(Tokenizer(parser_input)), custom_switcher)
return self.get_text_from_tokens(list(Tokenizer(parser_input)), custom_switcher, tracker)
return parser_input
@@ -194,7 +201,14 @@ class BaseParser:
return lst
@staticmethod
def get_text_from_tokens(tokens, custom_switcher=None):
def get_text_from_tokens(tokens, custom_switcher=None, tracker=None):
"""
Create the source code, from the list of token
:param tokens: list of tokens
:param custom_switcher: to override the behaviour (the return value) of some token
:param tracker: keep track of the original token value when custom switched
:return:
"""
if tokens is None:
return ""
res = ""
@@ -213,6 +227,8 @@ class BaseParser:
for token in tokens:
value = switcher.get(token.type, lambda t: t.value)(token)
res += value
if tracker is not None and token.type in custom_switcher:
tracker[value] = token.value
return res
@staticmethod