wdl-engine 0.13.2

Execution engine for Workflow Description Language (WDL) documents.
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! Implementation of syntax tree elements that are `Send + Sync`.
//!
//! This is used by the engine instead of the corresponding types from `rowan`
//! because that implementation is inherently not `Send`.
//!
//! As evaluation is required to be asynchronous, AST elements must be `Send`.

use std::fmt;
use std::hash::Hash;
use std::sync::Arc;

use rowan::GreenNode;
use rowan::GreenNodeData;
use rowan::GreenToken;
use rowan::GreenTokenData;
use rowan::Language;
use rowan::NodeOrToken;
use rowan::WalkEvent;
use wdl_analysis::Exceptable;
use wdl_ast::NewRoot;
use wdl_ast::Span;
use wdl_ast::SyntaxKind;
use wdl_ast::TreeNode;
use wdl_ast::TreeToken;
use wdl_ast::WorkflowDescriptionLanguage;

/// Internal data for an element in the tree.
#[derive(Clone, PartialEq, Eq, Hash)]
struct ElementData {
    /// The parent data.
    ///
    /// This is `None` for the root node.
    parent: Option<Arc<ElementData>>,
    /// The associated green element.
    green: NodeOrToken<GreenNode, GreenToken>,
    /// The index of this element in the parent's list of children.
    index: usize,
    /// The offset to the start of this element.
    offset: usize,
}

impl ElementData {
    /// Constructs a new element data.
    fn new(
        parent: Arc<ElementData>,
        green: NodeOrToken<GreenNode, GreenToken>,
        index: usize,
        offset: usize,
    ) -> Self {
        Self {
            parent: Some(parent),
            green,
            index,
            offset,
        }
    }

    /// Constructs element data for a new root node.
    fn new_root(green: GreenNode) -> Self {
        Self {
            parent: None,
            green: green.into(),
            index: 0,
            offset: 0,
        }
    }
}

/// Represents an element in a syntax tree.
pub type SyntaxElement = NodeOrToken<SyntaxNode, SyntaxToken>;

/// Represents a syntax node that is `Send + Sync`.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct SyntaxNode(Arc<ElementData>);

impl Exceptable for SyntaxNode {}

impl SyntaxNode {
    /// Constructs a new child node for this node.
    fn new_child_node(&self, green: GreenNode, index: usize, offset: usize) -> Self {
        Self(Arc::new(ElementData::new(
            self.0.clone(),
            green.into(),
            index,
            offset,
        )))
    }

    /// Constructs a new child token for this node.
    fn new_child_token(&self, green: GreenToken, index: usize, offset: usize) -> SyntaxToken {
        SyntaxToken(Arc::new(ElementData::new(
            self.0.clone(),
            green.into(),
            index,
            offset,
        )))
    }

    /// Gets the first child node.
    ///
    /// Returns `None` if there are no children or if all the children are
    /// tokens.
    pub fn first_child(&self) -> Option<SyntaxNode> {
        self.children().next()
    }

    /// Gets the first child node or token.
    ///
    /// Returns `None` if there are no children.
    pub fn first_child_or_token(&self) -> Option<SyntaxElement> {
        self.children_with_tokens().next()
    }

    /// Gets the next sibling node.
    ///
    /// Returns `None` if there is no sibling node.
    pub fn next_sibling(&self) -> Option<SyntaxNode> {
        // This should also be a constant-time access rather than having to iterate.
        // We need the offset relative to the start of the parent from the green node to
        // do that; currently that information is private in `rowan`.

        let parent = self.parent()?;
        let mut children = parent.children();

        while let Some(child) = children.next() {
            if child.eq(self) {
                return children.next();
            }
        }

        None
    }

    /// Gets the next sibling node or token.
    ///
    /// Returns `None` if there is no sibling node or token.
    pub fn next_sibling_or_token(&self) -> Option<SyntaxElement> {
        // This should also be a constant-time access rather than having to iterate.
        // We need the offset relative to the start of the parent from the green node to
        // do that; currently that information is private in `rowan`.

        let parent = self.parent()?;
        let mut children = parent.children_with_tokens();

        while let Some(child) = children.next() {
            if let Some(node) = child.as_node()
                && node.eq(self)
            {
                return children.next();
            }
        }

        None
    }

    /// Gets a preorder traversal iterator starting at this node.
    #[inline]
    pub fn preorder(&self) -> Preorder {
        Preorder::new(self.clone())
    }

    /// Gets a preorder-with-tokens traversal iterator starting at this node.
    #[inline]
    pub fn preorder_with_tokens(&self) -> PreorderWithTokens {
        PreorderWithTokens::new(self.clone())
    }

    /// Gets an iterator over the descendants with tokens for this node.
    fn descendants_with_tokens(&self) -> impl Iterator<Item = SyntaxElement> {
        self.preorder_with_tokens().filter_map(|event| match event {
            WalkEvent::Enter(it) => Some(it),
            WalkEvent::Leave(_) => None,
        })
    }
}

impl TreeNode for SyntaxNode {
    type Token = SyntaxToken;

    fn parent(&self) -> Option<Self> {
        self.0.parent.clone().map(Self)
    }

    fn kind(&self) -> SyntaxKind {
        WorkflowDescriptionLanguage::kind_from_raw(self.0.green.kind())
    }

    fn text(&self) -> impl fmt::Display {
        SyntaxText(self.clone())
    }

    fn span(&self) -> Span {
        Span::new(self.0.offset, usize::from(self.0.green.text_len()))
    }

    fn children(&self) -> impl Iterator<Item = Self> {
        let mut offset = self.0.offset;
        self.0
            .green
            .as_node()
            .expect("should be node")
            .children()
            .enumerate()
            .filter_map(move |(index, child)| {
                let start = offset;
                offset += usize::from(child.text_len());
                Some(self.new_child_node(child.into_node()?.to_owned(), index, start))
            })
    }

    fn children_with_tokens(&self) -> impl Iterator<Item = SyntaxElement> {
        let mut offset = self.0.offset;
        self.0
            .green
            .as_node()
            .expect("should be node")
            .children()
            .enumerate()
            .map(move |(index, child)| {
                let start = offset;
                offset += usize::from(child.text_len());
                match child {
                    NodeOrToken::Node(n) => self.new_child_node(n.to_owned(), index, start).into(),
                    NodeOrToken::Token(t) => {
                        self.new_child_token(t.to_owned(), index, start).into()
                    }
                }
            })
    }

    fn first_token(&self) -> Option<Self::Token> {
        let first: Option<NodeOrToken<&GreenNodeData, &GreenTokenData>> = self
            .0
            .green
            .as_node()
            .expect("should be node")
            .children()
            .next();

        match first? {
            NodeOrToken::Node(n) => self
                .new_child_node(n.to_owned(), 0, self.0.offset)
                .first_token(),
            NodeOrToken::Token(t) => Some(self.new_child_token(t.to_owned(), 0, self.0.offset)),
        }
    }

    fn last_token(&self) -> Option<Self::Token> {
        // Unfortunately `rowan` does not expose the relative offset of each green
        // child. If it did, we could easily just look at the last child here
        // instead of iterating to find the last child's start.
        let mut last: Option<(usize, NodeOrToken<&GreenNodeData, &GreenTokenData>)> = None;
        let mut start = self.0.offset;

        for (index, child) in self
            .0
            .green
            .as_node()
            .expect("should be node")
            .children()
            .enumerate()
        {
            if let Some((_, prev)) = last {
                start += usize::from(prev.text_len());
            }

            last = Some((index, child));
        }

        match last? {
            (index, NodeOrToken::Node(n)) => {
                self.new_child_node(n.to_owned(), index, start).last_token()
            }
            (index, NodeOrToken::Token(t)) => {
                Some(self.new_child_token(t.to_owned(), index, start))
            }
        }
    }

    fn descendants(&self) -> impl Iterator<Item = Self> {
        self.preorder().filter_map(|event| match event {
            WalkEvent::Enter(node) => Some(node),
            WalkEvent::Leave(_) => None,
        })
    }

