From 01809b2b3547ac474585016a1a8b43af18956322 Mon Sep 17 00:00:00 2001 From: ilonachan Date: Tue, 1 Oct 2024 07:29:19 +0200 Subject: [PATCH] wip --- formats/gds/cmddef.py | 4 ++-- formats/gds/gda.py | 42 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/formats/gds/cmddef.py b/formats/gds/cmddef.py index 7cb5483..7726284 100755 --- a/formats/gds/cmddef.py +++ b/formats/gds/cmddef.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -from typing import Optional, List, Union, Set +from typing import Optional, List, Union, Set, Any, Mapping import os from dataclasses import dataclass, field @@ -395,4 +395,4 @@ def init_commands(root: str = None): pprint.pp(COMMANDS_BYID) pprint.pp(COMMANDS_BYNAME) else: - init_commands() \ No newline at end of file + init_commands() diff --git a/formats/gds/gda.py b/formats/gds/gda.py index fa92f3a..0b3c0b3 100644 --- a/formats/gds/gda.py +++ b/formats/gds/gda.py @@ -7,6 +7,9 @@ from typing import Optional, List from dataclasses import dataclass + +from utils import round_perfect +from .cmddef import GDSCommand from .model import ( GDSProgram, GDSElement, @@ -18,8 +21,6 @@ GDSConditionToken ) -from utils import round_perfect - def read_gda(data: str, path: Optional[str] = None) -> GDSProgram: pass @@ -36,9 +37,10 @@ def nl(self): self.result += '\n' + ' '*self.indent def insert_comment(self, comment: str): self.result, latest_line = self.result.rsplit("\n", 1) - self.nl() - self.write(f"# {comment}\n") - self.write(latest_line) + for line in comment.splitlines(): + self.nl() + self.write(f"# {line}") + self.write("\n"+latest_line) def write_gda(prog: GDSProgram, filename: Optional[str] = None) -> str: @@ -185,3 +187,33 @@ def write_while(ctx: WriterContext, cmd: GDSLoopInvocation): "repeatN": write_repeatN, "while": write_while, } + + + +def format_comment(comment: str, filename: str, args: List[GDSValue], cmd: GDSCommand): + from parsy import forward_declaration, regex, seq, string, alt + + str_part = regex(r'[^$]') + str_part_fvar = regex(r'[^$}:]') + str_part_expr = regex(r'[^$)]') + fvar_esc = string(r'}}') | string(r'::') + expr_esc = string(r'))') + + format_args = regex(r"0\d+") + fvar = forward_declaration() + expr = forward_declaration() + str_var = string('$') >> ( + string('{') >> fvar << string('}') + | string('(') >> expr << string(')') + ) + + varname = regex(r"\w+") + + fvar_expr = (str_part_fvar | fvar_esc | str_var).many().concat() + fvar.become( seq ( (varname) , string(":") >> format_args | None ) ) + expr.become( (str_part_expr | expr_esc | str_var).many().concat() ) + + str_ = (str_part | str_var).many().concat() + + # TODO: parse variables like $TEST or ${1:03} and $(filepath) + pass \ No newline at end of file