Skip to main content

MissingToken

Struct MissingToken 

Source
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 found
  • Kind - 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>

Source

pub const fn new(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.

Source§

impl<'a, Kind: Clone, O, Lang: ?Sized> MissingToken<'a, Kind, O, Lang>

Source

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.

Source

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.

Source

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.

Source

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, "}");
}
Source

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));
Source

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));
Source

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));
Source

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));
Source

pub const fn offset(&self) -> O
where 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));
Source

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);
Source

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);
Source

pub const fn message(&self) -> Option<&CowStr>

Returns a reference to the custom message, if any.

Source

pub fn message_mut(&mut self) -> Option<&mut CowStr>

Returns a mutable reference to the custom message, if any.

Source

pub const fn name(&self) -> Option<&CowStr>

Returns the stamped token name, if any — see with_name.

Source

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 == "}"));
Source

pub fn bump(&mut self, offset: &O)
where O: for<'b> AddAssign<&'b 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);
Source

pub fn map_expected<F, Kind2>(self, f: F) -> MissingToken<'a, Kind2, O, Lang>
where F: FnOnce(Expected<'a, Kind>) -> Expected<'a, Kind2>, Kind2: Clone,

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())
});
Source

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>

Source

pub fn debug_fmt(&self, f: &mut Formatter<'_>) -> Result
where O: Debug, Kind: Debug,

Formats the error using the provided formatter in debug style.

Source

pub fn display_fmt(&self, f: &mut Formatter<'_>) -> Result
where O: Display, Kind: Display,

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>

Source§

fn clone(&self) -> MissingToken<'a, Kind, O, Lang>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, Kind: Eq + Clone, O: Eq, Lang: Eq + ?Sized> Eq for MissingToken<'a, Kind, O, Lang>

Source§

impl<'a, Kind: Clone, O, Lang: ?Sized> From<MissingToken<'a, Kind, O, Lang>> for ()

Source§

fn from(_: MissingToken<'a, Kind, O, Lang>) -> Self

Converts to this type from the input type.
Source§

impl<'a, Kind: Hash + Clone, O: Hash, Lang: Hash + ?Sized> Hash for MissingToken<'a, Kind, O, Lang>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a, Kind: PartialEq + Clone, O: PartialEq, Lang: PartialEq + ?Sized> PartialEq for MissingToken<'a, Kind, O, Lang>

Source§

fn eq(&self, other: &MissingToken<'a, Kind, O, Lang>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<'a, Kind: PartialEq + Clone, O: PartialEq, Lang: PartialEq + ?Sized> StructuralPartialEq for MissingToken<'a, Kind, O, Lang>

Auto Trait Implementations§

§

impl<'a, Kind, O, Lang> Freeze for MissingToken<'a, Kind, O, Lang>
where O: Freeze, Option<Expected<'a, Kind>>: Freeze, PhantomData<Lang>: Freeze, Lang: ?Sized,

§

impl<'a, Kind, O, Lang> RefUnwindSafe for MissingToken<'a, Kind, O, Lang>

§

impl<'a, Kind, O, Lang> Send for MissingToken<'a, Kind, O, Lang>
where O: Send, Option<Expected<'a, Kind>>: Send, PhantomData<Lang>: Send, Lang: ?Sized,

§

impl<'a, Kind, O, Lang> Sync for MissingToken<'a, Kind, O, Lang>
where O: Sync, Option<Expected<'a, Kind>>: Sync, PhantomData<Lang>: Sync, Lang: ?Sized,

§

impl<'a, Kind, O, Lang> Unpin for MissingToken<'a, Kind, O, Lang>
where O: Unpin, Option<Expected<'a, Kind>>: Unpin, PhantomData<Lang>: Unpin, Lang: ?Sized,

§

impl<'a, Kind, O, Lang> UnsafeUnpin for MissingToken<'a, Kind, O, Lang>
where O: UnsafeUnpin, Option<Expected<'a, Kind>>: UnsafeUnpin, PhantomData<Lang>: UnsafeUnpin, Lang: ?Sized,

§

impl<'a, Kind, O, Lang> UnwindSafe for MissingToken<'a, Kind, O, Lang>
where O: UnwindSafe, Option<Expected<'a, Kind>>: UnwindSafe, PhantomData<Lang>: UnwindSafe, Lang: ?Sized,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<'a, T, L, Lang> FromMissingLeadingSeparatorError<'a, L, Lang> for T
where L: Lexer<'a>, T: From<MissingToken<'a, <<L as Lexer<'a>>::Token as Token<'a>>::Kind, <L as Lexer<'a>>::Offset, Lang>>, Lang: ?Sized,

Source§

fn from_missing_leading_separator( name: CowStr, err: MissingToken<'a, <<L as Lexer<'a>>::Token as Token<'a>>::Kind, <L as Lexer<'a>>::Offset, Lang>, ) -> T
where L: Lexer<'a>,

Creates an emitter error from a missing leading separator error.
Source§

impl<'a, T, L, Lang> FromMissingTrailingSeparatorError<'a, L, Lang> for T
where L: Lexer<'a>, T: From<MissingToken<'a, <<L as Lexer<'a>>::Token as Token<'a>>::Kind, <L as Lexer<'a>>::Offset, Lang>>, Lang: ?Sized,

Source§

fn from_missing_trailing_separator( name: CowStr, err: MissingToken<'a, <<L as Lexer<'a>>::Token as Token<'a>>::Kind, <L as Lexer<'a>>::Offset, Lang>, ) -> T
where L: Lexer<'a>,

Creates an emitter error from a missing trailing separator error.
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.