Skip to main content

parse_many

Function parse_many 

Source
pub fn parse_many(input: &str) -> Result<Vec<Statement>, QueryError>
Expand description

The real implementation behind lib.rs’s public parse_many – parses a ;-separated batch of one or more statements ("CREATE (a); CREATE (b); MATCH (n) RETURN n"). Splits the input into individual statements itself (split_statements, respecting Cypher’s quoting rules) and parses each one independently via parse_antlr, rather than parsing the whole batch as one shared ANTLR tree the way the grammar’s own queries : query (SEMI query)* EOF rule (a mars-specific extension, see grammar/README.md) would: building one tree for a large batch means every statement’s tree is alive in memory simultaneously until the last one is converted to a lightweight Statement and the whole tree can finally drop. Confirmed via /usr/bin/time -l: a real 29MB/9,771-statement import script peaked at 13GB RSS in the parse step alone (before any execution) parsed the old way. Splitting first means only the largest single statement’s tree is ever alive at once.

Also strips a single genuinely-trailing ; first, same as before – script : query SEMI? EOF (what parse_antlr uses per statement) already tolerates one, but stripping it here first keeps split_statements from ever seeing a trailing empty segment.

Examples found in repository?
examples/parse_only.rs (line 9)
6fn main() {
7    let path = env::args().nth(1).expect("usage: parse_only <file>");
8    let input = fs::read_to_string(&path).unwrap();
9    let stmts = marsdb_query::parse_many(&input).unwrap();
10    println!("parsed {} statements", stmts.len());
11}