telltale-language 11.2.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
//! Integration-oriented helpers built on top of the shared choreography frontend.
//!
//! These APIs are intended for downstream crates that need validated ASTs,
//! projected local types, theory-facing artifacts, or ordered annotation
//! extraction without reimplementing Telltale's frontend pipeline.

use crate::ast::{
    choreography_to_global, local_to_local_r, Choreography, ConversionError, DslAnnotationEntry,
    LocalType, Protocol, Role,
};
use crate::compiler::parser::{parse_choreography_str, ParseError};
use crate::compiler::projection::{project, ProjectionError};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

/// Scope for one collected annotation record.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AnnotationScope {
    /// Annotation attached to the statement itself.
    Statement,
    /// Annotation attached to the sender side of a statement.
    Sender,
    /// Annotation attached to the receiver side of a statement.
    Receiver,
}

/// One ordered annotation record collected from the AST.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProtocolAnnotationRecord {
    /// Structural path to the protocol node.
    pub path: String,
    /// Protocol node kind such as `send`, `choice`, or `broadcast`.
    pub node_kind: String,
    /// Whether the annotation came from statement, sender, or receiver metadata.
    pub scope: AnnotationScope,
    /// Role most directly associated with the annotation.
    pub role: Option<String>,
    /// Peer roles associated with the protocol node.
    #[serde(default)]
    pub peer_roles: Vec<String>,
    /// Raw annotation key.
    pub key: String,
    /// Raw annotation value.
    pub value: String,
}

/// Parsed choreography plus projected locals for integration work.
#[derive(Debug)]
pub struct CompiledChoreography {
    /// Validated authoritative choreography AST.
    pub choreography: Choreography,
    /// Per-role projected local types in choreography role order.
    pub local_types: Vec<(Role, LocalType)>,
}

impl CompiledChoreography {
    /// Return role names in choreography source order.
    #[must_use]
    pub fn role_names(&self) -> Vec<String> {
        self.choreography
            .roles
            .iter()
            .map(|role| role.name().to_string())
            .collect()
    }

    /// Look up one projected local type by role name.
    #[must_use]
    pub fn local_type(&self, role_name: &str) -> Option<&LocalType> {
        self.local_types
            .iter()
            .find_map(|(role, local_type)| (*role.name() == *role_name).then_some(local_type))
    }

    /// Convert the authoritative choreography to a theory-level global type.
    ///
    /// This only succeeds for the subset of the DSL that has a direct theory
    /// correspondence.
    pub fn try_global_type(&self) -> Result<crate::ast::GlobalTypeCore, ConversionError> {
        choreography_to_global(&self.choreography)
    }

    /// Convert projected locals to theory-level local types keyed by role name.
    pub fn try_local_type_r_map(
        &self,
    ) -> Result<BTreeMap<String, crate::ast::LocalTypeR>, ConversionError> {
        let mut out = BTreeMap::new();
        for (role, local_type) in &self.local_types {
            out.insert(role.name().to_string(), local_to_local_r(local_type)?);
        }
        Ok(out)
    }

    /// Serialize the theory-level global type as JSON.
    pub fn global_type_json(&self) -> Result<String, CompileArtifactsError> {
        let global = self
            .try_global_type()
            .map_err(CompileArtifactsError::Conversion)?;
        serde_json::to_string(&global).map_err(CompileArtifactsError::Serialization)
    }

    /// Serialize theory-level local types as JSON.
    pub fn local_type_r_json(&self) -> Result<String, CompileArtifactsError> {
        let locals = self
            .try_local_type_r_map()
            .map_err(CompileArtifactsError::Conversion)?;
        serde_json::to_string(&locals).map_err(CompileArtifactsError::Serialization)
    }

    /// Collect ordered annotation records from the authoritative choreography AST.
    #[must_use]
    pub fn annotation_records(&self) -> Vec<ProtocolAnnotationRecord> {
        collect_choreography_annotation_records(&self.choreography)
    }
}

