Lexer

Struct Lexer 

Source
pub struct Lexer {
    pub source: Source,
    pub tokens: Vec<Token>,
    pub position: Position,
}
Expand description

The lexer is responsible for converting the source code into a list of tokens.

Fields§

§source: Source§tokens: Vec<Token>§position: Position

Implementations§

Source§

impl Lexer

Source

pub fn new(source: Source) -> Lexer

Create a new lexer from a source string.

§Arguments
  • source - An instance of Source containing the source code.
§Example
use roan_ast::{Lexer, TokenKind};
use roan_ast::source::Source;
let source = Source::from_string("let x = 10;".to_string());
let mut lexer = Lexer::new(source);
let tokens = lexer.lex().expect("Failed to lex source code");

assert_eq!(tokens.first().unwrap().kind, TokenKind::Let);
Source§

impl Lexer

Source

pub fn lex(&mut self, lex_comments: bool) -> Result<Vec<Token>, Error>

Lex the source code and return a list of tokens.

During the lexing process, the lexer will consume the source code character by character and convert it into a list of tokens. The lexer will skip whitespace and comments.

When EOF is reached, the lexer will return the list of tokens.

Source

pub fn is_eof(&self) -> bool

Check if the lexer has reached the end of the source code.

Source

pub fn current(&mut self) -> Option<char>

Get the current character in the source code.

Source

pub fn consume(&mut self) -> Option<char>

Consume the current character and move to the next one.

Source

pub fn is_identifier_start(&self, c: char) -> bool

Check if the character is a valid identifier start character.

Source

pub fn is_number_start(&self, c: char) -> bool

Check if the character is a valid number start character.

Source

pub fn peek(&self) -> Option<char>

Peek at the next character in the source code.

Source

pub fn next_token(&mut self) -> Result<Option<Token>, Error>

Get the next token in the source code.

Source

pub fn consume_number(&mut self) -> (NumberType, String)

Consume a number.

Can be either an integer or a float.

Source

pub fn parse_char(&mut self) -> Result<char, Error>

Parses a character literal. Throws an error if more than one character is found.

Source

pub fn lex_potential_double( &mut self, expected: char, one_char: TokenKind, double_char: TokenKind, ) -> TokenKind

Source

pub fn lex_potential_triple( &mut self, expected: char, one_char: TokenKind, double_char: TokenKind, triple_char: TokenKind, ) -> TokenKind

Source

pub fn match_next(&mut self, ch: char) -> bool

Check if the next character matches the given character.

Auto Trait Implementations§

§

impl Freeze for Lexer

§

impl RefUnwindSafe for Lexer

§

impl Send for Lexer

§

impl Sync for Lexer

§

impl Unpin for Lexer

§

impl UnwindSafe for Lexer

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