Merge branch 'master' into wip/lexer-info
This commit is contained in:
+1
-2
@@ -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,
|
||||
|
||||
+1
-1
@@ -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);
|
||||
|
||||
+40
-9
@@ -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<String>),
|
||||
Repeated(Vec<TokenType>),
|
||||
Optional(TokenType),
|
||||
Else
|
||||
}
|
||||
@@ -36,11 +37,7 @@ pub fn parse(tokens: &mut Vec<Token>) -> Box<Vec<Ast>> {
|
||||
let ast_def: Vec<PNode> = 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<Token>) -> Box<Vec<Ast>> {
|
||||
];
|
||||
let tokens_len = tokens.len();
|
||||
let mut ast: Vec<Ast> = 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<AstTypes> = vec![];
|
||||
let mut is_match = false;
|
||||
let mut matched: Vec<Token> = Vec::new();
|
||||
for ast_match in &ast_node.ast_match {
|
||||
@@ -71,6 +66,42 @@ pub fn parse(tokens: &mut Vec<Token>) -> Box<Vec<Ast>> {
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user