sway_ast/
literal.rs

1use crate::priv_prelude::*;
2
3#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)]
4pub struct LitString {
5    pub span: Span,
6    pub parsed: String,
7}
8
9#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)]
10pub struct LitChar {
11    pub span: Span,
12    pub parsed: char,
13}
14
15#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)]
16pub struct LitInt {
17    pub span: Span,
18    pub parsed: BigUint,
19    pub ty_opt: Option<(LitIntType, Span)>,
20    /// True if this [LitInt] represents a `b256` hex literal
21    /// in a manually generated lexed tree.
22    ///
23    /// `b256` hex literals are not explicitly modeled in the
24    /// [Literal]. During parsing, they are parsed as [LitInt]
25    /// with [LitInt::ty_opt] set to `None`.
26    ///
27    /// To properly render `b256` manually created hex literals,
28    /// that are not backed by a [Span] in the source code,
29    /// we need this additional information, to distinguish
30    /// them from `u256` hex literals.
31    pub is_generated_b256: bool,
32}
33
34#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)]
35pub enum LitIntType {
36    U8,
37    U16,
38    U32,
39    U64,
40    U256,
41    I8,
42    I16,
43    I32,
44    I64,
45}
46
47#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)]
48pub struct LitBool {
49    pub span: Span,
50    pub kind: LitBoolType,
51}
52
53#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)]
54pub enum LitBoolType {
55    True,
56    False,
57}
58
59impl From<LitBoolType> for bool {
60    fn from(item: LitBoolType) -> Self {
61        match item {
62            LitBoolType::True => true,
63            LitBoolType::False => false,
64        }
65    }
66}
67
68#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)]
69pub enum Literal {
70    String(LitString),
71    Char(LitChar),
72    Int(LitInt),
73    Bool(LitBool),
74}
75
76impl Spanned for LitString {
77    fn span(&self) -> Span {
78        self.span.clone()
79    }
80}
81
82impl Spanned for LitChar {
83    fn span(&self) -> Span {
84        self.span.clone()
85    }
86}
87
88impl Spanned for LitInt {
89    fn span(&self) -> Span {
90        match &self.ty_opt {
91            Some((_lit_int_ty, span)) => Span::join(self.span.clone(), span),
92            None => self.span.clone(),
93        }
94    }
95}
96
97impl Spanned for LitBool {
98    fn span(&self) -> Span {
99        self.span.clone()
100    }
101}
102
103impl Spanned for Literal {
104    fn span(&self) -> Span {
105        match self {
106            Literal::String(lit_string) => lit_string.span(),
107            Literal::Char(lit_char) => lit_char.span(),
108            Literal::Int(lit_int) => lit_int.span(),
109            Literal::Bool(lit_bool) => lit_bool.span(),
110        }
111    }
112}