chapter 8 done

This commit is contained in:
Moritz Gmeiner 2023-01-22 23:33:57 +01:00
commit 956c4d0f28
13 changed files with 570 additions and 177 deletions

View file

@ -71,7 +71,7 @@ impl Lexer {
me.scan_token();
}
me.tokens.push(Token::new(TokenType::EOF, "".to_owned(), me.code_pos));
me.tokens.push(Token::new(TokenType::EOF, me.code_pos));
if me.errors.is_empty() {
Ok(me.tokens)
@ -234,7 +234,7 @@ impl Lexer {
fn push_token(&mut self, token_type: TokenType) {
let lexeme: String = self.source[self.start..self.current].iter().collect();
self.tokens.push(Token::new(token_type, lexeme, self.code_pos));
self.tokens.push(Token::new(token_type, self.code_pos));
}
fn try_parse_string(&mut self) -> Option<TokenType> {

View file

@ -27,33 +27,25 @@ pub enum TokenType {
}
pub struct Token {
token_type: TokenType,
lexeme: String,
code_pos: CodePos,
pub token_type: TokenType,
// pub lexeme: String,
pub code_pos: CodePos,
}
impl Token {
pub fn new(token_type: TokenType, lexeme: String, pos: CodePos) -> Self {
pub fn new(token_type: TokenType, pos: CodePos) -> Self {
Token {
token_type,
lexeme: lexeme,
// lexeme,
code_pos: pos,
}
}
pub fn token_type(&self) -> &TokenType {
&self.token_type
}
pub fn code_pos(&self) -> CodePos {
self.code_pos
}
}
impl std::fmt::Debug for Token {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "<{:?}> (\"{}\")", self.token_type, self.lexeme)
write!(f, "<{:?}>", self.token_type)
// write!(f, "<{:?}> (\"{}\")", self.token_type, self.lexeme)
}
}