Skip to main content

miden_assembly_syntax/ast/
form.rs

1use alloc::{string::String, sync::Arc};
2
3use miden_debug_types::{SourceSpan, Span, Spanned};
4
5use super::{
6    AdviceMapEntry, Block, Constant, EnumType, Ident, ImportDecl, Item, Path, Procedure,
7    SubmoduleDecl, TypeAlias, TypeDecl,
8};
9
10/// This type represents the top-level forms of a Miden Assembly module
11#[derive(Debug, PartialEq, Eq)]
12pub enum Form {
13    /// A documentation string for the entire module
14    ModuleDoc(Span<String>),
15    /// A documentation string
16    Doc(Span<String>),
17    /// An explicit `namespace` declaration
18    ///
19    /// Only valid when present in the root module of a project target
20    Namespace(Span<Arc<Path>>),
21    /// An explicit `extern package` declaration
22    ///
23    /// Only valid when present in the root module of a project target
24    ExternPackage(Ident),
25    /// A submodule declaration, i.e. `mod foo` or `pub mod foo`
26    Submodule(SubmoduleDecl),
27    /// A type declaration
28    Type(TypeAlias),
29    /// An enum type/constant declaration
30    Enum(EnumType),
31    /// A constant definition, possibly unresolved
32    Constant(Constant),
33    /// An executable block, represents a program entrypoint
34    Begin(Block),
35    /// A procedure
36    Procedure(Procedure),
37    /// A source-level import declaration.
38    Import(ImportDecl),
39    /// An entry into the Advice Map
40    AdviceMapEntry(AdviceMapEntry),
41}
42
43impl From<Span<String>> for Form {
44    fn from(doc: Span<String>) -> Self {
45        Self::Doc(doc)
46    }
47}
48
49impl From<SubmoduleDecl> for Form {
50    fn from(value: SubmoduleDecl) -> Self {
51        Self::Submodule(value)
52    }
53}
54
55impl From<TypeAlias> for Form {
56    fn from(value: TypeAlias) -> Self {
57        Self::Type(value)
58    }
59}
60
61impl From<EnumType> for Form {
62    fn from(value: EnumType) -> Self {
63        Self::Enum(value)
64    }
65}
66
67impl From<Constant> for Form {
68    fn from(constant: Constant) -> Self {
69        Self::Constant(constant)
70    }
71}
72
73impl From<ImportDecl> for Form {
74    fn from(import: ImportDecl) -> Self {
75        Self::Import(import)
76    }
77}
78
79impl From<Block> for Form {
80    fn from(block: Block) -> Self {
81        Self::Begin(block)
82    }
83}
84
85impl From<Item> for Form {
86    fn from(item: Item) -> Self {
87        match item {
88            Item::Constant(item) => Self::Constant(item),
89            Item::Type(TypeDecl::Alias(item)) => Self::Type(item),
90            Item::Type(TypeDecl::Enum(item)) => Self::Enum(item),
91            Item::Procedure(item) => Self::Procedure(item),
92        }
93    }
94}
95
96impl Spanned for Form {
97    fn span(&self) -> SourceSpan {
98        match self {
99            Self::Namespace(spanned) => spanned.span(),
100            Self::ExternPackage(spanned) => spanned.span(),
101            Self::Submodule(spanned) => spanned.name.span(),
102            Self::ModuleDoc(spanned) | Self::Doc(spanned) => spanned.span(),
103            Self::Type(spanned) => spanned.span(),
104            Self::Enum(spanned) => spanned.span(),
105            Self::Constant(Constant { span, .. })
106            | Self::AdviceMapEntry(AdviceMapEntry { span, .. }) => *span,
107            Self::Begin(spanned) => spanned.span(),
108            Self::Procedure(spanned) => spanned.span(),
109            Self::Import(spanned) => spanned.span(),
110        }
111    }
112}