rename scan state for lexer to LexerState (#12)

This commit is contained in:
Josh Thomas 2024-10-14 10:58:06 -05:00 committed by GitHub
parent ab09e6058b
commit a34ac87242
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 6 deletions

View file

@ -1,11 +1,11 @@
use crate::error::LexerError;
use crate::scanner::{Scanner, ScannerState};
use crate::scanner::{LexerState, Scanner};
use crate::token::{Token, TokenType, Tokenizer};
pub struct Lexer<'a> {
source: &'a str,
tokens: Vec<Token>,
state: ScannerState,
state: LexerState,
}
impl<'a> Lexer<'a> {
@ -13,7 +13,7 @@ impl<'a> Lexer<'a> {
Lexer {
source,
tokens: Vec::new(),
state: ScannerState::new(),
state: LexerState::new(),
}
}
@ -262,6 +262,17 @@ impl<'a> Scanner for Lexer<'a> {
.unwrap_or('\0')
}
fn previous(&self) -> Self::Item {
if self.state.current > 0 {
self.source[..self.state.current]
.chars()
.last()
.unwrap_or('\0')
} else {
'\0'
}
}
fn is_at_end(&self) -> bool {
self.state.current >= self.source.len()
}

View file

@ -1,14 +1,14 @@
use std::fmt::Debug;
pub struct ScannerState {
pub struct LexerState {
pub start: usize,
pub current: usize,
pub line: usize,
}
impl ScannerState {
impl LexerState {
pub fn new() -> Self {
ScannerState {
LexerState {
start: 0,
current: 0,
line: 1,
@ -22,5 +22,6 @@ pub trait Scanner {
fn advance(&mut self) -> Self::Item;
fn peek(&self) -> Self::Item;
fn peek_next(&self) -> Self::Item;
fn previous(&self) -> Self::Item;
fn is_at_end(&self) -> bool;
}