Skip to main content

Token

Struct Token 

Source
pub struct Token {
    pub kind: TokenKind,
    pub text: String,
    pub span: Span,
    pub space_before: bool,
    pub line_start: bool,
    pub expanded_from: Option<Span>,
}
Expand description

One token.

Fields§

§kind: TokenKind

What the token is.

§text: String

The token’s text. A token made by macro pasting or # has text that is not in any file.

§span: Span

Where the token’s text is: in the file, or in the macro body it came from.

§space_before: bool

Space or a comment comes before the token on its line.

§line_start: bool

The token is the first one on its line.

§expanded_from: Option<Span>

For a token from a macro body: the macro call in the source it came from.

Implementations§

Source§

impl Token

Source

pub fn site(&self) -> Span

Where the token is in the text the author wrote: the macro call for a token from a macro body, else the token itself.

Source

pub fn is(&self, punct: Punct) -> bool

The token is this punctuation mark.

Examples found in repository?
examples/milkdrop_corpus.rs (line 136)
131fn until_last_brace(text: &str) -> &str {
132    let mut ignored = Vec::new();
133    lex(FileId(0), text, &mut ignored)
134        .iter()
135        .rev()
136        .find(|t| t.is(Punct::RBrace))
137        .map_or(text, |t| &text[..t.span.end as usize])
138}
139
140/// The deepest nesting of parentheses and of braces in the text.
141fn nesting(text: &str) -> (usize, usize) {
142    let mut ignored = Vec::new();
143    let (mut parens, mut braces, mut max_parens, mut max_braces) = (0usize, 0usize, 0, 0);
144    for t in lex(FileId(0), text, &mut ignored) {
145        if t.is(Punct::LParen) {
146            parens += 1;
147            max_parens = max_parens.max(parens);
148        } else if t.is(Punct::RParen) {
149            parens = parens.saturating_sub(1);
150        } else if t.is(Punct::LBrace) {
151            braces += 1;
152            max_braces = max_braces.max(braces);
153        } else if t.is(Punct::RBrace) {
154            braces = braces.saturating_sub(1);
155        }
156    }
157    (max_parens, max_braces)
158}
Source

pub fn is_word(&self, word: &str) -> bool

The token is a name or keyword with this text.

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

Source§

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

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

impl Eq for Token

Source§

impl PartialEq for Token

Source§

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

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

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Token

Auto Trait Implementations§

§

impl Forget for Token

§

impl Freeze for Token
where *const u8 is !null: Freeze, PhantomData<u8>: Freeze,

§

impl Move for Token

§

impl RefUnwindSafe for Token

§

impl Send for Token
where RawVecInner: Send,

§

impl Sync for Token
where RawVecInner: Sync,

§

impl Unpin for Token
where *const u8 is !null: Unpin,

§

impl UnsafeUnpin for Token

§

impl UnwindSafe for Token
where RawVecInner: UnwindSafe,

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> 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.