Struct sqparse::ParseError

source ·
pub struct ParseError {
    pub ty: ParseErrorType,
    pub token_index: usize,
    pub context: Option<ParseErrorContext>,
    /* private fields */
}
Expand description

An error emitted while trying to parse a token list.

Each error has a type with more information, the token where the error occurred, and possibly some contextual information.

Fields§

§ty: ParseErrorType

The type of error.

§token_index: usize

The index of the token where the error occurred.

§context: Option<ParseErrorContext>

Contextual information if available.

Implementations§

source§

impl ParseError

source

pub fn new(ty: ParseErrorType, token_index: usize) -> Self

Creates a new ParseError.

source

pub fn with_context(self, ty: ContextType, token_range: Range<usize>) -> Self

Attaches some context to the error.

source

pub fn replace_context(
    self,
    from_ty: ContextType,
    to_ty: ContextType,
    token_range: Range<usize>
) -> Self

source

pub fn display<'s>(
    &'s self,
    source: &'s str,
    tokens: &'s [TokenItem<'s>]
) -> impl Display + 's

Returns an implementation of std::fmt::Display that pretty-prints the error and context using display_error.

Examples found in repository?
examples/print_parser_error.rs (line 8)
3
4
5
6
7
8
9
fn main() {
    let source = include_str!("print_parser_error_script.nut");
    let tokens = tokenize(source, Flavor::SquirrelRespawn).unwrap();
    let parse_err = parse(&tokens).unwrap_err();

    println!("{}", parse_err.display(source, &tokens));
}
More examples
Hide additional examples
examples/print_ast.rs (line 17)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
fn main() {
    let source = include_str!("print_ast_script.nut");

    let tokens = match tokenize(source, Flavor::SquirrelRespawn) {
        Ok(tokens) => tokens,
        Err(err) => {
            eprintln!("{}", err.display(source));
            return;
        }
    };

    let ast = match parse(&tokens) {
        Ok(ast) => ast,
        Err(err) => {
            eprintln!("{}", err.display(source, &tokens));
            return;
        }
    };

    println!("{ast:#?}");
}
examples/dryrun.rs (line 53)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
fn main() {
    let mut args = std::env::args();
    let exe = args.next().unwrap();

    let base_path = match args.next() {
        Some(arg) => PathBuf::from(arg),
        None => {
            eprintln!("Usage: {exe} [path]");
            eprintln!();
            eprintln!("Provide a path to a file to parse that file, or a path to a directory to");
            eprintln!("recursively parse all .nut and .gnut files in the directory");
            std::process::exit(1);
        }
    };

    let mut total_size_bytes = 0;
    let mut total_lex_secs = 0.;
    let mut total_parse_secs = 0.;

    visit(&base_path, &mut |path| {
        let extension = path.extension().and_then(|val| val.to_str());
        if !matches!(extension, Some("nut") | Some("gnut")) {
            return;
        }

        println!("{}", path.display());

        let file_text = match std::fs::read_to_string(path) {
            Ok(text) => text,
            Err(err) => {
                println!("  could not read: {err}");
                return;
            }
        };

        let lex_start = Instant::now();
        let tokens = match tokenize(&file_text, Flavor::SquirrelRespawn) {
            Ok(tokens) => tokens,
            Err(err) => {
                eprintln!("{}", err.display(&file_text));
                std::process::exit(1);
            }
        };
        let lex_secs = lex_start.elapsed().as_secs_f64();
        println!("  tokenize: {lex_secs}s");

        let parse_start = Instant::now();
        if let Err(err) = parse(&tokens) {
            eprintln!("{}", err.display(&file_text, &tokens));
            std::process::exit(1);
        }
        let parse_secs = parse_start.elapsed().as_secs_f64();
        println!("  parse: {parse_secs}s");

        total_size_bytes += file_text.bytes().len();
        total_lex_secs += lex_secs;
        total_parse_secs += parse_secs;
    });

    let total_mb = total_size_bytes as f64 / 1048576.;
    println!("Finished!");
    println!(
        "Tokenize: {:.4}s, {:.2} MB/s",
        total_lex_secs,
        total_mb / total_lex_secs
    );
    println!(
        "Parse: {:.4}s, {:.2} MB/s",
        total_parse_secs,
        total_mb / total_parse_secs
    );
}

Trait Implementations§

source§

impl Clone for ParseError

source§

fn clone(&self) -> ParseError

Returns a copy of the value. Read more
1.0.0 · 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

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere
    T: Clone,

§

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, U> TryFrom<U> for Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.