| name | macro-systems |
| description | Expert skill for designing and implementing macro systems including hygienic macros, procedural macros, and macro expansion. Supports pattern-based macros, quasi-quotation, and hygiene management. |
| allowed-tools | Read, Write, Edit, Bash, Glob, Grep |
| graph | {"domains":["domain:software-engineering"],"specializations":["specialization:programming-languages"],"skillAreas":["skill-area:compiler-implementation","skill-area:language-design"],"roles":["role:backend-engineer"]} |
Macro Systems Skill
Design and implement macro systems for programming languages, from simple text-based macros to sophisticated hygienic macro systems.
Capabilities
- Design macro invocation syntax and patterns
- Implement pattern-based macro matching and expansion
- Implement hygienic macro expansion with scope management
- Handle macro-generated identifier collision avoidance
- Implement procedural/syntax macros with AST manipulation
- Design quasi-quotation systems for code generation
- Handle macro debugging and error reporting
- Implement macro expansion tracing for development
Usage
Invoke this skill when you need to:
- Design a macro system for a new language
- Implement hygienic macro expansion
- Create procedural macro APIs
- Build quasi-quotation facilities
- Debug macro expansion issues
Inputs
| Parameter | Type | Required | Description |
|---|
| macroType | string | Yes | Type of macro system (pattern, procedural, hygienic) |
| targetLanguage | string | Yes | Language for macro implementation (Rust, Scheme, etc.) |
| syntax | object | No | Custom syntax specifications |
| hygieneModel | string | No | Hygiene model (sets-of-scopes, marks, none) |
| features | array | No | Features to implement |
Feature Options
{
"features": [
"pattern-matching",
"quasi-quotation",
"hygiene",
"procedural-macros",
"expansion-tracing",
"error-recovery",
"recursive-macros",
"macro-modularization"
]
}
Output Structure
macro-system/
โโโ syntax/
โ โโโ macro-definition.grammar # Macro definition syntax
โ โโโ macro-invocation.grammar # Invocation syntax
โ โโโ quasi-quote.grammar # Quasi-quotation syntax
โโโ expansion/
โ โโโ pattern-matcher.ts # Pattern matching engine
โ โโโ template-substitution.ts # Template instantiation
โ โโโ hygiene-manager.ts # Hygiene/scope management
โ โโโ expander.ts # Main expansion driver
โโโ procedural/
โ โโโ proc-macro-api.ts # Procedural macro API
โ โโโ token-stream.ts # Token manipulation
โ โโโ quote.ts # Quasi-quotation impl
โโโ debugging/
โ โโโ expansion-trace.ts # Expansion tracing
โ โโโ error-reporter.ts # Macro error messages
โโโ tests/
โโโ hygiene.test.ts
โโโ patterns.test.ts
โโโ expansion.test.ts
Macro System Types
Pattern-Based Macros (Scheme-style)
;; Definition
(define-syntax my-or
(syntax-rules ()
[(my-or) #f]
[(my-or e) e]
[(my-or e1 e2 ...)
(let ([t e1])
(if t t (my-or e2 ...)))]))
;; Implementation pattern
interface MacroRule {
pattern: Pattern;
template: Template;
literals: string[];
}
function matchPattern(pattern: Pattern, syntax: Syntax): Bindings | null {
// Pattern matching with ellipsis handling
}
function substituteTemplate(template: Template, bindings: Bindings): Syntax {
// Template instantiation with hygiene
}
Procedural Macros (Rust-style)
#[proc_macro_derive(Debug)]
pub fn derive_debug(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
impl_debug(&ast)
}
interface ProcMacroContext {
inputTokens: TokenStream;
span: Span;
hygiene: HygieneContext;
}
interface ProcMacro {
expand(ctx: ProcMacroContext): TokenStream;
}
Hygienic Expansion
interface Syntax {
datum: any;
scopes: Set<Scope>;
srcLoc: SourceLocation;
}
interface Scope {
id: number;
bindings: Map<Symbol, Binding>;
}
function introduceScope(syntax: Syntax, scope: Scope): Syntax {
return { ...syntax, scopes: new Set([...syntax.scopes, scope]) };
}
function flipScope(syntax: Syntax, scope: Scope): Syntax {
const newScopes = new Set(syntax.scopes);
if (newScopes.has(scope)) {
newScopes.delete(scope);
} else {
newScopes.add(scope);
}
return { ...syntax, scopes: newScopes };
}
function resolve(): | {
}
Quasi-Quotation Implementation
interface QuasiQuote {
template: QQTemplate;
}
type QQTemplate =
| { type: 'literal'; value: any }
| { type: 'unquote'; expr: Syntax }
| { type: 'unquote-splicing'; expr: Syntax }
| { type: 'list'; elements: QQTemplate[] };
function expandQuasiQuote(qq: QuasiQuote, env: Environment): Syntax {
function expand(template: QQTemplate): Syntax {
switch (template.type) {
case 'literal':
return quoteLiteral(template.value);
case 'unquote':
return evaluate(template.expr, env);
case 'unquote-splicing':
(template., env);
:
(template..(expand));
}
}
(qq.);
}
Expansion Tracing
interface ExpansionStep {
macroName: string;
inputSyntax: Syntax;
outputSyntax: Syntax;
bindings: Map<string, Syntax>;
location: SourceLocation;
}
class ExpansionTracer {
private steps: ExpansionStep[] = [];
recordStep(step: ExpansionStep): void {
this.steps.push(step);
}
formatTrace(): string {
return this.steps.map((step, i) =>
`Step ${i + 1}: ${step.macroName}\n` +
` Input: ${formatSyntax(step.inputSyntax)}\n` +
` Output: ${formatSyntax(step.outputSyntax)}\n` +
` Bindings: ${formatBindings(step.bindings)}`
).join('\n\n');
}
}
Error Handling
interface MacroError {
type: 'pattern-mismatch' | 'hygiene-violation' | 'expansion-limit' | 'syntax-error';
message: string;
macroName: string;
inputSyntax: Syntax;
suggestions: string[];
}
function reportMacroError(error: MacroError): string {
const base = `Macro expansion error in '${error.macroName}':\n${error.message}`;
const context = `\nInput syntax:\n ${formatSyntax(error.inputSyntax)}`;
const hints = error.suggestions.length > 0
? `\n\nSuggestions:\n${error.suggestions.map(s => ` - ${s}`).join('\n')}`
: '';
return base + context + hints;
}
Workflow
- Define macro syntax - Grammar for definition and invocation
- Implement pattern matching - Match invocations against patterns
- Build template substitution - Instantiate templates with bindings
- Add hygiene management - Handle identifier scoping
- Create procedural API - For complex transformations
- Build quasi-quotation - Code generation helpers
- Implement tracing - For debugging macro expansion
- Generate test suite - Hygiene, patterns, edge cases
Best Practices Applied
- Clear separation of macro definition from expansion
- Hygiene by default, explicit unhygienic escape hatches
- Informative error messages with expansion context
- Expansion depth limits to prevent infinite recursion
- Source location preservation through expansion
- Incremental expansion for IDE support
References
Target Processes
- macro-system-implementation.js
- parser-development.js
- semantic-analysis.js