1
0
Fork 0
forked from AbleOS/ableos

Compare commits

..

2 commits

Author SHA1 Message Date
Talha Qamar b5cdc9c4fd changed file extension to .aldi and made it so that decorators don't work 2024-09-11 01:29:18 +05:00
Talha Qamar 94b7aabdec Added struct declarations to parsers 2024-09-11 01:17:36 +05:00
5 changed files with 171 additions and 32 deletions

View file

@ -1,19 +1,30 @@
pub mod protocol;
mod parser;
use std::io::Read;
use self::parser::parse;
use {
logos::{Lexer, Logos},
protocol::Protocol,
};
#[derive(Logos, Debug, PartialEq, Clone)]
#[logos(skip r"[ \t\n\f]+")] // Ignore this regex pattern between tokens
enum Token {
#[regex("//[^\n]*\n", logos::skip)]
Ignored,
// Tokens can be literal strings, of any length.
#[token("protocol")]
Protocol,
#[token("enum")]
Enum,
#[token("struct")]
Struct,
#[token("{")]
LBrace,
@ -41,7 +52,7 @@ enum Token {
RArrow,
#[regex("[a-zA-Z_]+", |lex|{lex.slice().to_string()})]
Text(String),
Identifier(String),
#[regex("[1234567890]+", |lex|{lex.slice().parse::<u64>().unwrap()})]
Number(u64),
@ -66,17 +77,15 @@ pub fn build_idl(name: String) {
Err(err) => println!("{:?}", err),
}
}
build(tokens);
println!("{:?}", parse(tokens));
}
fn build(a: Vec<Token>) {
for toke in a {
println!("{:?}", toke);
}
}
fn open_protocol(name: String) -> String {
let path = format!("sysdata/idl/{}/src/protocol.aidl", name);
let path = format!("sysdata/idl/{}/src/protocol.aldi", name);
let mut file = std::fs::File::open(path).unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();

129
dev/src/idl/parser.rs Normal file
View file

@ -0,0 +1,129 @@
use std::{iter::Peekable, slice::Iter};
use super::Token;
#[derive(Debug, Clone, PartialEq)]
pub struct AST(Vec<DecoratedDeclaration>);
#[derive(Debug, Clone, PartialEq)]
struct DecoratedDeclaration(Option<Decorator>, Declaration);
#[derive(Debug, Clone, PartialEq)]
struct Decorator(String);
#[derive(Debug, Clone, PartialEq)]
enum Declaration{
EnumDeclaration,
StructDeclaration(StructDeclaration),
ProtocolDeclaration,
}
#[derive(Debug, Clone, PartialEq)]
struct StructDeclaration {
name : String,
members : Vec<StructMember>,
}
#[derive(Debug, Clone, PartialEq)]
struct StructMember {
name : String,
type_name : String,
}
fn parse_decorated_declarations(tokens : &mut Peekable<Iter<'_, Token>>) -> Vec<DecoratedDeclaration>{
let mut declarations : Vec<DecoratedDeclaration> = Vec::new();
loop {
match tokens.peek() {
Some(_) => {
let decoration = parse_decoration(tokens);
assert!(decoration == None, "NO DECORATIONS!!!");
let declaration = parse_declaration(tokens);
declarations.push(DecoratedDeclaration(decoration, declaration));
},
None => break,
}
}
return declarations
}
fn parse_decoration(tokens : &mut Peekable<Iter<'_, Token>>) -> Option<Decorator> {
match tokens.peek().unwrap() {
Token::Decorator(s) => {
tokens.next();
Some(Decorator(s.to_string()))
},
Token::DecoratorOption(s) => {
tokens.next();
Some(Decorator(s.to_string()))
},
_ => None,
}
}
fn parse_declaration(tokens : &mut Peekable<Iter<'_, Token>>) -> Declaration {
match tokens.next().expect("Expected a statement, got end of tokens") {
Token::Enum => {
Declaration::EnumDeclaration
},
Token::Struct => {
Declaration::StructDeclaration(parse_struct_declaration(tokens))
},
Token::Protocol => {
Declaration::ProtocolDeclaration
},
_ => panic!("Expected a enumeration, struct or protocol")
}
}
fn parse_struct_declaration(tokens : &mut Peekable<Iter<'_, Token>>) -> StructDeclaration{
if let Token::Identifier(identifier) = tokens.next().unwrap() {
let mut members : Vec<StructMember> = Vec::new();
assert!(tokens.next().expect("expected a {{, got end of token list") == &Token::LBrace, "expected a {{ after identifier in struct declaration");
loop {
match tokens.peek().expect("expected }} or struct members") {
&Token::RBrace => break,
_ => {
// if we're in this branch then there MUST a struct member here
members.push(parse_struct_member(tokens));
// is there a comma here?
// nightmare
if **(tokens.peek().unwrap()) == Token::Comma {
// consume comma
tokens.next();
}
// if there isn't a comma nor a right brace after the struct member
else if **(tokens.peek().unwrap()) != Token::RBrace {
panic!("expected a comma to separate struct members");
}
}
}
}
tokens.next(); // consume closing bracket
return StructDeclaration{
name : identifier.to_string(),
members,
}
}
else {
panic!("Expected an identifier after 'struct'")
}
}
fn parse_struct_member(tokens : &mut Peekable<Iter<'_, Token>>) -> StructMember {
if let Token::Identifier(name) = tokens.next().unwrap(){
assert!(tokens.next().unwrap() == &Token::Colon, "expected colon after struct member name");
if let Token::Identifier(type_name) = tokens.next().unwrap(){
StructMember{name: name.to_string(), type_name: type_name.to_string()}
}
else{
panic!("Expected type name in Struct Member");
}
}
else {
panic!("Expected identifier in Struct Member");
}
}
pub fn parse(tokens : Vec<Token>) -> AST{
let mut tokens_iter = tokens.iter().peekable();
AST(parse_decorated_declarations(&mut tokens_iter))
}

View file

@ -57,7 +57,7 @@ pub fn new(development_type: DevelopmentType, name: String) {
let (folder_hierarchy, entry_name) = match development_type {
DevelopmentType::Program => ("programs", "main.hb"),
DevelopmentType::Library => ("libraries", "lib.hb"),
DevelopmentType::IDL => ("idl", "protocol.aidl"),
DevelopmentType::IDL => ("idl", "protocol.aldi"),
};
let project_folder_path_string = format!("sysdata/{folder_hierarchy}/{name}");

View file

@ -1,24 +0,0 @@
@auto_increment
enum LogLevel {
Error = 0,
Warn,
Info,
Debug,
Trace,
}
@auto_increment
enum LogResult {
Err = 0,
Ok,
}
struct Log {
log_level: LogLevel,
}
@visibility(public)
protocol Log {
fn log(Log) -> LogResult;
fn flush() -> LogResult;
}

View file

@ -0,0 +1,25 @@
// A comment
//@auto_increment
//enum LogLevel {
// Error = 0,
// Warn,
// Info,
// Debug,
// Trace,
//}
//
//@auto_increment
//enum LogResult {
// Err = 0,
// Ok,
//}
struct Log {
log_level: LogLevel,
}
//@visibility(public)
//protocol Log {
// fn log(Log) -> LogResult;
// fn flush() -> LogResult;
//}