Skip to content

Instantly share code, notes, and snippets.

@kripken
Created May 28, 2026 20:06
Show Gist options
  • Select an option

  • Save kripken/4488d732f81feb0a276e0f2a9f7538b6 to your computer and use it in GitHub Desktop.

Select an option

Save kripken/4488d732f81feb0a276e0f2a9f7538b6 to your computer and use it in GitHub Desktop.
s-to-json
import json
import re
import sys
def tokenize(s_expr):
"""
Extracts tokens from an S-expression string.
Matches:
1. Strings enclosed in double quotes (handling escaped characters)
2. Parentheses '(' and ')'
3. Any other contiguous sequence of non-whitespace, non-parenthesis characters
"""
token_pattern = r'"(?:\\.|[^"\\])*"|[()]|[^\s()]+'
return re.findall(token_pattern, s_expr)
def parse(tokens):
"""
Parses a list of tokens into a nested Python list structure.
"""
stack = [[]]
for token in tokens:
if token == '(':
new_list = []
stack[-1].append(new_list)
stack.append(new_list)
elif token == ')':
if len(stack) > 1:
stack.pop()
else:
raise ValueError("Parsing error: Too many closing parentheses.")
else:
stack[-1].append(token)
if len(stack) != 1:
raise ValueError("Parsing error: Missing closing parentheses.")
return stack[0][0] if stack[0] else []
def s_expr_to_json(s_expr, indent=2):
"""
Converts a raw S-expression string to a JSON string.
"""
tokens = tokenize(s_expr)
parsed_data = parse(tokens)
return json.dumps(parsed_data, indent=indent)
# Main
wat_content = open(sys.argv[1]).read()
json_output = s_expr_to_json(wat_content)
print(json_output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment