Refactored cargo structure

This commit is contained in:
2026-05-22 04:25:33 +02:00
parent b7c68da6de
commit a8ec265c1c
31 changed files with 14 additions and 3 deletions
+93
View File
@@ -0,0 +1,93 @@
use serde::Serialize;
use super::*;
#[derive(Debug, Clone, Serialize)]
pub enum Expression {
Literal(Literal),
Path(ExprPath),
Statement(Box<Statement>),
Fix {
initial: Box<Expression>,
prefixes: Vec<Prefix>,
postfixes: Vec<Postfix>,
},
Binary {
lhs: Box<Expression>,
op: String,
rhs: Box<Expression>,
},
}
#[derive(Debug, Clone, Serialize)]
pub enum Literal {
String(String),
Int(i64),
Float(f64),
Bool(bool),
Tuple(Vec<Expression>),
Array(Vec<Expression>),
ArrayRepeat(Box<Expression>, Box<Expression>),
}
#[derive(Debug, Clone, Serialize)]
pub enum Pattern {
NamedTuple(Path, Vec<Identifier>),
Struct(Path, Vec<Identifier>),
Tuple(Vec<Identifier>),
Literal(Literal),
Path(Path),
Id(Identifier),
}
#[derive(Debug, Clone, Serialize)]
pub enum Postfix {
FieldAccess(Identifier, Option<Generics>),
Call(Vec<Expression>),
MacroCall(String),
StructCall(Vec<(Identifier, Expression)>),
Assign(String, Box<Expression>),
Index(Expression),
As(TypeExpr),
Increment,
Decrement,
Try,
}
#[derive(Debug, Clone, Serialize)]
pub enum Prefix {
Ref,
RefMut,
Deref,
New(Option<Generics>),
Not,
Neg,
}
#[derive(Debug, Clone, Serialize)]
pub struct ExprPathSegment {
pub ident: Identifier,
pub generics: Option<Generics>,
}
#[derive(Debug, Clone, Serialize)]
pub struct ExprPath(pub Vec<ExprPathSegment>);
#[derive(Debug, Clone, Serialize)]
pub struct Generics(pub Vec<Generic>);
#[derive(Debug, Clone, Serialize)]
pub enum Generic {
Lifetime(Identifier),
Type(TypeExpr),
}
impl Expression {
pub fn is_block(&self) -> bool {
if let Expression::Statement(stmt) = self {
stmt.is_block()
} else {
false
}
}
}
+68
View File
@@ -0,0 +1,68 @@
use serde::Serialize;
pub mod expr;
pub mod statement;
pub mod top_level;
pub use expr::*;
pub use statement::*;
pub use top_level::*;
#[derive(Debug, Clone, Serialize)]
pub struct Path(pub Vec<Identifier>);
#[derive(Debug, Clone, Serialize)]
pub struct Identifier(pub String);
#[derive(Debug, Clone, Serialize, Default)]
pub struct ParamList(pub Vec<VarDecl>);
#[derive(Debug, Clone, Serialize)]
pub struct TypeExpr(pub TypeExprKind, pub Vec<TypePostfix>);
#[derive(Debug, Clone, Serialize)]
pub enum TypePostfix {
Ref,
RefMut,
RefLifetime(Identifier),
RefMutLifetime(Identifier),
Dyn,
}
#[derive(Debug, Clone, Serialize)]
pub enum TypeExprKind {
Path(Path),
PathParams(Path, Vec<TypeExpr>),
Tuple(Vec<TypeExpr>),
Lifetime(Identifier),
}
#[derive(Debug, Clone, Serialize)]
pub struct Spanned<T> {
pub line: usize,
pub column: usize,
pub item: T,
}
impl TypeExpr {
pub fn no_px(kind: TypeExprKind) -> Self {
Self(kind, Vec::new())
}
}
impl From<GenericDecl> for Generic {
fn from(value: GenericDecl) -> Self {
match value {
GenericDecl::Lifetime(life) => Generic::Lifetime(life),
GenericDecl::Type(ty, _) => {
Generic::Type(TypeExpr(TypeExprKind::Path(Path(vec![ty])), Vec::new()))
}
}
}
}
impl<T> Spanned<T> {
pub fn get_comment(&self) -> String {
format!("/* {}:{} */", self.line, self.column)
}
}
+82
View File
@@ -0,0 +1,82 @@
use serde::Serialize;
use super::*;
#[derive(Debug, Clone, Serialize, Default)]
pub struct Block(
pub Vec<Spanned<Expression>>,
pub Option<Spanned<Expression>>,
);
#[derive(Debug, Clone, Serialize)]
pub enum Statement {
Block(Block),
If {
initial: StatementBranch,
else_if: Vec<StatementBranch>,
else_branch: Option<Box<Expression>>,
},
Loop(Expression),
While(StatementBranch),
CStyleFor {
init: Expression,
condition: Expression,
update: Expression,
body: Expression,
},
For {
mutable: bool,
pattern: Pattern,
iterator: Expression,
body: Box<Expression>,
},
Match(Expression, Vec<(Vec<Pattern>, Expression)>),
VarDecl(VarDeclStmt),
Return(Option<Expression>),
Break,
Continue,
}
#[derive(Debug, Clone, Serialize)]
pub struct VarDecl {
pub mutable: bool,
pub name: Pattern,
pub type_: Option<TypeExpr>,
}
#[derive(Debug, Clone, Serialize)]
pub struct VarDeclStmt {
pub decl: VarDecl,
pub init: Option<Expression>,
}
#[derive(Debug, Clone, Serialize)]
pub struct StatementBranch {
pub condition: Expression,
pub body: Box<Expression>,
}
impl Statement {
pub fn is_block(&self) -> bool {
match self {
Self::Block(_) => true,
Self::If {
initial,
else_if,
else_branch,
} => {
else_branch
.as_ref()
.map(|v| v.is_block())
.unwrap_or_default()
|| else_if
.last()
.map(|b| b.body.is_block())
.unwrap_or_default()
|| initial.body.is_block()
}
_ => false,
}
}
}
+122
View File
@@ -0,0 +1,122 @@
use serde::Serialize;
use super::*;
#[derive(Debug, Clone, Serialize)]
pub enum Visibility {
Public,
PublicTarget(Path),
Private,
}
#[derive(Debug, Clone, Serialize)]
pub enum Attribute {
/// #[test]
Path(Path),
/// #[name = "value"]
NameValue { path: Path, value: Literal },
/// #[derive(Clone, Copy)]
List { path: Path, items: Vec<Attribute> },
}
#[derive(Debug, Clone, Serialize)]
pub struct TopLevel(pub Spanned<TopLevelKind>, pub Vec<Attribute>);
#[derive(Debug, Clone, Serialize)]
pub enum TopLevelKind {
ModAttribute,
Import(Visibility, Path),
Mod(Visibility, Identifier),
ImplDecl(ImplDecl),
TraitDecl {
visibility: Visibility,
name: Identifier,
generics: GenericsDecl,
requirements: Vec<TypeExpr>,
items: Vec<Spanned<FunctionDecl>>,
},
EnumDecl {
visibility: Visibility,
name: Identifier,
generics: GenericsDecl,
fields: Vec<Spanned<EnumItem>>,
},
StructDecl {
visibility: Visibility,
name: Identifier,
generics: GenericsDecl,
fields: Vec<Spanned<FieldDecl>>,
},
FunctionDecl(FunctionDecl),
ClassDecl {
visibility: Visibility,
name: Identifier,
generics: GenericsDecl,
fields: Vec<Spanned<FieldDeclStmt>>,
constructor: Spanned<ClassConstructor>,
items: Vec<ClassItem>,
},
}
#[derive(Debug, Clone, Serialize)]
pub enum ClassItem {
Method(Spanned<FunctionDecl>),
ImplDecl(Spanned<ImplDecl>),
}
#[derive(Debug, Clone, Serialize, Default)]
pub struct GenericsDecl(pub Vec<GenericDecl>);
#[derive(Debug, Clone, Serialize)]
pub enum GenericDecl {
Lifetime(Identifier),
Type(Identifier, Vec<TypeExpr>),
}
#[derive(Debug, Clone, Serialize)]
pub enum EnumItem {
Named(Identifier),
Tuple(Identifier, Vec<TypeExpr>),
Struct(Identifier, Vec<FieldDecl>),
}
#[derive(Debug, Clone, Serialize)]
pub struct ClassConstructor {
pub visibility: Visibility,
pub generics: GenericsDecl,
pub params: ParamList,
pub body: Block,
}
#[derive(Debug, Clone, Serialize)]
pub struct FunctionDecl {
pub visibility: Visibility,
pub name: Identifier,
pub generics: GenericsDecl,
pub params: ParamList,
pub return_type: TypeExpr,
pub body: Option<Block>,
}
#[derive(Debug, Clone, Serialize)]
pub struct ImplDecl {
pub generics: GenericsDecl,
pub target: TypeExpr,
pub trait_: Option<TypeExpr>,
pub methods: Vec<Spanned<FunctionDecl>>,
}
#[derive(Debug, Clone, Serialize)]
pub struct FieldDecl {
pub visibility: Visibility,
pub type_: TypeExpr,
pub name: Identifier,
}
#[derive(Debug, Clone, Serialize)]
pub struct FieldDeclStmt {
pub decl: FieldDecl,
pub init: Option<Expression>,
}