colorful printing
This commit is contained in:
+1
-1
@@ -41,7 +41,7 @@ impl fmt::Debug for Token {
|
|||||||
|
|
||||||
impl fmt::Display for Token {
|
impl fmt::Display for Token {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
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
@@ -4,7 +4,7 @@ mod parser;
|
|||||||
use parser::Parser;
|
use parser::Parser;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let input = "int<Test> x";
|
let input = "int mut x";
|
||||||
println!("\n\n\n\n");
|
println!("\n\n\n\n");
|
||||||
let tokens = lex(input, false);
|
let tokens = lex(input, false);
|
||||||
let mut ast = Parser::new(tokens.clone());
|
let mut ast = Parser::new(tokens.clone());
|
||||||
|
|||||||
+19
-7
@@ -3,8 +3,8 @@ use std::fmt;
|
|||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum AstType {
|
pub enum AstType {
|
||||||
FunctionDecleration,
|
FunctionDeceleration,
|
||||||
VariableDecleration,
|
VariableDeceleration,
|
||||||
Other
|
Other
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ pub struct Ast {
|
|||||||
|
|
||||||
impl fmt::Display for Ast {
|
impl fmt::Display for Ast {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
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() {
|
for (i, token) in self.tokens.iter().enumerate() {
|
||||||
if i < self.tokens.len() - 1 {
|
if i < self.tokens.len() - 1 {
|
||||||
write!(f, " {},\n", token)?;
|
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 struct Parser {
|
||||||
pub tokens: Vec<Token>,
|
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+1].clone());
|
||||||
ast_res.tokens.push(self.tokens[index+2].clone());
|
ast_res.tokens.push(self.tokens[index+2].clone());
|
||||||
ast_res.tokens.push(self.tokens[index+3].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;
|
self.index += 3;
|
||||||
} else {
|
} else {
|
||||||
loop {
|
loop {
|
||||||
@@ -63,11 +75,11 @@ impl Parser {
|
|||||||
self.index += 1;
|
self.index += 1;
|
||||||
} else if ntk.token_type==TokenType::Identifier {
|
} else if ntk.token_type==TokenType::Identifier {
|
||||||
ast_res.tokens.push(ntk.clone());
|
ast_res.tokens.push(ntk.clone());
|
||||||
ast_res.ast_type = AstType::VariableDecleration;
|
ast_res.ast_type = AstType::VariableDeceleration;
|
||||||
self.index += 1;
|
self.index += 1;
|
||||||
} else if ntk.token_type==TokenType::Keyword {
|
} else if ntk.token_type==TokenType::Keyword {
|
||||||
ast_res.tokens.push(ntk.clone());
|
ast_res.tokens.push(ntk.clone());
|
||||||
ast_res.ast_type = AstType::VariableDecleration;
|
ast_res.ast_type = AstType::VariableDeceleration;
|
||||||
self.index += 1;
|
self.index += 1;
|
||||||
} else if ntk.token_type==TokenType::Angle {
|
} else if ntk.token_type==TokenType::Angle {
|
||||||
ast_res.tokens.push(ntk.clone());
|
ast_res.tokens.push(ntk.clone());
|
||||||
|
|||||||
Reference in New Issue
Block a user