pub struct ParseError {
pub ty: ParseErrorType,
pub token_index: usize,
pub token_affinity: TokenAffinity,
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: ParseErrorTypeThe type of error.
token_index: usizeThe index of the token where the error occurred.
token_affinity: TokenAffinityAffinity of the token.
context: Option<ParseErrorContext>Contextual information if available.
Implementations§
Source§impl ParseError
impl ParseError
Sourcepub fn new(
ty: ParseErrorType,
token_index: usize,
token_affinity: TokenAffinity,
) -> Self
pub fn new( ty: ParseErrorType, token_index: usize, token_affinity: TokenAffinity, ) -> Self
Creates a new ParseError.
Sourcepub fn with_context(
self,
ty: ContextType,
token_range: Range<usize>,
end_affinity: TokenAffinity,
) -> Self
pub fn with_context( self, ty: ContextType, token_range: Range<usize>, end_affinity: TokenAffinity, ) -> Self
Attaches some context to the error.
Sourcepub fn replace_context(
self,
from_ty: ContextType,
to_ty: ContextType,
token_range: Range<usize>,
end_affinity: TokenAffinity,
) -> Self
pub fn replace_context( self, from_ty: ContextType, to_ty: ContextType, token_range: Range<usize>, end_affinity: TokenAffinity, ) -> Self
Replaces an existing context with a new one, if it matches.
Sourcepub fn display<'s>(
&'s self,
source: &'s str,
tokens: &'s [TokenItem<'s>],
file_name: Option<&'s str>,
) -> impl Display + 's
pub fn display<'s>( &'s self, source: &'s str, tokens: &'s [TokenItem<'s>], file_name: Option<&'s str>, ) -> impl Display + 's
Returns an implementation of std::fmt::Display that pretty-prints the error and context
using display_annotations.
Examples found in repository?
More examples
examples/print_ast.rs (line 19)
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 53)
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 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 · 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 Freeze for ParseError
impl RefUnwindSafe for ParseError
impl Send for ParseError
impl Sync for ParseError
impl Unpin 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