Introduced ParserInput
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
import logging
|
||||
|
||||
import core.builtin_helpers
|
||||
from core.builtin_concepts import ReturnValueConcept, BuiltinConcepts
|
||||
from core.concept import VARIABLE_PREFIX
|
||||
from core.sheerka.services.SheerkaExecute import ParserInput
|
||||
from core.tokenizer import Keywords, TokenKind, LexerError
|
||||
from core.utils import str_concept
|
||||
from parsers.BaseParser import BaseParser
|
||||
import core.builtin_helpers
|
||||
|
||||
|
||||
class ExactConceptParser(BaseParser):
|
||||
@@ -19,7 +20,7 @@ class ExactConceptParser(BaseParser):
|
||||
BaseParser.__init__(self, "ExactConcept", 80)
|
||||
self.max_word_size = max_word_size
|
||||
|
||||
def parse(self, context, parser_input):
|
||||
def parse(self, context, parser_input: ParserInput):
|
||||
"""
|
||||
text can be string, but text can also be an list of tokens
|
||||
:param context:
|
||||
@@ -31,6 +32,7 @@ class ExactConceptParser(BaseParser):
|
||||
sheerka = context.sheerka
|
||||
|
||||
try:
|
||||
parser_input.reset()
|
||||
words = self.get_words(parser_input)
|
||||
except LexerError as e:
|
||||
context.log(f"Error found in tokenizer {e}", self.name)
|
||||
@@ -38,8 +40,8 @@ class ExactConceptParser(BaseParser):
|
||||
|
||||
if len(words) > (self.max_word_size or self.MAX_WORDS_SIZE):
|
||||
context.log(f"Max words reached. Stopping.", self.name)
|
||||
too_long = sheerka.new(BuiltinConcepts.CONCEPT_TOO_LONG, body=parser_input)
|
||||
body = sheerka.new(BuiltinConcepts.NOT_FOR_ME, body=parser_input, reason=too_long)
|
||||
too_long = sheerka.new(BuiltinConcepts.CONCEPT_TOO_LONG, body=parser_input.as_text())
|
||||
body = sheerka.new(BuiltinConcepts.NOT_FOR_ME, body=parser_input.as_text(), reason=too_long)
|
||||
return sheerka.ret(self.name, False, body)
|
||||
|
||||
already_recognized = [] # keep track of the concepts founds
|
||||
@@ -78,12 +80,13 @@ class ExactConceptParser(BaseParser):
|
||||
|
||||
already_recognized.append(concept)
|
||||
|
||||
by_name = sheerka.resolve(self.get_input_as_text(parser_input))
|
||||
by_name = sheerka.resolve(parser_input.as_text())
|
||||
core.builtin_helpers.set_is_evaluated(by_name)
|
||||
recognized = self.merge_concepts(already_recognized, by_name)
|
||||
|
||||
if len(recognized) == 0:
|
||||
ret = sheerka.ret(self.name, False, sheerka.new(BuiltinConcepts.UNKNOWN_CONCEPT, body=parser_input))
|
||||
ret = sheerka.ret(self.name, False, sheerka.new(BuiltinConcepts.UNKNOWN_CONCEPT,
|
||||
body=parser_input.as_text()))
|
||||
self.log_result(context, parser_input, ret)
|
||||
return ret
|
||||
else:
|
||||
@@ -94,10 +97,10 @@ class ExactConceptParser(BaseParser):
|
||||
self.log_multiple_results(context, parser_input, res)
|
||||
return res
|
||||
|
||||
def get_words(self, text):
|
||||
tokens = self.get_input_as_tokens(text)
|
||||
@staticmethod
|
||||
def get_words(parser_input):
|
||||
res = []
|
||||
for t in tokens:
|
||||
for t in parser_input.as_tokens():
|
||||
if t.type == TokenKind.EOF:
|
||||
break
|
||||
if t.type == TokenKind.NEWLINE or t.type == TokenKind.WHITESPACE:
|
||||
@@ -173,6 +176,6 @@ class ExactConceptParser(BaseParser):
|
||||
context.sheerka.new(
|
||||
BuiltinConcepts.PARSER_RESULT,
|
||||
parser=self,
|
||||
source=parser_input if isinstance(parser_input, str) else self.get_text_from_tokens(parser_input),
|
||||
source=parser_input.as_text(),
|
||||
body=concept,
|
||||
try_parsed=concept))
|
||||
|
||||
Reference in New Issue
Block a user