Syntax transformers
koskari.syntax
Syntax dispatch scaffolding
- class koskari.syntax.BareTransform(slot, template)
Bases:
SyntaxTransformBare arm: template expansion for the syntax name used as a lone atom rather than at the head of a call form. The called-by symbol is the only capture, bound to
slot, so the arm has no pattern and no matcher- expand_bare(compiler, sym, position=None)
Expand
templatewithsymbound to the called-by slotThe capture carries the caller-threaded enclosing position: the atom itself is interned and positionless, so the enclosing form’s position is the best available location for diagnostics
- Parameters:
compiler – active compiler instance
sym – lone atom that triggered this expansion
position – enclosing source position threaded by the caller
- Returns:
expanded form after finalize
- class koskari.syntax.MacroTransform(pattern, handler, matcher=None, repeat_variables=None)
Bases:
TransformProcedural arm: invoke handler, then mark and finalize its output
- handle_expand(compiler, source, captures)
Run handler, then mark and finalize the result
- Parameters:
compiler – active compiler
source – full special form (proper list spine)
captures – the captures of
self.matcher
- Returns:
expanded form after finalize
- class koskari.syntax.SpecialTransform(pattern, handler, matcher=None, repeat_variables=None)
Bases:
TransformSpecial arm: handler return is the compile result (no marking)
- handle_expand(compiler, source, captures)
Return handler result unchanged
- Parameters:
compiler – active compiler
source – full special form (proper list spine)
captures – the captures of
self.matcher
- Returns:
compile product from the handler
- class koskari.syntax.Syntax(name, transforms=(), bare=None)
Bases:
CompiledOrdered dispatcher over
Transformclauses (compiled form). Try order followstransformstuple order; use__add__to prepend clausesA syntax may also carry one
BareTransformin itsbareslot, outside the orderedtransforms. That arm expands the syntax name when it appears as a lone atom rather than at the head of a call form- Parameters:
name – primary compile-time symbol for this form
transforms – ordered transforms tried on each expansion
bare – lone-atom arm, or None
- append_doc(doc)
Append an extension’s documentation to what this syntax already carries. Used by
extend-syntaxso an extension describes what it adds rather than replacing the original text- Parameters:
doc – documentation text contributed by the extension
- Returns:
self
- compile_impl(compiler, source_obj)
Try each transform in order; the first arm whose pattern matches expands, and its result is returned as it is
A non-pair source is a lone-atom use, which bypasses the matchers entirely and goes straight to the
barearmA bare-only syntax (no call arms at all) used in form position is an alias: the head expands through the bare arm and the tail rides along for recompilation. A syntax that has call arms never falls back to its bare arm – a form no call arm matches is a malformed use, and the bare arm stands in for the lone atom, not for a call
- Parameters:
compiler – active compiler instance
source_obj – full special form being compiled
- Raises:
SyntaxMatchError – when no arm matches
- Returns:
compile product from the winning transform
- match_transformer(form)
Match
formagainst each arm’s matcher in order through FIRST_MATCH; the first that matches selects its arm- Parameters:
form – special form pair spine
- Returns:
(transform, captures)wheretransformis the matching transform andcapturesits captures, or (None, None)
- property patterns
- class koskari.syntax.SyntaxTransform(pattern, template, pattern_src=nil, matcher=None, repeat_variables=None)
Bases:
TransformTemplate arm: template expansion with a fresh mark, then finalize
- handle_expand(compiler, source, captures)
expand
templatewith the captures- Parameters:
compiler – active compiler
source – full special form (proper list spine)
captures – the captures of
self.matcher
- Returns:
expanded form after finalize
- class koskari.syntax.Transform(pattern, matcher=None, repeat_variables=None)
Bases:
ABCOne clause of a
Syntaxdispatcher: itsmatcherfunction, andtry_expandthrough it- abstractmethod handle_expand(compiler, source, captures)
When the pattern matches, handle the expansion.
- try_expand(compiler, source_obj)
Try this arm on
source_obj; return compile result orNone- Parameters:
compiler – active compiler instance
source_obj – full special form as a proper list (pair spine)
- Returns:
fragment or form for
compile_special, orNonewhen the pattern does not match
- koskari.syntax.build_bare_syntax(name, slot, template)
Koskari runtime:
(build-bare-syntax NAME SLOT TMPL), producing aSyntaxcarrying only a bare arm.SLOTnames the called-by capture thatTMPLrefers to. Combine with+to attach the arm to an existing syntax
- koskari.syntax.build_macro(name, *pattern_and_lambdas)
Koskari runtime:
(build-macro NAME PAT LAMBDA ...)
- koskari.syntax.build_special(name, *pattern_and_lambdas)
Koskari runtime:
(build-special NAME PAT LAMBDA ...)
- koskari.syntax.build_syntax(name, *pattern_template_pairs)
Koskari runtime:
(build-syntax NAME ROW ...), each row a matcher function, its repeat variables, its pattern source and a template datum, ascompile/syntax-rulesandcompile/syntax-matchemit
- koskari.syntax.parse_syntax_rules_pattern(pattern_expr, literals, source_ctx, variables=None)
Lower a syntax-rules RULE pattern (cons tree) to a
matcher.Pattern.RULE dialect only; does not implement the MATCH dialect’s registry heads.
parse_rule_pattern()calls this when lowering pattern source.- Parameters:
pattern_expr – koskari pattern datum
literals – set, frozenset, list, or tuple of literal symbols
source_ctx – value recorded in error messages
variables – a dict that, when given, receives each pattern variable’s name mapped to its source symbol, in the order the variables are bound
- Return type:
- koskari.syntax.special(form_symbol, *, patterns, unpack=None)
Build a
Syntaxdispatcher whose arms areSpecialTransforminstances wrapping the decorated function.unpackselects one of three fixed invoke shapes at decoration time (no per-call unpack dispatch)- Parameters:
form_symbol – primary compile-time head symbol for this form
patterns – one
matcher.Patternor an iterable (try-order)unpack – only for this decorator:
None(receivecompiler, source_obj),match(receivecompiler, captures_dict), orkwargs(captures merged aftercompiler). Formatchandkwargs,captures_dictincludes only slots the matcher bound; values are matcher outputs (Datumwrappers may be present). Not available onbuild_*rule rows.