1
0
Fork 0
forked from AbleOS/ableos

Brought parser back completely from the dead

This commit is contained in:
Talha Qamar 2024-09-14 06:09:41 +05:00
parent d0d6f0475e
commit 0e5d5f7de7

View file

@ -88,9 +88,9 @@ fn declaration(tokens : &mut Peekable<Iter<'_, Token>>) -> Option<Declaration> {
None => None,
Some(tok) => match tok {
Token::Enum(_) => Some(enum_decl(tokens)),
// Token::Struct(_) => Some(struct_decl(tokens)),
// Token::Type(_) => Some(type_declaration(tokens)),
// Token::Protocol(_) => Some(protocol_declaration(tokens)),
Token::Struct(_) => Some(struct_decl(tokens)),
Token::Type(_) => Some(type_declaration(tokens)),
Token::Protocol(_) => Some(protocol_declaration(tokens)),
_ => None,
}
}
@ -141,58 +141,58 @@ fn enum_member(tokens : &mut Peekable<Iter<'_, Token>>) -> Option<EnumMember> {
None
}
}
//
// fn struct_decl(tokens : &mut Peekable<Iter<'_, Token>>) -> Declaration {
// consume(tokens, Token::Struct);
// let name = identifier(tokens).expect("Expected Identifier after `struct`");
// consume(tokens, Token::LBrace);
// let mut members = Vec::new();
//
// match tokens.peek().expect("Unexpected EOF after LBrace") {
// Token::RBrace => {}, // skip checking for struct_members if empty
// _ => {
// struct_members(tokens, &mut members);
// },
// }
//
// consume(tokens, Token::RBrace);
//
// Declaration::StructDeclaration(StructDeclaration{name, members})
// }
//
// fn struct_members(tokens : &mut Peekable<Iter<'_, Token>>, members: &mut Vec<StructMember>) {
// members.push(struct_member(tokens).unwrap());
// loop {
// match tokens.peek().expect("Unexpected EOF inside struct declaration") {
// Token::Comma => {
// consume(tokens, Token::Comma);
// if let Some(member) = struct_member(tokens) {
// members.push(member);
// } else {
// break;
// }
// },
// _ => {},
// }
// }
// }
// fn struct_member(tokens : &mut Peekable<Iter<'_, Token>>) -> Option<StructMember> {
// let name = identifier(tokens);
// if let Some(name) = name {
// consume(tokens, Token::Colon);
// let type_name = identifier(tokens).expect("Expected Type after Colon");
// Some(StructMember{name, type_name})
// } else {
// None
// }
// }
//
// fn type_declaration(tokens : &mut Peekable<Iter<'_, Token>>) -> Declaration {
// consume(tokens, Token::Type);
// let name = identifier(tokens).expect("Expected Identifier after `type`");
// let type_name = identifier(tokens).expect("Expected type after Identifier");
// Declaration::TypeDeclaration(TypeDeclaration{name, type_name})
// }
fn struct_decl(tokens : &mut Peekable<Iter<'_, Token>>) -> Declaration {
consume!(tokens, Token::Struct);
let name = identifier(tokens).expect("Expected Identifier after `struct`");
consume!(tokens, Token::LBrace);
let mut members = Vec::new();
match tokens.peek().expect("Unexpected EOF after LBrace") {
Token::RBrace(_) => {}, // skip checking for struct_members if empty
_ => {
struct_members(tokens, &mut members);
},
}
consume!(tokens, Token::RBrace);
Declaration::StructDeclaration(StructDeclaration{name, members})
}
fn struct_members(tokens : &mut Peekable<Iter<'_, Token>>, members: &mut Vec<StructMember>) {
members.push(struct_member(tokens).unwrap());
loop {
match tokens.peek().expect("Unexpected EOF inside struct declaration") {
Token::Comma(_) => {
consume!(tokens, Token::Comma);
if let Some(member) = struct_member(tokens) {
members.push(member);
} else {
break;
}
},
_ => {},
}
}
}
fn struct_member(tokens : &mut Peekable<Iter<'_, Token>>) -> Option<StructMember> {
let name = identifier(tokens);
if let Some(name) = name {
consume!(tokens, Token::Colon);
let type_name = identifier(tokens).expect("Expected Type after Colon");
Some(StructMember{name, type_name})
} else {
None
}
}
fn type_declaration(tokens : &mut Peekable<Iter<'_, Token>>) -> Declaration {
consume!(tokens, Token::Type);
let name = identifier(tokens).expect("Expected Identifier after `type`");
let type_name = identifier(tokens).expect("Expected type after Identifier");
Declaration::TypeDeclaration(TypeDeclaration{name, type_name})
}
fn identifier(tokens : &mut Peekable<Iter<'_, Token>>) -> Option<String> {
let result = tokens.peek().map_or(None, |x| match x {
Token::Identifier((_, _, s)) => {
@ -217,61 +217,61 @@ fn parse_number(tokens : &mut Peekable<Iter<'_, Token>>) -> Option<u64> {
}
result
}
//
// fn protocol_declaration(tokens : &mut Peekable<Iter<'_, Token>>) -> Declaration {
// consume(tokens, Token::Protocol);
// let name = identifier(tokens).expect("Expected Identifier after `protocol`");
// consume(tokens, Token::LBrace);
// let mut interface = Vec::new();
// match tokens.peek().expect("Unexpected EOF after LBrace") {
// Token::RBrace => {},
// _ => {
// functions(tokens, &mut interface);
// },
// };
// Declaration::ProtocolDeclaration(ProtocolDeclaration{name, interface})
// }
//
// fn functions(tokens : &mut Peekable<Iter<'_, Token>>, interface : &mut Vec<FuncDeclaration>) {
// loop {
// match function(tokens) {
// Some(x) => interface.push(x),
// None => break,
// }
// }
// }
//
// fn function(tokens : &mut Peekable<Iter<'_, Token>>) -> Option<FuncDeclaration>{
// if let Some(Token::Fn) = tokens.peek() {
// consume(tokens, Token::Fn);
// let name = identifier(tokens).expect("Expected Identifier after `fn`");
// consume(tokens, Token::LParen);
// let arg_list = arg_list(tokens);
// consume(tokens, Token::RParen);
// consume(tokens, Token::RArrow((1,1)));
// let return_type = identifier(tokens).expect("Expected return type after `->");
// Some(FuncDeclaration{name, arg_list, return_type})
// } else {
// None
// }
// }
//
// fn arg_list(tokens : &mut Peekable<Iter<'_, Token>>) -> Vec<String> {
// let mut result = Vec::new();
// loop {
// match identifier(tokens) {
// None => break,
// Some(i) =>{
// result.push(i);
// if **tokens.peek().expect("Unexpected EOF in argument list") == Token::Comma{
// consume(tokens, Token::Comma);
// match tokens.peek().expect("Unexpected EOF in argument list") {
// Token::Identifier(_) => {},
// _ => panic!("Unexpected symbol after Comma in argument list"),
// }
// }
// }
// }
// }
// result
// }
fn protocol_declaration(tokens : &mut Peekable<Iter<'_, Token>>) -> Declaration {
consume!(tokens, Token::Protocol);
let name = identifier(tokens).expect("Expected Identifier after `protocol`");
consume!(tokens, Token::LBrace);
let mut interface = Vec::new();
match tokens.peek().expect("Unexpected EOF after LBrace") {
Token::RBrace(_) => {},
_ => {
functions(tokens, &mut interface);
},
};
Declaration::ProtocolDeclaration(ProtocolDeclaration{name, interface})
}
fn functions(tokens : &mut Peekable<Iter<'_, Token>>, interface : &mut Vec<FuncDeclaration>) {
loop {
match function(tokens) {
Some(x) => interface.push(x),
None => break,
}
}
}
fn function(tokens : &mut Peekable<Iter<'_, Token>>) -> Option<FuncDeclaration>{
if let Some(Token::Fn(_)) = tokens.peek() {
consume!(tokens, Token::Fn);
let name = identifier(tokens).expect("Expected Identifier after `fn`");
consume!(tokens, Token::LParen);
let arg_list = arg_list(tokens);
consume!(tokens, Token::RParen);
consume!(tokens, Token::RArrow);
let return_type = identifier(tokens).expect("Expected return type after `->");
Some(FuncDeclaration{name, arg_list, return_type})
} else {
None
}
}
fn arg_list(tokens : &mut Peekable<Iter<'_, Token>>) -> Vec<String> {
let mut result = Vec::new();
loop {
match identifier(tokens) {
None => break,
Some(i) =>{
result.push(i);
if let Token::Comma(_) = **tokens.peek().expect("Unexpected EOF in argument list"){
consume!(tokens, Token::Comma);
match tokens.peek().expect("Unexpected EOF in argument list") {
Token::Identifier(_) => {},
_ => panic!("Unexpected symbol after Comma in argument list"),
}
}
}
}
}
result
}