Skip to main content

ScannerState

Struct ScannerState 

Source
pub struct ScannerState {
    pub interner: Interner,
    /* private fields */
}
Expand description

The scanner state that holds the current position and token information.

ZERO-COPY OPTIMIZATION: Source is stored as UTF-8 text directly (no Vec). For ASCII-only files (99% of TypeScript), byte position == character position. Positions are byte-based internally for performance, converted when needed.

Fields§

§interner: Interner

String interner for identifier deduplication

Implementations§

Source§

impl ScannerState

Source

pub fn new(text: String, skip_trivia: bool) -> ScannerState

Exported scanner accessors are JS bindings and cannot be made const because #[wasm_bindgen] methods in this crate are non-const. Create a new scanner state with the given text. ZERO-COPY: No Vec allocation, works directly with UTF-8 bytes.

Source

pub fn get_pos(&self) -> usize

Get the current position (end position of current token).

Source

pub fn set_pos(&mut self, pos: usize)

Set the current position (used for rescanning compound tokens). This allows consuming partial tokens like splitting >> into > + >.

Source

pub fn get_token_full_start(&self) -> usize

Get the full start position (including leading trivia).

Source

pub fn get_token_start(&self) -> usize

Get the start position of the current token (excluding trivia).

Source

pub fn get_token_end(&self) -> usize

Get the end position of the current token.

Source

pub fn get_token(&self) -> SyntaxKind

Get the current token kind.

Source

pub fn get_token_value(&self) -> String

Get the current token’s string value. Note: Prefer get_token_value_ref() to avoid allocation when possible.

Source

pub fn get_token_text(&self) -> String

Get the current token’s text from the source.

Source

pub fn get_token_flags(&self) -> u32

Get the token flags.

Source

pub fn has_preceding_line_break(&self) -> bool

Check if there was a preceding line break.

Source

pub fn is_unterminated(&self) -> bool

Check if the token is unterminated.

Source

pub fn is_identifier(&self) -> bool

Check if the current token is an identifier.

Source

pub fn is_reserved_word(&self) -> bool

Check if the current token is a reserved word.

Source

pub fn set_text( &mut self, text: String, start: Option<usize>, length: Option<usize>, )

Set the text to scan. ZERO-COPY: Works directly with UTF-8 bytes.

Source

pub fn reset_token_state(&mut self, new_pos: usize)

Reset the token state to a specific position.

Source

pub fn get_text(&self) -> String

Get the source text.

Source

pub fn scan(&mut self) -> SyntaxKind

Scan the next token.

Source

pub fn re_scan_greater_token(&mut self) -> SyntaxKind

Re-scan the current > token to see if it should be >=, >>, >>>, >>=, or >>>=. This is used by the parser for type arguments and bitwise operators.

Source

pub fn re_scan_slash_token(&mut self) -> SyntaxKind

Re-scan the current / or /= token as a regex literal. This is used by the parser when it determines the context requires a regex.

Source

pub fn re_scan_asterisk_equals_token(&mut self) -> SyntaxKind

Re-scan the current *= token as * followed by =. Used when parsing computed property names.

Source

pub fn re_scan_template_token( &mut self, _is_tagged_template: bool, ) -> SyntaxKind

Re-scan the current } token as the continuation of a template literal. Called by the parser when it determines that a } is closing a template expression.

§Arguments
  • is_tagged_template - If true, invalid escape sequences should not report errors (tagged templates can have invalid escapes that get passed to the tag function as raw). For now, we don’t report errors anyway, so this parameter affects nothing.
Source

pub fn re_scan_template_head_or_no_substitution_template( &mut self, ) -> SyntaxKind

Re-scan template head or no-substitution template. Used when the parser needs to rescan the start of a template.

Source

pub fn scan_jsx_identifier(&mut self) -> SyntaxKind

Scan a JSX identifier. In JSX, identifiers can contain hyphens (like data-testid).

Source

pub fn re_scan_jsx_token( &mut self, allow_multiline_jsx_text: bool, ) -> SyntaxKind

Re-scan the current token as a JSX token. Used when the parser enters JSX context and needs to rescan. Must reset to full_start_pos (before trivia), not token_start (after trivia), so that JSX text nodes include leading whitespace/newlines. Matches tsc: pos = tokenStart = fullStartPos;

Source

pub fn scan_jsx_attribute_value(&mut self) -> SyntaxKind

Scan a JSX attribute value (string literal or expression).

Source

pub fn re_scan_jsx_attribute_value(&mut self) -> SyntaxKind

Re-scan a JSX attribute value from the current token position.

Source

pub fn re_scan_less_than_token(&mut self) -> SyntaxKind

Re-scan a < token in JSX context. Returns LessThanSlashToken if followed by /, otherwise LessThanToken.

Source

pub fn re_scan_hash_token(&mut self) -> SyntaxKind

Re-scan the current # token as a hash token or private identifier.

Source

pub fn re_scan_question_token(&mut self) -> SyntaxKind

Re-scan the current ? token for optional chaining.

Source

pub fn scan_jsdoc_token(&mut self) -> SyntaxKind

Scan a JSDoc token. Used when parsing JSDoc comments.

Source

pub fn scan_jsdoc_comment_text_token( &mut self, in_backticks: bool, ) -> SyntaxKind

Scan JSDoc comment text token. Used for scanning the text content within JSDoc comments.

Source

pub fn scan_shebang_trivia(&mut self) -> usize