    fn ancestors(&self) -> impl Iterator<Item = Self> {
        std::iter::successors(Some(self.clone()), SyntaxNode::parent)
    }
}

impl fmt::Debug for SyntaxNode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if f.alternate() {
            let mut level = 0;
            for event in self.preorder_with_tokens() {
                match event {
                    WalkEvent::Enter(element) => {
                        for _ in 0..level {
                            write!(f, "  ")?;
                        }
                        match element {
                            NodeOrToken::Node(node) => writeln!(f, "{node:?}")?,
                            NodeOrToken::Token(token) => writeln!(f, "{token:?}")?,
                        }
                        level += 1;
                    }
                    WalkEvent::Leave(_) => level -= 1,
                }
            }
            assert_eq!(level, 0);
            Ok(())
        } else {
            write!(f, "{:?}@{}", self.kind(), self.span())
        }
    }
}

impl NewRoot<wdl_ast::SyntaxNode> for SyntaxNode {
    fn new_root(root: wdl_ast::SyntaxNode) -> Self {
        Self(Arc::new(ElementData::new_root(root.green().into())))
    }
}

impl From<SyntaxNode> for SyntaxElement {
    fn from(value: SyntaxNode) -> Self {
        Self::Node(value)
    }
}

/// Represents a syntax token that is `Send + Sync`.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct SyntaxToken(Arc<ElementData>);

impl SyntaxToken {
    /// Gets the next sibling node or token.
    ///
    /// Returns `None` if there is no next sibling node or token.
    pub fn next_sibling_or_token(&self) -> Option<SyntaxElement> {
        // This should also be a constant-time access rather than having to iterate.
        // We need the offset relative to the start of the parent from the green node to
        // do that.

        let parent = self.parent();
        let mut children = parent.children_with_tokens();

        while let Some(child) = children.next() {
            if let Some(token) = child.as_token()
                && token.eq(self)
            {
                return children.next();
            }
        }

        None
    }
}

impl TreeToken for SyntaxToken {
    type Node = SyntaxNode;

    fn parent(&self) -> Self::Node {
        self.0
            .parent
            .clone()
            .map(SyntaxNode)
            .expect("tokens should always have parents")
    }

    fn kind(&self) -> SyntaxKind {
        WorkflowDescriptionLanguage::kind_from_raw(self.0.green.kind())
    }

    fn text(&self) -> &str {
        self.0.green.as_token().expect("should be token").text()
    }

    fn span(&self) -> Span {
        Span::new(self.0.offset, usize::from(self.0.green.text_len()))
    }
}

impl fmt::Debug for SyntaxToken {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}@{:?}", self.kind(), self.span())?;
        if self.text().len() < 25 {
            return write!(f, " {:?}", self.text());
        }
        let text = self.text();
        for idx in 21..25 {
            if text.is_char_boundary(idx) {
                let text = format!("{} ...", &text[..idx]);
                return write!(f, " {text:?}");
            }
        }
        unreachable!()
    }
}

impl From<SyntaxToken> for SyntaxElement {
    fn from(value: SyntaxToken) -> Self {
        Self::Token(value)
    }
}

/// Constant that asserts types are `Send + Sync`; if not, it fails to compile.
const _: () = {
    /// Helper that will fail to compile if T is not `Send + Sync`.
    const fn _assert<T: Send + Sync>() {}
    _assert::<SyntaxNode>();
    _assert::<SyntaxToken>();
};

/// Implements a preorder iterator.
#[derive(Debug)]
pub struct Preorder {
    /// The starting node.
    start: SyntaxNode,
    /// The next event for the iterator.
    next: Option<WalkEvent<SyntaxNode>>,
}

impl Preorder {
    /// Constructs a new preorder iterator for the given start node.
    fn new(start: SyntaxNode) -> Preorder {
        let next = Some(WalkEvent::Enter(start.clone()));
        Preorder { start, next }
    }
}

impl Iterator for Preorder {
    type Item = WalkEvent<SyntaxNode>;

