pub struct MissingToken<'a, Kind: Clone, O = usize, Lang: ?Sized = ()> { /* private fields */ }Expand description
An error representing a missing token encountered during parsing.
This error type captures the location (offset) and what token(s) were expected. It’s commonly used in parsers to provide detailed error messages when the input doesn’t match the expected syntax.
Three optional channels ride alongside the offset, each answering a different question:
expected is machine-readable (token kinds), message
is the caller’s free text, and name is the human-readable name of the token
this error is about — what a separated-sequence driver calls its separator, for instance.
They are separate because a conversion that needs to stamp one must not have to choose
between clobbering another and dropping its own information.
§Type Parameters
T- The type of the actual token that was foundKind- The type of the expected token (often an enum of token kinds)
§Examples
use tokora::{SimpleSpan, utils::Expected, error::token::MissingToken};
// Error when expecting a specific token
let error: MissingToken<'_, &str, SimpleSpan> = MissingToken::expected_one(
SimpleSpan::new(10, 15),
"}"
);
assert_eq!(error.offset(), SimpleSpan::new(10, 15));
// Error when expecting one of multiple tokens
let error: MissingToken<'_, &str, SimpleSpan> = MissingToken::expected_one_of(
SimpleSpan::new(0, 10),
&["if", "while", "for"]
);
if let Some(Expected::OneOf(values)) = error.expected() {
assert_eq!(values.as_slice(), &["if", "while", "for"]);
}Implementations§
Source§impl<Kind: Clone, O> MissingToken<'_, Kind, O>
impl<Kind: Clone, O> MissingToken<'_, Kind, O>
Source§impl<'a, Kind: Clone, O, Lang: ?Sized> MissingToken<'a, Kind, O, Lang>
impl<'a, Kind: Clone, O, Lang: ?Sized> MissingToken<'a, Kind, O, Lang>
Sourcepub const fn of(offset: O) -> Self
pub const fn of(offset: O) -> Self
Creates a new missing token error.
This error indicates that a missing token was encountered, without specifying what token was found or expected.
Sourcepub fn with_message(self, message: CowStr) -> Self
pub fn with_message(self, message: CowStr) -> Self
Adds knowledge to the MissingToken error.
This method allows attaching additional context or information to the error, which can be useful for debugging or reporting.
Sourcepub fn with_name(self, name: CowStr) -> Self
pub fn with_name(self, name: CowStr) -> Self
Stamps the human-readable name of the token this error is about — the separator name a separated-sequence driver supplied, for the errors the separator conversions produce.
A channel of its own rather than a phrasing pushed into
with_message: the message is the caller’s free text, and a
conversion that overwrote it would lose whatever the caller meant by it — while a
conversion that declined to overwrite it would lose the name instead. Keeping the two
apart also keeps the name safe from with_expected, which clears
the message channel.
Sourcepub fn with_expected(self, expected: Expected<'a, Kind>) -> Self
pub fn with_expected(self, expected: Expected<'a, Kind>) -> Self
Creates a missing token error without a found token.
This is useful when the parser reaches the end of input with a missing token. The error will indicate “missing end of input” in its display message.
§Examples
use tokora::{SimpleSpan, utils::Expected, error::token::MissingToken};
let error: MissingToken<'_, &str, usize> = MissingToken::new(
100,
).with_expected(Expected::one("}"));
assert_eq!(error.offset(), 100);
if let Some(Expected::One(value)) = error.expected() {
assert_eq!(*value, "}");
}Sourcepub const fn expected_one(offset: O, expected: Kind) -> Self
pub const fn expected_one(offset: O, expected: Kind) -> Self
Creates a new missing token error with a single expected token.
This is a convenience method that combines new with Expected::one.
The error has no found token, indicating the end of input was reached.
§Examples
use tokora::{SimpleSpan, error::token::MissingToken};
let error: MissingToken<'_, &str, SimpleSpan> = MissingToken::expected_one(
SimpleSpan::new(50, 51),
";"
);
assert_eq!(error.offset(), SimpleSpan::new(50, 51));Sourcepub const fn expected_one_with_found(offset: O, expected: Kind) -> Self
pub const fn expected_one_with_found(offset: O, expected: Kind) -> Self
Creates a new missing token error with a single expected token.
This is a convenience method that combines new with Expected::one.
The error has no found token, indicating the end of input was reached.
§Examples
use tokora::{SimpleSpan, error::token::MissingToken};
let error: MissingToken<'_, &str, SimpleSpan> = MissingToken::expected_one_with_found(
SimpleSpan::new(50, 51),
";"
);
assert_eq!(error.offset(), SimpleSpan::new(50, 51));Sourcepub const fn expected_one_of(offset: O, expected: &'static [Kind]) -> Self
pub const fn expected_one_of(offset: O, expected: &'static [Kind]) -> Self
Creates a new missing token error with multiple expected tokens.
This is a convenience method that combines new with Expected::one_of.
The error has no found token, indicating the end of input was reached.
§Examples
use tokora::{SimpleSpan, error::token::MissingToken};
let error: MissingToken<'_, &str, SimpleSpan> = MissingToken::expected_one_of(
SimpleSpan::new(25, 26),
&["+", "-", "*", "/"]
);
assert_eq!(error.offset(), SimpleSpan::new(25, 26));Sourcepub const fn expected_one_of_with_found(
offset: O,
expected: &'static [Kind],
) -> Self
pub const fn expected_one_of_with_found( offset: O, expected: &'static [Kind], ) -> Self
Creates a new missing token error with multiple expected tokens.
This is a convenience method that combines new with Expected::one_of.
The error has no found token, indicating the end of input was reached.
§Examples
use tokora::{SimpleSpan, error::token::MissingToken};
let error: MissingToken<'_, &str, SimpleSpan> = MissingToken::expected_one_of_with_found(
SimpleSpan::new(25, 26),
&["+", "-", "*", "/"]
);
assert_eq!(error.offset(), SimpleSpan::new(25, 26));Sourcepub const fn offset(&self) -> Owhere
O: Copy,
pub const fn offset(&self) -> Owhere
O: Copy,
Returns the offset of the missing token.
§Examples
use tokora::{SimpleSpan, error::token::MissingToken};
let error: MissingToken<'_, &str, SimpleSpan> = MissingToken::expected_one(
SimpleSpan::new(10, 15),
"identifier"
);
assert_eq!(error.offset(), SimpleSpan::new(10, 15));Sourcepub const fn offset_ref(&self) -> &O
pub const fn offset_ref(&self) -> &O
Returns the offset of the missing token.
§Examples
use tokora::error::token::MissingToken;
let error: MissingToken<'_, &str> = MissingToken::expected_one(10, "identifier");
assert_eq!(error.offset_ref(), &10);Sourcepub const fn offset_mut(&mut self) -> &mut O
pub const fn offset_mut(&mut self) -> &mut O
Returns the offset of the missing token.
§Examples
use tokora::error::token::MissingToken;
let mut error: MissingToken<'_, &str> = MissingToken::expected_one(10, "identifier");
*error.offset_mut() = 12;
assert_eq!(error.offset(), 12);Sourcepub const fn message(&self) -> Option<&CowStr>
pub const fn message(&self) -> Option<&CowStr>
Returns a reference to the custom message, if any.
Sourcepub fn message_mut(&mut self) -> Option<&mut CowStr>
pub fn message_mut(&mut self) -> Option<&mut CowStr>
Returns a mutable reference to the custom message, if any.
Sourcepub const fn name(&self) -> Option<&CowStr>
pub const fn name(&self) -> Option<&CowStr>
Returns the stamped token name, if any — see with_name.
Sourcepub const fn expected(&self) -> Option<&Expected<'a, Kind>>
pub const fn expected(&self) -> Option<&Expected<'a, Kind>>
Returns a reference to the expected token(s).
§Examples
use tokora::{SimpleSpan, utils::Expected, error::token::MissingToken};
let error: MissingToken<'_, &str, SimpleSpan> = MissingToken::expected_one(SimpleSpan::new(5, 6), "}");
assert!(matches!(error.expected(), Some(Expected::One(value)) if *value == "}"));Sourcepub fn bump(&mut self, offset: &O)
pub fn bump(&mut self, offset: &O)
Bumps the offset by the given amount.
This is useful when adjusting error positions after processing or when combining offsets from different contexts.
§Examples
use tokora::error::token::MissingToken;
let mut error: MissingToken<'_, &str> = MissingToken::expected_one(10, "}");
error.bump(&5);
assert_eq!(error.offset(), 15);Sourcepub fn map_expected<F, Kind2>(self, f: F) -> MissingToken<'a, Kind2, O, Lang>
pub fn map_expected<F, Kind2>(self, f: F) -> MissingToken<'a, Kind2, O, Lang>
Maps the expected token(s) using the provided function.
This is useful for transforming the expected token type while preserving the rest of the error information.
§Examples
use tokora::{utils::Expected, error::token::MissingToken};
let error: MissingToken<'_, &str> = MissingToken::expected_one(0, "identifier");
let mapped_error = error.map_expected(|expected| {
// Transform the expected token type here
Expected::one(expected.unwrap_one().to_string())
});Sourcepub fn into_components(
self,
) -> (O, Option<Expected<'a, Kind>>, Option<CowStr>, Option<CowStr>)
pub fn into_components( self, ) -> (O, Option<Expected<'a, Kind>>, Option<CowStr>, Option<CowStr>)
Consumes the error and returns its components: the offset, the expected token(s), the
optional message, and the stamped token name, in field order.
The name is returned rather than dropped for the same reason
SeparatedError::into_components
returns its own: a downstream From<MissingToken> impl that takes the error apart is
precisely the consumer the separator conversions stamp the name for, and those
conversions are blanket impls such a type cannot override. A tuple that omitted the name
would simply move the loss one seam further along.
The message and the name are both Option<CowStr> but are distinct channels — the
caller’s free text and the token’s own name — and arrive in that order.
§Examples
use tokora::{SimpleSpan, utils::{CowStr, Expected}, error::token::MissingToken};
let error: MissingToken<'_, &str, SimpleSpan> =
MissingToken::expected_one(SimpleSpan::new(5, 6), "}").with_name(CowStr::from_static("brace"));
let (offset, expected, message, name) = error.into_components();
assert_eq!(offset, SimpleSpan::new(5, 6));
assert_eq!(expected, Some(Expected::one("}")));
assert_eq!(message, None);
assert_eq!(name.as_ref().map(CowStr::as_str), Some("brace"));Source§impl<Kind: Clone, O, Lang: ?Sized> MissingToken<'_, Kind, O, Lang>
impl<Kind: Clone, O, Lang: ?Sized> MissingToken<'_, Kind, O, Lang>
Sourcepub fn debug_fmt(&self, f: &mut Formatter<'_>) -> Result
pub fn debug_fmt(&self, f: &mut Formatter<'_>) -> Result
Formats the error using the provided formatter in debug style.
Sourcepub fn display_fmt(&self, f: &mut Formatter<'_>) -> Result
pub fn display_fmt(&self, f: &mut Formatter<'_>) -> Result
Formats the error using the provided formatter in display style.
A stamped name is quoted into the opening clause — missing token 'comma' at 12 — matching how the rest of the crate renders a token’s own name
(unclosed delimiter '(', unopened delimiter ')'). Naming it there rather than
appending a clause is what makes the separator conversions’ stamp reach a reader: the
point of the channel is that the diagnostic says which separator was missing, not that
one was.
Without a name the rendering is byte-for-byte what it always was, so the channel is purely additive for every error that never carried one.
Trait Implementations§
Source§impl<'a, Kind: Clone + Clone, O: Clone, Lang: Clone + ?Sized> Clone for MissingToken<'a, Kind, O, Lang>
impl<'a, Kind: Clone + Clone, O: Clone, Lang: Clone + ?Sized> Clone for MissingToken<'a, Kind, O, Lang>
Source§fn clone(&self) -> MissingToken<'a, Kind, O, Lang>
fn clone(&self) -> MissingToken<'a, Kind, O, Lang>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more