use lark
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user