PParenWick
Workflows

Coding workflows · Restricted S-expression parsing

03
Built around your work

A concrete starting point

Parse one parenthesized S-expression of at most 4096 characters into lists and ASCII atom strings. Test empty lists, depth 16 and 1024 nodes, rejecting extra forms, unclosed lists and unsupported quote syntax without evaluating anything.

01

Open the context

Choose the project folder and name the files or behavior involved.

02

Define the change

Describe the input, expected output and boundaries that must stay intact.

03

Inspect the work

Review the proposed edits and any commands before relying on their result.

04

Check the outcome

Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.

Restricted S-expression parsing

A concrete starting point
import re

def read_sexpression(text):
    if type(text) is not str or len(text) > 4096:
        raise ValueError("sexpr.input")
    stack, result, count = [], None, 0
    for match in re.finditer(r"\(|\)|[^\s()]+", text):
        token = match[0]
        if token == "(":
            if len(stack) == 16 or (not stack and result is not None):
                raise ValueError("sexpr.depth")
            item = []
            if stack:
                stack[-1].append(item)
            stack.append(item)
            count += 1
        elif token == ")":
            if not stack:
                raise ValueError("sexpr.close")
            item = stack.pop()
            if not stack:
                result = item
        else:
            if not stack or re.fullmatch(r"[A-Za-z_][A-Za-z0-9_-]*|[+-]?[0-9]+", token) is None:
                raise ValueError("sexpr.atom")
            stack[-1].append(token)
            count += 1
        if count > 1024:
            raise ValueError("sexpr.nodes")
    if stack or result is None:
        raise ValueError("sexpr.unclosed")
    return result

The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.

AI coding workspace

Your next change starts here.

Get the Windows app ↗