Enum ParseError
#[non_exhaustive]pub enum ParseError {
UnexpectedToken {
token_type: CompactString,
token_value: CompactString,
line: Option<u32>,
column: Option<u32>,
pos_in_stream: Option<u32>,
end_line: Option<u32>,
end_column: Option<u32>,
end_pos: Option<u32>,
expected: Vec<String>,
state: Option<u32>,
},
UnexpectedCharacters {
char_pos: u32,
line: u32,
column: u32,
ch: char,
allowed: Vec<String>,
state: Option<u32>,
},
Callback {
message: String,
},
Postlex {
message: String,
},
UnexpectedEOF {
expected: Vec<String>,
},
CustomLex {
message: String,
payload: Option<ForeignError>,
},
Unsupported {
message: String,
},
Configuration {
message: String,
},
}Expand description
Structured parse failure, mirroring Lark’s UnexpectedInput subclasses.
$END rejection raises UnexpectedToken with the $END token exactly
as Lark’s LALR does — UnexpectedEOF is
Earley-only and the POC’s rewrite is a named parity bug we do not
replicate.
#[non_exhaustive]: this enum has grown repeatedly (Postlex, CustomLex,
Unsupported, Configuration all landed after the original set) and will
again, so out-of-crate matches must carry a wildcard arm. Free to add before
the first published release, permanently breaking after — same reasoning as
Ambiguity.
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
UnexpectedToken
Token (or synthetic $END) with no action in the current state.
expected carries Lark’s precise accepts set (what its message
shows): computed by callback-free trial feeds over a copy of the
parser state, NOT the raw
state-table row (which over-approximates and omits $END). Sorted.
Fields
token_type: CompactStringThe offending token; $END carries positions aliased from the
last real token (corpus contract). All six positions ride along
(Lark’s error token keeps its end_* trio too — with a custom
source they are caller-supplied data).
token_value: CompactStringstate: Option<u32>The LALR parser state at the failure (ParserState::position()), for
match_examples error classification. None for non-LALR origins /
grammar-build synthesized errors. State ids are implementation-defined
but consistent within a parser instance — the only property
match_examples relies on.
UnexpectedCharacters
Lexer failure surfaced through the parse path (wraps
LexError::UnexpectedCharacters; the allowed set here is the
parser-context one where applicable, else the basic-lexer set).
Fields
state: Option<u32>The LALR parser state at the lex failure (None for non-LALR origins).
Companion to ParseError::UnexpectedToken’s state, for
match_examples on a lexer-level error.
Callback
A user callback/transformer failed.
Postlex
A postlex (indenter) session failed mid-stream — Lark’s DedentError
(a LarkError), raised by the indent state machine on an
unexpected dedent. Carries the verbatim message; the conformance runner
surfaces it as the DedentError exception class (Lark canonicalizes it to
just the class name — no position fields).
UnexpectedEOF
Earley-only end-of-input failure (UnexpectedEOF): the
whole input was consumed but no start-symbol derivation completed. This is
genuine Earley behaviour (unrelated to the LALR $END
→UnexpectedToken path, which the POC’s UnexpectedEOF rewrite is a named
parity bug for). expected carries the accepts set Lark
shows.
CustomLex
A custom crate::custom::TokenSource failed mid-pull with an
arbitrary (non-lex-structured) error. payload is the opaque
identity-preserving channel: in Lark, a custom lexer’s own exception
escapes parse unwrapped, so a binding stores its host
exception here and re-raises it by downcast. Message-only for pure-Rust
sources. (Structured failures — CustomLexError::Lex — keep the
built-in arms’ error classes instead.)
Unsupported
A parse-time configuration reject Lark raises at parse() call time — the
only member being on_error under a non-LALR parser, which Lark surfaces as
a NotImplementedError. Kept out of the build-time
crate::LarkBuildError channel because the offending kwarg (on_error) is
a parse() argument, not a constructor one. Bindings map this to Python’s
NotImplementedError (not an UnexpectedInput subclass).
Configuration
A parse-time configuration reject Lark raises as ConfigurationError: an
unknown start symbol, or start=None on a multi-start grammar (Lark’s
_verify_start). Distinct from Self::Callback (a user
reduce/postlex callback failure) so bindings surface the correct
ConfigurationError class rather than a generic LarkError.
Implementations§
§impl ParseError
impl ParseError
pub fn line(&self) -> Option<u32>
pub fn line(&self) -> Option<u32>
The 1-based line of the failure when the variant carries a position —
ParseError::UnexpectedToken (still Option: a synthetic $END can be
position-less) and ParseError::UnexpectedCharacters. None for the
position-free variants (Callback / Postlex / UnexpectedEOF / …).
pub fn column(&self) -> Option<u32>
pub fn column(&self) -> Option<u32>
The 1-based column of the failure; see ParseError::line for which
variants carry one.
pub fn pos_in_stream(&self) -> Option<u32>
pub fn pos_in_stream(&self) -> Option<u32>
The failure’s offset into the input in code points (Unicode scalar
values — exactly Python len, the position contract these errors carry),
when the variant has one. This is the index ParseError::get_context
slices at.
pub fn get_context(&self, text: &str) -> String
pub fn get_context(&self, text: &str) -> String
A caret view of the failure site — the input line around the error with a
^ under the offending code point:
1 + * 2
^This is Lark’s UnexpectedInput.get_context (span 40), ported verbatim,
tab-expanded caret included. The parser retains no copy of the input, so
the caller passes the original text back in; a position-free variant
draws the caret at offset 0.
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 Eq 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()
§impl From<PostlexError> for ParseError
A configuration/stream error surfaces as ParseError::Postlex. Replaces
the three copies of map_postlex_error the drivers used to carry.
impl From<PostlexError> for ParseError
A configuration/stream error surfaces as ParseError::Postlex. Replaces
the three copies of map_postlex_error the drivers used to carry.
§fn from(e: PostlexError) -> Self
fn from(e: PostlexError) -> Self
§impl PartialEq for ParseError
impl PartialEq for ParseError
impl StructuralPartialEq for ParseError
Auto Trait Implementations§
impl !RefUnwindSafe for ParseError
impl !UnwindSafe for ParseError
impl Freeze for ParseError
impl Send for ParseError
impl Sync for ParseError
impl Unpin for ParseError
impl UnsafeUnpin 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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string()] Read more§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString]. Read more