1
0
Fork 0
forked from AbleOS/ableos

Fixed lexer not taking identifiers with number in them and finished parsing support for protocols

This commit is contained in:
Talha Qamar 2024-09-11 11:05:05 +05:00
parent 45950fdb34
commit 39f7d5aba4
2 changed files with 62 additions and 5 deletions

View file

@ -22,6 +22,8 @@ enum Token {
// Tokens can be literal strings, of any length. // Tokens can be literal strings, of any length.
#[token("type")] #[token("type")]
Type, Type,
#[token("fn", priority = 5)]
Fn,
#[token("enum")] #[token("enum")]
Enum, Enum,
@ -56,7 +58,7 @@ enum Token {
#[token("->")] #[token("->")]
RArrow, RArrow,
#[regex("[a-zA-Z_]+", |lex|{lex.slice().to_string()})] #[regex("[a-zA-Z_][a-zA-Z_1234567890]+", |lex|{lex.slice().to_string()})]
Identifier(String), Identifier(String),
#[regex("[1234567890]+", |lex|{lex.slice().parse::<u64>().unwrap()})] #[regex("[1234567890]+", |lex|{lex.slice().parse::<u64>().unwrap()})]
@ -86,9 +88,6 @@ pub fn build_idl(name: String) {
println!("{:#?}", parse(tokens)); println!("{:#?}", parse(tokens));
} }
fn build(a: Vec<Token>) {
}
fn open_protocol(name: String) -> String { fn open_protocol(name: String) -> String {
let path = format!("sysdata/idl/{}/src/protocol.aldi", name); let path = format!("sysdata/idl/{}/src/protocol.aldi", name);
let mut file = std::fs::File::open(path).unwrap(); let mut file = std::fs::File::open(path).unwrap();

View file

@ -47,7 +47,7 @@ struct ProtocolDeclaration{
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
struct FuncDeclaration { struct FuncDeclaration {
name : String, name : String,
input_types : Vec<String>, arg_list : Vec<String>,
return_type : String, return_type : String,
} }
@ -87,6 +87,7 @@ fn declaration(tokens : &mut Peekable<Iter<'_, Token>>) -> Option<Declaration> {
Token::Enum => Some(enum_decl(tokens)), Token::Enum => Some(enum_decl(tokens)),
Token::Struct => Some(struct_decl(tokens)), Token::Struct => Some(struct_decl(tokens)),
Token::Type => Some(type_declaration(tokens)), Token::Type => Some(type_declaration(tokens)),
Token::Protocol => Some(protocol_declaration(tokens)),
_ => None, _ => None,
} }
} }
@ -214,3 +215,60 @@ fn parse_number(tokens : &mut Peekable<Iter<'_, Token>>) -> Option<u64> {
result 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 **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
}