telltale-language 11.3.0

Shared choreography frontend for Telltale DSL parsing, projection, and macro code generation
Documentation
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! Parsers for authority expressions and capability statements.

use std::collections::{HashMap, HashSet};

use crate::ast::{AuthorityBindingMode, Role};

use super::super::role::parse_role_ref;
use super::super::statement::{parse_block, parse_duration, parse_statement};
use super::super::types::{AuthorityExprSpec, CaseBranchSpec, CasePatternSpec, Statement};
use super::super::Rule;
use super::{next_required, syntax_error};

fn parse_authority_expr(pair: pest::iterators::Pair<Rule>) -> Result<AuthorityExprSpec, String> {
    let pair = if pair.as_rule() == Rule::authority_expr {
        pair.into_inner()
            .next()
            .ok_or_else(|| "authority expression is empty".to_string())?
    } else {
        pair
    };

    match pair.as_rule() {
        Rule::check_expr => {
            let call = pair
                .into_inner()
                .next()
                .ok_or_else(|| "check is missing effect call".to_string())?;
            parse_effect_call(call).map(|(effect, operation, args)| AuthorityExprSpec::Check {
                effect,
                operation,
                args,
            })
        }
        Rule::observe_expr => {
            let call = pair
                .into_inner()
                .next()
                .ok_or_else(|| "observe is missing effect call".to_string())?;
            parse_effect_call(call).map(|(effect, operation, args)| AuthorityExprSpec::Observe {
                effect,
                operation,
                args,
            })
        }
        Rule::transfer_expr => {
            let span = pair.as_span();
            let mut inner = pair.into_inner();
            let subject = inner
                .next()
                .ok_or_else(|| "transfer is missing subject".to_string())?
                .as_str()
                .to_string();
            let from = inner
                .next()
                .ok_or_else(|| "transfer is missing source role".to_string())?
                .as_str()
                .to_string();
            let to = inner
                .next()
                .ok_or_else(|| "transfer is missing target role".to_string())?
                .as_str()
                .to_string();
            if span.as_str().trim().is_empty() {
                return Err("transfer expression is empty".to_string());
            }
            Ok(AuthorityExprSpec::Transfer { subject, from, to })
        }
        Rule::result_ctor_expr | Rule::maybe_ctor_expr => {
            let mut inner = pair.into_inner();
            let ctor = inner
                .next()
                .ok_or_else(|| "constructor expression is missing constructor".to_string())?
                .as_str()
                .to_string();
            let arg = inner.next().map(|arg| arg.as_str().trim().to_string());
            Ok(AuthorityExprSpec::Constructor { name: ctor, arg })
        }
        Rule::call_expr => {
            let mut inner = pair.into_inner();
            let name = inner
                .next()
                .ok_or_else(|| "call expression is missing name".to_string())?
                .as_str()
                .to_string();
            let args = inner
                .next()
                .map(parse_argument_list)
                .transpose()?
                .unwrap_or_default();
            Ok(AuthorityExprSpec::Call { name, args })
        }
        Rule::ident_expr | Rule::ident => {
            Ok(AuthorityExprSpec::Var(pair.as_str().trim().to_string()))
        }
        _ => Err(format!(
            "unsupported authority expression: {:?}",
            pair.as_rule()
        )),
    }
}

fn parse_argument_list(pair: pest::iterators::Pair<Rule>) -> Result<Vec<String>, String> {
    let mut args = Vec::new();
    for arg in pair.into_inner() {
        match arg.as_rule() {
            Rule::authority_atom
            | Rule::ident_expr
            | Rule::effect_call
            | Rule::string
            | Rule::integer => {
                args.push(arg.as_str().trim().to_string());
            }
            _ => {}
        }
    }
    Ok(args)
}

fn parse_branch_body(
    pair: pest::iterators::Pair<Rule>,
    declared_roles: &HashSet<String>,
    input: &str,
    protocol_defs: &HashMap<String, Vec<Statement>>,
) -> std::result::Result<Vec<Statement>, super::super::error::ParseError> {
    let pair = if pair.as_rule() == Rule::branch_body {
        let span = pair.as_span();
        pair.into_inner()
            .next()
            .ok_or_else(|| syntax_error(span, input, "branch body is empty"))?
    } else {
        pair
    };
    match pair.as_rule() {
        Rule::block => parse_block(pair, declared_roles, input, protocol_defs),
        Rule::statement => Ok(vec![parse_statement(
            pair,
            declared_roles,
            input,
            protocol_defs,
        )?]),
        _ => Err(syntax_error(
            pair.as_span(),
            input,
            "expected an indented branch body or compact statement body",
        )),
    }
}

