DSL
Technical deep-dive on the poly-bench DSL parser, lexer, AST, and validator
The poly-bench-dsl crate is the front-end of the poly-bench pipeline. It converts .bench source files into a structured Abstract Syntax Tree (AST) that downstream crates consume. This page covers the lexer, parser, AST types, validation, and formatter.
The DSL pipeline:
.bench files for consistent styleThe lexer (poly-bench-dsl/src/lexer.rs) tokenizes source text. It maintains line/column tracking for error reporting and handles:
suite, bench, setup, fixture, declare, use, import, helpers, after, before, each, globalSetup, performance, timeBased, sameDataset, baseline, warmup, targetTime, count, hex, data, etc.go, ts, rust, python, c, csharp, zig)ms suffix for durations){, }, (, ), :, ,, @, =The lexer produces a Vec<Token> with TokenKind and Span for each token. Errors include source location for diagnostics.
The parser (poly-bench-dsl/src/parser.rs) consumes tokens and builds an AST. The grammar is defined in poly-bench-grammar (Tree-sitter) and the parser produces:
use directives and suite declarationsdeclare suite or suite blocks with configuration, setup blocks, fixtures, benchmarks, and after blocksimport, declare, init, and helpershex, data, encoding, format, or selector propertiesbefore/each/after hooksThe parser is recursive-descent and handles error recovery. Invalid input produces ParseError with span information.
The AST (poly-bench-dsl/src/ast.rs) defines:
AST nodes are serializable (Serialize/Deserialize) for caching and tooling.
The validator (poly-bench-dsl/src/validate.rs) runs semantic checks:
sameDataset: true, all benchmarks use the same fixture setbaseline, warmup, targetTime, count are validValidation produces ValidationResult with ValidationError and ValidationWarning for each issue.
The formatter (poly-bench-dsl/src/formatter.rs) pretty-prints .bench files:
format_file(source, filename) — Returns formatted stringformat_file_with_options — Configurable indentation and line widthUsed by poly-bench fmt and the LSP for incremental formatting.
| Crate | Role |
|---|---|
poly-bench-grammar | Tree-sitter grammar for .bench files; used by LSP and parser |
poly-bench-syntax | Tree-sitter-based parsing for LSP (error-tolerant) |
poly-bench-ir | Lowers AST to IR for codegen |