Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- **Keywords may contain `?`**: `:person/alive?` now lexes as a single keyword. Previously `?` terminated the keyword, so `[:e :alive? true]` lexed as `:alive` followed by a stray `?` symbol and was rejected as a four-element fact — reporting `Optional 4th element of a fact must be a map`, which named the value rather than the keyword. `?` is a constituent character in EDN keywords and predicate-style names (`:artist/dead?`) are idiomatic. Query variables are unaffected: a `?` not preceded by `:` still begins a symbol. `tests/grammar/grammar.pest` updated to match.

- **The parser no longer silently discards trailing input after a complete form** (#305). `parse_edn` returned successfully as soon as it parsed one complete top-level value, without checking whether any tokens remained — so `[1 2 3] garbage ) ] } 12345` parsed as `[1 2 3]` with everything after it dropped, no error, no indication anything was ignored. `parse_edn` now errors if tokens remain once the top-level value is parsed. Separately, `(query [...])` and `(rule [...])` accepted and silently ignored any arguments after their vector, the same defect `(transact ...)`/`(retract ...)` already closed for #303 — so `(query [:find ...] :max-results 5)` parsed as the query alone, discarding `:max-results 5` (the correct place for that clause is *inside* the vector: `[:find ... :max-results 5]`) rather than rejecting the malformed call. Both now reject extra trailing arguments with the same "unexpected trailing argument(s)" wording `transact`/`retract` already use.

### Performance

- **`BrowserDb` no longer allocates a redundant LRU page cache** (#275): `open_in_memory()`, `open()`, and `import_graph()` now pass page cache capacity `0` to `PersistentFactStorage::new` instead of `256`. The backing `BrowserBufferBackend` is already a `HashMap`-resident, fully-loaded page store, so every `PageCache` hit was a second RAM-to-RAM copy plus LRU bookkeeping for no benefit.
Expand Down
91 changes: 90 additions & 1 deletion src/query/datalog/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,14 @@ impl Parser {
pub fn parse_edn(input: &str) -> Result<EdnValue, String> {
let tokens = tokenize(input)?;
let mut parser = Parser::new(tokens);
parser.parse_value()
let value = parser.parse_value()?;
if let Some(token) = parser.peek() {
return Err(format!(
"unexpected trailing input after a complete form: {:?}",
token
));
}
Ok(value)
}

/// Parse a Datalog command from EDN
Expand Down Expand Up @@ -735,6 +742,13 @@ fn parse_query(elements: &[EdnValue]) -> Result<DatalogCommand, String> {
return Err("Query requires a map argument".to_string());
}

if elements.len() > 1 {
return Err(format!(
"query takes (query [...]); found {} unexpected trailing argument(s)",
elements.len() - 1
));
}

// SAFETY: is_empty() check above guarantees index 0 exists
#[allow(clippy::indexing_slicing)]
let query_vector = elements[0]
Expand Down Expand Up @@ -1836,6 +1850,13 @@ fn parse_rule(elements: &[EdnValue]) -> Result<DatalogCommand, String> {
return Err("Rule must have a body".to_string());
}

if elements.len() > 1 {
return Err(format!(
"rule takes (rule [...]); found {} unexpected trailing argument(s)",
elements.len() - 1
));
}

// Parse the rule body (single vector containing head + body patterns)
// SAFETY: is_empty() check above guarantees index 0 exists
#[allow(clippy::indexing_slicing)]
Expand Down Expand Up @@ -2606,6 +2627,74 @@ mod tests {
);
}

#[test]
fn test_parse_edn_rejects_trailing_tokens_after_complete_form() {
// A syntactically complete top-level form followed by garbage tokens
// must be a hard error, not silently truncated parsing (#305).
let result = parse_edn("[1 2 3] garbage-trailing-tokens ) ] } 12345");
assert!(
result.is_err(),
"trailing tokens after a complete form must be rejected"
);
let msg = result.unwrap_err();
assert!(
msg.contains("unexpected trailing input"),
"wrong error: {}",
msg
);
}

#[test]
fn test_parse_datalog_command_rejects_trailing_tokens_after_complete_form() {
let result = parse_datalog_command(
r#"(query [:find ?e :where [?e :entity-type :type/commit]]) garbage-trailing-tokens ) ] } 12345"#,
);
assert!(
result.is_err(),
"trailing tokens after a complete command must be rejected"
);
let msg = result.unwrap_err();
assert!(
msg.contains("unexpected trailing input"),
"wrong error: {}",
msg
);
}

#[test]
fn test_parse_query_rejects_extra_argument_after_vector() {
// :max-results must be a keyword INSIDE the query vector; putting it
// after the vector as a sibling argument must not be silently dropped (#305).
let result =
parse_datalog_command(r#"(query [:find ?e :where [?e :a :b]] :max-results 5)"#);
assert!(
result.is_err(),
"extra trailing argument to query must be rejected, not silently ignored"
);
let msg = result.unwrap_err();
assert!(
msg.contains("unexpected trailing argument"),
"wrong error: {}",
msg
);
}

#[test]
fn test_parse_rule_rejects_extra_argument_after_vector() {
let result =
parse_datalog_command(r#"(rule [(reachable ?a ?b) [?a :edge ?b]] :unexpected-extra)"#);
assert!(
result.is_err(),
"extra trailing argument to rule must be rejected, not silently ignored"
);
let msg = result.unwrap_err();
assert!(
msg.contains("unexpected trailing argument"),
"wrong error: {}",
msg
);
}

#[test]
fn test_parse_limits_order_independent() {
// :max-derived-facts before :find — order should not matter
Expand Down
Loading