Début du scanner

This commit is contained in:
2024-04-02 08:29:25 +02:00
parent 648a76e2e9
commit 63d9addfc4
4 changed files with 66 additions and 6 deletions

View File

@@ -65,4 +65,4 @@ fn run( _script: String ) -> i32 {
// https://github.com/munificent/craftinginterpreters/wiki/Lox-implementations#rust
// Pause :
// http://www.craftinginterpreters.com/scanning.html#regular-languages-and-expressions
// http://www.craftinginterpreters.com/scanning.html#lexical-errors

View File

@@ -1,5 +1,6 @@
mod token_type;
mod token;
mod scanner;
pub struct RLox {
pub had_error: bool,

59
src/rlox/scanner.rs Normal file
View File

@@ -0,0 +1,59 @@
use super::token_type::TokenType;
use super::token::Token;
struct Scanner {
source: Vec<char>,
tokens: Vec<Token>,
start: usize,
current: usize,
line: u32,
}
impl Scanner {
fn scan_tokens(&mut self) {
while (!self.is_at_end()) {
self.start = self.current;
self.scan_token();
}
// Ajout d'un token final quand il n'y a plus rien à parser
self.tokens.push(Token{ token_type: TokenType::Eof, lexeme: String::from(""), literal: String::from(""), line: self.line } );
}
fn is_at_end(&self) -> bool {
self.current>= self.source.len()
}
fn scan_token(&mut self) {
let c = self.advance();
match c {
'(' => self.add_simple_token( TokenType::LeftParen ),
')' => self.add_simple_token( TokenType::RightParen ),
'{' => self.add_simple_token( TokenType::LeftBrace ),
'}' => self.add_simple_token( TokenType::RightBrace ),
',' => self.add_simple_token( TokenType::Comma ),
'.' => self.add_simple_token( TokenType::Dot ),
'-' => self.add_simple_token( TokenType::Minus ),
'+' => self.add_simple_token( TokenType::Plus ),
';' => self.add_simple_token( TokenType::Semicolon ),
'*' => self.add_simple_token( TokenType::Star ),
_ => ()
}
}
fn advance(&mut self) -> char {
self.current += 1;
self.source[self.current]
}
fn add_simple_token(&mut self, t: TokenType) {
self.add_token(t, String::from(""));
}
fn add_token(&mut self, t: TokenType, l: String) {
let text = self.source[self.start..self.current].iter().collect();
self.tokens.push(Token{ token_type: t, lexeme: text, literal: l, line: self.line } );
}
}

View File

@@ -1,11 +1,11 @@
use super::token_type::TokenType;
#[derive(Debug)]
struct Token {
token_type: TokenType,
lexeme: String,
literal: String,
line: u32,
pub struct Token {
pub token_type: TokenType,
pub lexeme: String,
pub literal: String,
pub line: u32,
}
impl std::fmt::Display for Token {