Token

Struct Token 

Source
pub struct Token<'s> {
    pub ty: TokenType<'s>,
    pub range: Range<usize>,
    pub before_lines: Vec<TokenLine<'s>>,
    pub comments: Vec<Comment<'s>>,
    pub new_line: Option<TokenLine<'s>>,
}
Expand description

A source-preserving token.

Each token contains a TokenType, defining the actual parsed token, as well as a source range and comment metadata.

§Comments

Consider this token with surrounding comments:

// this is my function
/* hi */ // there
/* here it comes: */ function // do you like it?
/* some other comment */

Each comment in the source string ends up “owned” by a single token. Each token owns any comments on empty lines before it, any comments on the same line before it, and any comments on the same line after it if the token is at the end of the line. In general this is meant to match how comments are normally attached to pieces of code.

In the example above, the function token would end up looking like this:

use sqparse::token::{Comment, TerminalToken, Token, TokenLine, TokenType};
let _ = Token {
    ty: TokenType::Terminal(TerminalToken::Function),
    range: 65..73,

    before_lines: vec![
        TokenLine { comments: vec![Comment::SingleLine("this is my function")] },
        TokenLine { comments: vec![Comment::MultiLine("hi"), Comment::SingleLine("there")] },
    ],
    comments: vec![
        Comment::MultiLine("here it comes:")
    ],
    new_line: Some(TokenLine { comments: vec![Comment::SingleLine("do you like it?")] })
};

Notice how the token keeps track of the separate lines of comments before it, and whether it is followed by a newline. Also notice that the last /* some other comment */ isn’t included, since it is on a different line after the token it would be owned by whatever token comes later.

If there are un-owned comments at the end of an input when parsing completes, an Empty token will be emitted.

Fields§

§ty: TokenType<'s>

The type of token.

§range: Range<usize>

The character range of the token in the source string.

§before_lines: Vec<TokenLine<'s>>

Empty lines that appear before the token. The lines may contain comments.

§comments: Vec<Comment<'s>>

Comments that appear before the token on the same line.

§new_line: Option<TokenLine<'s>>

If this token ends a line, includes any comments between the token and newline.

Trait Implementations§

Source§

impl<'s> Clone for Token<'s>

Source§

fn clone(&self) -> Token<'s>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'s> Debug for Token<'s>

Source§

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

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

impl<'s> PartialEq for Token<'s>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'s> StructuralPartialEq for Token<'s>

Auto Trait Implementations§

§

impl<'s> Freeze for Token<'s>

§

impl<'s> RefUnwindSafe for Token<'s>

§

impl<'s> Send for Token<'s>

§

impl<'s> Sync for Token<'s>

§

impl<'s> Unpin for Token<'s>

§

impl<'s> UnwindSafe for Token<'s>

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