diff --git a/src/lexer.rs b/src/lexer.rs index 85b9bb0..ddb91bc 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -29,13 +29,12 @@ pub struct Token { pub line: usize, pub column: usize } -/* + impl<'a> fmt::Debug for Token { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "(TokenType: {:?}, TokenValue: {})", self.token_type, self.token_values.join(", ")) } } -*/ pub struct Node { token_type: TokenType, diff --git a/src/main.rs b/src/main.rs index 6dc5963..74304bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ mod parser; use parser::parse; fn main() { - let input = "int main() {}"; + let input = "if a + b {}"; println!("\n\n\n\n"); // let input = "test->xy"; let mut tokens = lex(input, false); diff --git a/src/parser.rs b/src/parser.rs index eb31c7e..a521fb9 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -19,10 +19,11 @@ impl<'a> fmt::Debug for Ast { } } -#[derive(Debug, PartialEq, Clone, Copy)] +#[derive(Debug, PartialEq, Clone)] pub enum AstDef { Normal(TokenType), - Repeated(TokenType), + NormalValue(TokenType, Vec), + Repeated(Vec), Optional(TokenType), Else } @@ -36,11 +37,7 @@ pub fn parse(tokens: &mut Vec) -> Box> { let ast_def: Vec = vec![ PNode { ast_type: AstTypes::FunctionDecleration, - ast_match: vec![AstDef::Normal(TokenType::Identifier), AstDef::Normal(TokenType::Identifier), AstDef::Normal(TokenType::Round), AstDef::Normal(TokenType::Curly)] - }, - PNode { - ast_type: AstTypes::FunctionDecleration, - ast_match: vec![AstDef::Normal(TokenType::Keyword)] + ast_match: vec![AstDef::Optional(TokenType::Identifier)] }, PNode { ast_type: AstTypes::Other, @@ -49,14 +46,12 @@ pub fn parse(tokens: &mut Vec) -> Box> { ]; let tokens_len = tokens.len(); let mut ast: Vec = Vec::new(); - let mut current = 0; let mut i = 0; while tokens.len()>0 { for ast_node in &ast_def { if tokens_len < ast_node.ast_match.len() { continue; } - let mut ast_types: Vec = vec![]; let mut is_match = false; let mut matched: Vec = Vec::new(); for ast_match in &ast_node.ast_match { @@ -71,6 +66,42 @@ pub fn parse(tokens: &mut Vec) -> Box> { break; } } + AstDef::NormalValue(token_type, token_value) => { + if &tokens[i].token_type == token_type && &tokens[i].token_values == token_value { + matched.push(tokens[i].clone()); + tokens.drain(0..1); + is_match = true; + } else { + is_match = false; + break; + } + } + AstDef::Repeated(nodes) => { + loop { + let mut node_match = false; + for &node_ in nodes { + if node_ == tokens[i].token_type { + matched.push(tokens[i].clone()); + node_match = true; + i+=1; + } + } + if node_match { + tokens.drain(0..i); + is_match = true; + } else { + break; + } + } + } + AstDef::Optional(token_type) => { + if &tokens[i].token_type == token_type { + matched.push(tokens[i].clone()); + tokens.drain(0..1); + is_match = true; + break; + } + } AstDef::Else => { matched.push(tokens[i].clone()); tokens.drain(0..1);