from argparse import ArgumentParser from dataclasses import dataclass from decorators import deprecated, todo, wip from enum import Enum from pyne_errors import ParserError, FileError import datatypes import re import sys @dataclass class PyneObject: value: datatypes.Variable 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" ) def parse(self) -> list[PyneObject]: objects: list[PyneObject] = [] 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) if res: objects.append( PyneObject( datatypes.Variable(None, datatypes.VAR_MATCH[var], line) ) ) break continue else: raise ParserError(f"[{self.path}:{row}] Token {line} not found.") if len(objects) == 0: objects.append(datatypes.NONE_TYPE) return objects def _validate(self, path: str) -> bool: """ Validates the path and checks if it exists or not. """ with open(path) as f: f.seek(1) return True @todo("Project is a work in progress") def main() -> int: parser = ArgumentParser(description="A simple language written in Python") parser.add_argument("file", nargs=1, help="The input file") args = parser.parse_args() print(args) 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}") if __name__ == "__main__": raise SystemExit(main())