Skip to main content

TypedTokenizer

Struct TypedTokenizer 

Source
pub struct TypedTokenizer<G: TypedGrammar> { /* private fields */ }
Expand description

Tokenizer parameterized by grammar type G.

Useful for reusable tooling built against generated grammars.

  • Use this when grammar type is known at compile time.
  • Use Tokenizer for typical SQLite SQL app code.

Implementations§

Source§

impl<G: TypedGrammar> TypedTokenizer<G>

Source

pub fn new(grammar: G) -> Self

Create a tokenizer for grammar G.

§Examples
use syntaqlite_syntax::typed::{grammar, TypedTokenizer};

let _tokenizer = TypedTokenizer::new(grammar());
§Panics

Panics if tokenizer allocation fails (out of memory).

Source

pub fn tokenize<'a>( &self, source: &'a str, ) -> impl Iterator<Item = TypedToken<'a, G>> + use<'a, G>

Tokenize source and iterate typed tokens.

The source is copied; the original does not need to outlive the iterator. For zero-copy tokenization use tokenize_cstr.

§Examples
use syntaqlite_syntax::TokenType;
use syntaqlite_syntax::typed::{grammar, TypedTokenizer};

let tokenizer = TypedTokenizer::new(grammar());
let tokens: Vec<_> = tokenizer.tokenize("SELECT 1").collect();

assert_eq!(tokens[0].token_type(), TokenType::Select);
assert_eq!(tokens[0].text(), "SELECT");
§Panics

Panics if another cursor from this tokenizer is still active. Drop the previous iterator before starting a new one.

Source

pub fn tokenize_cstr<'a>( &self, source: &'a CStr, ) -> impl Iterator<Item = TypedToken<'a, G>> + use<'a, G>

Zero-copy tokenization over a null-terminated source buffer.

No copy is performed. The source must be valid UTF-8 (panics otherwise).

§Examples
use std::ffi::CString;
use syntaqlite_syntax::TokenType;
use syntaqlite_syntax::typed::{grammar, TypedTokenizer};

let tokenizer = TypedTokenizer::new(grammar());
let sql = CString::new("SELECT 1").unwrap();
let types: Vec<_> = tokenizer.tokenize_cstr(&sql).map(|t| t.token_type()).collect();

assert!(types.contains(&TokenType::Select));
§Panics

Panics if another cursor from this tokenizer is still active, or if source is not valid UTF-8.

Auto Trait Implementations§

§

impl<G> Freeze for TypedTokenizer<G>

§

impl<G> !RefUnwindSafe for TypedTokenizer<G>

§

impl<G> !Send for TypedTokenizer<G>

§

impl<G> !Sync for TypedTokenizer<G>

§

impl<G> Unpin for TypedTokenizer<G>
where G: Unpin,

§

impl<G> UnsafeUnpin for TypedTokenizer<G>

§

impl<G> !UnwindSafe for TypedTokenizer<G>

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.