miden_assembly_syntax/ast/
form.rs

1use alloc::string::String;
2
3use miden_debug_types::{SourceSpan, Span, Spanned};
4
5use super::{AdviceMapEntry, Block, Constant, EnumType, Export, Import, TypeAlias};
6
7/// This type represents the top-level forms of a Miden Assembly module
8#[derive(Debug, PartialEq, Eq)]
9pub enum Form {
10    /// A documentation string for the entire module
11    ModuleDoc(Span<String>),
12    /// A documentation string
13    Doc(Span<String>),
14    /// A type declaration
15    Type(TypeAlias),
16    /// An enum type/constant declaration
17    Enum(EnumType),
18    /// An import from another module
19    Import(Import),
20    /// A constant definition, possibly unresolved
21    Constant(Constant),
22    /// An executable block, represents a program entrypoint
23    Begin(Block),
24    /// A procedure
25    Procedure(Export),
26    /// An entry into the Advice Map
27    AdviceMapEntry(AdviceMapEntry),
28}
29
30impl From<Span<String>> for Form {
31    fn from(doc: Span<String>) -> Self {
32        Self::Doc(doc)
33    }
34}
35
36impl From<TypeAlias> for Form {
37    fn from(value: TypeAlias) -> Self {
38        Self::Type(value)
39    }
40}
41
42impl From<EnumType> for Form {
43    fn from(value: EnumType) -> Self {
44        Self::Enum(value)
45    }
46}
47
48impl From<Import> for Form {
49    fn from(import: Import) -> Self {
50        Self::Import(import)
51    }
52}
53
54impl From<Constant> for Form {
55    fn from(constant: Constant) -> Self {
56        Self::Constant(constant)
57    }
58}
59
60impl From<Block> for Form {
61    fn from(block: Block) -> Self {
62        Self::Begin(block)
63    }
64}
65
66impl From<Export> for Form {
67    fn from(export: Export) -> Self {
68        Self::Procedure(export)
69    }
70}
71
72impl Spanned for Form {
73    fn span(&self) -> SourceSpan {
74        match self {
75            Self::ModuleDoc(spanned) | Self::Doc(spanned) => spanned.span(),
76            Self::Type(spanned) => spanned.span(),
77            Self::Enum(spanned) => spanned.span(),
78            Self::Import(Import { span, .. })
79            | Self::Constant(Constant { span, .. })
80            | Self::AdviceMapEntry(AdviceMapEntry { span, .. }) => *span,
81            Self::Begin(spanned) => spanned.span(),
82            Self::Procedure(spanned) => spanned.span(),
83        }
84    }
85}