templ.js

Status

Accepted - February 2026

Context

The Temple DSL requires a production-grade parser capable of:

  1. Configurable Delimiters: Support {% %}, {{ }}, {# #} with user-configurable alternatives
  2. Positional Accuracy: Track exact line/column positions for error reporting
  3. Incremental Parsing: Support LSP use cases (partial file edits)
  4. Embedded Languages: Distinguish template syntax from base format (Markdown, JSON, HTML)
  5. Error Recovery: Best-effort parsing with actionable error messages

Parser Options Evaluated

Parser Language Approach Type Safety Performance Ecosystem
Chevrotain TypeScript Code-based (Combinators) Excellent (TS native) High (benchmarked) Active, well-maintained
Nearley JavaScript Grammar (BNF) Good (TS definitions) Moderate Stable, less active
Peggy JavaScript PEG grammar Good (generated code) High Active (Peg.js fork)
ANTLR4 Multi-language Grammar (ANTLR) Good (generated TS) High Large ecosystem, Java-centric
Lark.js JavaScript Grammar (Lark port) Poor (JS only) Unknown Port, not mature

Detailed Analysis

Chevrotain

Pros:

Cons:

Decision

Chevrotain as the primary parser for templ.js.

Rationale

  1. TypeScript-First: Code-based grammar provides full type safety and IDE support
  2. Zero Build Step: No grammar compilation means faster iteration
  3. Performance: Benchmarks show Chevrotain matches or exceeds alternatives
  4. Error Recovery: Built-in mechanisms for fault-tolerant parsing (critical for LSP)
  5. Position Tracking: First-class support for line/column tracking
  6. Maintenance: Well-documented, actively maintained, used in production (Monaco Editor)

Implementation Plan

  1. Tokenizer: Use Chevrotain’s Lexer class with configurable delimiter patterns
  2. Parser: Use Chevrotain’s CstParser (Concrete Syntax Tree) for maximum flexibility
  3. AST Transformation: Post-process CST to Temple AST format
  4. Error Reporting: Leverage Chevrotain’s error recovery for partial parses

Consequences

Positive

Negative

Neutral

Performance Targets

References