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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
use pest::iterators::{Pair, Pairs};
use pest::Parser;
use self::grammar::{Grammar, Rule};
pub use self::tokens::{BuiltinConstructor, Constructor, Field, Type};
pub mod grammar;
mod tokens;
#[derive(Debug, Clone, Default)]
pub struct Scheme<'a> {
pub builtins: Vec<BuiltinConstructor<'a>>,
pub type_declarations: Vec<Constructor<'a>>,
pub fn_declarations: Vec<Constructor<'a>>,
}
pub fn parse_scheme(scheme: &str) -> Result<Scheme, Error> {
let pairs = Grammar::parse(Rule::tl_scheme, scheme)?;
let mut scheme = Scheme::default();
for pair in pairs {
parse_declarations(&mut scheme, pair)?;
}
Ok(scheme)
}
pub fn parse_constructor(declaration: &str) -> Result<Constructor, Error> {
let pairs = Grammar::parse(Rule::tl_constructor, declaration)?;
let pair = pairs
.into_iter()
.next()
.ok_or(Error::ExpectedRule(Rule::combinator_decl))?;
parse_declaration(pair)
}
fn parse_declarations<'a>(scheme: &mut Scheme<'a>, pair: Pair<'a, Rule>) -> Result<(), Error> {
let declarations = match pair.as_rule() {
Rule::type_declarations => &mut scheme.type_declarations,
Rule::fn_declarations => &mut scheme.fn_declarations,
_ => return Ok(()),
};
for pair in pair.into_inner() {
if pair.as_rule() == Rule::builtin_combinator_decl {
scheme.builtins.push(parse_builtin_declaration(pair)?);
} else {
let decl = parse_declaration(pair)?;
declarations.push(decl);
}
}
Ok(())
}
fn parse_builtin_declaration<'a>(pair: Pair<'a, Rule>) -> Result<BuiltinConstructor<'a>, Error> {
let mut pairs = pair.into_inner();
let (variant, tl_id) = pairs
.next()
.ok_or(Error::ExpectedRule(Rule::variant))
.and_then(parse_variant)?;
let output_ty = pairs
.next()
.ok_or(Error::ExpectedRule(Rule::boxed_type_ident))?
.as_str();
Ok(BuiltinConstructor {
variant,
tl_id,
output_ty,
})
}
fn parse_declaration<'a>(pair: Pair<'a, Rule>) -> Result<Constructor<'a>, Error> {
let mut pairs = pair.into_inner();
let (variant, tl_id) = pairs
.next()
.ok_or(Error::ExpectedRule(Rule::variant))
.and_then(parse_variant)?;
let mut type_parameters = Vec::new();
read_same_rules(&mut pairs, Rule::type_arg, |pair| {
Ok(type_parameters.push(parse_type_arg(pair)?))
})?;
let mut fields = Vec::new();
read_same_rules(&mut pairs, Rule::field, |pair| {
Ok(fields.push(parse_field(pair)?))
})?;
let result_type = pairs.next().ok_or(Error::ExpectedRule(Rule::result_type))?;
let output = parse_result_type(result_type)?;
Ok(Constructor {
variant,
tl_id,
type_parameters,
fields,
output,
})
}
fn parse_variant<'a>(pair: Pair<'a, Rule>) -> Result<(&'a str, Option<u32>), Error> {
let mut pairs = pair.into_inner();
let name = pairs.next().ok_or(Error::ExpectedRule(Rule::lc_ident_ns))?;
let tl_id = pairs
.next()
.map(|pair| u32::from_str_radix(pair.as_str(), 16))
.transpose()
.map_err(Error::InvalidTlId)?;
Ok((name.as_str(), tl_id))
}
fn parse_type_arg<'a>(pair: Pair<'a, Rule>) -> Result<Field<'a>, Error> {
let mut pairs = pair.into_inner();
let name = pairs.next().ok_or(Error::ExpectedRule(Rule::field_ident))?;
Ok(Field {
name: Some(name.as_str()),
ty: pairs
.next()
.ok_or(Error::ExpectedRule(Rule::type_expr))
.and_then(parse_type_expr)?,
})
}
fn parse_field<'a>(pair: Pair<'a, Rule>) -> Result<Field<'a>, Error> {
let pair = pair
.into_inner()
.next()
.ok_or(Error::ExpectedRule(Rule::field_ident_opt))?;
match pair.as_rule() {
Rule::field_simple => {
let mut pairs = pair.into_inner();
let name = pairs
.next()
.ok_or(Error::ExpectedRule(Rule::field_ident_opt))?
.as_str();
let mut pair = pairs.next().ok_or(Error::ExpectedRule(Rule::type_expr))?;
let ty = if pair.as_rule() == Rule::conditional_def {
let mut conditional = pair.into_inner();
let flags_field = conditional
.next()
.ok_or(Error::ExpectedRule(Rule::field_ident))?
.as_str();
let bit = conditional
.next()
.map(|pair| pair.as_str().parse::<u8>())
.transpose()
.map_err(Error::InvalidFlagsBit)?
.ok_or(Error::ExpectedRule(Rule::nat_const))?;
pair = pairs.next().ok_or(Error::ExpectedRule(Rule::type_expr))?;
Type::Flagged {
flags_field,
bit,
ty: Box::new(parse_type_expr(pair)?),
}
} else {
parse_type_expr(pair)?
};
Ok(Field {
name: Some(name),
ty,
})
}
Rule::field_repeated => {
let mut pairs = pair.into_inner();
let mut pair = pairs.peek().ok_or(Error::ExpectedRule(Rule::field))?;
let mut name = None;
if pair.as_rule() == Rule::field_ident_opt {
name = Some(pair.as_str());
pairs.next();
pair = pairs.peek().ok_or(Error::ExpectedRule(Rule::field))?;
}
let mut multiplicity = None;
if pair.as_rule() == Rule::nat_const {
multiplicity = pair
.as_str()
.parse::<u32>()
.map(Some)
.map_err(Error::InvalidRepetition)?;
pairs.next();
}
let ty = pairs
.map(|pair| parse_field(pair))
.collect::<Result<Vec<_>, _>>()?;
Ok(Field {
name,
ty: Type::Repeated { multiplicity, ty },
})
}
Rule::type_expr => Ok(Field {
name: None,
ty: parse_type_expr(pair)?,
}),
rule => Err(Error::UnexpectedRule(rule)),
}
}
fn parse_result_type<'a>(pair: Pair<'a, Rule>) -> Result<Type<'a>, Error> {
let mut pairs = pair.into_inner();
let ty = pairs
.next()
.ok_or(Error::ExpectedRule(Rule::boxed_type_ident))?
.as_str();
let type_param = pairs.next().map(parse_type_expr).transpose()?.map(Box::new);
Ok(match type_param {
Some(ty_param) => Type::Generic { ty, ty_param },
None => Type::Named { ty },
})
}
fn parse_type_expr<'a>(pair: Pair<'a, Rule>) -> Result<Type<'a>, Error> {
if pair.as_rule() != Rule::type_expr {
return Err(Error::ExpectedRule(Rule::type_expr));
}
let mut pairs = pair.into_inner();
let param = pairs.next().ok_or(Error::ExpectedRule(Rule::type_expr))?;
let ty = match param.as_rule() {
Rule::type_ident => param.as_str(),
Rule::nat_type => return Ok(Type::Int),
rule => return Err(Error::UnexpectedRule(rule)),
};
Ok(if let Some(param) = pairs.next() {
Type::Generic {
ty,
ty_param: Box::new(parse_type_expr(param)?),
}
} else {
Type::Named { ty }
})
}
fn read_same_rules<'a, F>(pairs: &mut Pairs<'a, Rule>, rule: Rule, mut f: F) -> Result<(), Error>
where
F: FnMut(Pair<'a, Rule>) -> Result<(), Error>,
{
while pairs
.peek()
.map(|pair| pair.as_rule() == rule)
.unwrap_or_default()
{
if let Some(pair) = pairs.next() {
f(pair)?;
}
}
Ok(())
}
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("parser error: {0}")]
ParserError(#[from] pest::error::Error<Rule>),
#[error("unexpected rule: {0:?}")]
UnexpectedRule(Rule),
#[error("expected rule: {0:?}")]
ExpectedRule(Rule),
#[error("invalid TL id")]
InvalidTlId(#[source] std::num::ParseIntError),
#[error("invalid flags bit")]
InvalidFlagsBit(#[source] std::num::ParseIntError),
#[error("invalid repetition")]
InvalidRepetition(#[source] std::num::ParseIntError),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn correct_constructor_parser() {
assert_eq!(
parse_constructor("boolTrue = Bool")
.unwrap()
.compute_tl_id(),
0x997275b5
);
assert_eq!(
parse_constructor("pub.ed25519 key:int256 = PublicKey;")
.unwrap()
.compute_tl_id(),
0x4813b4c6
);
const PACKET_CONTENTS: &str = r###"
adnl.packetContents
rand1:bytes
flags:#
from:flags.0?PublicKey
from_short:flags.1?adnl.id.short
message:flags.2?adnl.Message
messages:flags.3?(vector adnl.Message)
address:flags.4?adnl.addressList
priority_address:flags.5?adnl.addressList
seqno:flags.6?long
confirm_seqno:flags.7?long
recv_addr_list_version:flags.8?int
recv_priority_addr_list_version:flags.9?int
reinit_date:flags.10?int
dst_reinit_date:flags.10?int
signature:flags.11?bytes
rand2:bytes
= adnl.PacketContents
"###;
assert_eq!(
parse_constructor(PACKET_CONTENTS).unwrap().compute_tl_id(),
0xd142cd89
);
}
#[test]
fn correct_scheme_parser() {
const DATA: &str = r###"
int ? = Int;
boolTrue = Bool;
boolFalse = Bool;
vector {t:Type} # [ t ] = Vector t;
int128 4*[ int ] = Int128;
testObject value:int o:object f:function = TestObject;
testString value:string = TestObject;
testInt value:int = TestObject;
testVectorBytes value:(vector bytes) = TestObject;
tcp.authentificate nonce:bytes = tcp.Message;
tcp.authentificationNonce nonce:bytes = tcp.Message;
---functions---
catchain.getBlock block:int256 = catchain.BlockResult;
---types---
tcp.authentificationComplete key:PublicKey signature:bytes = tcp.Message;
---functions---
catchain.getBlock block:int256 = catchain.BlockResult;
catchain.getBlocks blocks:(vector int256) = catchain.Sent;
catchain.getDifference rt:(vector int) = catchain.Difference;
catchain.getBlockHistory block:int256 height:long stop_if:(vector int256) = catchain.Sent;
//catchain.getForkDifference src:int fork:catchain.fork = catchain.ForkDifference;
"###;
let test = parse_scheme(DATA).unwrap();
println!("{:#?}", test);
}
}