Merge branch 'master' into wip/lexer-info

This commit is contained in:
xQuantx
2024-04-03 16:06:59 +02:00
committed by GitHub
3 changed files with 42 additions and 12 deletions
+1 -2
View File
@@ -29,13 +29,12 @@ pub struct Token {
pub line: usize, pub line: usize,
pub column: usize pub column: usize
} }
/*
impl<'a> fmt::Debug for Token { impl<'a> fmt::Debug for Token {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "(TokenType: {:?}, TokenValue: {})", self.token_type, self.token_values.join(", ")) write!(f, "(TokenType: {:?}, TokenValue: {})", self.token_type, self.token_values.join(", "))
} }
} }
*/
pub struct Node { pub struct Node {
token_type: TokenType, token_type: TokenType,
+1 -1
View File
@@ -4,7 +4,7 @@ mod parser;
use parser::parse; use parser::parse;
fn main() { fn main() {
let input = "int main() {}"; let input = "if a + b {}";
println!("\n\n\n\n"); println!("\n\n\n\n");
// let input = "test->xy"; // let input = "test->xy";
let mut tokens = lex(input, false); let mut tokens = lex(input, false);
+40 -9
View File
@@ -19,10 +19,11 @@ impl<'a> fmt::Debug for Ast {
} }
} }
#[derive(Debug, PartialEq, Clone, Copy)] #[derive(Debug, PartialEq, Clone)]
pub enum AstDef { pub enum AstDef {
Normal(TokenType), Normal(TokenType),
Repeated(TokenType), NormalValue(TokenType, Vec<String>),
Repeated(Vec<TokenType>),
Optional(TokenType), Optional(TokenType),
Else Else
} }
@@ -36,11 +37,7 @@ pub fn parse(tokens: &mut Vec<Token>) -> Box<Vec<Ast>> {
let ast_def: Vec<PNode> = vec![ let ast_def: Vec<PNode> = vec![
PNode { PNode {
ast_type: AstTypes::FunctionDecleration, ast_type: AstTypes::FunctionDecleration,
ast_match: vec![AstDef::Normal(TokenType::Identifier), AstDef::Normal(TokenType::Identifier), AstDef::Normal(TokenType::Round), AstDef::Normal(TokenType::Curly)] ast_match: vec![AstDef::Optional(TokenType::Identifier)]
},
PNode {
ast_type: AstTypes::FunctionDecleration,
ast_match: vec![AstDef::Normal(TokenType::Keyword)]
}, },
PNode { PNode {
ast_type: AstTypes::Other, ast_type: AstTypes::Other,
@@ -49,14 +46,12 @@ pub fn parse(tokens: &mut Vec<Token>) -> Box<Vec<Ast>> {
]; ];
let tokens_len = tokens.len(); let tokens_len = tokens.len();
let mut ast: Vec<Ast> = Vec::new(); let mut ast: Vec<Ast> = Vec::new();
let mut current = 0;
let mut i = 0; let mut i = 0;
while tokens.len()>0 { while tokens.len()>0 {
for ast_node in &ast_def { for ast_node in &ast_def {
if tokens_len < ast_node.ast_match.len() { if tokens_len < ast_node.ast_match.len() {
continue; continue;
} }
let mut ast_types: Vec<AstTypes> = vec![];
let mut is_match = false; let mut is_match = false;
let mut matched: Vec<Token> = Vec::new(); let mut matched: Vec<Token> = Vec::new();
for ast_match in &ast_node.ast_match { for ast_match in &ast_node.ast_match {
@@ -71,6 +66,42 @@ pub fn parse(tokens: &mut Vec<Token>) -> Box<Vec<Ast>> {
break; 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 => { AstDef::Else => {
matched.push(tokens[i].clone()); matched.push(tokens[i].clone());
tokens.drain(0..1); tokens.drain(0..1);