Skip to main content

shape_ast/ast/
modules.rs

1//! Module system types for Shape AST
2
3use serde::{Deserialize, Serialize};
4
5use super::DocComment;
6use super::functions::{Annotation, AnnotationDef, ForeignFunctionDef, FunctionDef};
7use super::program::Item;
8use super::program::{BuiltinFunctionDecl, BuiltinTypeDecl};
9use super::span::Span;
10use super::types::{EnumDef, InterfaceDef, StructTypeDef, TraitDef};
11
12#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct ImportStmt {
14    pub items: ImportItems,
15    pub from: String,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub enum ImportItems {
20    /// from module::path use { a, b as c }
21    Named(Vec<ImportSpec>),
22    /// use module::path / use module::path as alias (binds local tail segment or alias)
23    Namespace { name: String, alias: Option<String> },
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct ImportSpec {
28    pub name: String,
29    pub alias: Option<String>,
30    #[serde(default)]
31    pub is_annotation: bool,
32}
33
34#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct ExportStmt {
36    pub item: ExportItem,
37    /// For `pub let/const/var`, the original variable declaration so the compiler
38    /// can compile the initialization (ExportItem::Named only preserves the name).
39    #[serde(default, skip_serializing_if = "Option::is_none")]
40    pub source_decl: Option<super::program::VariableDecl>,
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize)]
44pub enum ExportItem {
45    /// pub fn name(...) { ... }
46    Function(FunctionDef),
47    /// pub builtin fn name(...) -> ReturnType;
48    BuiltinFunction(BuiltinFunctionDecl),
49    /// pub builtin type Name;
50    BuiltinType(BuiltinTypeDecl),
51    /// pub type Name = Type;
52    TypeAlias(super::types::TypeAliasDef),
53    /// pub { name1, name2 as alias }
54    Named(Vec<ExportSpec>),
55    /// pub enum Name { ... }
56    Enum(EnumDef),
57    /// pub type Name { field: Type, ... }
58    Struct(StructTypeDef),
59    /// pub interface Name { ... }
60    Interface(InterfaceDef),
61    /// pub trait Name { ... }
62    Trait(TraitDef),
63    /// pub annotation name(...) { ... }
64    Annotation(AnnotationDef),
65    /// pub fn python name(...) { ... }
66    ForeignFunction(ForeignFunctionDef),
67}
68
69#[derive(Debug, Clone, Serialize, Deserialize)]
70pub struct ExportSpec {
71    pub name: String,
72    pub alias: Option<String>,
73}
74
75#[derive(Debug, Clone, Serialize, Deserialize)]
76pub struct ModuleDecl {
77    pub name: String,
78    pub name_span: Span,
79    #[serde(default)]
80    pub doc_comment: Option<DocComment>,
81    pub annotations: Vec<Annotation>,
82    pub items: Vec<Item>,
83}