pub(crate) fn parse_effect_call(
    pair: pest::iterators::Pair<Rule>,
) -> Result<(String, String, Vec<String>), String> {
    let span = pair.as_span();
    let mut inner = pair.into_inner();
    let effect = inner
        .next()
        .ok_or_else(|| "effect call is missing interface".to_string())?
        .as_str()
        .to_string();
    let operation = inner
        .next()
        .ok_or_else(|| "effect call is missing operation".to_string())?
        .as_str()
        .to_string();
    let args = inner
        .next()
        .map(parse_argument_list)
        .transpose()?
        .unwrap_or_default();
    if span.as_str().trim().is_empty() {
        return Err("effect call is empty".to_string());
    }
    Ok((effect, operation, args))
}

fn parse_let_stmt_with_mode(
    pair: pest::iterators::Pair<Rule>,
    _declared_roles: &HashSet<String>,
    input: &str,
    mode: AuthorityBindingMode,
) -> std::result::Result<Statement, super::super::error::ParseError> {
    let span = pair.as_span();
    let mut inner = pair.into_inner();
    let name = next_required(&mut inner, span, input, "let binding is missing name")?
        .as_str()
        .to_string();
    let expr = next_required(&mut inner, span, input, "let binding is missing expression")?;
    let expr = parse_authority_expr(expr).map_err(|message| syntax_error(span, input, message))?;
    validate_binding_mode(mode, &expr, span, input)?;
    Ok(Statement::Let {
        name,
        mode,
        expr,
        body: None,
    })
}

pub(crate) fn parse_let_stmt(
    pair: pest::iterators::Pair<Rule>,
    declared_roles: &HashSet<String>,
    input: &str,
) -> std::result::Result<Statement, super::super::error::ParseError> {
    parse_let_stmt_with_mode(pair, declared_roles, input, AuthorityBindingMode::Plain)
}

pub(crate) fn parse_authority_let_stmt(
    pair: pest::iterators::Pair<Rule>,
    declared_roles: &HashSet<String>,
    input: &str,
) -> std::result::Result<Statement, super::super::error::ParseError> {
    parse_let_stmt_with_mode(
        pair,
        declared_roles,
        input,
        AuthorityBindingMode::Authoritative,
    )
}

pub(crate) fn parse_observe_let_stmt(
    pair: pest::iterators::Pair<Rule>,
    declared_roles: &HashSet<String>,
    input: &str,
) -> std::result::Result<Statement, super::super::error::ParseError> {
    parse_let_stmt_with_mode(pair, declared_roles, input, AuthorityBindingMode::Observe)
}

fn parse_let_in_stmt_with_mode(
    pair: pest::iterators::Pair<Rule>,
    declared_roles: &HashSet<String>,
    input: &str,
    protocol_defs: &HashMap<String, Vec<Statement>>,
    mode: AuthorityBindingMode,
) -> std::result::Result<Statement, super::super::error::ParseError> {
    let span = pair.as_span();
    let mut inner = pair.into_inner();
    let name = next_required(&mut inner, span, input, "let binding is missing name")?
        .as_str()
        .to_string();
    let expr = next_required(&mut inner, span, input, "let binding is missing expression")?;
    let expr = parse_authority_expr(expr).map_err(|message| syntax_error(span, input, message))?;
    validate_binding_mode(mode, &expr, span, input)?;
    let body_pair = next_required(&mut inner, span, input, "let ... in is missing body")?;
    let body = parse_block(body_pair, declared_roles, input, protocol_defs)?;
    Ok(Statement::Let {
        name,
        mode,
        expr,
        body: Some(body),
    })
}

pub(crate) fn parse_let_in_stmt(
    pair: pest::iterators::Pair<Rule>,
    declared_roles: &HashSet<String>,
    input: &str,
    protocol_defs: &HashMap<String, Vec<Statement>>,
) -> std::result::Result<Statement, super::super::error::ParseError> {
    parse_let_in_stmt_with_mode(
        pair,
        declared_roles,
        input,
        protocol_defs,
        AuthorityBindingMode::Plain,
    )
}

pub(crate) fn parse_authority_let_in_stmt(
    pair: pest::iterators::Pair<Rule>,
    declared_roles: &HashSet<String>,
    input: &str,
    protocol_defs: &HashMap<String, Vec<Statement>>,
) -> std::result::Result<Statement, super::super::error::ParseError> {
    parse_let_in_stmt_with_mode(
        pair,
        declared_roles,
        input,
        protocol_defs,
        AuthorityBindingMode::Authoritative,
    )
}

pub(crate) fn parse_observe_let_in_stmt(
    pair: pest::iterators::Pair<Rule>,
    declared_roles: &HashSet<String>,
    input: &str,
    protocol_defs: &HashMap<String, Vec<Statement>>,
) -> std::result::Result<Statement, super::super::error::ParseError> {
    parse_let_in_stmt_with_mode(
        pair,
        declared_roles,
        input,
        protocol_defs,
        AuthorityBindingMode::Observe,
    )
}

