Skip to main content

parse_xpath

Function parse_xpath 

Source
pub fn parse_xpath(src: &str) -> Result<Expr>
Expand description

Parse an XPath 1.0 expression string into its AST representation using the default strict options. See parse_xpath_with to opt into libxml2-compatible lexing.

Examples found in repository?
examples/probe_parser_rss.rs (line 36)
23fn main() {
24    let path = std::env::args().nth(1).expect("usage: probe_parser_rss <input-path>");
25    let bytes = std::fs::read(&path).expect("read input");
26    let s = std::str::from_utf8(&bytes).expect("UTF-8");
27    let iters: usize = std::env::args().nth(2)
28        .and_then(|s| s.parse().ok()).unwrap_or(1_000_000);
29
30    println!("input: {} bytes, {} iters", s.len(), iters);
31    println!("rss start:  {:>8.1} MB", rss_bytes() as f64 / 1_048_576.0);
32
33    let t0 = Instant::now();
34    let mid = iters / 2;
35    for i in 0..iters {
36        let _ = parse_xpath(s);
37        if i == mid {
38            println!("rss mid:    {:>8.1} MB  (after {} parses, {:.2?})",
39                rss_bytes() as f64 / 1_048_576.0, i, t0.elapsed());
40        }
41    }
42    let dt = t0.elapsed();
43    println!("rss end:    {:>8.1} MB  (after {} parses, {:.2?}, {:.0} parses/sec)",
44        rss_bytes() as f64 / 1_048_576.0, iters, dt,
45        iters as f64 / dt.as_secs_f64());
46}
More examples
Hide additional examples
examples/profile_xpath_slow_unit.rs (line 72)
59fn main() {
60    let path = std::env::args().nth(1)
61        .expect("usage: profile_xpath_slow_unit <artifact-path>");
62    let bytes = std::fs::read(&path).expect("read artifact");
63    let full = std::str::from_utf8(&bytes).expect("artifact is UTF-8");
64
65    let doc = parse_str(FIXTURE, &ParseOptions::default()).expect("fixture parses");
66    let ctx = XPathContext::new(&doc);
67
68    // Report the predicate-nesting depth of the original artifact so we
69    // can pick a sensible MAX_PREDICATE_NESTING_DEPTH.
70    use sup_xml_core::xpath::ast::max_predicate_nesting;
71    use sup_xml_core::xpath::parse_xpath;
72    match parse_xpath(full) {
73        Ok(e)  => println!("# original artifact parses; predicate-nesting depth = {}", max_predicate_nesting(&e)),
74        Err(e) => println!("# original artifact parse-error: {}",
75                           e.message.chars().take(80).collect::<String>()),
76    }
77
78    println!("# best-of-3 timing of slow-unit reductions");
79    println!("{:>9}  {:>8}  {}", "time", "len", "expression  →  result");
80    println!("{}", "-".repeat(78));
81
82    // ── baseline ──────────────────────────────────────────────────────────
83    time_one(&ctx, "FULL artifact (1018 B)", full);
84
85    // ── strip from the right, halving each time ──────────────────────────
86    time_one(&ctx, "first 512 B",  &full[..full.len().min(512)]);
87    time_one(&ctx, "first 256 B",  &full[..full.len().min(256)]);
88    time_one(&ctx, "first 128 B",  &full[..full.len().min(128)]);
89    time_one(&ctx, "first  64 B",  &full[..full.len().min(64)]);
90
91    // ── isolate specific patterns ────────────────────────────────────────
92    time_one(&ctx, "just nested substring chains",
93        "r*substring(/*substring(/*substring(/*substring(/*substring(/*substring(/*substring(//book[author='Hu'])))))))");
94    time_one(&ctx, "deep //*/*/* descent chain",
95        "//*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*");
96    time_one(&ctx, "nested predicates (the worst case)",
97        "//*[//*[//*[//*[//*[//*[//*[.='x']]]]]]]");
98    time_one(&ctx, "nested //*/* in predicate",
99        "//*[/*//*//*//*//*//*//*//*//*//*]");
100    time_one(&ctx, "wildcard arithmetic chain",
101        "//*/*/*/* * * * * * * * * * //*/*/*");
102    time_one(&ctx, "many top-level *",
103        "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
104
105    // ── single-step costs for comparison ─────────────────────────────────
106    time_one(&ctx, "trivial root",                                 "/");
107    time_one(&ctx, "trivial wildcard",                             "/*");
108    time_one(&ctx, "all elements",                                 "//*");
109    time_one(&ctx, "all elements with trivial predicate",          "//*[true()]");
110    time_one(&ctx, "all elements with attribute access",           "//*[@id]");
111    time_one(&ctx, "books filtered by author",                     "//book[author='Hunt']");
112    time_one(&ctx, "substring on string literal",                  "substring('hello', 2, 3)");
113    time_one(&ctx, "substring on node text",                       "substring(//title, 2, 3)");
114}