Skip to main content

Token

Struct Token 

Source
#[repr(C)]
pub struct Token { pub text: SymStr, pub code: Catcode, pub prov: u16, }
Expand description

The core immutable syntactic primitive resulting from TeX’s read-in and expansion process We allow the fields to be public, so that we can use builder macros such as

macro_rules! T_SPACE(() => {
    Token { text: arena::pin_static(" "), code: Catcode::SPACE}
  });

#[repr(C)] pins the field order text, code, prov (loc): code stays at byte 4, where it sat before prov existed, and with a two-byte Catcode the struct has no padding (prov at 6). Rust’s default layout put prov at 4 and code at 6, which made every move of a token through Option<Token>, CheckedRead::Tok and the read path’s Result slot a 4-piece field-wise copy (gullet::read_internal_token_checked: +1.29 % instructions on an empty article; +1.14 % with repr(C) alone, the pad byte still copied apart; +0.65 % gap-free). The guard is token_layout_code_at_byte_4.

Fields§

§text: SymStr

an arena id the character content for this token

§code: Catcode

a TeX catcode

§prov: u16

Out-of-band field provenance (0 = none): the frontmatter field whose stored value this token is a token of (the kernel \title/\author stamp their store, latexml_engine \lx@field@mark), so the ink digested from it is known for that field’s print. Not part of TeX’s token: excluded from PartialEq (\ifx, \meaning, \pdfstrcmp, xstring compare as pdflatex does), never written, never dumped. It sits in what was the struct’s padding: Token stays 8 bytes (token_size_invariant). OXIDIZED_DESIGN #715.

Implementations§

Source§

impl Token

Source

pub fn new<T: AsRef<str>>(text: T, code: Catcode) -> Self

simple Token constructor, wrapping over text and catcode

Source

pub fn from_sym(text: SymStr, code: Catcode) -> Self

Construct a token directly from an already-interned SymStr without re-probing or interning into the arena.

Source

pub fn try_cs<T: AsRef<str>>(text: T) -> Option<Self>

Look up an already-interned control-sequence token without interning a new symbol if text has never been seen.

Source

pub fn cycle_fingerprint(&self) -> u64

A cheap structural fingerprint for the cycle-detection guard (crate::cycle_guard). Matches PartialEq semantics: SPACE tokens fingerprint by catcode alone (their text is irrelevant to equality). NOT a stable hash across processes — for in-run loop detection only.

Source

pub fn get_cs_name(&self) -> SymStr

Get the CS Name of the token. This is the name that definitions will be stored under; It’s the same for various `different’ BEGIN tokens, eg.

Source

pub fn with_cs_name<R, FnR>(&self, caller: FnR) -> R
where FnR: FnOnce(&str) -> R,

Execute a closure using the CS Name of the token. This is the name that definitions will be stored under; It’s the same for various `different’ BEGIN tokens, eg.

Source

pub fn pin_cs_name(&self) -> SymStr

artificial, but avoids the data race

Source

pub fn get_primitive_name(&self) -> Option<&'static str>

Get the fixed name of a primitive catcode, or empty string otherwise

Source

pub fn get_executable_name(&self) -> String

Get the CS name only if the catcode is executable!

Source

pub fn get_executable_primitive_name(&self) -> Option<&'static str>

Intersect executable and primitive

Source

pub fn get_sym(&self) -> SymStr

Use the ticket representing the interned “text” of the token

Source

pub fn with_str<R, FnR>(&self, caller: FnR) -> R
where FnR: FnOnce(&str) -> R,

Use the interned &str “text” of the token use to_string instead for an owned String with simpler

Source

pub fn is_noexpand_family(&self) -> bool

True for any member of the \special_relax no-expand family — the representation of a \noexpand’d (expandable or undefined) CS/active token. Such a token is a CS whose NAME is \special_relax \x01 <shadowed text>, carrying the shadowed token’s identity PER-TOKEN (faithful to TeX’s no_expand_flag, which preserves cur_cs), while the whole family resolves to \special_relax’s \relax meaning (crate::state::lookup_meaning fallback). The bare \special_relax (no suffix) is the no-shadow case (\dont_expand at end-of-input). \x01 is never valid in a CS name or as an active char, so the encoding is unambiguous.

Source

pub fn noexpand_shadowed(&self) -> Option<Token>

Recover the shadowed token from a \special_relax-family token, if it shadows one (i.e. not the bare \special_relax). The shadowed token is a CS (text begins \) or an active char.

Source

pub fn get_charcode(&self) -> u32

Return the character code of character part of the token, or 256 if it is a control sequence. A SPACE token’s code is always 32: tex.web §289 space_token = 2^8·spacer + " " and §349 (mid_line + car_ret → cur_chr := " ") — an end-of-line becomes a space with character code 32, whatever produced it. This token keeps its "\n" TEXT (Perl’s data model, PRESERVE_NEWLINES, the tex= reversion) but Perl’s getCharcode returned ord("\n") = 10 for it (Token.pm:243), so \if compared an end-of-line space unequal to a typed one: stringstrings’ \if\BlankSpace#1 took the wrong branch on a \@for item that was a lone newline and desynchronised the conditional stack (Extra \else), then tikzviolinplots’ KDE loop ran away (batch 56gc, OXIDIZED_DESIGN #260).

Source

pub fn group_step(&self) -> i32

How far this token moves a brace count: 1 for a begin-group, -1 for an end-group, else 0. Brace balance is a property of a run of tokens, no reading of what they spell.

