From c965a4525710b0af69fb408c8d6a29a757ec24e2 Mon Sep 17 00:00:00 2001 From: H2Sxxa Date: Tue, 17 Sep 2024 11:13:06 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=97=EF=B8=8F=20Move=20token=20to=20par?= =?UTF-8?q?ser?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/saleyo/__init__.py | 2 + src/saleyo/operation/processor.py | 69 +------------------------------ src/saleyo/parser/__init__.py | 1 + src/saleyo/parser/token.py | 68 ++++++++++++++++++++++++++++++ tests/processor.py | 4 +- 5 files changed, 73 insertions(+), 71 deletions(-) create mode 100644 src/saleyo/parser/__init__.py create mode 100644 src/saleyo/parser/token.py diff --git a/src/saleyo/__init__.py b/src/saleyo/__init__.py index 91c3664..a3bbdb9 100644 --- a/src/saleyo/__init__.py +++ b/src/saleyo/__init__.py @@ -24,6 +24,7 @@ from . import broadcast as broadcast from . import decorator as decorator from . import operation as operation +from . import parser as parser from .base.template import MixinOperation as MixinOperation from .base.toolchain import Arguments as Arguments from .base.toolchain import CPyToolChain as CPyToolChain @@ -47,3 +48,4 @@ from .operation import Pre as Pre from .operation import Processor as Processor from .operation import ReName as ReName +from .parser import Token as Token diff --git a/src/saleyo/operation/processor.py b/src/saleyo/operation/processor.py index def2d95..af2032a 100644 --- a/src/saleyo/operation/processor.py +++ b/src/saleyo/operation/processor.py @@ -1,6 +1,6 @@ from inspect import getsource from types import ModuleType -from typing import Callable, List, Optional +from typing import Callable, Optional from ..base.template import MixinOperation from ..base.toolchain import Container, DefaultToolChain, ToolChain @@ -84,70 +84,3 @@ def mixin( ), ), ) - - -class Token: - """Token is used to parse a function and modify it easily""" - - decorators: List[str] - declearation: List[str] - code: List[str] - source: str - minspace: str - - def __init__(self, source: str) -> None: - self.source = source - self.declearation = [] - self.decorators = [] - self.code = [] - self.parse() - - def build(self): - decorators = "\n".join(self.decorators) - declearation = "\n".join(self.declearation) - code = "\n".join(self.code) - return f"{decorators}\n{declearation}\n{code}" - - def parse(self): - sourcelines = self.source.splitlines() - first = sourcelines[0] - self.minspace = first.removesuffix(first.lstrip()) - - define_flag = False - - for raw in sourcelines: - line = raw.removeprefix(self.minspace) - - if line.startswith("@"): - self.decorators.append(line) - elif line.startswith("def") and len(self.declearation) == 0: - if line.rstrip()[-1] != ":": - define_flag = True - self.declearation.append(line) - elif define_flag: - self.declearation.append(line) - if line.rstrip()[-1] == ":": - define_flag = False - else: - self.code.append(line) - - def __str__(self): - decorators = "\n".join(self.decorators) - declearation = "\n".join(self.declearation) - code = "\n".join(self.code) - - return f""" -# Token - -## Decorator - -{decorators} - -## Declearation - -{declearation} - -## Code - -{code} - """ diff --git a/src/saleyo/parser/__init__.py b/src/saleyo/parser/__init__.py new file mode 100644 index 0000000..248ebf4 --- /dev/null +++ b/src/saleyo/parser/__init__.py @@ -0,0 +1 @@ +from .token import Token as Token diff --git a/src/saleyo/parser/token.py b/src/saleyo/parser/token.py new file mode 100644 index 0000000..05c77f2 --- /dev/null +++ b/src/saleyo/parser/token.py @@ -0,0 +1,68 @@ +from typing import List + + +class Token: + """Token is used to parse a function and modify it easily""" + + decorators: List[str] + declearation: List[str] + code: List[str] + source: str + minspace: str + + def __init__(self, source: str) -> None: + self.source = source + self.declearation = [] + self.decorators = [] + self.code = [] + self.parse() + + def build(self): + decorators = "\n".join(self.decorators) + declearation = "\n".join(self.declearation) + code = "\n".join(self.code) + return f"{decorators}\n{declearation}\n{code}" + + def parse(self): + sourcelines = self.source.splitlines() + first = sourcelines[0] + self.minspace = first.removesuffix(first.lstrip()) + + define_flag = False + + for raw in sourcelines: + line = raw.removeprefix(self.minspace) + + if line.startswith("@"): + self.decorators.append(line) + elif line.startswith("def") and len(self.declearation) == 0: + if line.rstrip()[-1] != ":": + define_flag = True + self.declearation.append(line) + elif define_flag: + self.declearation.append(line) + if line.rstrip()[-1] == ":": + define_flag = False + else: + self.code.append(line) + + def __str__(self): + decorators = "\n".join(self.decorators) + declearation = "\n".join(self.declearation) + code = "\n".join(self.code) + + return f""" +# Token + +## Decorator + +{decorators} + +## Declearation + +{declearation} + +## Code + +{code} + """ diff --git a/tests/processor.py b/tests/processor.py index 1247c77..6b33bed 100644 --- a/tests/processor.py +++ b/tests/processor.py @@ -1,6 +1,4 @@ -from saleyo import Processor -from saleyo.decorator.mixin import Mixin -from saleyo.operation.processor import Token +from saleyo import Mixin, Processor, Token class Target: