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>
impl<'s> LexerError<'s>
Sourcepub fn new(ty: LexerErrorType<'s>, range: Range<usize>) -> Self
pub fn new(ty: LexerErrorType<'s>, range: Range<usize>) -> Self
Creates a new LexerError.
Sourcepub fn display<'a>(
&'a self,
source: &'a str,
file_name: Option<&'s str>,
) -> impl Display + 'a
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?
More 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>
impl<'s> Clone for LexerError<'s>
Source§fn clone(&self) -> LexerError<'s>
fn clone(&self) -> LexerError<'s>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto 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> 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