Source

pub fn get_catcode(&self) -> Catcode

Return the catcode of the token.

Source

pub fn is_executable(&self) -> bool

is the current one

Source

pub fn is_active_or_cs(&self) -> bool

A control sequence or an active character (Catcode::is_active_or_cs).

Source

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

neutralize really should only retroactively imitate what Semiverbatim would have done. So, it needs to neutralize those in SPECIALS NOTE that although ‘%’ gets it’s catcode changed in Semiverbatim, I’m pretty sure we do NOT want to neutralize comments (turn them into Catcode::OTHER) here, since if comments do get into the Tokens, that will introduce weird crap into the stream.

Source

pub fn as_other(&self) -> Token

Source

pub fn as_cs(&self) -> Token

Source

pub fn substitute_parameters(self, args: &[&Token]) -> Self

Source

pub fn revert(self) -> Token

A Token reverts to itself

Source

pub fn stringify(&self) -> String

A string form which is primarily used for error-reporting

Source

pub fn to_register(&self) -> Option<Rc<Register>>

Source

pub fn to_number(&self) -> Number

Source

pub fn to_dimension(&self) -> Dimension

Source

pub fn to_mu_dimension(&self) -> MuDimension

Source

pub fn to_glue(&self) -> Glue

Source

pub fn to_mu_glue(&self) -> MuGlue

Source

pub fn to_float(&self) -> Float

Source

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

Source

pub fn defined_as(&self, other: &Token) -> bool

Check whether the current token is defined as other. That is, whether it is equal to other, or \let to other. other is is presumed to be some “constant”, explicit token, such as T_SPACE or T_CS!("\\endcsname").

Source

pub fn spelled_as(&self, other: &Token) -> bool

Whether this token is other itself, as defined_as compares: the same catcode and, but for a space, the same text.

Source

pub fn meaning_name(&self) -> Option<Token>

The token this one’s meaning names, as defined_as reads it: the control sequence of its definition, or the token a \let copied. One meaning lookup; a character token that is not active names none.

Source

pub fn with_meaning_name<R>( &self, f: impl FnOnce(Option<Token>, Option<&Stored>) -> R, ) -> R

Calls f with meaning_name and the meaning itself, from one meaning lookup: for a caller that tests a token against several primitives (defined_as looks the meaning up per call) or reads its definition too (tex_fonts::read_font_ident). A character token that is not active has neither.

Trait Implementations§

Source§

impl Clone for Token

Source§

fn clone(&self) -> Self

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 Copy for Token

Source§

impl Debug for Token

Source§

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

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

impl Default for Token

Source§

fn default() -> Self

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

impl Display for Token

Source§

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

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

impl Eq for Token

The comparison is reflexive (a token is equal to itself, a space to any space).

Source§

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

Source§

fn from(value: &'a Stored) -> Token

Converts to this type from the input type.
Source§

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

Source§

fn from(value: &'a Token) -> Self

Converts to this type from the input type.
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<&Token> for Tokens

Source§

fn from(t: &Token) -> Tokens

Converts to this type from the input type.
Source§

impl From<&str> for Token

Source§

fn from(text: &str) -> Token

Converts to this type from the input type.
Source§

impl From<Option<Tokens>> for Token

Source§

fn from(ts_opt: Option<Tokens>) -> Token

Converts to this type from the input type.
Source§

impl From<Token> for Stored

Source§

fn from(value: Token) -> Self

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<Token> for Result<Tokens>

Source§

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

Converts to this type from the input type.
Source§

impl From<Token> for Vec<Token>

Source§

fn from(t: Token) -> Vec<Token>

Converts to this type from the input type.
Source§

impl From<Token> for Option<Tokens>

Source§

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

Converts to this type from the input type.
Source§

impl From<Token> for Option<Cow<'static, Tokens>>

Source§

fn from(t: Token) -> Option<Cow<'static, Tokens>>

Converts to this type from the input type.
Source§

impl From<Token> for ArgWrap

Source§

fn from(t: Token) -> Self

Converts to this type from the input type.
Source§

impl From<Token> for RegisterValue

Source§

fn from(n: Token) -> RegisterValue

Converts to this type from the input type.
Source§

impl From<Token> for ExpansionBody

Source§

fn from(t: Token) -> ExpansionBody

Converts to this type from the input type.
Source§

impl From<Token> for Option<ExpansionBody>

Source§

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

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 Hash for Token

Hashes what PartialEq compares: the catcode, and the text unless a space (every space is one), so that equal tokens hash alike (a set of the distinct tokens read: the frontmatter rehearsal’s \let-copy scan).

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 IntoRegisterValueOption<Option<RegisterValue>> for Token

Source§

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

Source§

fn into_tokens_result(self) -> Result<Tokens>

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

impl PartialEq for Token

Compare two tokens; They are equal if they both have same catcode & string

Source§

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

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

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

Inequality operator !=. Read more
Source§

impl RegisterCs for Token

Source§

impl RegisterCs for &Token

Source§

impl ToTokens for Token

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
Source§

impl TryFrom<ArgWrap> for Token

Source§

type Error = Error

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

fn try_from(aw: ArgWrap) -> Result<Token>

Performs the conversion.

Auto Trait Implementations§

§

impl Freeze for Token

§

impl RefUnwindSafe for Token

§

impl Send for Token

§

impl Sync for Token

§

impl Unpin for Token

§

impl UnsafeUnpin for Token

§

impl UnwindSafe for Token

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
§

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

§

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<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 = !

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.