Skip to main content

Tokens

Struct Tokens 

Source
pub struct Tokens(/* private fields */);
Expand description

Tokens are a thin wrapper over a vector of Token objects

They are usually read from a Mouth and treated as an immutable interface. For access to the inner Token contents, use one of the unlist methods.

Implementations§

Source§

impl Tokens

Source

pub fn new(tokens: Vec<Token>) -> Self

Create a Tokens object from a Vec of individual Token

Source

pub fn unlist(self) -> Vec<Token>

Return a list of the tokens making up this Tokens

Source

pub fn unlist_ref(&self) -> &Vec<Token>

Return a reference to the tokens making up this Tokens

Source

pub fn unlist_mut(&mut self) -> &mut Vec<Token>

Return a mutable reference to the tokens making up this Tokens

Source

pub fn is_empty(&self) -> bool

Are there any tokens at all contained in this Tokens object

Source

pub fn len(&self) -> usize

Number of contained Token entries

Source

pub fn starts_with_text(&self, prefix: &str) -> bool

Zero-alloc equivalent of self.to_string().starts_with(prefix). Walks tokens byte-by-byte into prefix using the same Display semantics as eq_text (COMMENT skipped, ARG prefixed with #). Returns true once the full prefix has been consumed, even if more token text follows.

Source

pub fn eq_text(&self, target: &str) -> bool

Zero-alloc equivalent of self.to_string() == target. Walks the contained tokens byte-by-byte, skipping COMMENT tokens (matching Display for Tokens) and prefixing ARG tokens with # (matching Display for Token). Returns true iff the rendered text exactly equals target. Used by DefMacro bodies that check keyword values like true / false / swapnumber without wanting to allocate a fresh String per invocation.

Source

pub fn revert(self) -> Vec<Token>

Source

pub fn to_number(&self) -> Number

to_number casts back to a parsed Number (usually via gullet::read_number) which had to be re-converted to a Tokens for reentering the expansion flow

Source

pub fn to_dimension(&self) -> Dimension

to_dimension casts back to a parsed Dimension (usually via gullet::read_dimension) which had to be re-converted to a Tokens for reentering the expansion flow

Source

pub fn to_glue(&self) -> Glue

to_glue casts back to a parsed Glue (usually via gullet::read_glue) which had to be re-converted to a Tokens for reentering the expansion flow

Source

pub fn to_mu_glue(&self) -> MuGlue

to_mu_glue casts back to a parsed MuGlue (usually via gullet::read_mu_glue) which had to be re-converted to a Tokens for reentering the expansion flow

Source

pub fn to_mu_dimension(&self) -> MuDimension

to_mu_dimension casts back to a parsed MuGlue (usually via gullet::read_mu_glue) which had to be re-converted to a Tokens for reentering the expansion flow

Source

pub fn to_float(&self) -> Float

to_float casts back to a parsed Float (usually via gullet::read_float) which had to be re-converted to a Tokens for reentering the expansion flow

Source

pub fn to_keyvals(&self) -> Result<KeyVals>

to_keyvals casts back to a parsed KeyVals (usually via a KeyVals parameter type) which had to be re-converted to a Tokens for reentering the expansion flow

Source

pub fn equals(&self, other: Tokens) -> bool

Methods for overloaded ops.

Source

pub fn unwrap_or_default(self) -> Tokens

returns self, for compatibility convenience with Option

Source

pub fn unwrap(&self) -> &Tokens

returns self, for compatibility convenience with Option

Source

pub fn stringify(&self) -> String

A string form which is primarily used for error-reporting

Source

pub fn be_digested(self) -> Result<Digested>

digest the current Tokens

Source

pub fn neutralize(self, extraspecials: &[char]) -> Tokens

neutralize each token

Source

pub fn is_balanced(&self) -> bool

Checks if any BEGIN/END code groups are correctly nested and closed

Source

pub fn substitute_parameters(&self, args: &[Option<Cow<'_, Tokens>>]) -> Self

substitutes the parameters (ARG catcode) in a Tokens list for concrete arguments

Source

pub fn untex(self) -> String

Consumes a Tokens to a string containing TeX that created it (or could have). Note that this is not necessarily the original TeX code; expansions or other substitutions may have taken place.

Design decision: The Perl UnTeX inserts %\n line-breaks (TeX comment + newline) when a token string would exceed 78 characters. The Rust port deliberately omits this feature. Line-break insertion is purely cosmetic and makes test expectations fragile — the %\n appears verbatim in tex= attributes of ltx:Math elements, causing test XML files to contain %&#10; escape sequences that depend on exact token lengths. We instead always produce compact, single-line output. Test .xml files should not contain %&#10;.

Source

pub fn untex_string(self) -> TeXString

untex, typed — the blessed way to get TeX markup out of a Tokens and back into a tokenizing sink.

Prefer this over untex() whenever the string is destined for Tokenize! / mouth::tokenize / mouth::tokenize_internal: it is the only TokensTeXString conversion, so the sink’s signature proves the round trip cannot weld a control word (\v S stays \v S, not \vS). untex() itself is unchanged for the callers that want a plain String (a tex= attribute, a log message, a comparison).

Source

pub fn pack_parameters(self) -> Result<Self>

Packs repeated CC_PARAM tokens into CC_ARG tokens for use as a macro body (and other token lists) Also unwraps \noexpand tokens, since that is also needed for macro bodies (but not strictly part of packing parameters)

Source

pub fn strip_braces(self) -> Self

Trims outer braces (if they balance each other). Strips exactly 1 layer of matching outer braces by default. Should this also trim whitespace? or only if there are braces?

Source

pub fn strip_braces_n(self, layers: usize) -> Self

Trims layers outer brace pairs (if they balance each other). Also trims whitespace outer to the removed braces. Follows the Perl Tokens.pm algorithm: first collects all balanced brace pairs, then strips from outside-in, only removing pairs that span the full remaining width.

Trait Implementations§

Source§

impl AsRef<Tokens> for Tokens

Source§

fn as_ref(&self) -> &Tokens

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Tokens

Source§

fn clone(&self) -> Tokens

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 Debug for Tokens

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Tokens

Source§

fn default() -> Tokens

Returns the “default value” for a type. Read more
Source§

impl Display for Tokens

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

to_string is used often, and for more keyword-like reasons, NOT for creating valid TeX (use revert or UnTeX for that!)

Source§

impl<'a> From<&'a Tokens> for Token

Source§

fn from(ts: &'a Tokens) -> Token

Converts to this type from the input type.
Source§

impl From<&Rc<Tokens>> for Tokens

Source§

fn from(t: &Rc<Tokens>) -> Tokens

Converts to this type from the input type.
Source§

impl From<&Token> for Tokens

Source§

fn from(t: &Token) -> Tokens

Converts to this type from the input type.
Source§

impl From<ArgWrap> for Tokens

Source§

fn from(t: ArgWrap) -> Tokens

Converts to this type from the input type.
Source§

impl From<Float> for Tokens

Source§

fn from(v: Float) -> Tokens

Converts to this type from the input type.
Source§

impl From<MuGlue> for Tokens

Source§

fn from(v: MuGlue) -> Tokens

Converts to this type from the input type.
Source§

impl From<Number> for Tokens

Source§

fn from(v: Number) -> Tokens

Converts to this type from the input type.
Source§

impl From<Rc<Tokens>> for Tokens

Source§

fn from(t: Rc<Tokens>) -> Tokens

Converts to this type from the input type.
Source§

impl From<RegisterValue> for Tokens

Source§

fn from(v: RegisterValue) -> Tokens

Converts to this type from the input type.
Source§

impl From<Token> for Tokens

Source§

fn from(t: Token) -> Tokens

Converts to this type from the input type.
Source§

impl From<Tokens> for Stored

Source§

fn from(value: Tokens) -> Self

Converts to this type from the input type.
Source§

impl From<Tokens> for Vec<Token>

Source§

fn from(ts: Tokens) -> Vec<Token>

Converts to this type from the input type.
Source§

impl From<Tokens> for Result<Tokens>

Source§

fn from(t: Tokens) -> Result<Tokens>

Converts to this type from the input type.
Source§

impl From<Tokens> for Token

Source§

fn from(ts: Tokens) -> Token

Converts to this type from the input type.
Source§

impl From<Tokens> for ArgWrap

Source§

fn from(t: Tokens) -> Self

Converts to this type from the input type.
Source§

impl From<Tokens> for RegisterValue

Source§

fn from(n: Tokens) -> RegisterValue

Converts to this type from the input type.
Source§

impl From<Tokens> for Reversion

Source§

fn from(ts: Tokens) -> Reversion

Converts to this type from the input type.
Source§

impl From<Tokens> for ExpansionBody

Source§

fn from(t: Tokens) -> ExpansionBody

Converts to this type from the input type.
Source§

impl From<Tokens> for Option<ExpansionBody>

Source§

fn from(t: Tokens) -> Option<ExpansionBody>

Converts to this type from the input type.
Source§

impl From<Tokens> for Digested

Source§

fn from(value: Tokens) -> Digested

Converts to this type from the input type.
Source§

impl From<Vec<Token>> for Tokens

Source§

fn from(ts: Vec<Token>) -> Tokens

Converts to this type from the input type.
Source§

impl IntoOption<Option<Reversion>> for Tokens

Source§

fn into_option(self) -> Option<Reversion>

Performs the conversion.
Source§

impl IntoRegisterValueOption<Option<RegisterValue>> for Tokens

Source§

impl IntoTokensResult<Result<Tokens, Error>> for Tokens

Source§

fn into_tokens_result(self) -> Result<Tokens>

Performs the conversion, used for DefMacro return values etc
Source§

impl PartialEq for Tokens

Source§

fn eq(&self, other: &Tokens) -> bool

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

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

Inequality operator !=. Read more
Source§

impl ToTokens for Tokens

Available on crate feature codegen only.
Source§

fn to_tokens(&self, stream: &mut TokenStream)

Write self to the given TokenStream. Read more
Source§

fn to_token_stream(&self) -> TokenStream

Convert self directly into a TokenStream object. Read more
Source§

fn into_token_stream(self) -> TokenStream
where Self: Sized,

Convert self directly into a TokenStream object. Read more

Auto Trait Implementations§

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> IntoResultArgWrap<Result<ArgWrap, Error>> for T
where T: Into<ArgWrap>,

Source§

fn into_result_argwrap(self) -> Result<ArgWrap, Error>

performs the conversion
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.