added support for all the AstDef types
This commit is contained in:
+1
-1
@@ -49,7 +49,7 @@ const SYNTAX: [Node; 9] = [
|
|||||||
},
|
},
|
||||||
Node {
|
Node {
|
||||||
token_type: TokenType::Keyword,
|
token_type: TokenType::Keyword,
|
||||||
token_regex: Lazy::new(|| Regex::new(r"^mut|try|catch|return|fn").unwrap())
|
token_regex: Lazy::new(|| Regex::new(r"^if|mut|try|catch|return|fn").unwrap())
|
||||||
},
|
},
|
||||||
Node {
|
Node {
|
||||||
token_type: TokenType::Identifier,
|
token_type: TokenType::Identifier,
|
||||||
|
|||||||
+1
-1
@@ -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
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user