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 Scalar(String),
47 Unsuffixed(String),
49 String(String),
51}
52
53impl fmt::Display for LiteralVariant {
54 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55 match &self {
56 Self::Address(address) => write!(f, "{address}"),
57 Self::Boolean(boolean) => write!(f, "{boolean}"),
58 Self::Field(field) => write!(f, "{field}field"),
59 Self::Group(group) => write!(f, "{group}group"),
60 Self::Integer(type_, value) => write!(f, "{value}{type_}"),
61 Self::None => write!(f, "none"),
62 Self::Scalar(scalar) => write!(f, "{scalar}scalar"),
63 Self::Unsuffixed(value) => write!(f, "{value}"),
64 Self::String(string) => write!(f, "\"{string}\""),
65 }
66 }
67}
68
69impl fmt::Display for Literal {
70 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 self.variant.fmt(f)
72 }
73}
74
75crate::simple_node_impl!(Literal);
76
77impl Literal {
78 pub fn string(s: String, span: Span, id: NodeID) -> Self {
79 Literal { variant: LiteralVariant::String(s), span, id }
80 }
81
82 pub fn field(s: String, span: Span, id: NodeID) -> Self {
83 Literal { variant: LiteralVariant::Field(s), span, id }
84 }
85
86 pub fn group(s: String, span: Span, id: NodeID) -> Self {
87 Literal { variant: LiteralVariant::Group(s), span, id }
88 }
89
90 pub fn address(s: String, span: Span, id: NodeID) -> Self {
91 Literal { variant: LiteralVariant::Address(s), span, id }
92 }
93
94 pub fn scalar(s: String, span: Span, id: NodeID) -> Self {
95 Literal { variant: LiteralVariant::Scalar(s), span, id }
96 }
97
98 pub fn boolean(s: bool, span: Span, id: NodeID) -> Self {
99 Literal { variant: LiteralVariant::Boolean(s), span, id }
100 }
101
102 pub fn integer(integer_type: IntegerType, s: String, span: Span, id: NodeID) -> Self {
103 Literal { variant: LiteralVariant::Integer(integer_type, s), span, id }
104 }
105
106 pub fn unsuffixed(s: String, span: Span, id: NodeID) -> Self {
107 Literal { variant: LiteralVariant::Unsuffixed(s), span, id }
108 }
109
110 pub fn none(span: Span, id: NodeID) -> Self {
111 Literal { variant: LiteralVariant::None, span, id }
112 }
113
114 pub fn as_u32(&self) -> Option<u32> {
116 if let LiteralVariant::Integer(_, s) = &self.variant {
117 u32::from_str_by_radix(&s.replace("_", "")).ok()
118 } else {
119 None
120 }
121 }
122}
123
124impl From<Literal> for Expression {
125 fn from(value: Literal) -> Self {
126 Expression::Literal(value)
127 }
128}
129
130pub trait FromStrRadix: Sized {
134 fn from_str_by_radix(src: &str) -> Result<Self, std::num::ParseIntError>;
135}
136
137macro_rules! implement_from_str_radix {
138 ($($ty:ident)*) => {
139 $(
140 impl FromStrRadix for $ty {
141 fn from_str_by_radix(src: &str) -> Result<Self, std::num::ParseIntError> {
142 if let Some(stripped) = src.strip_prefix("0x") {
143 Self::from_str_radix(stripped, 16)
144 } else if let Some(stripped) = src.strip_prefix("0o") {
145 Self::from_str_radix(stripped, 8)
146 } else if let Some(stripped) = src.strip_prefix("0b") {
147 Self::from_str_radix(stripped, 2)
148 } else if let Some(stripped) = src.strip_prefix("-0x") {
149 let mut s = String::new();
153 s.push('-');
154 s.push_str(stripped);
155 Self::from_str_radix(&s, 16)
156 } else if let Some(stripped) = src.strip_prefix("-0o") {
157 let mut s = String::new();
159 s.push('-');
160 s.push_str(stripped);
161 Self::from_str_radix(&s, 8)
162 } else if let Some(stripped) = src.strip_prefix("-0b") {
163 let mut s = String::new();
165 s.push('-');
166 s.push_str(stripped);
167 Self::from_str_radix(&s, 2)
168 } else {
169 Self::from_str_radix(src, 10)
170 }
171 }
172 }
173 )*
174 };
175}
176
177implement_from_str_radix! { u8 u16 u32 u64 u128 i8 i16 i32 i64 i128 }