Scan a shebang (#!) at the start of the file. Returns the length of the shebang line (including newline), or 0 if no shebang.

Source

pub fn re_scan_invalid_identifier(&mut self) -> SyntaxKind

Re-scan an invalid identifier to check if it’s valid in a specific context.

Source§

impl ScannerState

Source

pub fn save_state(&self) -> ScannerSnapshot

Save the current scanner state for look-ahead.

Source

pub fn restore_state(&mut self, snapshot: ScannerSnapshot)

Restore a saved scanner state.

Source

pub const fn get_token_atom(&self) -> Atom

Get the interned atom for the current identifier token. Returns Atom::NONE if the current token is not an identifier. This enables O(1) string comparison for identifiers.

Source

pub const fn get_invalid_separator_pos(&self) -> Option<usize>

Source

pub const fn invalid_separator_is_consecutive(&self) -> bool

Source

pub fn get_regex_flag_errors(&self) -> &[RegexFlagError]

Get the regex flag errors detected during scanning.

Source

pub fn get_scanner_diagnostics(&self) -> &[ScannerDiagnostic]

Get general scanner diagnostics (e.g., conflict marker errors).

Source

pub fn resolve_atom(&self, atom: Atom) -> &str

Resolve an atom back to its string value. Panics if the atom is invalid.

Source

pub const fn interner(&self) -> &Interner

Get a reference to the interner for direct use by the parser.

Source

pub const fn interner_mut(&mut self) -> &mut Interner

Get a mutable reference to the interner.

Source

pub fn take_interner(&mut self) -> Interner

Take ownership of the interner, replacing it with a new empty one. Used to transfer the interner to NodeArena after parsing.

Source

pub fn get_token_value_ref(&self) -> &str

ZERO-COPY: Get the current token value as a reference. For identifiers/keywords, returns the interned string. For other tokens, returns the token_value or raw source slice. This avoids allocation compared to get_token_value().

Source

pub fn get_token_text_ref(&self) -> &str

ZERO-COPY: Get the raw token text directly from source. This is the unprocessed text from token_start to current pos.

Source

pub fn source_slice(&self, start: usize, end: usize) -> &str

ZERO-COPY: Get a slice of the source text by positions.

Source

pub fn source_text(&self) -> &str

Get the source text reference.

Source§

impl ScannerState

Source

pub fn source_text_arc(&self) -> Arc<str>

Get a cloned handle to the shared source text.

Trait Implementations§

Source§

impl From<ScannerState> for JsValue

Source§

fn from(value: ScannerState) -> JsValue

Converts to this type from the input type.
Source§

impl FromWasmAbi for ScannerState

Source§

type Abi = u32

The Wasm ABI type that this converts from when coming back out from the ABI boundary.
Source§

unsafe fn from_abi(js: u32) -> ScannerState

Recover a Self from Self::Abi. Read more
Source§

impl IntoWasmAbi for ScannerState

Source§

type Abi = u32

The Wasm ABI type that this converts into when crossing the ABI boundary.
Source§

fn into_abi(self) -> u32

Convert self into Self::Abi so that it can be sent across the wasm ABI boundary.
Source§

impl LongRefFromWasmAbi for ScannerState

Source§

type Abi = u32

Same as RefFromWasmAbi::Abi
Source§

type Anchor = RcRef<ScannerState>

Same as RefFromWasmAbi::Anchor
Source§

unsafe fn long_ref_from_abi( js: <ScannerState as LongRefFromWasmAbi>::Abi, ) -> <ScannerState as LongRefFromWasmAbi>::Anchor

Same as RefFromWasmAbi::ref_from_abi
Source§

impl OptionFromWasmAbi for ScannerState

Source§

fn is_none(abi: &<ScannerState as FromWasmAbi>::Abi) -> bool

Tests whether the argument is a “none” instance. If so it will be deserialized as None, and otherwise it will be passed to FromWasmAbi.
Source§

impl OptionIntoWasmAbi for ScannerState

Source§

fn none() -> <ScannerState as IntoWasmAbi>::Abi

Returns an ABI instance indicating “none”, which JS will interpret as the None branch of this option. Read more
Source§

impl RefFromWasmAbi for ScannerState

Source§

type Abi = u32

The Wasm ABI type references to Self are recovered from.
Source§

type Anchor = RcRef<ScannerState>

The type that holds the reference to Self for the duration of the invocation of the function that has an &Self parameter. This is required to ensure that the lifetimes don’t persist beyond one function call, and so that they remain anonymous.
Source§

unsafe fn ref_from_abi( js: <ScannerState as RefFromWasmAbi>::Abi, ) -> <ScannerState as RefFromWasmAbi>::Anchor

Recover a Self::Anchor from Self::Abi. Read more
Source§

impl RefMutFromWasmAbi for ScannerState

Source§

type Abi = u32

Same as RefFromWasmAbi::Abi
Source§

type Anchor = RcRefMut<ScannerState>

Same as RefFromWasmAbi::Anchor
Source§

unsafe fn ref_mut_from_abi( js: <ScannerState as RefMutFromWasmAbi>::Abi, ) -> <ScannerState as RefMutFromWasmAbi>::Anchor

Same as RefFromWasmAbi::ref_from_abi
Source§

impl TryFromJsValue for ScannerState

Source§

fn try_from_js_value(value: JsValue) -> Result<ScannerState, JsValue>

Performs the conversion.
Source§

fn try_from_js_value_ref(value: &JsValue) -> Option<ScannerState>

Performs the conversion.
Source§

impl VectorFromWasmAbi for ScannerState

Source§

impl VectorIntoWasmAbi for ScannerState

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ReturnWasmAbi for T
where T: IntoWasmAbi,

Source§

type Abi = <T as IntoWasmAbi>::Abi

Same as IntoWasmAbi::Abi
Source§

fn return_abi(self) -> <T as ReturnWasmAbi>::Abi

Same as IntoWasmAbi::into_abi, except that it may throw and never return in the case of Err.
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.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more