This commit is contained in:
2025-07-17 22:35:07 +02:00
parent f482c8db87
commit e2ba07c8b4
2 changed files with 59 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
from lark import Lark, Transformer, Token, v_args
from pathlib import Path
GRAMMAR_FILE = Path(__file__).parent.joinpath("tcl.lark")
TCL_GRAMMAR = GRAMMAR_FILE.read_text(encoding="utf-8")
@v_args(inline=True)
class TCLTransformer(Transformer):
def start(self, *stmts):
return list(stmts)
def command(self, *elements):
return list(elements)
def braced(self, *content):
return "{" + "".join(c for c in content) + "}"
def quoted(self, *content):
return '"' + "".join(c for c in content) + '"'
def variable(self, token):
return token.value
def cmdsubst(self, *inner):
return "[" + " ".join(inner) + "]"
def bare(self, token):
return token.value
def COMMENT(self, token):
return token.value
+27
View File
@@ -0,0 +1,27 @@
start: statement*
?statement: command | COMMENT
COMMENT: /#[^\n]*/
command: element+
?element: braced
| quoted
| variable
| cmdsubst
| bare
braced: "{" inner_braced* "}"
inner_braced: /[^{}]+/ | braced
quoted: '"' inner_quoted* '"'
inner_quoted: /[^"{}]+/ | braced
variable: "$" /[A-Za-z_][A-Za-z0-9_]*/
cmdsubst: "[" start "]"
bare: /[^\s{}$"]+/
%import common.WS_INLINE
%ignore WS_INLINE