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

View file

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

View file

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