/// Errors produced by the integration helpers.
#[derive(Debug, thiserror::Error)]
pub enum CompileArtifactsError {
    #[error("parse error: {0}")]
    Parse(#[from] ParseError),

    #[error("validation error: {0}")]
    Validation(String),

    #[error("projection failed for role {role}: {source}")]
    Projection {
        role: String,
        #[source]
        source: ProjectionError,
    },

    #[error("theory conversion failed: {0}")]
    Conversion(#[from] ConversionError),

    #[error("serialization failed: {0}")]
    Serialization(#[from] serde_json::Error),
}

/// Parse, validate, and project a choreography from DSL source text.
pub fn compile_choreography(input: &str) -> Result<CompiledChoreography, CompileArtifactsError> {
    let choreography = parse_choreography_str(input)?;
    compile_choreography_ast(choreography)
}

/// Validate and project an already-parsed choreography.
pub fn compile_choreography_ast(
    choreography: Choreography,
) -> Result<CompiledChoreography, CompileArtifactsError> {
    choreography
        .validate()
        .map_err(|err| CompileArtifactsError::Validation(err.to_string()))?;

    let mut local_types = Vec::new();
    for role in &choreography.roles {
        let local_type =
            project(&choreography, role).map_err(|source| CompileArtifactsError::Projection {
                role: role.name().to_string(),
                source,
            })?;
        local_types.push((role.clone(), local_type));
    }

    Ok(CompiledChoreography {
        choreography,
        local_types,
    })
}

/// Collect every ordered annotation record from a choreography.
#[must_use]
pub fn collect_choreography_annotation_records(
    choreography: &Choreography,
) -> Vec<ProtocolAnnotationRecord> {
    collect_protocol_annotation_records(&choreography.protocol)
}

/// Collect every ordered annotation record from a protocol tree.
#[must_use]
pub fn collect_protocol_annotation_records(protocol: &Protocol) -> Vec<ProtocolAnnotationRecord> {
    let mut records = Vec::new();
    collect_protocol_annotation_records_inner(protocol, "root", &mut records);
    records
}

fn collect_protocol_annotation_records_inner(
    protocol: &Protocol,
    path: &str,
    records: &mut Vec<ProtocolAnnotationRecord>,
) {
    match protocol {
        Protocol::Send {
            from,
            to,
            continuation,
            ..
        } => {
            push_annotation_records(
                records,
                path,
                "send",
                AnnotationScope::Statement,
                Some(from),
                std::slice::from_ref(to),
                protocol.get_annotations().dsl_entries(),
            );
            if let Some(from_annotations) = protocol.get_from_annotations() {
                push_annotation_records(
                    records,
                    path,
                    "send",
                    AnnotationScope::Sender,
                    Some(from),
                    std::slice::from_ref(to),
                    from_annotations.dsl_entries(),
                );
            }
            if let Some(to_annotations) = protocol.get_to_annotations() {
                push_annotation_records(
                    records,
                    path,
                    "send",
                    AnnotationScope::Receiver,
                    Some(to),
                    std::slice::from_ref(from),
                    to_annotations.dsl_entries(),
                );
            }
            collect_protocol_annotation_records_inner(
                continuation,
                &format!("{path}.continuation"),
                records,
            );
        }
        Protocol::Broadcast {
            from,
            to_all,
            continuation,
            ..
        } => {
            let peers = to_all.iter().cloned().collect::<Vec<_>>();
            push_annotation_records(
                records,
                path,
                "broadcast",
                AnnotationScope::Statement,
                Some(from),
                &peers,
                protocol.get_annotations().dsl_entries(),
            );
            if let Some(from_annotations) = protocol.get_from_annotations() {
                push_annotation_records(
                    records,
                    path,
                    "broadcast",
                    AnnotationScope::Sender,
                    Some(from),
                    &peers,
                    from_annotations.dsl_entries(),
                );
            }
            collect_protocol_annotation_records_inner(
                continuation,
                &format!("{path}.continuation"),
                records,
            );
        }
        Protocol::Choice { role, branches, .. } => {
            push_annotation_records(
                records,
                path,
                "choice",
                AnnotationScope::Statement,
                Some(role),
                &[],
                protocol.get_annotations().dsl_entries(),
            );
            for branch in branches {
                collect_protocol_annotation_records_inner(
                    &branch.protocol,
                    &format!("{path}.branch[{}]", branch.label),
                    records,
                );
            }
        }
        Protocol::Loop { body, .. } => {
            collect_protocol_annotation_records_inner(body, &format!("{path}.body"), records);
        }
        Protocol::Parallel { protocols } => {
            for (idx, branch) in protocols.iter().enumerate() {
                collect_protocol_annotation_records_inner(
                    branch,
                    &format!("{path}.parallel[{idx}]"),
                    records,
                );
            }
        }
        Protocol::Rec { label, body } => {
            collect_protocol_annotation_records_inner(
                body,
                &format!("{path}.rec[{label}]"),
                records,
            );
        }
        Protocol::Timeout {
            body,
            on_timeout,
            on_cancel,
            ..
        } => {
            collect_protocol_annotation_records_inner(
                body,
                &format!("{path}.timeout.body"),
                records,
            );
            collect_protocol_annotation_records_inner(
                on_timeout,
                &format!("{path}.timeout.on_timeout"),
                records,
            );
            if let Some(on_cancel) = on_cancel {
                collect_protocol_annotation_records_inner(
                    on_cancel,
                    &format!("{path}.timeout.on_cancel"),
                    records,
                );
            }
        }
        Protocol::Case { branches, .. } => {
            for branch in branches {
                collect_protocol_annotation_records_inner(
                    &branch.protocol,
                    &format!("{path}.case[{}]", branch.pattern.constructor),
                    records,
                );
            }
        }
        Protocol::Extension { continuation, .. } => {
            push_annotation_records(
                records,
                path,
                "extension",
                AnnotationScope::Statement,
                None,
                &[],
                protocol.get_annotations().dsl_entries(),
            );
            collect_protocol_annotation_records_inner(
                continuation,
                &format!("{path}.continuation"),
                records,
            );
        }
        Protocol::Begin { continuation, .. }
        | Protocol::Await { continuation, .. }
        | Protocol::Resolve { continuation, .. }
        | Protocol::Invalidate { continuation, .. }
        | Protocol::Let { continuation, .. }
        | Protocol::Publish { continuation, .. }
        | Protocol::PublishAuthority { continuation, .. }
        | Protocol::Materialize { continuation, .. }
        | Protocol::Handoff { continuation, .. }
        | Protocol::DependentWork { continuation, .. } => {
            collect_protocol_annotation_records_inner(
                continuation,
                &format!("{path}.continuation"),
                records,
            );
        }
        Protocol::Var(_) | Protocol::End => {}
    }
}

fn push_annotation_records(
    records: &mut Vec<ProtocolAnnotationRecord>,
    path: &str,
    node_kind: &str,
    scope: AnnotationScope,
    role: Option<&Role>,
    peer_roles: &[Role],
    entries: Vec<DslAnnotationEntry>,
) {
    let role = role.map(|role| role.name().to_string());
    let peer_roles = peer_roles
        .iter()
        .map(|role| role.name().to_string())
        .collect::<Vec<_>>();

    for entry in entries {
        records.push(ProtocolAnnotationRecord {
            path: path.to_string(),
            node_kind: node_kind.to_string(),
            scope,
            role: role.clone(),
            peer_roles: peer_roles.clone(),
            key: entry.key,
            value: entry.value,
        });
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn ordered_annotation_records_preserve_sender_order() {
        let compiled = compile_choreography(
            r#"
protocol Demo =
  roles Alice, Bob
  Alice { guard_capability : "chat:send", flow_cost : 10, leak : external } -> Bob : Msg
"#,
        )
        .expect("compile choreography");

        let records = compiled
            .annotation_records()
            .into_iter()
            .filter(|record| {
                record.path == "root"
                    && record.scope == AnnotationScope::Sender
                    && record.role.as_deref() == Some("Alice")
            })
            .collect::<Vec<_>>();

        assert_eq!(
            records
                .iter()
                .map(|record| record.key.as_str())
                .collect::<Vec<_>>(),
            vec!["guard_capability", "flow_cost", "leak"]
        );
    }
}