Skip to main content

ParseError

Struct ParseError 

Source
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

Source

pub fn diagnostics(&self) -> &[ResolvedDiagnostic]

Every diagnostic recorded during the parse, in emission order.

Source

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.

Source

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

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl Debug for ParseError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for ParseError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

A single line — count plus the first error’s location and text — as error types are expected to produce. The full LLVM-style rendering (source line and caret) is messages; the structured form is diagnostics.

Source§

impl Error for ParseError

1.30.0 · Source§

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

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.