colorful printing

This commit is contained in:
2024-04-08 14:09:59 +02:00
parent c915c99d06
commit 3e41b5f3ac
3 changed files with 21 additions and 9 deletions
+1 -1
View File
@@ -41,7 +41,7 @@ impl fmt::Debug for Token {
impl fmt::Display for Token {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "(TokenType: {:?}, TokenValue: {}, Line: {}, Column {})", self.token_type, self.value, self.line, self.column)
write!(f, "(\x1b[33mTokenType:\x1b[0m \x1b[35m{:?}\x1b[0m, \x1b[33mTokenValue:\x1b[0m \x1b[32m\"{}\"\x1b[0m, \x1b[33mLine:\x1b[0m \x1b[36m{}\x1b[0m, \x1b[33mColumn:\x1b[0m \x1b[36m{}\x1b[0m)", self.token_type, self.value, self.line, self.column)
}
}
+1 -1
View File
@@ -4,7 +4,7 @@ mod parser;
use parser::Parser;
fn main() {
let input = "int<Test> x";
let input = "int mut x";
println!("\n\n\n\n");
let tokens = lex(input, false);
let mut ast = Parser::new(tokens.clone());
+19 -7
View File
@@ -3,8 +3,8 @@ use std::fmt;
#[derive(Clone, Debug)]
pub enum AstType {
FunctionDecleration,
VariableDecleration,
FunctionDeceleration,
VariableDeceleration,
Other
}
@@ -15,7 +15,7 @@ pub struct Ast {
impl fmt::Display for Ast {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}: [\n", self.ast_type)?;
write!(f, "\x1b[36m{:?}:\x1b[0m [\n", self.ast_type)?;
for (i, token) in self.tokens.iter().enumerate() {
if i < self.tokens.len() - 1 {
write!(f, " {},\n", token)?;
@@ -27,7 +27,19 @@ impl fmt::Display for Ast {
}
}
impl fmt::Debug for Ast {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}: [\n", self.ast_type)?;
for (i, token) in self.tokens.iter().enumerate() {
if i < self.tokens.len() - 1 {
write!(f, " {:?},\n", token)?;
} else {
write!(f, " {:?}\n", token)?;
}
}
write!(f, "]")
}
}
pub struct Parser {
pub tokens: Vec<Token>,
@@ -51,7 +63,7 @@ impl Parser {
ast_res.tokens.push(self.tokens[index+1].clone());
ast_res.tokens.push(self.tokens[index+2].clone());
ast_res.tokens.push(self.tokens[index+3].clone());
ast_res.ast_type = AstType::FunctionDecleration;
ast_res.ast_type = AstType::FunctionDeceleration;
self.index += 3;
} else {
loop {
@@ -63,11 +75,11 @@ impl Parser {
self.index += 1;
} else if ntk.token_type==TokenType::Identifier {
ast_res.tokens.push(ntk.clone());
ast_res.ast_type = AstType::VariableDecleration;
ast_res.ast_type = AstType::VariableDeceleration;
self.index += 1;
} else if ntk.token_type==TokenType::Keyword {
ast_res.tokens.push(ntk.clone());
ast_res.ast_type = AstType::VariableDecleration;
ast_res.ast_type = AstType::VariableDeceleration;
self.index += 1;
} else if ntk.token_type==TokenType::Angle {
ast_res.tokens.push(ntk.clone());