Fixed #18 : Parsing and evaluating Python

This commit is contained in:
2023-05-14 12:12:29 +02:00
parent e41094f908
commit 09a0246420
46 changed files with 2084 additions and 165 deletions
+13 -3
View File
@@ -1,3 +1,4 @@
from common.utils import get_text_from_tokens
from parsers.tokenizer import Tokenizer
@@ -5,15 +6,24 @@ class ParserInput:
def __init__(self, text, yield_oef=True):
self.original_text = text
self.yield_oef = yield_oef
self.tokens = None
self.all_tokens = None
self.exception = None
def init(self) -> bool:
try:
# the eof if forced, but will not be yield if not set to.
self.tokens = list(Tokenizer(self.original_text, yield_eof=True))
self.all_tokens = list(Tokenizer(self.original_text, yield_eof=True))
return True
except Exception as ex:
self.tokens = None
self.all_tokens = None
self.exception = ex
return False
def as_text(self, custom_switcher=None, tracker=None):
if self.all_tokens is None:
raise Exception("You must call init() first !")
return get_text_from_tokens(self.all_tokens, custom_switcher, tracker)
def __repr__(self):
return f"ParserInput('{self.original_text}', len={len(self.all_tokens)})"