Skip to main content

ParserToken

Struct ParserToken 

Source
pub struct ParserToken<'a>(/* private fields */);
Expand description

One parser-observed token from a parsed statement.

Returned by ParsedStatement::tokens. This is useful when building token-aware tooling such as:

  • Semantic syntax highlighting.
  • Identifier/function/type classification.
  • Statement-level token diagnostics.

Requires collect_tokens: true in ParserConfig.

§Examples

use syntaqlite_syntax::{Parser, ParserConfig, TokenType};

let parser = Parser::with_config(&ParserConfig::default().with_collect_tokens(true));
let mut session = parser.parse("SELECT max(x) FROM t;");
let stmt = session.next().transpose().unwrap().unwrap();

let tokens: Vec<_> = stmt.tokens().collect();
assert!(!tokens.is_empty());
assert!(tokens.iter().any(|t| t.token_type() == TokenType::Select));

// Flags expose parser-inferred role information (identifier/function/type).
let _has_semantic_role = tokens.iter().any(|t| {
    let f = t.flags();
    f.used_as_identifier() || f.used_as_function() || f.used_as_type()
});

Implementations§

Source§

impl<'a> ParserToken<'a>

Source

pub fn text(&self) -> &'a str

Exact source text for this token.

Preserves original casing and quoting from input SQL.

Source

pub fn token_type(&self) -> TokenType

Token kind from the SQLite SQL grammar.

This is the lexical class (keyword, identifier, operator, etc.).

Source

pub fn flags(&self) -> ParserTokenFlags

Semantic usage flags inferred by the parser.

Use this to distinguish contextual role, for example:

  • Keyword text used as an identifier.
  • Function-call names.
  • Type names.
Source

pub fn offset(&self) -> u32

Byte offset of the token start within the statement source.

Source

pub fn length(&self) -> u32

Byte length of the token text.

Trait Implementations§

Source§

impl Debug for ParserToken<'_>

Available on crate feature sqlite only.
Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for ParserToken<'a>

§

impl<'a> RefUnwindSafe for ParserToken<'a>

§

impl<'a> Send for ParserToken<'a>

§

impl<'a> Sync for ParserToken<'a>

§

impl<'a> Unpin for ParserToken<'a>

§

impl<'a> UnsafeUnpin for ParserToken<'a>

§

impl<'a> UnwindSafe for ParserToken<'a>

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