Début du scanner
This commit is contained in:
@@ -65,4 +65,4 @@ fn run( _script: String ) -> i32 {
|
|||||||
// https://github.com/munificent/craftinginterpreters/wiki/Lox-implementations#rust
|
// https://github.com/munificent/craftinginterpreters/wiki/Lox-implementations#rust
|
||||||
|
|
||||||
// Pause :
|
// Pause :
|
||||||
// http://www.craftinginterpreters.com/scanning.html#regular-languages-and-expressions
|
// http://www.craftinginterpreters.com/scanning.html#lexical-errors
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
mod token_type;
|
mod token_type;
|
||||||
mod token;
|
mod token;
|
||||||
|
mod scanner;
|
||||||
|
|
||||||
pub struct RLox {
|
pub struct RLox {
|
||||||
pub had_error: bool,
|
pub had_error: bool,
|
||||||
|
|||||||
59
src/rlox/scanner.rs
Normal file
59
src/rlox/scanner.rs
Normal 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 } );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
use super::token_type::TokenType;
|
use super::token_type::TokenType;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Token {
|
pub struct Token {
|
||||||
token_type: TokenType,
|
pub token_type: TokenType,
|
||||||
lexeme: String,
|
pub lexeme: String,
|
||||||
literal: String,
|
pub literal: String,
|
||||||
line: u32,
|
pub line: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for Token {
|
impl std::fmt::Display for Token {
|
||||||
|
|||||||
Reference in New Issue
Block a user