pub struct ParseError { /* private fields */ }Expand description
A parse that reported at least one error.
There is no AST to carry: the parser returns no tree once it has reported an error, so only the diagnostics survive.
Implementations§
Source§impl ParseError
impl ParseError
Sourcepub fn diagnostics(&self) -> &[ResolvedDiagnostic]
pub fn diagnostics(&self) -> &[ResolvedDiagnostic]
Every diagnostic recorded during the parse, in emission order.
Sourcepub fn error_count(&self) -> u32
pub fn error_count(&self) -> u32
How many of the diagnostics are errors. Greater than zero for every
ParseError the parser produces — it fails only after reporting.
Sourcepub fn messages(&self) -> Vec<String>
pub fn messages(&self) -> Vec<String>
The diagnostics rendered one string each, LLVM-style (location line, message, source line, caret), without ANSI colors.
Each string already ends with a newline — it is multi-line, and
hermes_support::render::render_diagnostic terminates every line it
writes. Print them with print!/eprint!; println! adds a blank
line between diagnostics.
use hermes_parser::{parse, ParseFlags};
let err = parse("1 +", ParseFlags::default()).expect_err("bad parse");
for m in err.messages() {
assert!(m.ends_with('\n'), "{m:?}");
eprint!("{m}");
}Examples found in repository?
examples/parse_to_estree_json.rs (line 52)
21fn main() {
22 let path = std::env::args().nth(1);
23 let (name, source) = match &path {
24 Some(p) => (
25 p.as_str(),
26 std::fs::read_to_string(p).unwrap_or_else(|e| {
27 eprintln!("cannot read '{p}': {e}");
28 std::process::exit(1);
29 }),
30 ),
31 None => (
32 "<builtin>",
33 "function greet(name) { return 'Hello, ' + name; }".to_string(),
34 ),
35 };
36
37 // Plain ECMAScript. A file extension says nothing about the dialect, so
38 // like `hermesc` this example assumes none; set the flags explicitly for
39 // the others, e.g.:
40 // ParseFlags { parse_flow: true, ..Default::default() } // Flow
41 // ParseFlags { parse_ts: true, ..Default::default() } // TypeScript
42 // ParseFlags { parse_jsx: true, ..Default::default() } // JSX
43 let flags = ParseFlags::default();
44
45 match parse_named(&source, name, flags) {
46 Ok(mut parsed) => print!("{}", parsed.to_estree_json(true)),
47 Err(e) => {
48 // `Display` is the one-line summary; `messages()` is the full
49 // LLVM-style rendering, which is what a CLI wants. Each string is
50 // already newline-terminated, so this is `eprint!`, not
51 // `eprintln!`.
52 for m in e.messages() {
53 eprint!("{m}");
54 }
55 std::process::exit(1);
56 }
57 }
58}Trait Implementations§
Source§impl Clone for ParseError
impl Clone for ParseError
Source§fn clone(&self) -> ParseError
fn clone(&self) -> ParseError
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ParseError
impl Debug for ParseError
Source§impl Display for ParseError
impl Display for ParseError
Source§impl Error for ParseError
impl Error for ParseError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0:
use the Display impl or to_string()
Auto Trait Implementations§
impl Freeze for ParseError
impl RefUnwindSafe for ParseError
impl Send for ParseError
impl Sync for ParseError
impl Unpin for ParseError
impl UnsafeUnpin for ParseError
impl UnwindSafe for ParseError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more