1
0
Fork 0
forked from AbleOS/ableos

preparing materials for adding protocol to parser and added type aliases to parser

This commit is contained in:
Talha Qamar 2024-09-11 05:18:11 +05:00
parent 6be6635e4e
commit f9451e3d7d
4 changed files with 36 additions and 13 deletions

View file

@ -3,7 +3,7 @@
declarations ::= <declaration> <declarations> declarations ::= <declaration> <declarations>
declaration ::= <enum_decl> | <struct_decl> | <type_decl> | <protocol_decl> declaration ::= <enum_decl> | <struct_decl> | <type_decl> | <protocol_decl>
<type_decl> ::= "type" <ident> <ident> ";" type_decl ::= "type" <ident> <ident> ";"
enum_decl ::= "enum" <ident> "{" "}" enum_decl ::= "enum" <ident> "{" "}"
| "enum" <ident> "{" <enum_members> "}" | "enum" <ident> "{" <enum_members> "}"
@ -22,4 +22,10 @@ struct_members ::= <struct_member>
| <struct_member> "," <struct_members> | <struct_member> "," <struct_members>
| <struct_member> "," <struct_members> | <struct_member> "," <struct_members>
// TODO protocol_decl protocol_decl ::= "protocol" <ident> "{" "}"
| "protocol" <ident> "{" <protocol_member>+ "}"
protocol_member ::= "fn" <ident>"("[<arg_list>]")" "->" <ident> ";"
arg_list ::= <ident> ["," <ident>]+

View file

@ -19,6 +19,10 @@ enum Token {
#[token("protocol")] #[token("protocol")]
Protocol, Protocol,
// Tokens can be literal strings, of any length.
#[token("type")]
Type,
#[token("enum")] #[token("enum")]
Enum, Enum,
@ -39,6 +43,7 @@ enum Token {
#[token(":")] #[token(":")]
Colon, Colon,
#[token(";")] #[token(";")]
SemiColon, SemiColon,
@ -57,11 +62,11 @@ enum Token {
#[regex("[1234567890]+", |lex|{lex.slice().parse::<u64>().unwrap()})] #[regex("[1234567890]+", |lex|{lex.slice().parse::<u64>().unwrap()})]
Number(u64), Number(u64),
#[regex(r"@[a-zA-Z_]+", |lex|{lex.slice().to_string()})] #[regex(r"@[a-zA-Z_]+", /*|lex|{lex.slice().to_string()}*/ logos::skip)]
Decorator(String), Decorator,
#[regex(r#"@[a-zA-Z_]+\([a-zA-Z,0-9=]+\)"#, |lex|{lex.slice().to_string()})] #[regex(r#"@[a-zA-Z_]+\([a-zA-Z,0-9=]+\)"#, /*|lex|{lex.slice().to_string()}*/ logos::skip)]
DecoratorOption(String), DecoratorOption,
} }
pub fn build_idl(name: String) { pub fn build_idl(name: String) {

View file

@ -4,13 +4,11 @@ use super::Token;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct AST(Vec<Declaration>); pub struct AST(Vec<Declaration>);
#[derive(Debug, Clone, PartialEq)]
struct Decorator(String);
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
enum Declaration{ enum Declaration{
EnumDeclaration(EnumDeclaration), EnumDeclaration(EnumDeclaration),
StructDeclaration(StructDeclaration), StructDeclaration(StructDeclaration),
TypeDeclaration(TypeDeclaration),
ProtocolDeclaration, ProtocolDeclaration,
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
@ -19,6 +17,11 @@ struct StructDeclaration {
members : Vec<StructMember>, members : Vec<StructMember>,
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
struct TypeDeclaration {
name : String,
type_name : String,
}
#[derive(Debug, Clone, PartialEq)]
struct StructMember { struct StructMember {
name : String, name : String,
type_name : String, type_name : String,
@ -70,6 +73,7 @@ fn declaration(tokens : &mut Peekable<Iter<'_, Token>>) -> Option<Declaration> {
Some(tok) => match tok { Some(tok) => match tok {
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)),
_ => None, _ => None,
} }
} }
@ -165,6 +169,13 @@ fn struct_member(tokens : &mut Peekable<Iter<'_, Token>>) -> Option<StructMember
None 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> { fn identifier(tokens : &mut Peekable<Iter<'_, Token>>) -> Option<String> {
let result = tokens.peek().map_or(None, |x| match x { let result = tokens.peek().map_or(None, |x| match x {
Token::Identifier(s) => { Token::Identifier(s) => {
@ -177,6 +188,7 @@ fn identifier(tokens : &mut Peekable<Iter<'_, Token>>) -> Option<String> {
tokens.next(); tokens.next();
} }
result result
} }
fn parse_number(tokens : &mut Peekable<Iter<'_, Token>>) -> Option<u64> { fn parse_number(tokens : &mut Peekable<Iter<'_, Token>>) -> Option<u64> {
let result = tokens.peek().map_or(None, |x| match x { let result = tokens.peek().map_or(None, |x| match x {

View file

@ -19,7 +19,7 @@ struct Log {
} }
//@visibility(public) //@visibility(public)
//protocol Log { protocol Log {
// fn log(Log) -> LogResult; fn log(Log) -> LogResult;
// fn flush() -> LogResult; fn flush() -> LogResult;
//} }