Struct Token

Source
pub struct Token { /* private fields */ }
Expand description

Token 结构体。

表示词法分析过程中识别出的一个词法单元(Token)。 包含 Token 的内容、类型、源码位置等信息。

§字段说明

  • token:处理后的 Token 内容字符串
  • origin_token_span:Token 在原始源码中的位置范围 (start, end)
  • source_code:共享的源码引用,用于错误报告和调试
  • token_type:Token 的类型分类

Implementations§

Source§

impl Token

Source

pub fn new( token: String, origin_token_span: (usize, usize), source_code: Source, token_type: TokenType, ) -> Token

创建新的 Token 实例。

§参数
  • token:处理后的 Token 内容字符串
  • origin_token_span:Token 在源码中的位置范围
  • source_code:源码的共享引用
  • token_type:Token 类型
§返回

新创建的 Token 实例

Source

pub fn token(&self) -> &String

获取 Token 的内容字符串。

§返回

Token 处理后的内容字符串引用

Source

pub fn origin_token_span(&self) -> (usize, usize)

获取 Token 在原始源码中的位置范围。

§返回

包含起始和结束位置的元组 (start, end)

Source

pub fn origin_token(&self) -> String

获取 Token 在原始源码中的原始内容。

从源码中提取 Token 的原始字符串,可能与处理后的内容不同 (例如字符串可能包含转义序列)。

§返回

Token 的原始源码字符串

Source

pub fn source_code(&self) -> &Source

获取源码的共享引用。

§返回

源码的引用,可用于访问完整的源码内容

Source

pub fn source_code_str(&self) -> String

获取完整源码的字符串表示。

将整个源码转换为字符串,主要用于调试和错误报告。

§返回

完整源码的字符串

Source

pub fn token_type(&self) -> TokenType

获取 Token 的类型。

§返回

Token 的类型枚举值

Trait Implementations§

Source§

impl Clone for Token

Source§

fn clone(&self) -> Token

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

Source§

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

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

impl PartialEq<&str> for Token

Source§

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

比较 Token 内容与字符串切片。

允许直接将 Token 与字符串字面量进行比较。

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 PartialEq<TokenType> for &Token

Source§

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

比较 Token 引用的类型与 TokenType。

允许对 Token 引用直接进行类型检查。

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 PartialEq<TokenType> for Token

Source§

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

比较 Token 类型与 TokenType。

允许直接检查 Token 是否为特定类型。

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 PartialEq<str> for Token

Source§

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

比较 Token 内容与字符串。

允许直接将 Token 与字符串进行比较。

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.

Auto Trait Implementations§

§

impl Freeze for Token

§

impl RefUnwindSafe for Token

§

impl Send for Token

§

impl Sync for Token

§

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