Compare commits

..

No commits in common. "aa8d03d29648646e29716988a97b5d79daf85462" and "44f88345a2d3ac237ec19caef5324a56438ab4cc" have entirely different histories.

2 changed files with 30 additions and 107 deletions

View File

@ -1,7 +1,6 @@
from dataclasses import dataclass from dataclasses import dataclass
from enum import auto, Enum from enum import auto, Enum
from typing import Any from typing import Any
import re
class Datatype(Enum): class Datatype(Enum):
@ -41,15 +40,11 @@ class Variable:
NONE_TYPE = Variable(None, Datatype.NONE, None) NONE_TYPE = Variable(None, Datatype.NONE, None)
STR_MATCH_BEGIN = re.compile(r"^\"")
STR_MATCH_END = re.compile(r"\"$")
VAR_ASSIGN_TOKEN = re.compile(r"[A-Za-z]+ ?= ?")
VAR_MATCH = { VAR_MATCH = {
re.compile(r"null"): Datatype.NULL, r"null": Datatype.NULL,
re.compile(r"\/\/.+"): Datatype.COMMENT, r"\/\/.+": Datatype.COMMENT,
re.compile(r"\".+\""): Datatype.STRING, r"\".+\"": Datatype.STRING,
re.compile(r"-?\d+\.?\d{0,}?\+\d+\.?\d{0,}?i"): Datatype.COMPLEX, r"-?\d+\.?\d{0,}?\+\d+\.?\d{0,}?i": Datatype.COMPLEX,
re.compile(r"-?\d+\.\d{0,}"): Datatype.FLOAT, r"-?\d+\.\d{0,}": Datatype.FLOAT,
re.compile(r"-?\d+"): Datatype.INTEGER, r"-?\d+": Datatype.INTEGER,
} }

120
pyne.py
View File

@ -4,107 +4,45 @@ from decorators import deprecated, todo, wip
from enum import Enum from enum import Enum
from pyne_errors import ParserError, FileError from pyne_errors import ParserError, FileError
import datatypes import datatypes
import os
import re import re
import sys import sys
def debugprint(output):
if "PYNE_DEBUG" in os.environ:
print(output)
@dataclass @dataclass
class PyneObject: class PyneObject:
value: datatypes.Variable value: datatypes.Variable
VAR_STORE: list[PyneObject] = []
class Pyne: class Pyne:
def __init__(self, path: str, args: list[str]): def __init__(self, path: str, args: list[str]):
self.args = args self.args = args
self.path = path if self._validate(path) else None self.path = path if self._validate(path) else None
@wip @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]: def parse(self) -> list[PyneObject]:
# VAR_STORE: list[PyneObject] = [] objects: list[PyneObject] = []
col = 1
is_str_begin = False
with open(self.path) as f: with open(self.path) as f:
for row, line in enumerate(f.readlines(), 1): for row, line in enumerate(f.readlines(), 1):
varname = "" for var in datatypes.VAR_MATCH:
strbuf = "" regex = re.compile(var)
debugprint(line) res = regex.match(line[:-1])
if "=" in line: if res:
debugprint("if '=' in line") objects.append(
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( PyneObject(
datatypes.Variable(varname, datatypes.STRING, strbuf) datatypes.Variable(
) None, datatypes.VAR_MATCH[var], line[:-1]
)
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) break
debugprint(f"{VAR_STORE=}") continue
# for var in datatypes.VAR_MATCH: else:
# res = var.match(line[:-1]) raise ParserError(f"[{self.path}:{row}] Token {line} not found.")
# if res: if len(objects) == 0:
# VAR_STORE.append( objects.append(datatypes.NONE_TYPE)
# PyneObject( return objects
# 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: def _validate(self, path: str) -> bool:
""" """
@ -121,23 +59,13 @@ def main() -> int:
parser.add_argument("file", nargs=1, help="The input file") parser.add_argument("file", nargs=1, help="The input file")
args = parser.parse_args() args = parser.parse_args()
print(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) pyne = Pyne(args.file[0], None)
for obj in pyne.parse(): try:
print(obj) for obj in pyne.parse():
return 0 print(obj)
return 0
except ParserError as e:
print(f"Found invalid token: {e}")
if __name__ == "__main__": if __name__ == "__main__":