RustLexer

Struct RustLexer 

Source
pub struct RustLexer<'config> { /* private fields */ }
Expand description

A lexer for the Rust programming language.

The RustLexer is responsible for tokenizing Rust source code into a sequence of tokens that can be used by the parser. It handles all Rust syntax including modern features like raw strings, byte strings, lifetimes, and all standard Rust keywords.

§Examples

Basic usage:

use oak_core::{Lexer, SourceText};
use oak_rust::{RustLanguage, RustLexer};

let language = RustLanguage::default();
let lexer = RustLexer::new(&language);
let source = SourceText::new("fn main() { println!(\"Hello, world!\"); }");
let output = lexer.lex(source, 0);

// The output contains tokens for the entire source code
assert!(!output.tokens.is_empty());

Tokenizing different Rust constructs:

use oak_core::{Lexer, SourceText};
use oak_rust::{RustLanguage, RustLexer, SourceText};

let language = RustLanguage::default();
let lexer = RustLexer::new(&language);

// Tokenize a function with various Rust features
let source = SourceText::new(
    r#"
fn calculate<'a>(x: &'a i32, y: i32) -> i32 {
    let result = x + y;
    println!("Result: {}", result);
    result
}
"#,
);
let output = lexer.lex(source, 0);

// Verify that tokens were generated
assert!(output.tokens.len() > 10);

Implementations§

Source§

impl<'config> RustLexer<'config>

Source

pub fn new(config: &'config RustLanguage) -> Self

Creates a new RustLexer with the given language configuration.

§Parameters
  • config - A reference to the RustLanguage configuration that controls language-specific parsing behavior.
§Examples

let language = RustLanguage::default();
let lexer = RustLexer::new(&language);
Source

pub fn whitespace_rules(&self) -> &WhitespaceConfig

Returns the whitespace configuration for the lexer.

This method defines how the lexer should handle whitespace characters. The configuration enables Unicode whitespace support, allowing the lexer to recognize all Unicode whitespace characters, not just ASCII spaces.

Source

pub fn comment_rules(&self) -> &CommentLine

Returns the comment configuration for the lexer.

This method defines how the lexer should handle line comments in Rust code. Rust uses double slashes (//) for line comments, which continue to the end of the line.

Source

pub fn string_rules(&self) -> &StringConfig

Returns the string literal configuration for the lexer.

This method defines how the lexer should handle string literals in Rust code. Rust strings are enclosed in double quotes (“) and support escape sequences using backslash () as the escape character.

Source

pub fn char_rules(&self) -> &StringConfig

Returns the character literal configuration for the lexer.

This method defines how the lexer should handle character literals in Rust code. Rust character literals are enclosed in single quotes (’) and do not use escape characters in the same way as strings.

Trait Implementations§

Source§

impl<'config> Clone for RustLexer<'config>

Source§

fn clone(&self) -> RustLexer<'config>

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<'config> Lexer<RustLanguage> for RustLexer<'config>

Source§

fn lex_incremental( &self, source: impl Source, changed: usize, cache: IncrementalCache<'_, RustLanguage>, ) -> LexOutput<RustLanguage>

Tokenizes source text using an existing cache for incremental parsing. Read more
Source§

fn lex( &self, source: impl Source, ) -> OakDiagnostics<Vec<Token<<L as Language>::SyntaxKind>>>

Tokenizes the given source text into a sequence of tokens. Read more

Auto Trait Implementations§

§

impl<'config> Freeze for RustLexer<'config>

§

impl<'config> RefUnwindSafe for RustLexer<'config>

§

impl<'config> Send for RustLexer<'config>

§

impl<'config> Sync for RustLexer<'config>

§

impl<'config> Unpin for RustLexer<'config>

§

impl<'config> UnwindSafe for RustLexer<'config>

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.
Source§

impl<T> ErasedDestructor for T
where T: 'static,