From 833809872f9de9afc744c1b8be955df5f39e72d9 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Fri, 8 May 2026 19:15:43 +0530 Subject: [PATCH 01/14] docs: add grammar conformance test harness design spec Covers corpus layout (valid/invalid/syntax/invalid/semantic), pest shadow grammar scope, three-bucket conformance test logic, and Cargo changes. Resolves the open design question for issue #230 pre-implementation. Co-Authored-By: Claude Sonnet 4.6 --- .../2026-05-08-grammar-conformance-design.md | 184 ++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 docs/superpowers/specs/2026-05-08-grammar-conformance-design.md diff --git a/docs/superpowers/specs/2026-05-08-grammar-conformance-design.md b/docs/superpowers/specs/2026-05-08-grammar-conformance-design.md new file mode 100644 index 0000000000..2afdf6cf58 --- /dev/null +++ b/docs/superpowers/specs/2026-05-08-grammar-conformance-design.md @@ -0,0 +1,184 @@ +# Grammar Conformance Test Harness — Design + +**Date:** 2026-05-08 +**Issue:** [#230](https://github.com/project-minigraf/minigraf/issues/230) — Documentation +**Scope:** Test infrastructure that verifies the formal grammar document exactly matches parser behaviour. The grammar document itself (EBNF + semantics + tutorials) is a separate deliverable; this spec covers only the conformance harness. + +--- + +## Problem + +Minigraf's Datalog query language needs a formal grammar document (EBNF). A grammar that silently diverges from the real parser is worse than no grammar — it misleads users and breaks tooling built on the spec. The conformance harness makes divergence impossible to ignore: it fails CI the moment the grammar doc and the parser disagree on any corpus fixture. + +--- + +## Architecture + +Three artefacts: + +1. **`tests/grammar/grammar.pest`** — the grammar encoded as an executable `pest` PEG grammar. Serves as the machine-checkable equivalent of the EBNF documentation. The EBNF doc is derived from this file; they must say the same thing. +2. **`tests/grammar/`** corpus — `.edn` fixture files partitioned into three buckets by expected behaviour. This is the shared ground truth that both parsers are checked against. +3. **`tests/grammar_conformance.rs`** — one `#[test]` per bucket; sweeps all files, runs both parsers, reports all failures before panicking. + +`pest` and `pest_derive` are added to `[dev-dependencies]` only. Zero production-code impact, no binary size change. + +--- + +## Corpus Layout + +``` +tests/grammar/ + grammar.pest ← pest shadow grammar + valid/ ← pest ACCEPTS + real parser ACCEPTS + transact_basic.edn + transact_valid_time_tx_level.edn + transact_valid_time_per_fact.edn + transact_valid_time_both.edn + retract_basic.edn + query_basic.edn + query_as_of_counter.edn + query_as_of_timestamp.edn + query_valid_at_timestamp.edn + query_valid_at_any_valid_time.edn + query_any_valid_time_shorthand.edn + query_with_clause.edn + query_aggregate_count.edn + query_aggregate_sum.edn + query_aggregate_udf.edn + query_window_sum.edn + query_window_rank.edn + query_window_partition_by.edn + query_not.edn + query_not_join.edn + query_or.edn + query_or_join.edn + query_expr_filter.edn + query_expr_binding.edn + query_expr_nested.edn + query_prepared_bind_slot.edn + rule_basic.edn + rule_recursive.edn + edn_uuid.edn + edn_string_escapes.edn + edn_all_value_types.edn + invalid/ + syntax/ ← pest REJECTS + real parser REJECTS + unclosed_paren.edn + unclosed_bracket.edn + unknown_command.edn + empty_command.edn + unknown_tagged_literal.edn + string_unterminated.edn + bind_slot_empty.edn + keyword_invalid_chars.edn + unexpected_bare_char.edn + semantic/ ← pest ACCEPTS, real parser REJECTS + not_safety_unbound_var.edn + not_nested_inside_not.edn + or_inside_not.edn + not_join_insufficient_args.edn + expr_unbound_var_filter.edn + aggregate_var_unbound.edn + with_without_aggregate.edn + with_var_unbound.edn + window_only_func_without_over.edn + window_incompatible_func_with_over.edn + fact_too_few_elements.edn + retract_wrong_arity.edn + invalid_uuid_format.edn +``` + +--- + +## Pest Grammar Scope + +### Covered (structural acceptance) + +- All EDN primitives: keywords, symbols, strings (with `\n \t \r \" \\` escapes), integers (signed), floats, booleans (`true`/`false`), `nil`, `#uuid "..."`, bind slots (`$name`) +- All EDN containers: list `(...)`, vector `[...]`, map `{k v ...}` +- Top-level command forms: `(transact ...)`, `(retract ...)`, `(query ...)`, `(rule ...)` +- Query vector structure: `:find` / `:where` / `:as-of` / `:valid-at` / `:with` / `:any-valid-time` — correct keyword placement and expected value types at each position +- Fact triple shape: `[e a v]` or `[e a v {map}]`; transaction-level options map `(transact {map} [facts])` +- `where` clause shapes: pattern vector, rule invocation list, `(not ...)`, `(not-join [...] ...)`, `(or ...)`, `(or-join [...] ...)`, expr vector `[(expr) ?out?]` +- Aggregate form: `(sym ?var)`; window form: `(sym ?var :over (...))` or `(sym :over (...))` +- Expression forms: unary `(op arg)`, binary `(op arg arg)` + +### Not covered (semantic layer — `invalid/semantic/` only) + +These are enforced by the real parser but are above the structural grammar layer. The `pest` grammar accepts them; the real parser rejects them. + +| Constraint | Corpus file | +|---|---| +| Vars in `not`/`not-join` must be bound in outer clauses | `not_safety_unbound_var.edn` | +| `(not ...)` cannot nest inside `(not ...)` | `not_nested_inside_not.edn` | +| `(or)`/`(or-join)` cannot appear inside `(not)`/`(not-join)` | `or_inside_not.edn` | +| `not-join` requires join-vars vector + ≥1 clause | `not_join_insufficient_args.edn` | +| Filter expr vars must already be bound | `expr_unbound_var_filter.edn` | +| Aggregate variable must be bound in `:where` | `aggregate_var_unbound.edn` | +| `:with` requires ≥1 aggregate in `:find` | `with_without_aggregate.edn` | +| `:with` vars must be bound in `:where` | `with_var_unbound.edn` | +| `avg`/`rank`/`row-number` require `:over` clause | `window_only_func_without_over.edn` | +| `count-distinct`/`sum-distinct` not compatible with `:over` | `window_incompatible_func_with_over.edn` | +| Fact must have ≥3 elements | `fact_too_few_elements.edn` | +| Retract fact must be exactly `[e a v]` | `retract_wrong_arity.edn` | +| UUID string must be valid RFC 4122 format | `invalid_uuid_format.edn` | + +Note: ISO 8601 timestamp parsing (`:as-of`, `:valid-at`, `:valid-from`, `:valid-to`) is also semantic — `pest` accepts any string in those positions. + +--- + +## Conformance Test Logic + +``` +tests/grammar_conformance.rs +``` + +Three `#[test]` functions. Each sweeps its directory, accumulates all failures, then panics with the full list — no early exit on first failure. + +``` +#[test] fn valid_corpus() + for each .edn in tests/grammar/valid/: + pest ACCEPTS → ok; failure means grammar.pest is too strict + parser ACCEPTS → ok; failure means test fixture is wrong or parser regressed + +#[test] fn invalid_syntax_corpus() + for each .edn in tests/grammar/invalid/syntax/: + pest REJECTS → ok; failure means grammar.pest is too permissive + parser REJECTS → ok; failure means parser is too permissive + +#[test] fn invalid_semantic_corpus() + for each .edn in tests/grammar/invalid/semantic/: + pest ACCEPTS → ok; failure means this should be in syntax/ instead + parser REJECTS → ok; failure means parser is missing a semantic check +``` + +Failure output per file: +``` +FAIL valid/query_not.edn: parser rejected (expected accept) +FAIL invalid/semantic/not_nested_inside_not.edn: pest rejected (expected accept — semantic bucket) +``` + +### Wiring pest + +A `Grammar` struct with `#[derive(Parser)]` and `#[grammar = "tests/grammar/grammar.pest"]` lives in `tests/grammar_conformance.rs` (or a helper module `tests/grammar_helpers.rs` if the file grows large). The conformance test calls `Grammar::parse(Rule::command, input)` — `Ok` = accepted, `Err` = rejected. + +--- + +## Cargo Changes + +```toml +[dev-dependencies] +pest = "2" +pest_derive = "2" +``` + +No production dependencies added. + +--- + +## What This Does Not Cover + +- **Grammar documentation** — the EBNF text, semantics prose, and tutorials are separate deliverables (the main output of issue #230). The `grammar.pest` file is the machine-checkable backing for the EBNF; they must agree. +- **Round-trip / pretty-print testing** — not in scope for this harness. +- **Property-based generation** — the corpus is manually curated. Fuzzing is a possible future addition. +- **Executor correctness** — the harness tests parse acceptance/rejection only, not query result correctness. From 7acd87db64f73acb5126d93fd4d4b7074c531402 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Fri, 8 May 2026 19:36:14 +0530 Subject: [PATCH 02/14] docs: update grammar spec to include EBNF doc and semantics as deliverables Scope now explicitly covers three deliverables: conformance harness, EBNF grammar document, and semantics prose. Links to sub-issues #233 and #234. Tutorials correctly scoped out to #234. Co-Authored-By: Claude Sonnet 4.6 --- .../2026-05-08-grammar-conformance-design.md | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/docs/superpowers/specs/2026-05-08-grammar-conformance-design.md b/docs/superpowers/specs/2026-05-08-grammar-conformance-design.md index 2afdf6cf58..7f8d5e31f1 100644 --- a/docs/superpowers/specs/2026-05-08-grammar-conformance-design.md +++ b/docs/superpowers/specs/2026-05-08-grammar-conformance-design.md @@ -1,8 +1,17 @@ -# Grammar Conformance Test Harness — Design +# Grammar Specification and Conformance Harness — Design **Date:** 2026-05-08 -**Issue:** [#230](https://github.com/project-minigraf/minigraf/issues/230) — Documentation -**Scope:** Test infrastructure that verifies the formal grammar document exactly matches parser behaviour. The grammar document itself (EBNF + semantics + tutorials) is a separate deliverable; this spec covers only the conformance harness. +**Issue:** [#233](https://github.com/project-minigraf/minigraf/issues/233) — EBNF grammar specification and semantics documentation (sub-issue of [#230](https://github.com/project-minigraf/minigraf/issues/230)) + +## Deliverables + +This work produces three things: + +1. **Conformance test harness** — `pest` shadow grammar + `.edn` corpus + `grammar_conformance.rs`. Verifies continuously that the grammar doc matches parser behaviour. +2. **EBNF grammar document** — human-readable formal grammar for the full Minigraf Datalog syntax, derived from `grammar.pest` and published in the wiki (`Datalog-Reference.md`). +3. **Semantics documentation** — prose covering all constraints enforced above the structural grammar layer (not-safety, expr binding, aggregate binding, window function rules, timestamp/UUID validation). Also published in `Datalog-Reference.md`. + +Tutorials are tracked separately in [#234](https://github.com/project-minigraf/minigraf/issues/234). --- @@ -178,7 +187,7 @@ No production dependencies added. ## What This Does Not Cover -- **Grammar documentation** — the EBNF text, semantics prose, and tutorials are separate deliverables (the main output of issue #230). The `grammar.pest` file is the machine-checkable backing for the EBNF; they must agree. -- **Round-trip / pretty-print testing** — not in scope for this harness. +- **Tutorials** — tracked in [#234](https://github.com/project-minigraf/minigraf/issues/234). +- **Round-trip / pretty-print testing** — not in scope. - **Property-based generation** — the corpus is manually curated. Fuzzing is a possible future addition. - **Executor correctness** — the harness tests parse acceptance/rejection only, not query result correctness. From 31796ca2adf74a282192c65f2e16f6112053da85 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Wed, 13 May 2026 14:38:43 +0530 Subject: [PATCH 03/14] test: add grammar conformance harness skeleton (Task 1) Add pest/pest_derive dev-dependencies, corpus directory structure (tests/grammar/valid, invalid/syntax, invalid/semantic), placeholder grammar.pest, and the full three-bucket conformance test harness. Tests fail with "corpus is empty" as expected until fixtures are added. Co-Authored-By: Claude Sonnet 4.6 --- Cargo.toml | 2 + tests/grammar/grammar.pest | 3 + tests/grammar_conformance.rs | 141 +++++++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 tests/grammar/grammar.pest create mode 100644 tests/grammar_conformance.rs diff --git a/Cargo.toml b/Cargo.toml index fe5cc3b001..c559f58568 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -67,6 +67,8 @@ web-sys = { version = "0.3", optional = true, features = [ [dev-dependencies] serde_json = "1.0" +pest = "2" +pest_derive = "2" [target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] tempfile = "3" diff --git a/tests/grammar/grammar.pest b/tests/grammar/grammar.pest new file mode 100644 index 0000000000..ed136ffc43 --- /dev/null +++ b/tests/grammar/grammar.pest @@ -0,0 +1,3 @@ +// Minigraf Datalog grammar — built incrementally; see implementation plan. +// This placeholder rejects all input until proper rules are added in Task 2. +command = { SOI ~ EOI } diff --git a/tests/grammar_conformance.rs b/tests/grammar_conformance.rs new file mode 100644 index 0000000000..f555972ad8 --- /dev/null +++ b/tests/grammar_conformance.rs @@ -0,0 +1,141 @@ +//! Grammar conformance tests. +//! +//! Three test functions verify that `grammar.pest` and the real parser agree: +//! +//! - `valid_corpus`: pest ACCEPTS + parser ACCEPTS +//! - `invalid_syntax_corpus`: pest REJECTS + parser REJECTS +//! - `invalid_semantic_corpus`: pest ACCEPTS + parser REJECTS +//! +//! Run with: cargo test grammar_conformance -- --nocapture + +use pest::Parser; +use pest_derive::Parser; +use std::fs; +use std::path::Path; + +#[derive(Parser)] +#[grammar = "tests/grammar/grammar.pest"] +struct DatalogGrammar; + +/// Returns true when the pest grammar accepts the full input as a `command`. +fn pest_accepts(input: &str) -> bool { + DatalogGrammar::parse(Rule::command, input.trim()).is_ok() +} + +/// Returns true when the real Minigraf parser accepts the input. +/// +/// Uses `db.prepare()` for `(query ...)` commands so that bind-slot +/// templates parse correctly without needing substituted values at test time. +/// Uses `db.execute()` for transact / retract / rule. +fn parser_accepts(input: &str) -> bool { + let input = input.trim(); + let Ok(db) = minigraf::Minigraf::in_memory() else { + return false; + }; + if input.starts_with("(query") { + db.prepare(input).is_ok() + } else { + db.execute(input).is_ok() + } +} + +/// Read every `.edn` file in `dir`, returning `(filename, content)` pairs +/// sorted by filename for deterministic test output. +fn load_corpus(dir: &str) -> Vec<(String, String)> { + let path = Path::new(dir); + let Ok(read_dir) = fs::read_dir(path) else { + return vec![]; + }; + let mut files: Vec<(String, String)> = read_dir + .filter_map(|e| { + let e = e.ok()?; + let p = e.path(); + if p.extension()?.to_str()? == "edn" { + let name = p.file_name()?.to_str()?.to_string(); + let content = fs::read_to_string(&p).ok()?; + Some((name, content)) + } else { + None + } + }) + .collect(); + files.sort_by(|a, b| a.0.cmp(&b.0)); + files +} + +// ── VALID ────────────────────────────────────────────────────────────────── + +#[test] +#[allow(clippy::panic)] +fn valid_corpus() { + let files = load_corpus("tests/grammar/valid"); + assert!(!files.is_empty(), "valid/ corpus is empty — add .edn fixtures"); + let mut failures: Vec = vec![]; + for (name, content) in &files { + if !pest_accepts(content) { + failures.push(format!("FAIL valid/{name}: pest rejected (expected accept)")); + } + if !parser_accepts(content) { + failures.push(format!("FAIL valid/{name}: parser rejected (expected accept)")); + } + } + if !failures.is_empty() { + panic!("\n{}", failures.join("\n")); + } +} + +// ── INVALID SYNTAX ───────────────────────────────────────────────────────── + +#[test] +#[allow(clippy::panic)] +fn invalid_syntax_corpus() { + let files = load_corpus("tests/grammar/invalid/syntax"); + assert!( + !files.is_empty(), + "invalid/syntax/ corpus is empty — add .edn fixtures" + ); + let mut failures: Vec = vec![]; + for (name, content) in &files { + if pest_accepts(content) { + failures.push(format!( + "FAIL invalid/syntax/{name}: pest accepted (expected reject)" + )); + } + if parser_accepts(content) { + failures.push(format!( + "FAIL invalid/syntax/{name}: parser accepted (expected reject)" + )); + } + } + if !failures.is_empty() { + panic!("\n{}", failures.join("\n")); + } +} + +// ── INVALID SEMANTIC ─────────────────────────────────────────────────────── + +#[test] +#[allow(clippy::panic)] +fn invalid_semantic_corpus() { + let files = load_corpus("tests/grammar/invalid/semantic"); + assert!( + !files.is_empty(), + "invalid/semantic/ corpus is empty — add .edn fixtures" + ); + let mut failures: Vec = vec![]; + for (name, content) in &files { + if !pest_accepts(content) { + failures.push(format!( + "FAIL invalid/semantic/{name}: pest rejected (expected accept)" + )); + } + if parser_accepts(content) { + failures.push(format!( + "FAIL invalid/semantic/{name}: parser accepted (expected reject)" + )); + } + } + if !failures.is_empty() { + panic!("\n{}", failures.join("\n")); + } +} From e182fde19fbe6aee6ad678a7997310d15c645b21 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Wed, 13 May 2026 14:44:06 +0530 Subject: [PATCH 04/14] test: add EDN primitives, transact, retract corpus + grammar (Task 2) Co-Authored-By: Claude Sonnet 4.6 --- tests/grammar/grammar.pest | 92 ++++++++++++++++++- tests/grammar/valid/edn_all_value_types.edn | 9 ++ tests/grammar/valid/edn_string_escapes.edn | 4 + tests/grammar/valid/edn_uuid.edn | 1 + tests/grammar/valid/retract_basic.edn | 1 + tests/grammar/valid/transact_basic.edn | 2 + .../valid/transact_valid_time_both.edn | 2 + .../valid/transact_valid_time_per_fact.edn | 1 + .../valid/transact_valid_time_tx_level.edn | 2 + 9 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 tests/grammar/valid/edn_all_value_types.edn create mode 100644 tests/grammar/valid/edn_string_escapes.edn create mode 100644 tests/grammar/valid/edn_uuid.edn create mode 100644 tests/grammar/valid/retract_basic.edn create mode 100644 tests/grammar/valid/transact_basic.edn create mode 100644 tests/grammar/valid/transact_valid_time_both.edn create mode 100644 tests/grammar/valid/transact_valid_time_per_fact.edn create mode 100644 tests/grammar/valid/transact_valid_time_tx_level.edn diff --git a/tests/grammar/grammar.pest b/tests/grammar/grammar.pest index ed136ffc43..83467c2b2a 100644 --- a/tests/grammar/grammar.pest +++ b/tests/grammar/grammar.pest @@ -1,3 +1,89 @@ -// Minigraf Datalog grammar — built incrementally; see implementation plan. -// This placeholder rejects all input until proper rules are added in Task 2. -command = { SOI ~ EOI } +// ── WHITESPACE (auto-skipped between every token) ───────────────────────── +WHITESPACE = _{ " " | "\t" | "\n" | "\r" | "," } + +// ── WORD-BOUNDARY HELPER ────────────────────────────────────────────────── +// Used in negative lookaheads to enforce keyword boundaries. +word_cont = _{ ASCII_ALPHANUMERIC | "?" | "_" | "-" | "/" } + +// ── EDN PRIMITIVES ──────────────────────────────────────────────────────── + +// boolean: "true" or "false" — only when NOT followed by a word character +// (so "trueish" is NOT a boolean, it's a plain symbol) +boolean_lit = @{ ("true" | "false") ~ !word_cont } + +// nil literal +nil_lit = @{ "nil" ~ !word_cont } + +// integer: optional minus, one or more digits, NOT followed by "." + digit +// (the negative lookahead prevents consuming "-3" as the start of "-3.14") +integer_lit = @{ "-"? ~ ASCII_DIGIT+ ~ !("." ~ ASCII_DIGIT) } + +// float: optional minus, digits, dot, optional more digits +float_lit = @{ "-"? ~ ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT* } + +// string with escape sequences: \n \t \r \" \\, or any other char after \ +str_char = _{ "\\" ~ ("n" | "t" | "r" | "\"" | "\\" | ANY) | !("\"" | "\\") ~ ANY } +string_lit = @{ "\"" ~ str_char* ~ "\"" } + +// UUID tagged literal: #uuid "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" +// Content validation (RFC 4122 format) is a semantic check — pest accepts any string. +uuid_lit = { "#uuid" ~ string_lit } + +// Prepared-query bind slot: $name +bind_slot = @{ "$" ~ (ASCII_ALPHANUMERIC | "_" | "-")+ } + +// Keyword: starts with ":", followed by alphanumeric, "/", "-", "_" +keyword = @{ ":" ~ (ASCII_ALPHANUMERIC | "/" | "-" | "_")+ } + +// Logic variable: starts with "?" +variable = @{ "?" ~ word_cont* } + +// Operator symbols used in expressions +op_sym = @{ "<=" | ">=" | "!=" | "<" | ">" | "=" | "+" | "*" | "/" } + +// Plain symbol: letter or underscore start; or "-" followed by at least one word char. +// Negative lookaheads prevent "true", "false", "nil" from matching as plain symbols. +plain_sym = @{ + !boolean_lit ~ !nil_lit ~ (ASCII_ALPHA | "_") ~ word_cont* + | "-" ~ word_cont+ +} + +// ── EDN VALUE (any EDN atom or container) ───────────────────────────────── +// Order matters: more specific alternatives first to avoid ambiguity. +edn_value = { + uuid_lit | boolean_lit | nil_lit | float_lit | integer_lit | string_lit | + bind_slot | keyword | op_sym | variable | plain_sym | + edn_list | edn_vector | edn_map +} +edn_list = { "(" ~ edn_value* ~ ")" } +edn_vector = { "[" ~ edn_value* ~ "]" } +edn_map = { "{" ~ (edn_value ~ edn_value)* ~ "}" } + +// ── TOP-LEVEL COMMAND ───────────────────────────────────────────────────── +command = { SOI ~ (transact_cmd | retract_cmd | query_cmd | rule_cmd) ~ EOI } + +// ── TRANSACT ────────────────────────────────────────────────────────────── +// (transact [facts...]) or (transact {:valid-from "ts"} [facts...]) +transact_cmd = { + "(" ~ "transact" ~ (valid_time_map ~ fact_vector | fact_vector) ~ ")" +} + +// ── RETRACT ─────────────────────────────────────────────────────────────── +retract_cmd = { "(" ~ "retract" ~ fact_vector ~ ")" } + +// Fact vector: a vector of individual fact vectors +fact_vector = { "[" ~ fact* ~ "]" } + +// A single fact: permissive (edn_value+) so arity violations are semantic, not syntax. +// The real parser enforces [E A V] or [E A V {map}]; pest only requires ≥1 element. +fact = { "[" ~ edn_value+ ~ "]" } + +// Transaction-level valid-time options map +valid_time_map = { "{" ~ valid_time_pair* ~ "}" } +valid_time_pair = { (":valid-from" | ":valid-to") ~ string_lit } + +// ── QUERY (stub — filled in Task 3+) ───────────────────────────────────── +query_cmd = { "(" ~ "query" ~ edn_value+ ~ ")" } + +// ── RULE (stub — filled in Task 8) ─────────────────────────────────────── +rule_cmd = { "(" ~ "rule" ~ edn_value+ ~ ")" } diff --git a/tests/grammar/valid/edn_all_value_types.edn b/tests/grammar/valid/edn_all_value_types.edn new file mode 100644 index 0000000000..d0fcd53138 --- /dev/null +++ b/tests/grammar/valid/edn_all_value_types.edn @@ -0,0 +1,9 @@ +(transact [[:e1 :type/string "hello world"] + [:e2 :type/integer 42] + [:e3 :type/neg-int -7] + [:e4 :type/float 3.14] + [:e5 :type/neg-float -2.5] + [:e6 :type/bool-true true] + [:e7 :type/bool-false false] + [:e8 :type/nil nil] + [:e9 :type/ref :other-entity]]) diff --git a/tests/grammar/valid/edn_string_escapes.edn b/tests/grammar/valid/edn_string_escapes.edn new file mode 100644 index 0000000000..a7a13b113b --- /dev/null +++ b/tests/grammar/valid/edn_string_escapes.edn @@ -0,0 +1,4 @@ +(transact [[:e :str/newline "line1\nline2"] + [:e :str/tab "col1\tcol2"] + [:e :str/quote "say \"hello\""] + [:e :str/backslash "path\\to\\file"]]) diff --git a/tests/grammar/valid/edn_uuid.edn b/tests/grammar/valid/edn_uuid.edn new file mode 100644 index 0000000000..8d26b06aa1 --- /dev/null +++ b/tests/grammar/valid/edn_uuid.edn @@ -0,0 +1 @@ +(transact [[#uuid "550e8400-e29b-41d4-a716-446655440000" :entity/name "by-uuid"]]) diff --git a/tests/grammar/valid/retract_basic.edn b/tests/grammar/valid/retract_basic.edn new file mode 100644 index 0000000000..1d778cb9b6 --- /dev/null +++ b/tests/grammar/valid/retract_basic.edn @@ -0,0 +1 @@ +(retract [[:alice :person/name "Alice"]]) diff --git a/tests/grammar/valid/transact_basic.edn b/tests/grammar/valid/transact_basic.edn new file mode 100644 index 0000000000..a3b346554a --- /dev/null +++ b/tests/grammar/valid/transact_basic.edn @@ -0,0 +1,2 @@ +(transact [[:alice :person/name "Alice"] + [:bob :person/name "Bob"]]) diff --git a/tests/grammar/valid/transact_valid_time_both.edn b/tests/grammar/valid/transact_valid_time_both.edn new file mode 100644 index 0000000000..dc8189fb09 --- /dev/null +++ b/tests/grammar/valid/transact_valid_time_both.edn @@ -0,0 +1,2 @@ +(transact {:valid-from "2024-01-01T00:00:00Z"} + [[:alice :contract/active true {:valid-to "2025-01-01T00:00:00Z"}]]) diff --git a/tests/grammar/valid/transact_valid_time_per_fact.edn b/tests/grammar/valid/transact_valid_time_per_fact.edn new file mode 100644 index 0000000000..152a22ba18 --- /dev/null +++ b/tests/grammar/valid/transact_valid_time_per_fact.edn @@ -0,0 +1 @@ +(transact [[:alice :contract/active true {:valid-from "2024-01-01T00:00:00Z" :valid-to "2025-01-01T00:00:00Z"}]]) diff --git a/tests/grammar/valid/transact_valid_time_tx_level.edn b/tests/grammar/valid/transact_valid_time_tx_level.edn new file mode 100644 index 0000000000..f0daeffb29 --- /dev/null +++ b/tests/grammar/valid/transact_valid_time_tx_level.edn @@ -0,0 +1,2 @@ +(transact {:valid-from "2024-01-01T00:00:00Z" :valid-to "2025-01-01T00:00:00Z"} + [[:alice :person/name "Alice"]]) From 427b6e3cbaa6be4685ccd878c7597c07ec571600 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Wed, 13 May 2026 14:48:52 +0530 Subject: [PATCH 05/14] test: add basic query corpus + full query grammar structure (Task 3) Co-Authored-By: Claude Sonnet 4.6 --- tests/grammar/grammar.pest | 81 ++++++++++++++++++++++++++++- tests/grammar/valid/query_basic.edn | 3 ++ 2 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 tests/grammar/valid/query_basic.edn diff --git a/tests/grammar/grammar.pest b/tests/grammar/grammar.pest index 83467c2b2a..0d0f5006ef 100644 --- a/tests/grammar/grammar.pest +++ b/tests/grammar/grammar.pest @@ -82,8 +82,85 @@ fact = { "[" ~ edn_value+ ~ "]" } valid_time_map = { "{" ~ valid_time_pair* ~ "}" } valid_time_pair = { (":valid-from" | ":valid-to") ~ string_lit } -// ── QUERY (stub — filled in Task 3+) ───────────────────────────────────── -query_cmd = { "(" ~ "query" ~ edn_value+ ~ ")" } +// ── QUERY ───────────────────────────────────────────────────────────────── +query_cmd = { "(" ~ "query" ~ query_vector ~ ")" } +query_vector = { "[" ~ query_section+ ~ "]" } + +// A query vector is a sequence of keyed sections in any order. +query_section = { + find_section | where_section | as_of_section | + valid_at_section | any_valid_time_section | with_section +} + +// ── FIND SECTION ────────────────────────────────────────────────────────── +// :find accepts variables, aggregates, and window functions +find_section = { ":find" ~ find_spec+ } +find_spec = { window_expr | aggregate_expr | variable } + +// Aggregate: (func-name ?var) — e.g. (count ?e), (sum ?salary) +aggregate_expr = { "(" ~ (op_sym | plain_sym) ~ variable ~ ")" } + +// Window function: (func ?var :over (...)) or (func :over (...)) for rank/row-number +window_expr = { + "(" ~ (op_sym | plain_sym) ~ (variable ~ ":over" | ":over") ~ over_clause ~ ")" +} +over_clause = { "(" ~ over_option* ~ ")" } +over_option = { + ":partition-by" ~ variable | + ":order-by" ~ variable | + ":desc" | ":asc" +} + +// ── WHERE SECTION ───────────────────────────────────────────────────────── +where_section = { ":where" ~ where_clause+ } + +// where_clause covers all clause forms +where_clause = { + expr_clause | not_clause | not_join_clause | + or_clause | or_join_clause | + pattern_clause | rule_invocation +} + +// Pattern: exactly [E A V] — three EDN values +pattern_clause = { "[" ~ edn_value ~ edn_value ~ edn_value ~ "]" } + +// Expression clause: [(expr) ?out?] — first element is a list (the expression) +expr_clause = { "[" ~ expr ~ variable? ~ "]" } + +// NOT / NOT-JOIN +not_clause = { "(" ~ "not" ~ where_clause+ ~ ")" } +not_join_clause = { "(" ~ "not-join" ~ join_vars ~ where_clause+ ~ ")" } + +// OR / OR-JOIN +or_clause = { "(" ~ "or" ~ or_branch+ ~ ")" } +or_join_clause = { "(" ~ "or-join" ~ join_vars ~ or_branch+ ~ ")" } +join_vars = { "[" ~ variable* ~ "]" } +or_branch = { and_branch | where_clause } +and_branch = { "(" ~ "and" ~ where_clause+ ~ ")" } + +// Rule invocation: (predicate-name args...) +rule_invocation = { "(" ~ plain_sym ~ edn_value* ~ ")" } + +// ── EXPRESSIONS ─────────────────────────────────────────────────────────── +expr = { "(" ~ expr_body ~ ")" } +expr_body = { unary_form | binary_form } +unary_form = { (unary_op | op_sym | plain_sym) ~ expr_arg } +binary_form = { binary_op ~ expr_arg ~ expr_arg } +unary_op = @{ "string?" | "integer?" | "float?" | "boolean?" | "nil?" } +binary_op = @{ + "<=" | ">=" | "!=" | "starts-with?" | "ends-with?" | "contains?" | "matches?" | + "<" | ">" | "=" | "+" | "-" | "*" | "/" +} +expr_arg = { + expr | boolean_lit | nil_lit | float_lit | integer_lit | + string_lit | bind_slot | keyword | variable +} + +// ── TEMPORAL / WITH SECTIONS ─────────────────────────────────────────────── +as_of_section = { ":as-of" ~ (integer_lit | string_lit | bind_slot) } +valid_at_section = { ":valid-at" ~ (string_lit | ":any-valid-time" | bind_slot) } +any_valid_time_section = { ":any-valid-time" } +with_section = { ":with" ~ variable+ } // ── RULE (stub — filled in Task 8) ─────────────────────────────────────── rule_cmd = { "(" ~ "rule" ~ edn_value+ ~ ")" } diff --git a/tests/grammar/valid/query_basic.edn b/tests/grammar/valid/query_basic.edn new file mode 100644 index 0000000000..5ede121766 --- /dev/null +++ b/tests/grammar/valid/query_basic.edn @@ -0,0 +1,3 @@ +(query [:find ?name ?age + :where [?e :person/name ?name] + [?e :person/age ?age]]) From eadc45ab2a9bd94b16e8f679d05bc99ac9280b26 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Wed, 13 May 2026 14:55:26 +0530 Subject: [PATCH 06/14] test: add temporal modifier corpus (Task 4) --- tests/grammar/valid/query_any_valid_time_shorthand.edn | 3 +++ tests/grammar/valid/query_as_of_counter.edn | 3 +++ tests/grammar/valid/query_as_of_timestamp.edn | 3 +++ tests/grammar/valid/query_valid_at_any_valid_time.edn | 3 +++ tests/grammar/valid/query_valid_at_timestamp.edn | 3 +++ 5 files changed, 15 insertions(+) create mode 100644 tests/grammar/valid/query_any_valid_time_shorthand.edn create mode 100644 tests/grammar/valid/query_as_of_counter.edn create mode 100644 tests/grammar/valid/query_as_of_timestamp.edn create mode 100644 tests/grammar/valid/query_valid_at_any_valid_time.edn create mode 100644 tests/grammar/valid/query_valid_at_timestamp.edn diff --git a/tests/grammar/valid/query_any_valid_time_shorthand.edn b/tests/grammar/valid/query_any_valid_time_shorthand.edn new file mode 100644 index 0000000000..ebd3236336 --- /dev/null +++ b/tests/grammar/valid/query_any_valid_time_shorthand.edn @@ -0,0 +1,3 @@ +(query [:find ?name + :any-valid-time + :where [?e :person/name ?name]]) diff --git a/tests/grammar/valid/query_as_of_counter.edn b/tests/grammar/valid/query_as_of_counter.edn new file mode 100644 index 0000000000..0cd60208b3 --- /dev/null +++ b/tests/grammar/valid/query_as_of_counter.edn @@ -0,0 +1,3 @@ +(query [:find ?name + :as-of 3 + :where [?e :person/name ?name]]) diff --git a/tests/grammar/valid/query_as_of_timestamp.edn b/tests/grammar/valid/query_as_of_timestamp.edn new file mode 100644 index 0000000000..93cd414a40 --- /dev/null +++ b/tests/grammar/valid/query_as_of_timestamp.edn @@ -0,0 +1,3 @@ +(query [:find ?name + :as-of "2024-01-15T10:00:00Z" + :where [?e :person/name ?name]]) diff --git a/tests/grammar/valid/query_valid_at_any_valid_time.edn b/tests/grammar/valid/query_valid_at_any_valid_time.edn new file mode 100644 index 0000000000..a10164726b --- /dev/null +++ b/tests/grammar/valid/query_valid_at_any_valid_time.edn @@ -0,0 +1,3 @@ +(query [:find ?name + :valid-at :any-valid-time + :where [?e :person/name ?name]]) diff --git a/tests/grammar/valid/query_valid_at_timestamp.edn b/tests/grammar/valid/query_valid_at_timestamp.edn new file mode 100644 index 0000000000..c03aa4e183 --- /dev/null +++ b/tests/grammar/valid/query_valid_at_timestamp.edn @@ -0,0 +1,3 @@ +(query [:find ?name + :valid-at "2023-06-01T00:00:00Z" + :where [?e :person/name ?name]]) From 2ca2126328b7e7a20cce7a9311de3ef313ce4ff1 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Wed, 13 May 2026 14:55:52 +0530 Subject: [PATCH 07/14] test: add aggregate, window function, and :with corpus (Task 5) --- tests/grammar/valid/query_aggregate_count.edn | 2 ++ tests/grammar/valid/query_aggregate_sum.edn | 2 ++ tests/grammar/valid/query_aggregate_udf.edn | 2 ++ tests/grammar/valid/query_window_partition_by.edn | 4 ++++ tests/grammar/valid/query_window_rank.edn | 2 ++ tests/grammar/valid/query_window_sum.edn | 3 +++ tests/grammar/valid/query_with_clause.edn | 4 ++++ 7 files changed, 19 insertions(+) create mode 100644 tests/grammar/valid/query_aggregate_count.edn create mode 100644 tests/grammar/valid/query_aggregate_sum.edn create mode 100644 tests/grammar/valid/query_aggregate_udf.edn create mode 100644 tests/grammar/valid/query_window_partition_by.edn create mode 100644 tests/grammar/valid/query_window_rank.edn create mode 100644 tests/grammar/valid/query_window_sum.edn create mode 100644 tests/grammar/valid/query_with_clause.edn diff --git a/tests/grammar/valid/query_aggregate_count.edn b/tests/grammar/valid/query_aggregate_count.edn new file mode 100644 index 0000000000..f525b0ed41 --- /dev/null +++ b/tests/grammar/valid/query_aggregate_count.edn @@ -0,0 +1,2 @@ +(query [:find (count ?e) + :where [?e :person/name ?name]]) diff --git a/tests/grammar/valid/query_aggregate_sum.edn b/tests/grammar/valid/query_aggregate_sum.edn new file mode 100644 index 0000000000..efc97b70b2 --- /dev/null +++ b/tests/grammar/valid/query_aggregate_sum.edn @@ -0,0 +1,2 @@ +(query [:find (sum ?salary) + :where [?e :emp/salary ?salary]]) diff --git a/tests/grammar/valid/query_aggregate_udf.edn b/tests/grammar/valid/query_aggregate_udf.edn new file mode 100644 index 0000000000..4f49536c00 --- /dev/null +++ b/tests/grammar/valid/query_aggregate_udf.edn @@ -0,0 +1,2 @@ +(query [:find (geomean ?v) + :where [?e :metric/value ?v]]) diff --git a/tests/grammar/valid/query_window_partition_by.edn b/tests/grammar/valid/query_window_partition_by.edn new file mode 100644 index 0000000000..c387535d64 --- /dev/null +++ b/tests/grammar/valid/query_window_partition_by.edn @@ -0,0 +1,4 @@ +(query [:find (sum ?salary :over (:partition-by ?dept :order-by ?hire-date)) + :where [?e :emp/salary ?salary] + [?e :emp/dept ?dept] + [?e :emp/hire-date ?hire-date]]) diff --git a/tests/grammar/valid/query_window_rank.edn b/tests/grammar/valid/query_window_rank.edn new file mode 100644 index 0000000000..99ac6bed03 --- /dev/null +++ b/tests/grammar/valid/query_window_rank.edn @@ -0,0 +1,2 @@ +(query [:find (rank :over (:order-by ?salary :desc)) + :where [?e :emp/salary ?salary]]) diff --git a/tests/grammar/valid/query_window_sum.edn b/tests/grammar/valid/query_window_sum.edn new file mode 100644 index 0000000000..7ff7097ea7 --- /dev/null +++ b/tests/grammar/valid/query_window_sum.edn @@ -0,0 +1,3 @@ +(query [:find (sum ?salary :over (:order-by ?hire-date)) + :where [?e :emp/salary ?salary] + [?e :emp/hire-date ?hire-date]]) diff --git a/tests/grammar/valid/query_with_clause.edn b/tests/grammar/valid/query_with_clause.edn new file mode 100644 index 0000000000..c76e040472 --- /dev/null +++ b/tests/grammar/valid/query_with_clause.edn @@ -0,0 +1,4 @@ +(query [:find ?dept (sum ?salary) + :with ?e + :where [?e :emp/dept ?dept] + [?e :emp/salary ?salary]]) From 3957ea10ef5d5e2d3857ac443538800099cd3561 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Wed, 13 May 2026 14:56:18 +0530 Subject: [PATCH 08/14] test: add not/not-join/or/or-join corpus (Task 6) --- tests/grammar/valid/query_not.edn | 3 +++ tests/grammar/valid/query_not_join.edn | 4 ++++ tests/grammar/valid/query_or.edn | 4 ++++ tests/grammar/valid/query_or_join.edn | 5 +++++ 4 files changed, 16 insertions(+) create mode 100644 tests/grammar/valid/query_not.edn create mode 100644 tests/grammar/valid/query_not_join.edn create mode 100644 tests/grammar/valid/query_or.edn create mode 100644 tests/grammar/valid/query_or_join.edn diff --git a/tests/grammar/valid/query_not.edn b/tests/grammar/valid/query_not.edn new file mode 100644 index 0000000000..aaf6f392fb --- /dev/null +++ b/tests/grammar/valid/query_not.edn @@ -0,0 +1,3 @@ +(query [:find ?name + :where [?e :person/name ?name] + (not [?e :person/banned true])]) diff --git a/tests/grammar/valid/query_not_join.edn b/tests/grammar/valid/query_not_join.edn new file mode 100644 index 0000000000..200f1adfa5 --- /dev/null +++ b/tests/grammar/valid/query_not_join.edn @@ -0,0 +1,4 @@ +(query [:find ?name + :where [?e :person/name ?name] + (not-join [?e] + [?e :person/banned true])]) diff --git a/tests/grammar/valid/query_or.edn b/tests/grammar/valid/query_or.edn new file mode 100644 index 0000000000..911627a410 --- /dev/null +++ b/tests/grammar/valid/query_or.edn @@ -0,0 +1,4 @@ +(query [:find ?name + :where [?e :person/name ?name] + (or [?e :person/city "London"] + [?e :person/city "Paris"])]) diff --git a/tests/grammar/valid/query_or_join.edn b/tests/grammar/valid/query_or_join.edn new file mode 100644 index 0000000000..34a3122048 --- /dev/null +++ b/tests/grammar/valid/query_or_join.edn @@ -0,0 +1,5 @@ +(query [:find ?name + :where [?e :person/name ?name] + (or-join [?e] + [?e :person/role :admin] + [?e :person/role :superuser])]) From fe262cbf83cb8139ea66197997cde86e358d22c9 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Wed, 13 May 2026 14:58:10 +0530 Subject: [PATCH 09/14] test: add expression clause corpus (Task 7) --- tests/grammar/valid/query_expr_binding.edn | 5 +++++ tests/grammar/valid/query_expr_filter.edn | 4 ++++ tests/grammar/valid/query_expr_nested.edn | 5 +++++ 3 files changed, 14 insertions(+) create mode 100644 tests/grammar/valid/query_expr_binding.edn create mode 100644 tests/grammar/valid/query_expr_filter.edn create mode 100644 tests/grammar/valid/query_expr_nested.edn diff --git a/tests/grammar/valid/query_expr_binding.edn b/tests/grammar/valid/query_expr_binding.edn new file mode 100644 index 0000000000..ffb84f879c --- /dev/null +++ b/tests/grammar/valid/query_expr_binding.edn @@ -0,0 +1,5 @@ +(query [:find ?name ?total + :where [?e :person/name ?name] + [?e :order/price ?price] + [?e :order/qty ?qty] + [(* ?price ?qty) ?total]]) diff --git a/tests/grammar/valid/query_expr_filter.edn b/tests/grammar/valid/query_expr_filter.edn new file mode 100644 index 0000000000..2f184f377e --- /dev/null +++ b/tests/grammar/valid/query_expr_filter.edn @@ -0,0 +1,4 @@ +(query [:find ?name ?age + :where [?e :person/name ?name] + [?e :person/age ?age] + [(> ?age 25)]]) diff --git a/tests/grammar/valid/query_expr_nested.edn b/tests/grammar/valid/query_expr_nested.edn new file mode 100644 index 0000000000..003daea7ba --- /dev/null +++ b/tests/grammar/valid/query_expr_nested.edn @@ -0,0 +1,5 @@ +(query [:find ?name ?result + :where [?e :person/name ?name] + [?e :metric/a ?a] + [?e :metric/b ?b] + [(+ (* ?a 2) ?b) ?result]]) From a9d6a444f66a5c09fe374b82901cac5ead202182 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Wed, 13 May 2026 14:59:01 +0530 Subject: [PATCH 10/14] =?UTF-8?q?fix:=20reorder=20expr=5Fbody=20alternativ?= =?UTF-8?q?es=20=E2=80=94=20binary=5Fform=20before=20unary=5Fform=20(Task?= =?UTF-8?q?=207)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 4.6 --- tests/grammar/grammar.pest | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/grammar/grammar.pest b/tests/grammar/grammar.pest index 0d0f5006ef..e826dfe17e 100644 --- a/tests/grammar/grammar.pest +++ b/tests/grammar/grammar.pest @@ -143,7 +143,7 @@ rule_invocation = { "(" ~ plain_sym ~ edn_value* ~ ")" } // ── EXPRESSIONS ─────────────────────────────────────────────────────────── expr = { "(" ~ expr_body ~ ")" } -expr_body = { unary_form | binary_form } +expr_body = { binary_form | unary_form } unary_form = { (unary_op | op_sym | plain_sym) ~ expr_arg } binary_form = { binary_op ~ expr_arg ~ expr_arg } unary_op = @{ "string?" | "integer?" | "float?" | "boolean?" | "nil?" } From 2952f32fa13496e31a03da21adb362383c41daff Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Wed, 13 May 2026 15:02:11 +0530 Subject: [PATCH 11/14] test: add bind-slot + rule corpus and real rule grammar (Task 8) Co-Authored-By: Claude Sonnet 4.6 --- tests/grammar/grammar.pest | 7 +++++-- tests/grammar/valid/query_prepared_bind_slot.edn | 3 +++ tests/grammar/valid/rule_basic.edn | 2 ++ tests/grammar/valid/rule_recursive.edn | 3 +++ 4 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 tests/grammar/valid/query_prepared_bind_slot.edn create mode 100644 tests/grammar/valid/rule_basic.edn create mode 100644 tests/grammar/valid/rule_recursive.edn diff --git a/tests/grammar/grammar.pest b/tests/grammar/grammar.pest index e826dfe17e..546dbdf0d1 100644 --- a/tests/grammar/grammar.pest +++ b/tests/grammar/grammar.pest @@ -162,5 +162,8 @@ valid_at_section = { ":valid-at" ~ (string_lit | ":any-valid-time" | bind_ any_valid_time_section = { ":any-valid-time" } with_section = { ":with" ~ variable+ } -// ── RULE (stub — filled in Task 8) ─────────────────────────────────────── -rule_cmd = { "(" ~ "rule" ~ edn_value+ ~ ")" } +// ── RULE ────────────────────────────────────────────────────────────────── +// (rule [(head ?args) body-clause ...]) +rule_cmd = { "(" ~ "rule" ~ rule_vector ~ ")" } +rule_vector = { "[" ~ rule_head ~ where_clause* ~ "]" } +rule_head = { "(" ~ plain_sym ~ variable* ~ ")" } diff --git a/tests/grammar/valid/query_prepared_bind_slot.edn b/tests/grammar/valid/query_prepared_bind_slot.edn new file mode 100644 index 0000000000..8db6805450 --- /dev/null +++ b/tests/grammar/valid/query_prepared_bind_slot.edn @@ -0,0 +1,3 @@ +(query [:find ?name + :where [?e :person/name ?name] + [?e :person/city $city]]) diff --git a/tests/grammar/valid/rule_basic.edn b/tests/grammar/valid/rule_basic.edn new file mode 100644 index 0000000000..8a20d8ed4e --- /dev/null +++ b/tests/grammar/valid/rule_basic.edn @@ -0,0 +1,2 @@ +(rule [(reachable ?from ?to) + [?from :connected ?to]]) diff --git a/tests/grammar/valid/rule_recursive.edn b/tests/grammar/valid/rule_recursive.edn new file mode 100644 index 0000000000..1d7b4f5a5f --- /dev/null +++ b/tests/grammar/valid/rule_recursive.edn @@ -0,0 +1,3 @@ +(rule [(reachable ?from ?to) + [?from :connected ?mid] + (reachable ?mid ?to)]) From 31bf6831decee9a640d1eeeaaf3b7aa1e96f4c9d Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Wed, 13 May 2026 15:06:40 +0530 Subject: [PATCH 12/14] test: add invalid/syntax corpus (Task 9) 10 .edn files in tests/grammar/invalid/syntax/ that are rejected by both the pest grammar and the real Minigraf parser. Note: the spec suggested not_join_no_clauses.edn but pest's rule_invocation fallthrough accepts (not-join [?e]), so the 10th file is transact_no_args.edn ((transact) with no argument) which both parsers structurally reject. Co-Authored-By: Claude Sonnet 4.6 --- tests/grammar/invalid/syntax/bind_slot_empty.edn | 1 + tests/grammar/invalid/syntax/empty_command.edn | 1 + tests/grammar/invalid/syntax/keyword_invalid_chars.edn | 1 + tests/grammar/invalid/syntax/string_unterminated.edn | 1 + tests/grammar/invalid/syntax/transact_no_args.edn | 1 + tests/grammar/invalid/syntax/unclosed_bracket.edn | 1 + tests/grammar/invalid/syntax/unclosed_paren.edn | 1 + tests/grammar/invalid/syntax/unexpected_bare_char.edn | 1 + tests/grammar/invalid/syntax/unknown_command.edn | 1 + tests/grammar/invalid/syntax/unknown_tagged_literal.edn | 1 + 10 files changed, 10 insertions(+) create mode 100644 tests/grammar/invalid/syntax/bind_slot_empty.edn create mode 100644 tests/grammar/invalid/syntax/empty_command.edn create mode 100644 tests/grammar/invalid/syntax/keyword_invalid_chars.edn create mode 100644 tests/grammar/invalid/syntax/string_unterminated.edn create mode 100644 tests/grammar/invalid/syntax/transact_no_args.edn create mode 100644 tests/grammar/invalid/syntax/unclosed_bracket.edn create mode 100644 tests/grammar/invalid/syntax/unclosed_paren.edn create mode 100644 tests/grammar/invalid/syntax/unexpected_bare_char.edn create mode 100644 tests/grammar/invalid/syntax/unknown_command.edn create mode 100644 tests/grammar/invalid/syntax/unknown_tagged_literal.edn diff --git a/tests/grammar/invalid/syntax/bind_slot_empty.edn b/tests/grammar/invalid/syntax/bind_slot_empty.edn new file mode 100644 index 0000000000..cb3fa6d2c9 --- /dev/null +++ b/tests/grammar/invalid/syntax/bind_slot_empty.edn @@ -0,0 +1 @@ +(query [:find ?name :where [?e :person/name $]]) diff --git a/tests/grammar/invalid/syntax/empty_command.edn b/tests/grammar/invalid/syntax/empty_command.edn new file mode 100644 index 0000000000..6a452c185a --- /dev/null +++ b/tests/grammar/invalid/syntax/empty_command.edn @@ -0,0 +1 @@ +() diff --git a/tests/grammar/invalid/syntax/keyword_invalid_chars.edn b/tests/grammar/invalid/syntax/keyword_invalid_chars.edn new file mode 100644 index 0000000000..58da949de9 --- /dev/null +++ b/tests/grammar/invalid/syntax/keyword_invalid_chars.edn @@ -0,0 +1 @@ +(transact [[:alice :person@name "Alice"]]) diff --git a/tests/grammar/invalid/syntax/string_unterminated.edn b/tests/grammar/invalid/syntax/string_unterminated.edn new file mode 100644 index 0000000000..125e9e434e --- /dev/null +++ b/tests/grammar/invalid/syntax/string_unterminated.edn @@ -0,0 +1 @@ +(transact [[:alice :person/name "Alice]]) diff --git a/tests/grammar/invalid/syntax/transact_no_args.edn b/tests/grammar/invalid/syntax/transact_no_args.edn new file mode 100644 index 0000000000..94a8f5db46 --- /dev/null +++ b/tests/grammar/invalid/syntax/transact_no_args.edn @@ -0,0 +1 @@ +(transact) diff --git a/tests/grammar/invalid/syntax/unclosed_bracket.edn b/tests/grammar/invalid/syntax/unclosed_bracket.edn new file mode 100644 index 0000000000..0cba08b087 --- /dev/null +++ b/tests/grammar/invalid/syntax/unclosed_bracket.edn @@ -0,0 +1 @@ +(transact [[:alice :person/name "Alice" diff --git a/tests/grammar/invalid/syntax/unclosed_paren.edn b/tests/grammar/invalid/syntax/unclosed_paren.edn new file mode 100644 index 0000000000..01c8216d5e --- /dev/null +++ b/tests/grammar/invalid/syntax/unclosed_paren.edn @@ -0,0 +1 @@ +(transact [[:alice :person/name "Alice"] diff --git a/tests/grammar/invalid/syntax/unexpected_bare_char.edn b/tests/grammar/invalid/syntax/unexpected_bare_char.edn new file mode 100644 index 0000000000..98e48d1d41 --- /dev/null +++ b/tests/grammar/invalid/syntax/unexpected_bare_char.edn @@ -0,0 +1 @@ +(transact [[:alice :person/name @bad]]) diff --git a/tests/grammar/invalid/syntax/unknown_command.edn b/tests/grammar/invalid/syntax/unknown_command.edn new file mode 100644 index 0000000000..2dd1cc9a10 --- /dev/null +++ b/tests/grammar/invalid/syntax/unknown_command.edn @@ -0,0 +1 @@ +(insert [[:alice :person/name "Alice"]]) diff --git a/tests/grammar/invalid/syntax/unknown_tagged_literal.edn b/tests/grammar/invalid/syntax/unknown_tagged_literal.edn new file mode 100644 index 0000000000..bf264f024e --- /dev/null +++ b/tests/grammar/invalid/syntax/unknown_tagged_literal.edn @@ -0,0 +1 @@ +(transact [[#ref "550e8400-e29b-41d4-a716-446655440000" :entity/name "test"]]) From 3f99ae3ea0be70c046f3e8d81c8ae3dc634c2b6c Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Wed, 13 May 2026 15:47:32 +0530 Subject: [PATCH 13/14] test: add invalid/semantic corpus (Task 10) 13 fixture files where pest accepts (structural syntax is valid) but the real Minigraf parser rejects (semantic check fails): not-safety violations, nested not/or-inside-not, not-join unbound join vars, unbound expr/aggregate/:with variables, :with without aggregate, window-only func without :over, non-window func with :over, fact too-few elements, retract wrong arity, invalid UUID format. Co-Authored-By: Claude Sonnet 4.6 --- tests/grammar/invalid/semantic/aggregate_var_unbound.edn | 2 ++ tests/grammar/invalid/semantic/expr_unbound_var_filter.edn | 3 +++ tests/grammar/invalid/semantic/fact_too_few_elements.edn | 1 + tests/grammar/invalid/semantic/invalid_uuid_format.edn | 1 + tests/grammar/invalid/semantic/not_join_unbound_join_var.edn | 4 ++++ tests/grammar/invalid/semantic/not_nested_inside_not.edn | 3 +++ tests/grammar/invalid/semantic/not_safety_unbound_var.edn | 3 +++ tests/grammar/invalid/semantic/or_inside_not.edn | 4 ++++ tests/grammar/invalid/semantic/retract_wrong_arity.edn | 1 + .../invalid/semantic/window_incompatible_func_with_over.edn | 2 ++ .../invalid/semantic/window_only_func_without_over.edn | 2 ++ tests/grammar/invalid/semantic/with_var_unbound.edn | 3 +++ tests/grammar/invalid/semantic/with_without_aggregate.edn | 3 +++ 13 files changed, 32 insertions(+) create mode 100644 tests/grammar/invalid/semantic/aggregate_var_unbound.edn create mode 100644 tests/grammar/invalid/semantic/expr_unbound_var_filter.edn create mode 100644 tests/grammar/invalid/semantic/fact_too_few_elements.edn create mode 100644 tests/grammar/invalid/semantic/invalid_uuid_format.edn create mode 100644 tests/grammar/invalid/semantic/not_join_unbound_join_var.edn create mode 100644 tests/grammar/invalid/semantic/not_nested_inside_not.edn create mode 100644 tests/grammar/invalid/semantic/not_safety_unbound_var.edn create mode 100644 tests/grammar/invalid/semantic/or_inside_not.edn create mode 100644 tests/grammar/invalid/semantic/retract_wrong_arity.edn create mode 100644 tests/grammar/invalid/semantic/window_incompatible_func_with_over.edn create mode 100644 tests/grammar/invalid/semantic/window_only_func_without_over.edn create mode 100644 tests/grammar/invalid/semantic/with_var_unbound.edn create mode 100644 tests/grammar/invalid/semantic/with_without_aggregate.edn diff --git a/tests/grammar/invalid/semantic/aggregate_var_unbound.edn b/tests/grammar/invalid/semantic/aggregate_var_unbound.edn new file mode 100644 index 0000000000..59d528c69d --- /dev/null +++ b/tests/grammar/invalid/semantic/aggregate_var_unbound.edn @@ -0,0 +1,2 @@ +(query [:find (sum ?salary) + :where [?e :person/name ?name]]) diff --git a/tests/grammar/invalid/semantic/expr_unbound_var_filter.edn b/tests/grammar/invalid/semantic/expr_unbound_var_filter.edn new file mode 100644 index 0000000000..4d0df620ee --- /dev/null +++ b/tests/grammar/invalid/semantic/expr_unbound_var_filter.edn @@ -0,0 +1,3 @@ +(query [:find ?name + :where [?e :person/name ?name] + [(> ?salary 50000)]]) diff --git a/tests/grammar/invalid/semantic/fact_too_few_elements.edn b/tests/grammar/invalid/semantic/fact_too_few_elements.edn new file mode 100644 index 0000000000..8b96ebc822 --- /dev/null +++ b/tests/grammar/invalid/semantic/fact_too_few_elements.edn @@ -0,0 +1 @@ +(transact [[:alice :person/name]]) diff --git a/tests/grammar/invalid/semantic/invalid_uuid_format.edn b/tests/grammar/invalid/semantic/invalid_uuid_format.edn new file mode 100644 index 0000000000..b2ff456f9a --- /dev/null +++ b/tests/grammar/invalid/semantic/invalid_uuid_format.edn @@ -0,0 +1 @@ +(transact [[#uuid "not-a-valid-uuid" :entity/name "test"]]) diff --git a/tests/grammar/invalid/semantic/not_join_unbound_join_var.edn b/tests/grammar/invalid/semantic/not_join_unbound_join_var.edn new file mode 100644 index 0000000000..6bdb308289 --- /dev/null +++ b/tests/grammar/invalid/semantic/not_join_unbound_join_var.edn @@ -0,0 +1,4 @@ +(query [:find ?name + :where [?e :person/name ?name] + (not-join [?unbound] + [?unbound :person/banned true])]) diff --git a/tests/grammar/invalid/semantic/not_nested_inside_not.edn b/tests/grammar/invalid/semantic/not_nested_inside_not.edn new file mode 100644 index 0000000000..8fd9a54e13 --- /dev/null +++ b/tests/grammar/invalid/semantic/not_nested_inside_not.edn @@ -0,0 +1,3 @@ +(query [:find ?name + :where [?e :person/name ?name] + (not (not [?e :person/banned true]))]) diff --git a/tests/grammar/invalid/semantic/not_safety_unbound_var.edn b/tests/grammar/invalid/semantic/not_safety_unbound_var.edn new file mode 100644 index 0000000000..2dfd1d019d --- /dev/null +++ b/tests/grammar/invalid/semantic/not_safety_unbound_var.edn @@ -0,0 +1,3 @@ +(query [:find ?name + :where [?e :person/name ?name] + (not [?unbound :person/banned true])]) diff --git a/tests/grammar/invalid/semantic/or_inside_not.edn b/tests/grammar/invalid/semantic/or_inside_not.edn new file mode 100644 index 0000000000..1d8eaf8e65 --- /dev/null +++ b/tests/grammar/invalid/semantic/or_inside_not.edn @@ -0,0 +1,4 @@ +(query [:find ?name + :where [?e :person/name ?name] + (not (or [?e :person/city "London"] + [?e :person/city "Paris"]))]) diff --git a/tests/grammar/invalid/semantic/retract_wrong_arity.edn b/tests/grammar/invalid/semantic/retract_wrong_arity.edn new file mode 100644 index 0000000000..ddfb7b16ff --- /dev/null +++ b/tests/grammar/invalid/semantic/retract_wrong_arity.edn @@ -0,0 +1 @@ +(retract [[:alice :person/name "Alice" "extra"]]) diff --git a/tests/grammar/invalid/semantic/window_incompatible_func_with_over.edn b/tests/grammar/invalid/semantic/window_incompatible_func_with_over.edn new file mode 100644 index 0000000000..ad2096107c --- /dev/null +++ b/tests/grammar/invalid/semantic/window_incompatible_func_with_over.edn @@ -0,0 +1,2 @@ +(query [:find (count-distinct ?e :over (:order-by ?name)) + :where [?e :person/name ?name]]) diff --git a/tests/grammar/invalid/semantic/window_only_func_without_over.edn b/tests/grammar/invalid/semantic/window_only_func_without_over.edn new file mode 100644 index 0000000000..c9a3f5c0ca --- /dev/null +++ b/tests/grammar/invalid/semantic/window_only_func_without_over.edn @@ -0,0 +1,2 @@ +(query [:find (avg ?salary) + :where [?e :emp/salary ?salary]]) diff --git a/tests/grammar/invalid/semantic/with_var_unbound.edn b/tests/grammar/invalid/semantic/with_var_unbound.edn new file mode 100644 index 0000000000..44df685139 --- /dev/null +++ b/tests/grammar/invalid/semantic/with_var_unbound.edn @@ -0,0 +1,3 @@ +(query [:find (count ?e) + :with ?unbound + :where [?e :person/name ?name]]) diff --git a/tests/grammar/invalid/semantic/with_without_aggregate.edn b/tests/grammar/invalid/semantic/with_without_aggregate.edn new file mode 100644 index 0000000000..745aac1611 --- /dev/null +++ b/tests/grammar/invalid/semantic/with_without_aggregate.edn @@ -0,0 +1,3 @@ +(query [:find ?name + :with ?e + :where [?e :person/name ?name]]) From dd67ec4421cfaf53619d14d30fb77e9d60dc7432 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Wed, 13 May 2026 16:03:03 +0530 Subject: [PATCH 14/14] style: apply cargo fmt to grammar_conformance.rs --- tests/grammar_conformance.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/grammar_conformance.rs b/tests/grammar_conformance.rs index f555972ad8..84b6375da2 100644 --- a/tests/grammar_conformance.rs +++ b/tests/grammar_conformance.rs @@ -69,14 +69,21 @@ fn load_corpus(dir: &str) -> Vec<(String, String)> { #[allow(clippy::panic)] fn valid_corpus() { let files = load_corpus("tests/grammar/valid"); - assert!(!files.is_empty(), "valid/ corpus is empty — add .edn fixtures"); + assert!( + !files.is_empty(), + "valid/ corpus is empty — add .edn fixtures" + ); let mut failures: Vec = vec![]; for (name, content) in &files { if !pest_accepts(content) { - failures.push(format!("FAIL valid/{name}: pest rejected (expected accept)")); + failures.push(format!( + "FAIL valid/{name}: pest rejected (expected accept)" + )); } if !parser_accepts(content) { - failures.push(format!("FAIL valid/{name}: parser rejected (expected accept)")); + failures.push(format!( + "FAIL valid/{name}: parser rejected (expected accept)" + )); } } if !failures.is_empty() {