1use crate::IntegerType;
18
19use super::*;
20
21#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
23pub struct Literal {
24 pub span: Span,
25 pub id: NodeID,
26 pub variant: LiteralVariant,
27}
28
29#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd, Serialize, Deserialize)]
30pub enum LiteralVariant {
31 Address(String),
33 Boolean(bool),
35 Field(String),
38 Group(String),
40 Integer(IntegerType, String),
42 None,
44 Identifier(String),
46 Scalar(String),
49 Signature(String),
51 String(String),
53 Unsuffixed(String),
55}
56
57impl fmt::Display for LiteralVariant {
58 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59 match &self {
60 Self::Address(address) => write!(f, "{address}"),
61 Self::Boolean(boolean) => write!(f, "{boolean}"),
62 Self::Field(field) => write!(f, "{field}field"),
63 Self::Group(group) => write!(f, "{group}group"),
64 Self::Integer(type_, value) => write!(f, "{value}{type_}"),
65 Self::None => write!(f, "none"),
66 Self::Identifier(string) => write!(f, "'{string}'"),
67 Self::Scalar(scalar) => write!(f, "{scalar}scalar"),
68 Self::Signature(signature) => write!(f, "{signature}"),
69 Self::String(string) => write!(f, "\"{string}\""),
70 Self::Unsuffixed(value) => write!(f, "{value}"),
71 }
72 }
73}
74
75impl fmt::Display for Literal {
76 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77 self.variant.fmt(f)
78 }
79}
80
81crate::simple_node_impl!(Literal);
82
83impl Literal {
84 pub fn address(s: String, span: Span, id: NodeID) -> Self {
85 Literal { variant: LiteralVariant::Address(s), span, id }
86 }
87
88 pub fn boolean(s: bool, span: Span, id: NodeID) -> Self {
89 Literal { variant: LiteralVariant::Boolean(s), span, id }
90 }
91
92 pub fn field(s: String, span: Span, id: NodeID) -> Self {
93 Literal { variant: LiteralVariant::Field(s), span, id }
94 }
95
96 pub fn group(s: String, span: Span, id: NodeID) -> Self {
97 Literal { variant: LiteralVariant::Group(s), span, id }
98 }
99
100 pub fn integer(integer_type: IntegerType, s: String, span: Span, id: NodeID) -> Self {
101 Literal { variant: LiteralVariant::Integer(integer_type, s), span, id }
102 }
103
104 pub fn none(span: Span, id: NodeID) -> Self {
105 Literal { variant: LiteralVariant::None, span, id }
106 }
107
108 pub fn identifier(s: String, span: Span, id: NodeID) -> Self {
109 Literal { variant: LiteralVariant::Identifier(s), span, id }
110 }
111
112 pub fn scalar(s: String, span: Span, id: NodeID) -> Self {
113 Literal { variant: LiteralVariant::Scalar(s), span, id }
114 }
115
116 pub fn signature(s: String, span: Span, id: NodeID) -> Self {
117 Literal { variant: LiteralVariant::Signature(s), span, id }
118 }
119
120 pub fn string(s: String, span: Span, id: NodeID) -> Self {
121 Literal { variant: LiteralVariant::String(s), span, id }
122 }
123
124 pub fn unsuffixed(s: String, span: Span, id: NodeID) -> Self {
125 Literal { variant: LiteralVariant::Unsuffixed(s), span, id }
126 }
127
128 pub fn as_u32(&self) -> Option<u32> {
130 if let LiteralVariant::Integer(_, s) = &self.variant {
131 u32::from_str_by_radix(&s.replace("_", "")).ok()
132 } else {
133 None
134 }
135 }
136}
137
138impl From<Literal> for Expression {
139 fn from(value: Literal) -> Self {
140 Expression::Literal(value)
141 }
142}
143
144pub trait FromStrRadix: Sized {
148 fn from_str_by_radix(src: &str) -> Result<Self, std::num::ParseIntError>;
149}
150
151macro_rules! implement_from_str_radix {
152 ($($ty:ident)*) => {
153 $(
154 impl FromStrRadix for $ty {
155 fn from_str_by_radix(src: &str) -> Result<Self, std::num::ParseIntError> {
156 if let Some(stripped) = src.strip_prefix("0x") {
157 Self::from_str_radix(stripped, 16)
158 } else if let Some(stripped) = src.strip_prefix("0o") {
159 Self::from_str_radix(stripped, 8)
160 } else if let Some(stripped) = src.strip_prefix("0b") {
161 Self::from_str_radix(stripped, 2)
162 } else if let Some(stripped) = src.strip_prefix("-0x") {
163 let mut s = String::new();
167 s.push('-');
168 s.push_str(stripped);
169 Self::from_str_radix(&s, 16)
170 } else if let Some(stripped) = src.strip_prefix("-0o") {
171 let mut s = String::new();
173 s.push('-');
174 s.push_str(stripped);
175 Self::from_str_radix(&s, 8)
176 } else if let Some(stripped) = src.strip_prefix("-0b") {
177 let mut s = String::new();
179 s.push('-');
180 s.push_str(stripped);
181 Self::from_str_radix(&s, 2)
182 } else {
183 Self::from_str_radix(src, 10)
184 }
185 }
186 }
187 )*
188 };
189}
190
191implement_from_str_radix! { u8 u16 u32 u64 u128 i8 i16 i32 i64 i128 }