bimr-engine/grammar/README.md
2026-09-01 15:32:49 +02:00

21 lines
1.5 KiB
Markdown

# Grammar
`bimr.bnf` is a human-readable reference describing the BIMR language grammar in BNF notation. It is not consumed by the build — it documents the rules that the lexer and parser implement in code.
## How the pipeline works
**Lexer** (`lexer/src/lexer.rs`) — powered by [`logos`](https://github.com/maciejhirsz/logos). The grammar is expressed directly as Rust attributes on the `Token` enum: regex patterns for identifiers, integers, and strings; literal patterns for punctuation (`=`, `,`, `(`, `)`). `logos` generates the tokeniser at compile time — no external grammar file is involved.
**Parser** (`parser/src/parser.rs`) — powered by [`chumsky`](https://github.com/zesterer/chumsky). Consumes the token stream produced by the lexer and builds an AST. The grammar rules map directly to parser combinators:
| BNF rule | Parser combinator |
|---|---|
| `program ::= statement*` | `statement().repeated().collect()` |
| `statement ::= identifier = expression` | `identifier().then_ignore(just(Equal)).then(expression())` |
| `expression ::= function_call \| identifier \| integer \| string` | `choice((function_call, base_expr()))` |
| `function_call ::= identifier ( arguments )` | `identifier().then(...).delimited_by(LeftParen, RightParen)` |
| `arguments ::= expression (, expression)*` | `base_expr().separated_by(just(Comma))` |
## Summary
`bimr.bnf` serves as a concise spec to understand the language at a glance. The source of truth is the Rust code in `lexer/` and `parser/`.