ableos_userland/programs/aidl/src/ast.rs

182 lines
3.2 KiB
Rust
Raw Normal View History

2023-05-04 11:19:32 +00:00
//! **note** the order of fields is the order of parsing.
2023-05-04 17:31:20 +00:00
use std::collections::HashMap;
2023-05-04 11:19:32 +00:00
/// An IDL module.
///
/// Parsing order:
/// - use declarations,
/// - items
#[derive(Debug)]
pub struct IDLModule {
2023-05-06 15:57:45 +00:00
pub name: String,
2023-05-04 11:19:32 +00:00
// why: only allow use before other items
// parser will error if use is present in any other place
pub uses: Vec<UseDecl>,
2023-05-04 12:44:49 +00:00
pub items: Vec<Item>,
2023-05-04 11:19:32 +00:00
}
#[derive(Debug)]
pub enum Item {
2023-05-05 11:21:24 +00:00
Interface(ItemInterface),
2023-05-04 13:47:05 +00:00
Alias(ItemAlias),
2023-05-04 12:44:49 +00:00
Constant(ItemConstant),
2023-05-05 11:21:24 +00:00
Function(Function),
Structure(ItemStructure),
2023-05-05 14:15:38 +00:00
Enumeration(ItemEnumeration),
2023-05-04 11:19:32 +00:00
}
2023-05-05 11:21:24 +00:00
#[derive(Debug, Default)]
2023-05-04 11:19:32 +00:00
pub struct Function {
pub name: String,
2023-05-04 17:31:20 +00:00
pub takes: Vec<Type>,
2023-05-05 11:21:24 +00:00
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),
2023-05-05 14:15:38 +00:00
arguments: TypeArguments::None,
2023-05-05 11:21:24 +00:00
}
}
2023-05-04 11:19:32 +00:00
}
2023-05-05 11:21:24 +00:00
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()
}
2023-05-04 17:31:20 +00:00
2023-05-04 11:19:32 +00:00
#[derive(Debug)]
pub struct ItemInterface {
pub name: String,
2023-05-04 12:44:49 +00:00
pub functions: Vec<Function>,
}
2023-05-05 11:21:24 +00:00
#[derive(Debug)]
pub struct ItemStructure {
pub name: String,
pub fields: HashMap<String, Type>,
2023-05-05 14:15:38 +00:00
pub arguments: TypeArguments,
2023-05-05 11:21:24 +00:00
}
2023-05-04 12:44:49 +00:00
#[derive(Debug)]
pub struct ItemAlias {
pub name: String,
2023-05-05 11:21:24 +00:00
pub referree: Type,
2023-05-04 11:19:32 +00:00
}
#[derive(Debug)]
2023-05-04 12:44:49 +00:00
pub struct ItemConstant {
2023-05-04 11:19:32 +00:00
pub name: String,
2023-05-05 11:21:24 +00:00
pub expr: Expr,
}
#[derive(Debug)]
pub struct ItemEnumeration {
pub name: String,
pub arguments: TypeArguments,
2023-05-05 14:15:38 +00:00
pub variants: Vec<EnumerationVariant>,
2023-05-05 11:21:24 +00:00
}
#[derive(Debug)]
pub struct EnumerationVariant {
pub name: String,
2023-05-05 14:15:38 +00:00
pub content: EnumerationContent,
2023-05-05 11:21:24 +00:00
}
#[derive(Debug, Default)]
pub enum EnumerationContent {
#[default]
None,
Tuple(Vec<Type>),
Structure(HashMap<String, Type>),
2023-05-05 14:15:38 +00:00
Value(NumberLiteral),
2023-05-04 11:19:32 +00:00
}
#[derive(Debug)]
pub struct UseDecl {
2023-05-06 15:57:45 +00:00
pub path: (String, Option<String>)
2023-05-04 12:44:49 +00:00
}
#[derive(Debug)]
pub enum Expr {
Literal(Literal),
2023-05-04 17:31:20 +00:00
_IdentAccess(String),
2023-05-05 11:21:24 +00:00
Make(Box<ExprMake>),
2023-05-04 17:31:20 +00:00
}
#[derive(Debug)]
2023-05-05 09:15:01 +00:00
pub struct ExprMake {
2023-05-04 17:31:20 +00:00
pub name: String,
2023-05-05 11:21:24 +00:00
pub params: HashMap<String, Expr>,
2023-05-04 12:44:49 +00:00
}
#[derive(Debug)]
pub enum Literal {
String(String),
Number(NumberLiteral),
2023-05-05 11:21:24 +00:00
Char(char),
2023-05-04 12:44:49 +00:00
}
2023-05-05 14:15:38 +00:00
#[derive(Debug, derive_more::Display)]
2023-05-04 12:44:49 +00:00
pub enum NumberLiteral {
2023-05-05 14:15:38 +00:00
#[display(fmt = "{_0}ptr")]
2023-05-04 12:44:49 +00:00
Ptr(usize),
2023-05-05 14:15:38 +00:00
#[display(fmt = "{_0}u8")]
2023-05-04 12:44:49 +00:00
U8(u8),
2023-05-05 14:15:38 +00:00
#[display(fmt = "{_0}i8")]
2023-05-04 12:44:49 +00:00
I8(i8),
2023-05-05 14:15:38 +00:00
#[display(fmt = "{_0}u16")]
2023-05-04 12:44:49 +00:00
U16(u16),
2023-05-05 14:15:38 +00:00
#[display(fmt = "{_0}i16")]
2023-05-04 12:44:49 +00:00
I16(i16),
2023-05-05 14:15:38 +00:00
#[display(fmt = "{_0}u32")]
2023-05-04 12:44:49 +00:00
U32(u32),
2023-05-05 14:15:38 +00:00
#[display(fmt = "{_0}i32")]
2023-05-04 12:44:49 +00:00
I32(i32),
2023-05-05 14:15:38 +00:00
#[display(fmt = "{_0}u64")]
2023-05-04 12:44:49 +00:00
U64(u64),
2023-05-05 14:15:38 +00:00
#[display(fmt = "{_0}i64")]
2023-05-04 12:44:49 +00:00
I64(i64),
2023-05-04 13:47:05 +00:00
2023-05-05 14:15:38 +00:00
#[display(fmt = "{_0}")]
2023-05-04 13:51:31 +00:00
Infer(i64),
2023-05-04 13:47:05 +00:00
}
/// seg1.seg2.seg3.segN
#[derive(Debug)]
pub struct ModulePath {
2023-05-04 13:51:31 +00:00
pub segments: Vec<String>,
2023-05-04 11:19:32 +00:00
}