Add Special Forms docs page at /docs/special-forms
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 3m54s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 3m54s
Parses special-forms.sx spec into categorized form cards with syntax, description, tail-position info, and highlighted examples. Follows the same pattern as the Primitives page: Python helper returns structured data, .sx components render it. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -44,6 +44,8 @@
|
||||
"evaluator" (~docs-evaluator-content)
|
||||
"primitives" (~docs-primitives-content
|
||||
:prims (~doc-primitives-tables :primitives (primitives-data)))
|
||||
"special-forms" (~docs-special-forms-content
|
||||
:forms (~doc-special-forms-tables :forms (special-forms-data)))
|
||||
"css" (~docs-css-content)
|
||||
"server-rendering" (~docs-server-rendering-content)
|
||||
:else (~docs-introduction-content)))
|
||||
|
||||
@@ -14,6 +14,7 @@ def _register_sx_helpers() -> None:
|
||||
register_page_helpers("sx", {
|
||||
"highlight": _highlight,
|
||||
"primitives-data": _primitives_data,
|
||||
"special-forms-data": _special_forms_data,
|
||||
"reference-data": _reference_data,
|
||||
"attr-detail-data": _attr_detail_data,
|
||||
"header-detail-data": _header_detail_data,
|
||||
@@ -29,6 +30,89 @@ def _primitives_data() -> dict:
|
||||
return PRIMITIVES
|
||||
|
||||
|
||||
def _special_forms_data() -> dict:
|
||||
"""Parse special-forms.sx and return categorized form data.
|
||||
|
||||
Returns a dict of category → list of form dicts, each with:
|
||||
name, syntax, doc, tail_position, example
|
||||
"""
|
||||
import os
|
||||
from shared.sx.parser import parse_all, serialize
|
||||
from shared.sx.types import Symbol, Keyword
|
||||
|
||||
spec_path = os.path.join(
|
||||
os.path.dirname(os.path.abspath(__file__)),
|
||||
"..", "..", "..", "shared", "sx", "ref", "special-forms.sx",
|
||||
)
|
||||
with open(spec_path) as f:
|
||||
exprs = parse_all(f.read())
|
||||
|
||||
# Categories inferred from comment sections in the file.
|
||||
# We assign forms to categories based on their order in the spec.
|
||||
categories: dict[str, list[dict]] = {}
|
||||
current_category = "Other"
|
||||
|
||||
# Map form names to categories
|
||||
category_map = {
|
||||
"if": "Control Flow", "when": "Control Flow", "cond": "Control Flow",
|
||||
"case": "Control Flow", "and": "Control Flow", "or": "Control Flow",
|
||||
"let": "Binding", "let*": "Binding", "letrec": "Binding",
|
||||
"define": "Binding", "set!": "Binding",
|
||||
"lambda": "Functions & Components", "fn": "Functions & Components",
|
||||
"defcomp": "Functions & Components", "defmacro": "Functions & Components",
|
||||
"begin": "Sequencing & Threading", "do": "Sequencing & Threading",
|
||||
"->": "Sequencing & Threading",
|
||||
"quote": "Quoting", "quasiquote": "Quoting",
|
||||
"reset": "Continuations", "shift": "Continuations",
|
||||
"dynamic-wind": "Guards",
|
||||
"map": "Higher-Order Forms", "map-indexed": "Higher-Order Forms",
|
||||
"filter": "Higher-Order Forms", "reduce": "Higher-Order Forms",
|
||||
"some": "Higher-Order Forms", "every?": "Higher-Order Forms",
|
||||
"for-each": "Higher-Order Forms",
|
||||
"defstyle": "Domain Definitions", "defkeyframes": "Domain Definitions",
|
||||
"defhandler": "Domain Definitions", "defpage": "Domain Definitions",
|
||||
"defquery": "Domain Definitions", "defaction": "Domain Definitions",
|
||||
}
|
||||
|
||||
for expr in exprs:
|
||||
if not isinstance(expr, list) or len(expr) < 2:
|
||||
continue
|
||||
head = expr[0]
|
||||
if not isinstance(head, Symbol) or head.name != "define-special-form":
|
||||
continue
|
||||
|
||||
name = expr[1]
|
||||
# Extract keyword args
|
||||
kwargs: dict[str, str] = {}
|
||||
i = 2
|
||||
while i < len(expr) - 1:
|
||||
if isinstance(expr[i], Keyword):
|
||||
key = expr[i].name
|
||||
val = expr[i + 1]
|
||||
if isinstance(val, list):
|
||||
# For :syntax, avoid quote sugar (quasiquote → `x)
|
||||
items = [serialize(item) for item in val]
|
||||
kwargs[key] = "(" + " ".join(items) + ")"
|
||||
else:
|
||||
kwargs[key] = str(val)
|
||||
i += 2
|
||||
else:
|
||||
i += 1
|
||||
|
||||
category = category_map.get(name, "Other")
|
||||
if category not in categories:
|
||||
categories[category] = []
|
||||
categories[category].append({
|
||||
"name": name,
|
||||
"syntax": kwargs.get("syntax", ""),
|
||||
"doc": kwargs.get("doc", ""),
|
||||
"tail-position": kwargs.get("tail-position", ""),
|
||||
"example": kwargs.get("example", ""),
|
||||
})
|
||||
|
||||
return categories
|
||||
|
||||
|
||||
def _reference_data(slug: str) -> dict:
|
||||
"""Return reference table data for a given slug.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user