pub fn parse(source: &str, flags: ParseFlags) -> Result<ParsedJS, ParseError>Expand description
Parse source as a script named "input" in diagnostics.
See parse_named, which this calls, for the details.
use hermes_parser::{parse, ParseFlags};
let parsed = parse("let x = 1;", ParseFlags::default()).unwrap();Examples found in repository?
examples/walk_ast.rs (line 54)
52fn main() {
53 let flags = ParseFlags::default();
54 let mut parsed = parse(SOURCE, flags).expect("snippet must parse");
55
56 // The AST is only reachable while the arena is locked, so collect owned
57 // data inside the closure and return it.
58 let counts = parsed.with_program(|_gc, program| {
59 let mut hist = Histogram {
60 counts: HashMap::new(),
61 };
62 hist.visit_node(program);
63 hist.counts
64 });
65
66 let mut rows: Vec<(NodeKind, usize)> = counts.into_iter().collect();
67 // Most frequent first; ties broken by kind name for a stable listing.
68 rows.sort_by(|a, b| {
69 b.1.cmp(&a.1)
70 .then_with(|| format!("{:?}", a.0).cmp(&format!("{:?}", b.0)))
71 });
72
73 let total: usize = rows.iter().map(|(_, n)| n).sum();
74 println!("{total} nodes, {} distinct kinds", rows.len());
75 for (kind, n) in rows {
76 println!("{:<24} {:>3} {}", format!("{kind:?}"), n, "#".repeat(n));
77 }
78}