ableos_userland/programs/aidl/src/ast.rs

182 lines
3.2 KiB
Rust

//! **note** the order of fields is the order of parsing.
use std::collections::HashMap;
/// An IDL module.
///
/// Parsing order:
/// - use declarations,
/// - items
#[derive(Debug)]
pub struct IDLModule {
pub name: String,
// why: only allow use before other items
// parser will error if use is present in any other place
pub uses: Vec<UseDecl>,
pub items: Vec<Item>,
}
#[derive(Debug)]
pub enum Item {
Interface(ItemInterface),
Alias(ItemAlias),
Constant(ItemConstant),
Function(Function),
Structure(ItemStructure),
Enumeration(ItemEnumeration),
}
#[derive(Debug, Default)]
pub struct Function {
pub name: String,
pub takes: Vec<Type>,
pub returns: Type,
}
#[derive(Debug)]
pub struct Type {
pub name: String,
pub arguments: TypeArguments,
}
impl Type {
pub fn infer() -> Self {
Self {
name: String::from(INFER_TYPE),
arguments: TypeArguments::None,
}
}
}
pub const NOTHING_TYPE: &str = "Nothing";
pub const INFER_TYPE: &str = "_";
impl Default for Type {
fn default() -> Self {
Self {
name: String::from(NOTHING_TYPE),
arguments: TypeArguments::None,
}
}
}
#[derive(Debug, Default)]
pub enum TypeArguments {
/// TypeName
#[default]
None,
/// TypeName<T1, T2, T3, TN>
AngleBracketed(Vec<Box<Type>>),
}
pub fn nothing() -> Type {
Type::default()
}
#[derive(Debug)]
pub struct ItemInterface {
pub name: String,
pub functions: Vec<Function>,
}
#[derive(Debug)]
pub struct ItemStructure {
pub name: String,
pub fields: HashMap<String, Type>,
pub arguments: TypeArguments,
}
#[derive(Debug)]
pub struct ItemAlias {
pub name: String,
pub referree: Type,
}
#[derive(Debug)]
pub struct ItemConstant {
pub name: String,
pub expr: Expr,
}
#[derive(Debug)]
pub struct ItemEnumeration {
pub name: String,
pub arguments: TypeArguments,
pub variants: Vec<EnumerationVariant>,
}
#[derive(Debug)]
pub struct EnumerationVariant {
pub name: String,
pub content: EnumerationContent,
}
#[derive(Debug, Default)]
pub enum EnumerationContent {
#[default]
None,
Tuple(Vec<Type>),
Structure(HashMap<String, Type>),
Value(NumberLiteral),
}
#[derive(Debug)]
pub struct UseDecl {
pub path: (String, Option<String>)
}
#[derive(Debug)]
pub enum Expr {
Literal(Literal),
_IdentAccess(String),
Make(Box<ExprMake>),
}
#[derive(Debug)]
pub struct ExprMake {
pub name: String,
pub params: HashMap<String, Expr>,
}
#[derive(Debug)]
pub enum Literal {
String(String),
Number(NumberLiteral),
Char(char),
}
#[derive(Debug, derive_more::Display)]
pub enum NumberLiteral {
#[display(fmt = "{_0}ptr")]
Ptr(usize),
#[display(fmt = "{_0}u8")]
U8(u8),
#[display(fmt = "{_0}i8")]
I8(i8),
#[display(fmt = "{_0}u16")]
U16(u16),
#[display(fmt = "{_0}i16")]
I16(i16),
#[display(fmt = "{_0}u32")]
U32(u32),
#[display(fmt = "{_0}i32")]
I32(i32),
#[display(fmt = "{_0}u64")]
U64(u64),
#[display(fmt = "{_0}i64")]
I64(i64),
#[display(fmt = "{_0}")]
Infer(i64),
}
/// seg1.seg2.seg3.segN
#[derive(Debug)]
pub struct ModulePath {
pub segments: Vec<String>,
}