1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//! Procedural macro for 256-bit integer literals.
//!
//! See [`ethnum::int`](https://docs.rs/ethuint/latest/ethuint/macro.int.html)
//! and [`ethnum::uint`](https://docs.rs/ethnum/latest/ethnum/macro.uint.html)
//! documentation for more information.

extern crate proc_macro;

mod parse;

use self::parse::{Int, LiteralError};
use parse::Uint;
use proc_macro::{Delimiter, Literal, Span, TokenStream, TokenTree};

#[proc_macro]
pub fn int(input: TokenStream) -> TokenStream {
    match IntLiteral::generate(input) {
        Ok(value) => value.into_tokens(),
        Err(err) => err.into_tokens(),
    }
}

#[proc_macro]
pub fn uint(input: TokenStream) -> TokenStream {
    match UintLiteral::generate(input) {
        Ok(value) => value.into_tokens(),
        Err(err) => err.into_tokens(),
    }
}

struct IntLiteral(Int);

impl IntLiteral {
    fn generate(input: TokenStream) -> Result<Self, CompileError> {
        let input = Input::parse(input)?;
        Ok(Self(Int::from_literal(&input.value)?))
    }

    fn into_tokens(self) -> TokenStream {
        let (hi, lo) = self.0.into_words();
        format!("::ethnum::I256::from_words({hi}, {lo})")
            .parse()
            .unwrap()
    }
}

struct UintLiteral(Uint);

impl UintLiteral {
    fn generate(input: TokenStream) -> Result<Self, CompileError> {
        let input = Input::parse(input)?;
        Ok(Self(Uint::from_literal(&input.value)?))
    }

    fn into_tokens(self) -> TokenStream {
        let (hi, lo) = self.0.into_words();
        format!("::ethnum::U256::from_words({hi}, {lo})")
            .parse()
            .unwrap()
    }
}

struct Input {
    value: String,
    span: Span,
}

impl Input {
    fn parse(input: TokenStream) -> Result<Self, CompileError> {
        let mut result = Input {
            value: String::new(),
            span: Span::call_site(),
        };
        ParserState::start().input(input, &mut result)?.end()?;

        Ok(result)
    }
}

enum ParserState {
    String,
    Eof,
}

impl ParserState {
    fn start() -> Self {
        Self::String
    }

    fn input(self, input: TokenStream, result: &mut Input) -> Result<Self, CompileError> {
        input
            .into_iter()
            .try_fold(self, |state, token| state.next(token, result))
    }

    fn next(self, token: TokenTree, result: &mut Input) -> Result<Self, CompileError> {
        match (&self, &token) {
            // Procedural macros invoked from withing `macro_rules!` expansions
            // may be grouped with a `Ø` delimiter (which allows operator
            // precidence to be preserved).
            //
            // See <https://doc.rust-lang.org/stable/proc_macro/enum.Delimiter.html#variant.None>
            (_, TokenTree::Group(g)) if g.delimiter() == Delimiter::None => {
                self.input(g.stream(), result)
            }

            (Self::String, TokenTree::Literal(l)) => match parse_string(l) {
                Some(value) => {
                    result.value = value;
                    result.span = token.span();
                    Ok(Self::Eof)
                }
                None => Err(self.unexpected(Some(token))),
            },

            _ => Err(self.unexpected(Some(token))),
        }
    }

    fn end(self) -> Result<(), CompileError> {
        match self {
            ParserState::Eof => Ok(()),
            _ => Err(self.unexpected(None)),
        }
    }

    fn unexpected(self, token: Option<TokenTree>) -> CompileError {
        let expected = match self {
            ParserState::String => "string literal",
            ParserState::Eof => "<eof>",
        };
        let (value, span) = match token {
            Some(TokenTree::Group(g)) => {
                let delim = match g.delimiter() {
                    Delimiter::Parenthesis => "(",
                    Delimiter::Brace => "{",
                    Delimiter::Bracket => "[",
                    Delimiter::None => "Ø",
                };
                (delim.to_string(), Some(g.span_open()))
            }
            Some(t) => (t.to_string(), Some(t.span())),
            None => ("<eof>".to_owned(), None),
        };

        CompileError {
            message: format!("expected {expected} but found `{value}`"),
            span,
        }
    }
}

struct CompileError {
    message: String,
    span: Option<Span>,
}

impl CompileError {
    fn into_tokens(self) -> TokenStream {
        let error = format!("compile_error!({:?})", self.message)
            .parse::<TokenStream>()
            .unwrap();

        match self.span {
            Some(span) => error
                .into_iter()
                .map(|mut token| {
                    token.set_span(span);
                    token
                })
                .collect(),
            None => error,
        }
    }
}

impl From<LiteralError> for CompileError {
    fn from(err: LiteralError) -> Self {
        Self {
            message: err.to_string(),
            span: None,
        }
    }
}

fn parse_string(literal: &Literal) -> Option<String> {
    Some(
        literal
            .to_string()
            .strip_prefix('"')?
            .strip_suffix('"')?
            .to_owned(),
    )
}