June 12, 2026 · Yunus Emre Vurgun

A Tiny TOON Parser in 50 Lines of Python

toon · parser · python · tutorial

Writing a TOON parser is a good Sunday afternoon. The spec is small, the corner cases are obvious, and the resulting code fits in a single screen.

The shape of the parser

Tokenize lines into blank, header, row, and comment. Then group headers with their rows. Return a list of tables.

The 50-line version

import re\n\nROW = re.compile(r"^\s*([^:#]+?)\s*:\s*(.*)$")\n\ndef parse(text):\n    tables, cur = [], None\n    for raw in text.splitlines():\n        if not raw.strip() or raw.lstrip().startswith("#"):\n            continue\n        if raw.startswith((" ", "\t")) and cur is not None:\n            cur["rows"].append(raw.strip().split("\t"))\n            continue\n        if cur is not None: tables.append(cur); cur = None\n        m = ROW.match(raw)\n        if m:\n            cur = {"name": m.group(1).strip(), "header": None, "rows": []}\n            cur["rows"].append(m.group(2).split("\t"))\n    if cur is not None: tables.append(cur)\n    return tables\n

What it skips

Quoted strings with embedded colons, type tags in headers, nested table references. Each of those is a real spec feature; none of them are required for a learning parser.

Tests before growth

Lock down the empty file, the comment-only file, the single-key-value file, and a tabular file with a header. Four tests catch 90% of the bugs you will introduce when you start adding features.