fn validate_binding_mode(
    mode: AuthorityBindingMode,
    expr: &AuthorityExprSpec,
    span: pest::Span<'_>,
    input: &str,
) -> std::result::Result<(), super::super::error::ParseError> {
    match mode {
        AuthorityBindingMode::Plain => Ok(()),
        AuthorityBindingMode::Authoritative => {
            if matches!(expr, AuthorityExprSpec::Check { .. }) {
                Ok(())
            } else {
                Err(syntax_error(
                    span,
                    input,
                    "`authoritative let` must bind a `check` expression",
                ))
            }
        }
        AuthorityBindingMode::Observe => {
            if matches!(expr, AuthorityExprSpec::Observe { .. }) {
                Ok(())
            } else {
                Err(syntax_error(
                    span,
                    input,
                    "`observe let` must bind an `observe` expression",
                ))
            }
        }
    }
}

pub(crate) fn parse_case_stmt(
    pair: pest::iterators::Pair<Rule>,
    declared_roles: &HashSet<String>,
    input: &str,
    protocol_defs: &HashMap<String, Vec<Statement>>,
) -> std::result::Result<Statement, super::super::error::ParseError> {
    let span = pair.as_span();
    let mut inner = pair.into_inner();
    let expr_pair = next_required(&mut inner, span, input, "case is missing scrutinee")?;
    let expr =
        parse_authority_expr(expr_pair).map_err(|message| syntax_error(span, input, message))?;
    let block = next_required(&mut inner, span, input, "case is missing branches")?;
    let mut branches = Vec::new();
    for item in block.into_inner() {
        if item.as_rule() != Rule::case_branch {
            continue;
        }
        let branch_span = item.as_span();
        let mut branch_inner = item.into_inner();
        let pattern_pair = next_required(
            &mut branch_inner,
            branch_span,
            input,
            "case branch is missing pattern",
        )?;
        let pattern = parse_case_pattern(pattern_pair);
        let body_pair = next_required(
            &mut branch_inner,
            branch_span,
            input,
            "case branch is missing body",
        )?;
        let statements = parse_branch_body(body_pair, declared_roles, input, protocol_defs)?;
        branches.push(CaseBranchSpec {
            pattern,
            statements,
        });
    }
    if branches.is_empty() {
        return Err(syntax_error(
            span,
            input,
            "case/of requires at least one branch",
        ));
    }
    Ok(Statement::Case { expr, branches })
}

fn parse_case_pattern(pair: pest::iterators::Pair<Rule>) -> CasePatternSpec {
    let mut inner = pair.into_inner();
    let constructor = inner
        .next()
        .map(|p| p.as_str().to_string())
        .unwrap_or_default();
    let binders = inner
        .flat_map(|p| match p.as_rule() {
            Rule::match_pattern_payload => p
                .into_inner()
                .filter(|inner| inner.as_rule() == Rule::ident)
                .map(|ident| ident.as_str().to_string())
                .collect::<Vec<_>>(),
            Rule::ident => vec![p.as_str().to_string()],
            _ => Vec::new(),
        })
        .collect();
    CasePatternSpec {
        constructor,
        binders,
    }
}

pub(crate) fn parse_timeout_stmt(
    pair: pest::iterators::Pair<Rule>,
    declared_roles: &HashSet<String>,
    input: &str,
    protocol_defs: &HashMap<String, Vec<Statement>>,
) -> std::result::Result<Statement, super::super::error::ParseError> {
    let span = pair.as_span();
    let mut duration_ms = None;
    let mut role: Option<Role> = None;
    let mut blocks: Vec<Vec<Statement>> = Vec::new();
    for item in pair.into_inner() {
        match item.as_rule() {
            Rule::duration => duration_ms = Some(parse_duration(item, input)?),
            Rule::role_ref => role = Some(parse_role_ref(item, declared_roles, input)?),
            Rule::block | Rule::statement | Rule::branch_body => blocks.push(parse_branch_body(
                item,
                declared_roles,
                input,
                protocol_defs,
            )?),
            _ => {}
        }
    }
    if blocks.len() < 2 {
        return Err(syntax_error(
            span,
            input,
            "timeout requires body and on timeout branch",
        ));
    }
    Ok(Statement::Timeout {
        role: role.ok_or_else(|| syntax_error(span, input, "timeout is missing deciding role"))?,
        duration_ms: duration_ms
            .ok_or_else(|| syntax_error(span, input, "timeout is missing duration"))?,
        body: blocks.remove(0),
        on_timeout: blocks.remove(0),
        on_cancel: blocks.pop(),
    })
}