Skip to main content

TokenStream

Struct TokenStream 

Source
pub struct TokenStream {
    pub tokens: Vec<Token>,
    pub index: usize,
    /* private fields */
}
Expand description

§5.3 L1725-1754: A token stream.

Fields§

§tokens: Vec<Token>

§5.3 L1730-1738: A list of tokens and/or component values. We model component values as Tokens here for simplicity; the §5.5 algorithms that consume “component values” wrap tokens into crate::types::ComponentValue at the boundary.

§index: usize

§5.3 L1740-1748: An index into tokens, representing parsing progress. Starts at 0. Never decreases except via Self::restore_mark.

Implementations§

Source§

impl TokenStream

Source

pub fn new(tokens: Vec<Token>) -> Self

§5.3: Construct a new token stream over tokens. The stream implicitly appends an EOF token (§5.3 L1811-1813); we model it by returning Token::Eof from Self::next_token when index is out of bounds, rather than storing a sentinel.

构造的 stream 不带 source-text tracking。如需追踪原始 source (用于 §5.5.6 original_text),请用 Self::with_source。

Source

pub fn with_source(source: &str) -> Self

构造一个带 source-text tracking 的 TokenStream。

tokenize source 并记录每个 token 的 byte range,使 Self::source_slice 能返回原始 source 片段。供 §5.5.6 original_text(custom property)使用。

Source

pub fn record_nesting_error(&self, depth: u32)

记录嵌套深度超限错误(仅首次覆盖,保留最早的超限点)。

Source

pub fn nesting_error(&self) -> Option<ParseError>

返回解析过程中记录的第一个嵌套深度超限错误。

Source

pub fn enter_nesting(&self) -> Option<u32>

进入一层嵌套(consume_a_simple_block / consume_a_function 入口调用)。

返回 Some(depth) 表示该层已超过 MAX_NESTING_DEPTH: 错误已被记录,调用方应停止下降(不再递归)并返回空结果。 返回 None 表示正常进入。

Source

pub fn leave_nesting(&self)

退出一层嵌套(与 Self::enter_nesting 成对调用)。

Source

pub fn source_slice(&self, start_index: usize, end_index: usize) -> Option<&str>

返回 tokens[start_index..end_index] 对应的原始 source text。

参数是 token 索引(不是 char/byte 索引)。 返回 None 如果无 source tracking 或 index 越界。

Source

pub fn next_token(&self) -> Token

§5.3 L1769-1773: The item of tokens at index. If out-of-bounds, return Token::Eof.

Source

pub fn is_empty(&self) -> bool

§5.3 L1775-1777: A token stream is empty if the next token is <EOF-token>.

Source

pub fn consume_token(&mut self) -> Token

§5.3 L1779-1782: Let token be the next token. Increment index, then return token.

Source

pub fn discard_token(&mut self)

§5.3 L1784-1786: If not empty, increment index.

Source

pub fn mark(&mut self)

§5.3 L1788-1789: Append index to marked_indexes.

Source

pub fn restore_mark(&mut self)

§5.3 L1791-1793: Pop from marked_indexes and set index to the popped value. No-op if stack is empty (defensive).

Source

pub fn discard_mark(&mut self)

§5.3 L1795-1797: Pop from marked_indexes and discard.

Source

pub fn discard_whitespace(&mut self)

§5.3 L1799-1801: While the next token is a <whitespace-token>, discard a token.

Trait Implementations§

Source§

impl Clone for TokenStream

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 TokenStream

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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.