diff --git a/dev/src/idl/parser.rs b/dev/src/idl/parser.rs index a3a96c60..d73acf1e 100644 --- a/dev/src/idl/parser.rs +++ b/dev/src/idl/parser.rs @@ -88,9 +88,9 @@ fn declaration(tokens : &mut Peekable>) -> Option { 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>) -> Option { None } } -// -// fn struct_decl(tokens : &mut Peekable>) -> 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>, members: &mut Vec) { -// 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>) -> Option { -// 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>) -> 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>) -> 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>, members: &mut Vec) { + 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>) -> Option { + 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>) -> 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>) -> Option { let result = tokens.peek().map_or(None, |x| match x { Token::Identifier((_, _, s)) => { @@ -217,61 +217,61 @@ fn parse_number(tokens : &mut Peekable>) -> Option { } result } -// -// fn protocol_declaration(tokens : &mut Peekable>) -> 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>, interface : &mut Vec) { -// loop { -// match function(tokens) { -// Some(x) => interface.push(x), -// None => break, -// } -// } -// } -// -// fn function(tokens : &mut Peekable>) -> Option{ -// 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>) -> Vec { -// 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>) -> 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>, interface : &mut Vec) { + loop { + match function(tokens) { + Some(x) => interface.push(x), + None => break, + } + } +} + +fn function(tokens : &mut Peekable>) -> Option{ + 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>) -> Vec { + 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 +}