Skip to content

Commit

Permalink
🏗️ Move token to parser
Browse files Browse the repository at this point in the history
  • Loading branch information
H2Sxxa committed Sep 17, 2024
1 parent bba48be commit c965a45
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 71 deletions.
2 changes: 2 additions & 0 deletions src/saleyo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
69 changes: 1 addition & 68 deletions src/saleyo/operation/processor.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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}
"""
1 change: 1 addition & 0 deletions src/saleyo/parser/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .token import Token as Token
68 changes: 68 additions & 0 deletions src/saleyo/parser/token.py
Original file line number Diff line number Diff line change
@@ -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}
"""
4 changes: 1 addition & 3 deletions tests/processor.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit c965a45

Please sign in to comment.