LexerError

Struct LexerError 

Source
pub struct LexerError<'s> {
    pub ty: LexerErrorType<'s>,
    pub range: Range<usize>,
}
Expand description

An error emitted while trying to tokenize an input string.

Each error has a type with more information, and a range indicating where in the source string the error occurred.

Fields§

§ty: LexerErrorType<'s>

The type of error.

§range: Range<usize>

The character range of where the error occurred.

Implementations§

Source§

impl<'s> LexerError<'s>

Source

pub fn new(ty: LexerErrorType<'s>, range: Range<usize>) -> Self

Creates a new LexerError.

Source

pub fn display<'a>( &'a self, source: &'a str, file_name: Option<&'s str>, ) -> impl Display + 'a

Returns an implementation of std::fmt::Display that pretty-prints the error with source context using display_annotations.

Examples found in repository?
examples/print_lexer_error.rs (line 9)
3fn main() {
4    let source = include_str!("print_lexer_error_script.nut");
5    let tokens_err = tokenize(source, Flavor::SquirrelRespawn).unwrap_err();
6
7    println!(
8        "{}",
9        tokens_err.display(source, Some("print_lexer_error_script.nut"))
10    );
11}
More examples
Hide additional examples
examples/print_ast.rs (line 9)
3fn main() {
4    let source = include_str!("print_ast_script.nut");
5
6    let tokens = match tokenize(source, Flavor::SquirrelRespawn) {
7        Ok(tokens) => tokens,
8        Err(err) => {
9            eprintln!("{}", err.display(source, Some("print_ast_script.nut")));
10            return;
11        }
12    };
13
14    let ast = match parse(&tokens) {
15        Ok(ast) => ast,
16        Err(err) => {
17            eprintln!(
18                "{}",
19                err.display(source, &tokens, Some("print_ast_script.nut"))
20            );
21            return;
22        }
23    };
24
25    println!("{ast:#?}");
26}
examples/dryrun.rs (line 44)
5fn main() {
6    let mut args = std::env::args();
7    let exe = args.next().unwrap();
8
9    let base_path = match args.next() {
10        Some(arg) => PathBuf::from(arg),
11        None => {
12            eprintln!("Usage: {exe} [path]");
13            eprintln!();
14            eprintln!("Provide a path to a file to parse that file, or a path to a directory to");
15            eprintln!("recursively parse all .nut and .gnut files in the directory");
16            std::process::exit(1);
17        }
18    };
19
20    let mut total_size_bytes = 0;
21    let mut total_lex_secs = 0.;
22    let mut total_parse_secs = 0.;
23
24    visit(&base_path, &mut |path| {
25        let extension = path.extension().and_then(|val| val.to_str());
26        if !matches!(extension, Some("nut") | Some("gnut")) {
27            return;
28        }
29
30        println!("{}", path.display());
31
32        let file_text = match std::fs::read_to_string(path) {
33            Ok(text) => text,
34            Err(err) => {
35                println!("  could not read: {err}");
36                return;
37            }
38        };
39
40        let lex_start = Instant::now();
41        let tokens = match tokenize(&file_text, Flavor::SquirrelRespawn) {
42            Ok(tokens) => tokens,
43            Err(err) => {
44                eprintln!("{}", err.display(&file_text, path.to_str()));
45                std::process::exit(1);
46            }
47        };
48        let lex_secs = lex_start.elapsed().as_secs_f64();
49        println!("  tokenize: {lex_secs}s");
50
51        let parse_start = Instant::now();
52        if let Err(err) = parse(&tokens) {
53            eprintln!("{}", err.display(&file_text, &tokens, path.to_str()));
54            std::process::exit(1);
55        }
56        let parse_secs = parse_start.elapsed().as_secs_f64();
57        println!("  parse: {parse_secs}s");
58
59        total_size_bytes += file_text.bytes().len();
60        total_lex_secs += lex_secs;
61        total_parse_secs += parse_secs;
62    });
63
64    let total_mb = total_size_bytes as f64 / 1048576.;
65    println!("Finished!");
66    println!(
67        "Tokenize: {:.4}s, {:.2} MB/s",
68        total_lex_secs,
69        total_mb / total_lex_secs
70    );
71    println!(
72        "Parse: {:.4}s, {:.2} MB/s",
73        total_parse_secs,
74        total_mb / total_parse_secs
75    );
76}

Trait Implementations§

Source§

impl<'s> Clone for LexerError<'s>

Source§

fn clone(&self) -> LexerError<'s>

Returns a duplicate 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<'s> Debug for LexerError<'s>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'s> Freeze for LexerError<'s>

§

impl<'s> RefUnwindSafe for LexerError<'s>

§

impl<'s> Send for LexerError<'s>

§

impl<'s> Sync for LexerError<'s>

§

impl<'s> Unpin for LexerError<'s>

§

impl<'s> UnwindSafe for LexerError<'s>

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, 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.