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.
§Examples
See Self::new, Self::new_range and Self::with_cause.
Implementations§
Source§impl ParseError
impl ParseError
Sourcepub fn with_cause(self, cause: impl Error + 'static) -> Self
pub fn with_cause(self, cause: impl Error + 'static) -> Self
Retains an immutable frontend cause without replacing its concrete error type.
Cloning the parse error shares this cause. Conversion to CELError retains
only the message and primary span for thread-safe reporting.
§Examples
use std::error::Error;
let error = cel_parser::ParseError::new("frontend", proc_macro2::Span::call_site())
.with_cause(std::io::Error::other("original"));
assert!(error.source().unwrap().is::<std::io::Error>());Sourcepub fn new(message: impl Into<String>, span: Span) -> Self
pub fn new(message: impl Into<String>, span: Span) -> Self
Creates a new parse error with the given message and token span.
- Complexity: O(m) time and space for m borrowed message bytes; transferring a
Stringis O(1). Other message types contribute theirInto<String>conversion cost.
§Examples
use proc_macro2::Span;
use cel_parser::ParseError;
let e = ParseError::new("unexpected token", Span::call_site());
assert_eq!(e.message(), "unexpected token");Sourcepub fn new_range(message: impl Into<String>, start: Span, end: Span) -> Self
pub fn new_range(message: impl Into<String>, start: Span, end: Span) -> Self
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.
- Complexity: Matches the message conversion in
Self::new.
§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());Sourcepub 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.
§Examples
use cel_parser::ParseError;
let error = ParseError::new("bad name", proc_macro2::Span::call_site());
assert_eq!(error.span().start().column, 0);Sourcepub fn end_span(&self) -> Option<Span>
pub fn end_span(&self) -> Option<Span>
Returns the end span of this error, or None for single-point errors.
Some for errors created with new_range;
None for errors created with new.
§Examples
See Self::new_range.
Sourcepub fn with_secondary(self, secondary: Vec<SpanLabel>) -> Self
pub fn with_secondary(self, secondary: Vec<SpanLabel>) -> Self
Attaches secondary labelled spans, rendered as extra carets alongside the primary.
- Complexity: O(k) to release k previously attached labels; transfers the new vector.
§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");Sourcepub 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);Sourcepub fn from_lex_error(source: &str, err: LexError) -> Self
pub fn from_lex_error(source: &str, err: LexError) -> Self
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).
- Complexity: O(s) inspection for s source bytes, plus lexer error display when needed; O(s) auxiliary/output space in delimiter classification.
§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 `)`");Sourcepub 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.
- Complexity: O(k * s + l) annotation work for k total labels, s source bytes and l secondary-label bytes, plus renderer layout; O(k + l + s + b) auxiliary/output space for b output bytes.
§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§
Source§impl Clone for ParseError
impl Clone for ParseError
Source§impl Debug for ParseError
impl Debug for ParseError
Source§impl Display for ParseError
impl Display for ParseError
Source§impl Error for ParseError
impl Error for ParseError
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Borrows the retained original frontend cause, if present.
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
Source§impl From<ParseError> for CELError
Converts a ParseError to a CELError by extracting the
SourceSpan from the token span.
impl From<ParseError> for CELError
Converts a ParseError to a CELError by extracting the
SourceSpan from the token span.
Use this at async/runtime boundaries where Send + Sync is required.
Source§fn from(e: ParseError) -> Self
fn from(e: ParseError) -> Self
Transfers the message and complete primary range into a thread-safe error.
- Complexity: O(k) to release k secondary labels, plus releasing the retained cause.