added 2 ast nodes

This commit is contained in:
2024-04-06 17:57:00 +02:00
parent 59aaba35f9
commit cafc52da16
3 changed files with 31 additions and 10 deletions
+14 -4
View File
@@ -19,6 +19,8 @@ pub enum TokenType {
Operator, Operator,
Round, Round,
Curly, Curly,
Square,
Angle,
// EOF, // EOF,
} }
@@ -41,7 +43,7 @@ pub struct Node {
token_regex: Lazy<Regex> token_regex: Lazy<Regex>
} }
const SYNTAX: [Node; 9] = [ const SYNTAX: [Node; 11] = [
Node { Node {
token_type: TokenType::Newline, token_type: TokenType::Newline,
token_regex: Lazy::new(|| Regex::new(r"^\n+").unwrap()), token_regex: Lazy::new(|| Regex::new(r"^\n+").unwrap()),
@@ -56,7 +58,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"^mut|try|catch|return|fn\b").unwrap())
}, },
Node { Node {
token_type: TokenType::Identifier, token_type: TokenType::Identifier,
@@ -76,8 +78,16 @@ const SYNTAX: [Node; 9] = [
}, },
Node { Node {
token_type: TokenType::Curly, token_type: TokenType::Curly,
token_regex: Lazy::new(|| Regex::new(r"^[\{|\}]").unwrap()) token_regex: Lazy::new(|| Regex::new(r"\{(?:[^{}]|(?R))*}").unwrap())
} },
Node {
token_type: TokenType::Square,
token_regex: Lazy::new(|| Regex::new(r"\[(?:[^\[\]]|(?R))*]").unwrap())
},
Node {
token_type: TokenType::Angle,
token_regex: Lazy::new(|| Regex::new(r"<(?:[^<>]|(?R))*>").unwrap())
},
]; ];
pub fn lex(mut code: &str, use_whitespace: bool) -> Vec<Token> { pub fn lex(mut code: &str, use_whitespace: bool) -> Vec<Token> {
+6 -2
View File
@@ -4,7 +4,7 @@ mod parser;
use parser::parse; use parser::parse;
fn main() { fn main() {
let input = "if a + b {}"; let input = "void main() {} <>";
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);
@@ -14,7 +14,11 @@ fn main() {
// } // }
let mut _x = 1; let mut _x = 1;
for a in ast { for a in ast {
println!("{:?}: {:?}\n", a.type_, a.values); println!("{:?}: [", a.type_);
for v in a.values {
println!(" {:?},", v);
}
println!("]");
_x+=1; _x+=1;
} }
} }
+11 -4
View File
@@ -1,10 +1,13 @@
// use std::collections::HashMap; // use std::collections::HashMap;
use crate::lexer::{Token, TokenType}; use crate::lexer::{Token, TokenType};
use std::fmt; use std::fmt;
use crate::parser::AstDef::{Else, Normal};
use crate::parser::AstTypes::{FunctionDecleration, Other};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum AstTypes { pub enum AstTypes {
FunctionDecleration, FunctionDecleration,
VariableDecleration,
Other Other
} }
@@ -36,13 +39,17 @@ pub struct PNode {
pub fn parse(tokens: &mut Vec<Token>) -> Box<Vec<Ast>> { 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: FunctionDecleration,
ast_match: vec![AstDef::NormalValue(TokenType::Identifier, vec!["if".to_string()]), AstDef::Repeated(vec![TokenType::Operator, TokenType::Identifier, ])] ast_match: vec![Normal(TokenType::Identifier), Normal(TokenType::Identifier), Normal(TokenType::Round), Normal(TokenType::Curly)]
}, },
PNode { PNode {
ast_type: AstTypes::Other, ast_type: AstTypes::VariableDecleration,
ast_match: vec![AstDef::Else] ast_match: vec![Normal(TokenType::Identifier), Normal(TokenType::Identifier)]
}, },
PNode {
ast_type: Other,
ast_match: vec![Else]
}
]; ];
let tokens_len = tokens.len(); let tokens_len = tokens.len();
let mut ast: Vec<Ast> = Vec::new(); let mut ast: Vec<Ast> = Vec::new();