1
0
Fork 0
forked from AbleOS/ableos

Compare commits

..

No commits in common. "b5cdc9c4fd397958dd70ce943628f4e6c27f6f4a" and "0594b99a59aa1f86e7d24e0ac2bd043e93570db7" have entirely different histories.

5 changed files with 32 additions and 171 deletions

View file

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

View file

@ -1,129 +0,0 @@
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 { let (folder_hierarchy, entry_name) = match development_type {
DevelopmentType::Program => ("programs", "main.hb"), DevelopmentType::Program => ("programs", "main.hb"),
DevelopmentType::Library => ("libraries", "lib.hb"), DevelopmentType::Library => ("libraries", "lib.hb"),
DevelopmentType::IDL => ("idl", "protocol.aldi"), DevelopmentType::IDL => ("idl", "protocol.aidl"),
}; };
let project_folder_path_string = format!("sysdata/{folder_hierarchy}/{name}"); let project_folder_path_string = format!("sysdata/{folder_hierarchy}/{name}");

View file

@ -0,0 +1,24 @@
@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

@ -1,25 +0,0 @@
// 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;
//}