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: TokenKindWhat the token is.
text: StringThe token’s text. A token made by macro pasting or # has text that is
not in any file.
span: SpanWhere the token’s text is: in the file, or in the macro body it came from.
space_before: boolSpace or a comment comes before the token on its line.
line_start: boolThe 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
impl Token
Sourcepub fn site(&self) -> Span
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.
Sourcepub fn is(&self, punct: Punct) -> bool
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}Trait Implementations§
impl Eq for Token
impl StructuralPartialEq for Token
Auto Trait Implementations§
impl Forget for Token
impl Freeze for Token
impl Move for Token
impl RefUnwindSafe for Token
impl Send for Tokenwhere
RawVecInner: Send,
impl Sync for Tokenwhere
RawVecInner: Sync,
impl Unpin for Token
impl UnsafeUnpin for Token
impl UnwindSafe for Tokenwhere
RawVecInner: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more