    fn next(&mut self) -> Option<WalkEvent<SyntaxNode>> {
        let next = self.next.take();
        self.next = next.as_ref().and_then(|next| {
            Some(match next {
                WalkEvent::Enter(node) => match node.first_child() {
                    Some(child) => WalkEvent::Enter(child),
                    None => WalkEvent::Leave(node.clone()),
                },
                WalkEvent::Leave(node) => {
                    if node == &self.start {
                        return None;
                    }
                    match node.next_sibling() {
                        Some(sibling) => WalkEvent::Enter(sibling),
                        None => WalkEvent::Leave(node.parent()?),
                    }
                }
            })
        });
        next
    }
}

/// Implements a preorder-with-tokens iterator.
pub struct PreorderWithTokens {
    /// The starting element.
    start: SyntaxElement,
    /// The next event for the iterator.
    next: Option<WalkEvent<SyntaxElement>>,
}

impl PreorderWithTokens {
    /// Constructs a new preorder-with-tokens iterator for the given start node.
    fn new(start: SyntaxNode) -> PreorderWithTokens {
        let next = Some(WalkEvent::Enter(start.clone().into()));
        PreorderWithTokens {
            start: start.into(),
            next,
        }
    }
}

impl Iterator for PreorderWithTokens {
    type Item = WalkEvent<SyntaxElement>;

    fn next(&mut self) -> Option<WalkEvent<SyntaxElement>> {
        let next = self.next.take();
        self.next = next.as_ref().and_then(|next| {
            Some(match next {
                WalkEvent::Enter(el) => match el {
                    NodeOrToken::Node(node) => match node.first_child_or_token() {
                        Some(child) => WalkEvent::Enter(child),
                        None => WalkEvent::Leave(node.clone().into()),
                    },
                    NodeOrToken::Token(token) => WalkEvent::Leave(token.clone().into()),
                },
                WalkEvent::Leave(el) if el == &self.start => return None,
                WalkEvent::Leave(el) => {
                    let sibling = match el {
                        NodeOrToken::Node(n) => n.next_sibling_or_token(),
                        NodeOrToken::Token(t) => t.next_sibling_or_token(),
                    };

                    match sibling {
                        Some(sibling) => WalkEvent::Enter(sibling),
                        None => match el {
                            NodeOrToken::Node(n) => WalkEvent::Leave(n.parent()?.into()),
                            NodeOrToken::Token(t) => WalkEvent::Leave(t.parent().into()),
                        },
                    }
                }
            })
        });
        next
    }
}

/// Represents the text of a syntax node.
///
/// The text of a syntax node is the cumulation of its descendant token texts.
struct SyntaxText(SyntaxNode);

impl SyntaxText {
    /// Calls a fallible callback for each chunk of text.
    pub fn try_for_each_chunk<F: FnMut(&str) -> Result<(), E>, E>(
        &self,
        mut f: F,
    ) -> Result<(), E> {
        self.try_fold_chunks((), move |(), chunk| f(chunk))
    }

    /// Attempts to fold each chunk of text into the given accumulator
    pub fn try_fold_chunks<T, F, E>(&self, init: T, mut f: F) -> Result<T, E>
    where
        F: FnMut(T, &str) -> Result<T, E>,
    {
        self.tokens_with_spans()
            .try_fold(init, move |acc, (token, span)| {
                f(acc, &token.text()[span.start()..span.end()])
            })
    }

    /// Gets an iterator over all descendant tokens with their spans.
    fn tokens_with_spans(&self) -> impl Iterator<Item = (SyntaxToken, Span)> {
        let span = self.0.span();
        self.0.descendants_with_tokens().filter_map(move |element| {
            let token = element.into_token()?;
            let token_span = token.span();
            let intersection = span.intersect(token_span)?;
            Some((
                token,
                Span::new(
                    intersection.start() - token_span.start(),
                    intersection.len() - (intersection.end() - token_span.end()),
                ),
            ))
        })
    }
}

impl fmt::Display for SyntaxText {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.try_for_each_chunk(|chunk| fmt::Display::fmt(chunk, f))
    }
}