miden_assembly_syntax/ast/attribute/meta/
expr.rs

1use alloc::{string::String, sync::Arc};
2
3use miden_debug_types::{SourceSpan, Span, Spanned};
4
5use crate::{
6    Felt,
7    ast::Ident,
8    parser::{IntValue, WordValue},
9    prettier,
10};
11
12/// Represents a metadata expression of an [crate::ast::Attribute]
13#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
14pub enum MetaExpr {
15    /// An identifier/keyword, e.g. `inline`
16    Ident(Ident),
17    /// A decimal or hexadecimal integer value
18    Int(Span<IntValue>),
19    /// A quoted string or identifier
20    String(Ident),
21}
22
23impl prettier::PrettyPrint for MetaExpr {
24    fn render(&self) -> prettier::Document {
25        use prettier::*;
26
27        match self {
28            Self::Ident(id) => text(id),
29            Self::Int(value) => text(value),
30            Self::String(id) => text(format!("\"{}\"", id.as_str().escape_default())),
31        }
32    }
33}
34
35impl From<Ident> for MetaExpr {
36    fn from(value: Ident) -> Self {
37        Self::Ident(value)
38    }
39}
40
41impl From<&str> for MetaExpr {
42    fn from(value: &str) -> Self {
43        Self::String(Ident::from_raw_parts(Span::new(SourceSpan::UNKNOWN, Arc::from(value))))
44    }
45}
46
47impl From<String> for MetaExpr {
48    fn from(value: String) -> Self {
49        Self::String(Ident::from_raw_parts(Span::new(
50            SourceSpan::UNKNOWN,
51            Arc::from(value.into_boxed_str()),
52        )))
53    }
54}
55
56impl From<u8> for MetaExpr {
57    fn from(value: u8) -> Self {
58        Self::Int(Span::new(SourceSpan::UNKNOWN, IntValue::U8(value)))
59    }
60}
61
62impl From<u16> for MetaExpr {
63    fn from(value: u16) -> Self {
64        Self::Int(Span::new(SourceSpan::UNKNOWN, IntValue::U16(value)))
65    }
66}
67
68impl From<u32> for MetaExpr {
69    fn from(value: u32) -> Self {
70        Self::Int(Span::new(SourceSpan::UNKNOWN, IntValue::U32(value)))
71    }
72}
73
74impl From<Felt> for MetaExpr {
75    fn from(value: Felt) -> Self {
76        Self::Int(Span::new(SourceSpan::UNKNOWN, IntValue::Felt(value)))
77    }
78}
79
80impl From<WordValue> for MetaExpr {
81    fn from(value: WordValue) -> Self {
82        Self::Int(Span::new(SourceSpan::UNKNOWN, IntValue::Word(value)))
83    }
84}
85
86impl Spanned for MetaExpr {
87    fn span(&self) -> SourceSpan {
88        match self {
89            Self::Ident(spanned) | Self::String(spanned) => spanned.span(),
90            Self::Int(spanned) => spanned.span(),
91        }
92    }
93}