Struct ParseError
pub struct ParseError { /* private fields */ }Expand description
A parse error carrying the original proc_macro2::Span of the offending token or
expression start, plus an optional end span for range errors.
Used as the return type of all parser methods. Not Send + Sync because
proc_macro2::Span wraps a compiler-internal handle that is only valid on
the proc-macro thread. Convert to [CELError] via From when the error
must cross thread boundaries or be stored for async reporting.
Implementations§
§impl ParseError
impl ParseError
pub fn new(message: impl Into<String>, span: Span) -> ParseError
pub fn new(message: impl Into<String>, span: Span) -> ParseError
Creates a new parse error with the given message and token span.
§Examples
use proc_macro2::Span;
use cel_parser::ParseError;
let e = ParseError::new("unexpected token", Span::call_site());
assert_eq!(e.message(), "unexpected token");pub fn new_range(
message: impl Into<String>,
start: Span,
end: Span,
) -> ParseError
pub fn new_range( message: impl Into<String>, start: Span, end: Span, ) -> ParseError
Creates a parse error spanning a sub-expression from start to end.
Use this for op-lookup failures where the error implicates a full
sub-expression rather than a single token. At runtime the two spans
are merged into a contiguous underline; at compile time two separate
compile_error!() diagnostics are emitted.
§Examples
use proc_macro2::Span;
use cel_parser::ParseError;
let start = Span::call_site();
let end = Span::call_site();
let e = ParseError::new_range("type mismatch", start, end);
assert_eq!(e.message(), "type mismatch");
assert!(e.end_span().is_some());pub fn span(&self) -> Span
pub fn span(&self) -> Span
Returns the proc_macro2::Span of the offending token.
Use this span with quote_spanned! in proc-macro code to attach the
compile_error! to the exact source location.
pub fn with_secondary(self, secondary: Vec<SpanLabel>) -> ParseError
pub fn with_secondary(self, secondary: Vec<SpanLabel>) -> ParseError
Attaches secondary labelled spans, rendered as extra carets alongside the primary.
§Examples
use proc_macro2::Span;
use cel_parser::{ParseError, SourceSpan, SpanLabel};
let e = ParseError::new("bad", Span::call_site()).with_secondary(vec![SpanLabel {
span: SourceSpan::new(1, 4, 1, 7),
label: "also here".into(),
}]);
assert_eq!(e.message(), "bad");pub fn secondary(&self) -> &[SpanLabel]
pub fn secondary(&self) -> &[SpanLabel]
Returns the secondary labelled spans attached to this error.
§Examples
use cel_parser::{ParseError, SourceSpan, SpanLabel};
use proc_macro2::Span;
let e = ParseError::new("bad", Span::call_site()).with_secondary(vec![SpanLabel {
span: SourceSpan::new(1, 0, 1, 3),
label: "related".into(),
}]);
assert_eq!(e.secondary().len(), 1);pub fn from_lex_error(source: &str, err: LexError) -> ParseError
pub fn from_lex_error(source: &str, err: LexError) -> ParseError
Converts a lex failure (e.g. from proc_macro2::TokenStream::from_str) into a
ParseError with a message describing the likely cause.
proc_macro2::LexError’s Display is the same generic string (“cannot parse string
into token stream”) regardless of cause, but its span reliably points at the offending
character. This inspects that character in source to report why lexing failed —
unclosed/unexpected delimiter, unterminated string or character literal, unterminated
block comment, or another invalid character — falling back to err’s own message only
when the span doesn’t resolve to a character in source (e.g. it points past the end).
§Examples
use cel_parser::ParseError;
use proc_macro2::TokenStream;
use std::str::FromStr;
let source = "(1 + 2";
let lex_err = TokenStream::from_str(source).unwrap_err();
let err = ParseError::from_lex_error(source, lex_err);
assert_eq!(err.message(), "unclosed delimiter `(`: expected a matching `)`");pub fn format_rustc_style(
&self,
source_code: &str,
filename: &str,
start_line: u32,
renderer: &Renderer,
) -> String
pub fn format_rustc_style( &self, source_code: &str, filename: &str, start_line: u32, renderer: &Renderer, ) -> String
Formats this error in rustc diagnostic style with source context.
Identical contract to [CELError::format_rustc_style] when no secondary spans are
attached; prefer calling this directly on a ParseError rather than converting to
CELError first when you have the source text at hand. When secondaries have been
added via with_secondary, renders a multi-span diagnostic
(the primary span plus one context caret per secondary) via [format_multi_span].
§Examples
use annotate_snippets::Renderer;
use cel_parser::CELParser;
use cel_parser::OpLookup;
let line = line!() + 1;
let source = "10 + 20 30";
let mut parser = CELParser::new(OpLookup::new());
if let Err(e) = parser.parse_str(source) {
println!("{}", e.format_rustc_style(source, file!(), line, &Renderer::plain()));
}Trait Implementations§
§impl Clone for ParseError
impl Clone for ParseError
§fn clone(&self) -> ParseError
fn clone(&self) -> ParseError
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more§impl Debug for ParseError
impl Debug for ParseError
§impl Display for ParseError
impl Display for ParseError
§impl Error for ParseError
impl Error for ParseError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()