ableos/dev/src/idl/mod.rs

85 lines
1.7 KiB
Rust
Raw Normal View History

2024-08-30 12:31:45 -05:00
pub mod protocol;
use std::io::Read;
2024-09-03 03:34:29 -05:00
use {
logos::{Lexer, Logos},
protocol::Protocol,
};
2024-08-30 07:38:04 -05:00
2024-09-03 03:34:29 -05:00
#[derive(Logos, Debug, PartialEq, Clone)]
2024-08-30 07:38:04 -05:00
#[logos(skip r"[ \t\n\f]+")] // Ignore this regex pattern between tokens
enum Token {
// Tokens can be literal strings, of any length.
#[token("protocol")]
Protocol,
2024-08-30 12:31:45 -05:00
#[token("{")]
LBrace,
#[token("}")]
RBrace,
2024-09-02 21:50:43 -05:00
#[token("(")]
LParen,
#[token(")")]
RParen,
#[token(":")]
Colon,
#[token(";")]
SemiColon,
#[token(",")]
Comma,
#[token("=")]
Equal,
#[token("->")]
RArrow,
#[regex("[a-zA-Z_]+", |lex|{lex.slice().to_string()})]
2024-08-30 12:31:45 -05:00
Text(String),
2024-09-03 03:34:29 -05:00
#[regex("[1234567890]+", |lex|{lex.slice().parse::<u64>().unwrap()})]
Number(u64),
2024-09-02 21:50:43 -05:00
#[regex(r"@[a-zA-Z_]+", |lex|{lex.slice().to_string()})]
2024-08-30 12:31:45 -05:00
Decorator(String),
2024-09-02 21:50:43 -05:00
2024-09-03 03:34:29 -05:00
#[regex(r#"@[a-zA-Z_]+\([a-zA-Z,0-9=]+\)"#, |lex|{lex.slice().to_string()})]
2024-09-02 21:50:43 -05:00
DecoratorOption(String),
2024-08-30 12:31:45 -05:00
}
2024-08-30 07:38:04 -05:00
2024-08-30 12:31:45 -05:00
pub fn build_idl(name: String) {
let contents = open_protocol(name);
let lex = Token::lexer(&contents);
2024-09-03 03:34:29 -05:00
let mut tokens = vec![];
2024-08-30 12:31:45 -05:00
for x in lex {
2024-09-02 21:50:43 -05:00
match x {
2024-09-03 03:34:29 -05:00
Ok(token) => {
println!("{:?}", token);
tokens.push(token);
}
2024-09-02 21:50:43 -05:00
Err(err) => println!("{:?}", err),
}
2024-08-30 12:31:45 -05:00
}
2024-09-03 03:34:29 -05:00
build(tokens);
}
fn build(a: Vec<Token>) {
for toke in a {
println!("{:?}", toke);
}
2024-08-30 07:38:04 -05:00
}
2024-08-30 12:31:45 -05:00
fn open_protocol(name: String) -> String {
let path = format!("sysdata/idl/{}/src/protocol.aidl", name);
let mut file = std::fs::File::open(path).unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
contents
2024-08-30 07:38:04 -05:00
}