Skip to content

Instantly share code, notes, and snippets.

@patham9
Created July 1, 2026 08:54
Show Gist options
  • Select an option

  • Save patham9/d7fa9fb8739eee651f763ac84d9fec55 to your computer and use it in GitHub Desktop.

Select an option

Save patham9/d7fa9fb8739eee651f763ac84d9fec55 to your computer and use it in GitHub Desktop.
Multi-line command argument parsing of MeTTaClaw
import json
LLM_COMMANDS = {
"pin",
"remember",
"query",
"episodes",
"search",
"send",
"promote",
"demote",
"metta",
"shell",
"read-file",
"write-file",
"append-file",
}
def quote_arg(x):
return json.dumps(x, ensure_ascii=False)
def starts_command_line(line):
s = line.lstrip()
if not s:
return False
# allow "(send ...)" as command start too
if s.startswith("("):
s = s[1:].lstrip()
if not s:
return False
first = s.split(maxsplit=1)[0].rstrip(")")
return first in LLM_COMMANDS
def split_command_blocks(s):
blocks = []
cur = []
for raw in s.splitlines():
if not raw.strip():
if cur:
cur.append(raw)
continue
if starts_command_line(raw) and cur:
blocks.append("\n".join(cur).strip())
cur = [raw]
else:
cur.append(raw)
if cur:
blocks.append("\n".join(cur).strip())
return blocks
def balance_parentheses(s):
s = s.replace("_quote_", '"').replace("_newline_", "\n")
sexprs = []
special_two_arg_cmds = {"write-file", "append-file"}
for line in split_command_blocks(s):
line = line.strip()
if not line:
continue
if line.startswith("(-"):
line = "(pin -" + line[2:]
elif line.startswith("-"):
line = "pin " + line
# remove one outer (...) if present
if line.startswith("(") and line.endswith(")"):
line = line[1:-1].strip()
elif line.startswith("("):
line = line[1:].strip()
parts = line.split(maxsplit=1)
if not parts:
continue
cmd = parts[0]
rest = parts[1].strip() if len(parts) > 1 else ""
if cmd in special_two_arg_cmds:
if not rest:
sexprs.append(f"({cmd})")
continue
# filename is first token unless already quoted
if rest.startswith('"'):
end = 1
escaped = False
while end < len(rest):
ch = rest[end]
if ch == '"' and not escaped:
break
escaped = (ch == '\\' and not escaped)
if ch != '\\':
escaped = False
end += 1
if end < len(rest) and rest[end] == '"':
filename = rest[:end+1]
content = rest[end+1:].strip()
else:
filename = quote_arg(rest[1:])
content = ""
else:
split_rest = rest.split(maxsplit=1)
filename = quote_arg(split_rest[0])
content = split_rest[1].strip() if len(split_rest) > 1 else ""
if content:
if content.startswith('"') and content.endswith('"') and "\n" not in content:
sexprs.append(f"({cmd} {filename} {content})")
else:
sexprs.append(f"({cmd} {filename} {quote_arg(content)})")
else:
sexprs.append(f"({cmd} {filename})")
continue
if rest:
if rest.startswith('"') and rest.endswith('"') and "\n" not in rest:
sexprs.append(f"({cmd} {rest})")
else:
sexprs.append(f"({cmd} {quote_arg(rest)})")
else:
sexprs.append(f"({cmd})")
ret = " ".join(sexprs)
return "(" + ret + ")"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment