From aa8d03d29648646e29716988a97b5d79daf85462 Mon Sep 17 00:00:00 2001 From: Ruben Dahl Date: Sun, 9 Apr 2023 20:13:03 +0200 Subject: [PATCH] Started tokenizing, still struggling with strings --- pyne.py | 122 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 97 insertions(+), 25 deletions(-) diff --git a/pyne.py b/pyne.py index 2c4852b..5fb148a 100644 --- a/pyne.py +++ b/pyne.py @@ -4,45 +4,107 @@ from decorators import deprecated, todo, wip from enum import Enum from pyne_errors import ParserError, FileError import datatypes +import os import re import sys +def debugprint(output): + if "PYNE_DEBUG" in os.environ: + print(output) + + @dataclass class PyneObject: value: datatypes.Variable +VAR_STORE: list[PyneObject] = [] + + class Pyne: def __init__(self, path: str, args: list[str]): self.args = args self.path = path if self._validate(path) else None - @todo( - "Tokenize the parser so it doesn't try to match the whole line, gotta find a way for it to do the strings though" - ) + @wip def parse(self) -> list[PyneObject]: - objects: list[PyneObject] = [] + # VAR_STORE: list[PyneObject] = [] + col = 1 + is_str_begin = False with open(self.path) as f: for row, line in enumerate(f.readlines(), 1): - for var in datatypes.VAR_MATCH: - regex = re.compile(var) - res = regex.match(line[:-1]) - if res: - objects.append( + varname = "" + strbuf = "" + debugprint(line) + if "=" in line: + debugprint("if '=' in line") + varname = line.split("=")[0].split(" ")[0] + debugprint(f"{varname = }") + col += len(varname) + line.find("=") + splitline = ( + line[line.find("=") + 1 if varname != "" else 0 :] + .strip() + .split(" ") + ) + debugprint(f"{splitline = }") + for token in splitline: + debugprint(f"{token = }") + if is_str_begin: + debugprint("is_str_begin = true") + strbuf += token + if datatypes.STR_MATCH_BEGIN.match(token): + debugprint("Matched opening quote") + is_str_begin = True + strbuf += token + if datatypes.STR_MATCH_END.match(token): + debugprint("Matched end quote") + is_str_begin = False + VAR_STORE.append( PyneObject( - datatypes.Variable( - None, datatypes.VAR_MATCH[var], line[:-1] - ) + datatypes.Variable(varname, datatypes.STRING, strbuf) ) ) - break - continue - else: - raise ParserError(f"[{self.path}:{row}] Token {line} not found.") - if len(objects) == 0: - objects.append(datatypes.NONE_TYPE) - return objects + for var in datatypes.VAR_MATCH: + if token == varname: + break + if token == "": + break + if is_str_begin: + break + res = var.match(token) + if res: + VAR_STORE.append( + PyneObject( + datatypes.Variable( + varname, datatypes.VAR_MATCH[var], token + ) + ) + ) + break + else: + raise ParserError( + f"{self.path}:{row}:{col}: Invalid token `{token}`" + ) + col += len(token) + debugprint(f"{VAR_STORE=}") + # for var in datatypes.VAR_MATCH: + # res = var.match(line[:-1]) + # if res: + # VAR_STORE.append( + # PyneObject( + # datatypes.Variable( + # None, datatypes.VAR_MATCH[var], line[:-1] + # ) + # ) + # ) + # break + # continue + # else: + # raise ParserError( + # f"[{self.path}:{row}] Token `{line[:-1]}` not found." + # ) + return VAR_STORE def _validate(self, path: str) -> bool: """ @@ -59,13 +121,23 @@ def main() -> int: parser.add_argument("file", nargs=1, help="The input file") args = parser.parse_args() print(args) + # obj = PyneObject( + # datatypes.Variable( + # "test", + # datatypes.Datatype.STRING, + # datatypes.Data(datatypes.Datatype.STRING, '"Hello World"'), + # ) + # ) + # # dt = datatypes.Data(datatypes.Datatype.INTEGER, 24) + # # obj.value.value = dt + # dt = datatypes.Data(datatypes.Datatype.STRING, '"Goodbye World"') + # obj.value.value = dt + # print(obj.value.value.type, obj.value.value.value) + # return 0 pyne = Pyne(args.file[0], None) - try: - for obj in pyne.parse(): - print(obj) - return 0 - except ParserError as e: - print(f"Found invalid token: {e}") + for obj in pyne.parse(): + print(obj) + return 0 if __name__ == "__main__":