DotPrompt.Compiler (anantha_dot_prompt v1.1.0)

Copy Markdown

Compiles DotPrompt (.prompt) template files into rendered output.

Purpose

The Compiler is the core of the DotPrompt system. It takes a .prompt file path or raw content, tokenizes it via DotPrompt.Parser.Lexer, parses it into an AST, validates the structure, and compiles the AST into rendered prompt text with interpolated parameters.

Key Functions

  • do_parse_schema/5 — Parses a prompt file and returns its schema (params, fragments, docs, response contract) without rendering
  • compile_fresh_with_content/8 — Full compile pipeline: tokenize, parse, validate, compile, and resolve vary blocks
  • compile_ast/2 — Recursively compiles AST nodes (text, if, case, vary, fragments) into rendered output
  • stale?/1 — Checks if cached compiled output is stale by comparing file mtimes

Compilation Pipeline

  1. Tokenize → parse → validate (structural validation)
  2. Apply parameter defaults
  3. Validate params against declarations
  4. Resolve response contracts across blocks
  5. Compile AST with @param interpolation
  6. Resolve {vary} blocks (at-runtime variation)
  7. Cache parsed schema and compiled structural output