From 95f253c0cecbff060a90cf67977b852a79f405a7 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Sat, 16 May 2026 23:04:05 +0200 Subject: [PATCH] Refactored ast structure --- parser/src/ast.rs | 282 ------------------------------------ parser/src/ast/expr.rs | 69 +++++++++ parser/src/ast/mod.rs | 43 ++++++ parser/src/ast/statement.rs | 65 +++++++++ parser/src/ast/top_level.rs | 121 ++++++++++++++++ 5 files changed, 298 insertions(+), 282 deletions(-) delete mode 100644 parser/src/ast.rs create mode 100644 parser/src/ast/expr.rs create mode 100644 parser/src/ast/mod.rs create mode 100644 parser/src/ast/statement.rs create mode 100644 parser/src/ast/top_level.rs diff --git a/parser/src/ast.rs b/parser/src/ast.rs deleted file mode 100644 index 9498fac..0000000 --- a/parser/src/ast.rs +++ /dev/null @@ -1,282 +0,0 @@ -use serde::Serialize; - -#[derive(Debug, Clone, Serialize)] -pub struct Identifier(pub String); - -#[derive(Debug, Clone, Serialize, Default)] -pub struct ParamList(pub Vec); - -#[derive(Debug, Clone, Serialize, Default)] -pub struct Block(pub Vec); - -#[derive(Debug, Clone, Serialize)] -pub enum TypePostfix { - Ref, - RefMut, - RefLifetime(Identifier), - RefMutLifetime(Identifier), -} - -#[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 }, -} - -#[derive(Debug, Clone, Serialize)] -pub enum TypeExprKind { - Path(Path), - PathParams(Path, Vec), - Tuple(Vec), - Lifetime(Identifier), -} - -#[derive(Debug, Clone, Serialize)] -pub struct TypeExpr(pub TypeExprKind, pub Vec); - -#[derive(Debug, Clone, Serialize)] -pub struct Path(pub Vec); - -#[derive(Debug, Clone, Serialize)] -pub enum BinaryOp { - Plus, - Minus, - Multiply, - Divide, - Modulo, - Equal, - NotEqual, - LessThan, - GreaterThan, - LessThanOrEqual, - GreaterThanOrEqual, - And, - Or, -} - -#[derive(Debug, Clone, Serialize)] -pub struct TopLevel(pub TopLevelKind, pub Vec); - -#[derive(Debug, Clone, Serialize)] -pub enum TopLevelKind { - ModAttribute, - Import(Visibility, Path), - Mod(Visibility, Identifier), - ImplDecl(ImplDecl), - TraitDecl { - visibility: Visibility, - name: Identifier, - generics: Generics, - requirements: Vec, - items: Vec, - }, - EnumDecl { - visibility: Visibility, - name: Identifier, - generics: Generics, - fields: Vec, - }, - StructDecl { - visibility: Visibility, - name: Identifier, - generics: Generics, - fields: Vec, - }, - FunctionDecl(FunctionDecl), - ClassDecl { - visibility: Visibility, - name: Identifier, - generics: Generics, - fields: Vec, - constructor: ClassConstructor, - items: Vec, - }, -} - -#[derive(Debug, Clone, Serialize)] -pub enum ClassItem { - Method(FunctionDecl), - ImplDecl(ImplDecl), -} - -#[derive(Debug, Clone, Serialize, Default)] -pub struct Generics(pub Vec); - -#[derive(Debug, Clone, Serialize)] -pub enum Generic { - Lifetime(Identifier), - Type(Identifier, Vec), -} - -#[derive(Debug, Clone, Serialize)] -pub enum Pattern { - NamedTuple(Path, Vec), - Struct(Path, Vec), - Tuple(Vec), - Literal(Literal), - Path(Path), - Id(Identifier), -} - -#[derive(Debug, Clone, Serialize)] -pub enum EnumItem { - Named(Identifier), - Tuple(Identifier, Vec), - Struct(Identifier, Vec), -} - -#[derive(Debug, Clone, Serialize)] -pub struct ClassConstructor { - pub visibility: Visibility, - pub generics: Generics, - pub params: ParamList, - pub body: Block, -} - -#[derive(Debug, Clone, Serialize)] -pub struct FunctionDecl { - pub visibility: Visibility, - pub name: Identifier, - pub generics: Generics, - pub params: ParamList, - pub return_type: TypeExpr, - pub body: Option, -} - -#[derive(Debug, Clone, Serialize)] -pub enum Postfix { - FieldAccess(Identifier), - Call(Vec), - MacroCall(String), - StructCall(Vec<(Identifier, Expression)>), - Index(Expression), - Binary(BinaryOp, Expression), -} - -#[derive(Debug, Clone, Serialize)] -pub enum Prefix { - Ref, - RefMut, - Deref, - New, - Not, -} - -#[derive(Debug, Clone, Serialize)] -pub enum Statement { - Expression(Expression), - Block(Block), - - VarDecl(VarDeclStmt), - Assign { - target: Expression, - value: Expression, - }, - CompoundAssign { - target: Expression, - compound: String, - value: Expression, - }, - If { - initial: StatementBranch, - else_if: Vec, - else_branch: Option>, - }, - While(StatementBranch), - CStyleFor { - init: Box, - condition: Expression, - update: Box, - body: Box, - }, - For { - mutable: bool, - pattern: Pattern, - iterator: Expression, - body: Box, - }, - Match(Expression, Vec<(Pattern, Block)>), - - Return(Option), - Break, - Continue, -} - -#[derive(Debug, Clone, Serialize)] -pub struct ImplDecl { - pub generics: Generics, - pub target: TypeExpr, - pub trait_: Option, - pub methods: Vec, -} - -#[derive(Debug, Clone, Serialize)] -pub struct VarDecl { - pub mutable: bool, - pub name: Pattern, - pub type_: Option, -} - -#[derive(Debug, Clone, Serialize)] -pub struct VarDeclStmt { - pub decl: VarDecl, - pub init: Option, -} - -#[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, -} - -#[derive(Debug, Clone, Serialize)] -pub struct StatementBranch { - pub condition: Expression, - pub body: Box, -} - -#[derive(Debug, Clone, Serialize)] -pub enum Expression { - Literal(Literal), - Path(Path), - Fix { - initial: Box, - prefixes: Vec, - postfixes: Vec, - }, -} - -#[derive(Debug, Clone, Serialize)] -pub enum Literal { - String(String), - Int(i64), - Float(f64), - Bool(bool), - Tuple(Vec), -} - -impl TypeExpr { - pub fn no_px(kind: TypeExprKind) -> Self { - Self(kind, Vec::new()) - } -} diff --git a/parser/src/ast/expr.rs b/parser/src/ast/expr.rs new file mode 100644 index 0000000..fc54780 --- /dev/null +++ b/parser/src/ast/expr.rs @@ -0,0 +1,69 @@ +use serde::Serialize; + +use super::*; + +#[derive(Debug, Clone, Serialize)] +pub enum BinaryOp { + Plus, + Minus, + Multiply, + Divide, + Modulo, + Equal, + NotEqual, + LessThan, + GreaterThan, + LessThanOrEqual, + GreaterThanOrEqual, + And, + Or, +} + +#[derive(Debug, Clone, Serialize)] +pub enum Expression { + Literal(Literal), + Path(Path), + Fix { + initial: Box, + prefixes: Vec, + postfixes: Vec, + }, +} + +#[derive(Debug, Clone, Serialize)] +pub enum Literal { + String(String), + Int(i64), + Float(f64), + Bool(bool), + Tuple(Vec), +} + +#[derive(Debug, Clone, Serialize)] +pub enum Pattern { + NamedTuple(Path, Vec), + Struct(Path, Vec), + Tuple(Vec), + Literal(Literal), + Path(Path), + Id(Identifier), +} + +#[derive(Debug, Clone, Serialize)] +pub enum Postfix { + FieldAccess(Identifier), + Call(Vec), + MacroCall(String), + StructCall(Vec<(Identifier, Expression)>), + Index(Expression), + Binary(BinaryOp, Expression), +} + +#[derive(Debug, Clone, Serialize)] +pub enum Prefix { + Ref, + RefMut, + Deref, + New, + Not, +} diff --git a/parser/src/ast/mod.rs b/parser/src/ast/mod.rs new file mode 100644 index 0000000..1dd636d --- /dev/null +++ b/parser/src/ast/mod.rs @@ -0,0 +1,43 @@ +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); + +#[derive(Debug, Clone, Serialize)] +pub struct Identifier(pub String); + +#[derive(Debug, Clone, Serialize, Default)] +pub struct ParamList(pub Vec); + +#[derive(Debug, Clone, Serialize)] +pub enum TypePostfix { + Ref, + RefMut, + RefLifetime(Identifier), + RefMutLifetime(Identifier), +} + +#[derive(Debug, Clone, Serialize)] +pub enum TypeExprKind { + Path(Path), + PathParams(Path, Vec), + Tuple(Vec), + Lifetime(Identifier), +} + +#[derive(Debug, Clone, Serialize)] +pub struct TypeExpr(pub TypeExprKind, pub Vec); + +impl TypeExpr { + pub fn no_px(kind: TypeExprKind) -> Self { + Self(kind, Vec::new()) + } +} diff --git a/parser/src/ast/statement.rs b/parser/src/ast/statement.rs new file mode 100644 index 0000000..69b12b1 --- /dev/null +++ b/parser/src/ast/statement.rs @@ -0,0 +1,65 @@ +use serde::Serialize; + +use super::*; + +#[derive(Debug, Clone, Serialize, Default)] +pub struct Block(pub Vec); + +#[derive(Debug, Clone, Serialize)] +pub enum Statement { + Expression(Expression), + Block(Block), + + VarDecl(VarDeclStmt), + Assign { + target: Expression, + value: Expression, + }, + CompoundAssign { + target: Expression, + compound: String, + value: Expression, + }, + If { + initial: StatementBranch, + else_if: Vec, + else_branch: Option>, + }, + While(StatementBranch), + CStyleFor { + init: Box, + condition: Expression, + update: Box, + body: Box, + }, + For { + mutable: bool, + pattern: Pattern, + iterator: Expression, + body: Box, + }, + Match(Expression, Vec<(Pattern, Block)>), + + Return(Option), + Break, + Continue, +} + +#[derive(Debug, Clone, Serialize)] +pub struct VarDecl { + pub mutable: bool, + pub name: Pattern, + pub type_: Option, +} + +#[derive(Debug, Clone, Serialize)] +pub struct VarDeclStmt { + pub decl: VarDecl, + pub init: Option, +} + +#[derive(Debug, Clone, Serialize)] +pub struct StatementBranch { + pub condition: Expression, + pub body: Box, +} diff --git a/parser/src/ast/top_level.rs b/parser/src/ast/top_level.rs new file mode 100644 index 0000000..30021ea --- /dev/null +++ b/parser/src/ast/top_level.rs @@ -0,0 +1,121 @@ +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 }, +} + +#[derive(Debug, Clone, Serialize)] +pub struct TopLevel(pub TopLevelKind, pub Vec); + +#[derive(Debug, Clone, Serialize)] +pub enum TopLevelKind { + ModAttribute, + Import(Visibility, Path), + Mod(Visibility, Identifier), + ImplDecl(ImplDecl), + TraitDecl { + visibility: Visibility, + name: Identifier, + generics: Generics, + requirements: Vec, + items: Vec, + }, + EnumDecl { + visibility: Visibility, + name: Identifier, + generics: Generics, + fields: Vec, + }, + StructDecl { + visibility: Visibility, + name: Identifier, + generics: Generics, + fields: Vec, + }, + FunctionDecl(FunctionDecl), + ClassDecl { + visibility: Visibility, + name: Identifier, + generics: Generics, + fields: Vec, + constructor: ClassConstructor, + items: Vec, + }, +} + +#[derive(Debug, Clone, Serialize)] +pub enum ClassItem { + Method(FunctionDecl), + ImplDecl(ImplDecl), +} + +#[derive(Debug, Clone, Serialize, Default)] +pub struct Generics(pub Vec); + +#[derive(Debug, Clone, Serialize)] +pub enum Generic { + Lifetime(Identifier), + Type(Identifier, Vec), +} +#[derive(Debug, Clone, Serialize)] +pub enum EnumItem { + Named(Identifier), + Tuple(Identifier, Vec), + Struct(Identifier, Vec), +} + +#[derive(Debug, Clone, Serialize)] +pub struct ClassConstructor { + pub visibility: Visibility, + pub generics: Generics, + pub params: ParamList, + pub body: Block, +} + +#[derive(Debug, Clone, Serialize)] +pub struct FunctionDecl { + pub visibility: Visibility, + pub name: Identifier, + pub generics: Generics, + pub params: ParamList, + pub return_type: TypeExpr, + pub body: Option, +} + +#[derive(Debug, Clone, Serialize)] +pub struct ImplDecl { + pub generics: Generics, + pub target: TypeExpr, + pub trait_: Option, + pub methods: Vec, +} + +#[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, +}