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: ParseErrorTypeThe type of error.
token_index: usizeThe index of the token where the error occurred.
context: Option<ParseErrorContext>Contextual information if available.
Implementations§
source§impl ParseError
impl ParseError
sourcepub fn new(ty: ParseErrorType, token_index: usize) -> Self
pub fn new(ty: ParseErrorType, token_index: usize) -> Self
Creates a new ParseError.
sourcepub fn with_context(self, ty: ContextType, token_range: Range<usize>) -> Self
pub fn with_context(self, ty: ContextType, token_range: Range<usize>) -> Self
Attaches some context to the error.
pub fn replace_context(
self,
from_ty: ContextType,
to_ty: ContextType,
token_range: Range<usize>
) -> Self
sourcepub fn display<'s>(
&'s self,
source: &'s str,
tokens: &'s [TokenItem<'s>]
) -> impl Display + 's
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?
More 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
impl Clone for ParseError
source§fn clone(&self) -> ParseError
fn clone(&self) -> ParseError
Returns a copy 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 more