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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use crate::{
address::ParsedAddress,
parser::{Parser, Token},
};
use anyhow::bail;
use move_core_types::{
account_address::AccountAddress,
identifier::{self, Identifier},
value::{MoveStruct, MoveValue},
};
use std::{
collections::BTreeMap,
fmt::{self, Display},
};
#[derive(Eq, PartialEq, Debug, Clone, Copy)]
pub enum ValueToken {
Number,
NumberTyped,
True,
False,
ByteString,
HexString,
Ident,
AtSign,
LBrace,
RBrace,
LBracket,
RBracket,
LParen,
RParen,
Comma,
Colon,
ColonColon,
Whitespace,
}
#[derive(Eq, PartialEq, Debug, Clone)]
pub enum ParsedValue<Extra: ParsableValue = ()> {
Address(ParsedAddress),
InferredNum(u128),
U8(u8),
U64(u64),
U128(u128),
Bool(bool),
Vector(Vec<ParsedValue<Extra>>),
Struct(
ParsedAddress,
String,
String,
BTreeMap<String, ParsedValue<Extra>>,
),
Custom(Extra),
}
pub trait ParsableValue: Sized {
type ConcreteValue;
fn parse_value<'a, I: Iterator<Item = (ValueToken, &'a str)>>(
parser: &mut Parser<'a, ValueToken, I>,
) -> Option<anyhow::Result<Self>>;
fn move_value_into_concrete(v: MoveValue) -> anyhow::Result<Self::ConcreteValue>;
fn concrete_vector(elems: Vec<Self::ConcreteValue>) -> anyhow::Result<Self::ConcreteValue>;
fn concrete_struct(
addr: AccountAddress,
module: String,
name: String,
values: BTreeMap<String, Self::ConcreteValue>,
) -> anyhow::Result<Self::ConcreteValue>;
fn into_concrete_value(
self,
mapping: &impl Fn(&str) -> Option<AccountAddress>,
) -> anyhow::Result<Self::ConcreteValue>;
}
impl ParsableValue for () {
type ConcreteValue = MoveValue;
fn parse_value<'a, I: Iterator<Item = (ValueToken, &'a str)>>(
_: &mut Parser<'a, ValueToken, I>,
) -> Option<anyhow::Result<Self>> {
None
}
fn move_value_into_concrete(v: MoveValue) -> anyhow::Result<Self::ConcreteValue> {
Ok(v)
}
fn concrete_vector(elems: Vec<Self::ConcreteValue>) -> anyhow::Result<Self::ConcreteValue> {
Ok(MoveValue::Vector(elems))
}
fn concrete_struct(
_address: AccountAddress,
_module: String,
_name: String,
values: BTreeMap<String, Self::ConcreteValue>,
) -> anyhow::Result<Self::ConcreteValue> {
Ok(MoveValue::Struct(MoveStruct::WithFields(
values
.into_iter()
.map(|(f, v)| Ok((Identifier::new(f)?, v)))
.collect::<anyhow::Result<_>>()?,
)))
}
fn into_concrete_value(
self,
_mapping: &impl Fn(&str) -> Option<AccountAddress>,
) -> anyhow::Result<Self::ConcreteValue> {
unreachable!()
}
}
impl Display for ValueToken {
fn fmt<'f>(&self, formatter: &mut fmt::Formatter<'f>) -> Result<(), fmt::Error> {
let s = match self {
ValueToken::Number => "[num]",
ValueToken::NumberTyped => "[num typed]",
ValueToken::True => "true",
ValueToken::False => "false",
ValueToken::ByteString => "[byte string]",
ValueToken::HexString => "[hex string]",
ValueToken::Whitespace => "[whitespace]",
ValueToken::Ident => "[identifier]",
ValueToken::AtSign => "@",
ValueToken::LBrace => "{",
ValueToken::RBrace => "}",
ValueToken::LBracket => "[",
ValueToken::RBracket => "]",
ValueToken::LParen => "(",
ValueToken::RParen => ")",
ValueToken::Comma => ",",
ValueToken::Colon => ":",
ValueToken::ColonColon => "::",
};
fmt::Display::fmt(s, formatter)
}
}
impl Token for ValueToken {
fn is_whitespace(&self) -> bool {
matches!(self, Self::Whitespace)
}
fn next_token(s: &str) -> anyhow::Result<Option<(Self, usize)>> {
fn number_maybe_with_suffix(text: &str, num_text_len: usize) -> (ValueToken, usize) {
let rest = &text[num_text_len..];
if rest.starts_with("u8") {
(ValueToken::NumberTyped, num_text_len + 2)
} else if rest.starts_with("u64") {
(ValueToken::NumberTyped, num_text_len + 3)
} else if rest.starts_with("u128") {
(ValueToken::NumberTyped, num_text_len + 4)
} else {
(ValueToken::Number, num_text_len)
}
}
if s.starts_with("true") {
return Ok(Some((Self::True, 4)));
}
if s.starts_with("false") {
return Ok(Some((Self::False, 5)));
}
let mut chars = s.chars().peekable();
let c = match chars.next() {
None => return Ok(None),
Some(c) => c,
};
Ok(Some(match c {
'@' => (Self::AtSign, 1),
'{' => (Self::LBrace, 1),
'}' => (Self::RBrace, 1),
'[' => (Self::LBracket, 1),
']' => (Self::RBracket, 1),
'(' => (Self::LParen, 1),
')' => (Self::RParen, 1),
',' => (Self::Comma, 1),
':' if matches!(chars.peek(), Some(':')) => (Self::ColonColon, 2),
':' => (Self::Colon, 1),
'0' if matches!(chars.peek(), Some('x')) => {
chars.next().unwrap();
match chars.next() {
Some(c) if c.is_ascii_hexdigit() => {
let len = 3 + chars.take_while(char::is_ascii_hexdigit).count();
number_maybe_with_suffix(s, len)
}
_ => bail!("unrecognized token: {}", s),
}
}
'b' if matches!(chars.peek(), Some('"')) => {
chars.next().unwrap();
let mut len = 2;
loop {
len += 1;
match chars.next() {
Some('"') => break,
Some(c) if c.is_ascii() => (),
Some(c) => bail!(
"Unexpected non-ascii character '{}' in byte string: {}",
c.escape_default(),
s
),
None => bail!("Unexpected end of string before end quote: {}", s),
}
}
if s[..len].chars().any(|c| c == '\\') {
bail!(
"Escape characters not yet supported in byte string: {}",
&s[..len]
)
}
(ValueToken::ByteString, len)
}
'x' if matches!(chars.peek(), Some('"')) => {
chars.next().unwrap();
let mut len = 2;
loop {
len += 1;
match chars.next() {
Some('"') => break,
Some(c) if c.is_ascii_hexdigit() => (),
Some(c) => bail!(
"Unexpected non-hexdigit '{}' in hex string: {}",
c.escape_default(),
s
),
None => bail!("Unexpected end of string before end quote: {}", s),
}
}
assert!(len >= 3);
let num_digits = len - 3;
if num_digits % 2 != 0 {
bail!(
"Expected an even number of hex digits in hex string: {}",
&s[..len]
)
}
(ValueToken::HexString, len)
}
c if c.is_ascii_digit() => {
let len = 1 + chars.take_while(char::is_ascii_digit).count();
number_maybe_with_suffix(s, len)
}
c if c.is_ascii_whitespace() => {
let len = 1 + chars.take_while(char::is_ascii_whitespace).count();
(Self::Whitespace, len)
}
c if c.is_ascii_alphabetic() => {
let len = 1 + chars
.take_while(|c| identifier::is_valid_identifier_char(*c))
.count();
(Self::Ident, len)
}
_ => bail!("unrecognized token: {}", s),
}))
}
}
impl<Extra: ParsableValue> ParsedValue<Extra> {
pub fn into_concrete_value(
self,
mapping: &impl Fn(&str) -> Option<AccountAddress>,
) -> anyhow::Result<Extra::ConcreteValue> {
match self {
ParsedValue::Address(a) => Extra::move_value_into_concrete(MoveValue::Address(
a.into_account_address(mapping)?,
)),
ParsedValue::U8(u) => Extra::move_value_into_concrete(MoveValue::U8(u)),
ParsedValue::U64(u) => Extra::move_value_into_concrete(MoveValue::U64(u)),
ParsedValue::InferredNum(u) if u <= (u64::MAX as u128) => {
Extra::move_value_into_concrete(MoveValue::U64(u as u64))
}
ParsedValue::InferredNum(u) | ParsedValue::U128(u) => {
Extra::move_value_into_concrete(MoveValue::U128(u))
}
ParsedValue::Bool(b) => Extra::move_value_into_concrete(MoveValue::Bool(b)),
ParsedValue::Vector(values) => Extra::concrete_vector(
values
.into_iter()
.map(|value| value.into_concrete_value(mapping))
.collect::<anyhow::Result<_>>()?,
),
ParsedValue::Struct(addr, module, name, values) => Extra::concrete_struct(
ParsedAddress::into_account_address(addr, mapping)?,
module,
name,
values
.into_iter()
.map(|(field, value)| Ok((field, value.into_concrete_value(mapping)?)))
.collect::<anyhow::Result<_>>()?,
),
ParsedValue::Custom(c) => Extra::into_concrete_value(c, mapping),
}
}
}