wdl-grammar 0.25.0

A parse tree 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
//! Module for the lexer implementation.

use logos::Logos;

use super::Span;
use super::parser::ParserToken;
use super::tree::SyntaxKind;

pub mod v1;

/// Represents a set of tokens as a bitset.
///
/// As Rust does not currently support const functions in traits,
/// `TokenSet` operates on "raw" forms of tokens (i.e. `u8`).
///
/// This allows `TokenSet` to work with different token types but also
/// allow for the sets to be created in const contexts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TokenSet(u128);

impl TokenSet {
    /// An empty token set.
    pub const EMPTY: Self = Self(0);

    /// Constructs a token set from a slice of tokens.
    pub const fn new(tokens: &[u8]) -> Self {
        let mut bits = 0u128;
        let mut i = 0;
        while i < tokens.len() {
            bits |= Self::mask(tokens[i]);
            i += 1;
        }
        Self(bits)
    }

    /// Unions two token sets together.
    pub const fn union(self, other: Self) -> Self {
        Self(self.0 | other.0)
    }

    /// Returns a new token set with all tokens from `other` removed from
    /// `self`. If a token from `other` is not present in `self`, it has no
    /// effect.
    pub const fn without(self, other: Self) -> Self {
        Self(self.0 & !other.0)
    }

    /// Checks if the token is contained in the set.
    pub const fn contains(&self, token: u8) -> bool {
        self.0 & Self::mask(token) != 0
    }

    /// Gets the count of tokens in the set.
    pub const fn count(&self) -> usize {
        self.0.count_ones() as usize
    }

    /// Iterates the raw tokens in the set.
    pub fn iter(&self) -> impl Iterator<Item = u8> + use<> {
        let mut bits = self.0;
        std::iter::from_fn(move || {
            if bits == 0 {
                return None;
            }

            let token = u8::try_from(bits.trailing_zeros())
                .expect("the maximum token value should be less than 128");

            bits ^= bits & bits.overflowing_neg().0;
            Some(token)
        })
    }

    /// Masks the given token to a `u128`.
    const fn mask(token: u8) -> u128 {
        1u128 << (token as usize)
    }
}

/// Represents a token for lexing WDL document preambles.
///
/// A WDL parser may initially use this token to lex the version
/// statement at the start of a WDL document.
///
/// Once the version statement has been parsed, the parser will then
/// [morph][Lexer::morph] the lexer to the appropriate token for the
/// document's WDL version and pass the lexer to the matching version
/// of the WDL grammar.
#[derive(Logos, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum PreambleToken {
    /// Contiguous whitespace.
    #[regex(r"[ \t\r\n]+")]
    Whitespace,

    /// A comment.
    #[regex(r"#[^\r\n]*", allow_greedy = true)]
    Comment,

    /// The `version` keyword.
    #[token("version")]
    VersionKeyword,

    /// Any other token that isn't whitespace, comment, or the `version`
    /// keyword.
    #[regex("[^ \t\r\n#]")]
    Any,

    // WARNING: this must always be the last variant.
    /// The exclusive maximum token value.
    MAX,
}

/// Asserts that PreambleToken can fit in a TokenSet.
const _: () = assert!(PreambleToken::MAX as u8 <= 128);

impl ParserToken<'_> for PreambleToken {
    fn into_syntax(self) -> SyntaxKind {
        match self {
            Self::Whitespace => SyntaxKind::Whitespace,
            Self::Comment => SyntaxKind::Comment,
            Self::VersionKeyword => SyntaxKind::VersionKeyword,
            Self::Any | Self::MAX => unreachable!(),
        }
    }

    fn into_raw(self) -> u8 {
        self as u8
    }

    fn from_raw(token: u8) -> Self {
        assert!(token < Self::MAX as u8, "invalid token value");
        unsafe { std::mem::transmute(token) }
    }

    fn describe(self) -> &'static str {
        match self {
            Self::Whitespace => "whitespace",
            Self::Comment => "comment",
            Self::VersionKeyword => "`version` keyword",
            Self::Any | Self::MAX => unreachable!(),
        }
    }

    fn is_trivia(self) -> bool {
        matches!(self, Self::Whitespace | Self::Comment)
    }
}

/// Represents a token for lexing WDL version statements.
///
/// This exists as a separate token type because WDL versions and
/// identifiers overlap on their regex.
///
/// Therefore, version statements are tokenized separately from the rest
/// of the WDL document.
#[derive(Logos, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum VersionStatementToken {
    /// Contiguous whitespace.
    #[regex(r"[ \t\r\n]+")]
    Whitespace,

    /// A comment.
    #[regex(r"#[^\r\n]*", allow_greedy = true)]
    Comment,

    /// A WDL version.
    #[regex(r"[a-zA-Z0-9][a-zA-Z0-9.\-]*")]
    Version,

    // WARNING: this must always be the last variant.
    /// The exclusive maximum token value.
    MAX,
}

/// Asserts that VersionStatementToken can fit in a TokenSet.
const _: () = assert!(VersionStatementToken::MAX as u8 <= 128);

impl ParserToken<'_> for VersionStatementToken {
    fn into_syntax(self) -> SyntaxKind {
        match self {
            Self::Whitespace => SyntaxKind::Whitespace,
            Self::Comment => SyntaxKind::Comment,
            Self::Version => SyntaxKind::Version,
            Self::MAX => unreachable!(),
        }
    }

    fn into_raw(self) -> u8 {
        self as u8
    }

    fn from_raw(token: u8) -> Self {
        assert!(token < Self::MAX as u8, "invalid token value");
        unsafe { std::mem::transmute(token) }
    }

    fn describe(self) -> &'static str {
        match self {
            Self::Whitespace => "whitespace",
            Self::Comment => "comment",
            Self::Version => "version",
            Self::MAX => unreachable!(),
        }
    }

    fn is_trivia(self) -> bool {
        matches!(self, Self::Whitespace | Self::Comment)
    }
}

/// The result type for the lexer.
pub type LexerResult<T> = Result<T, ()>;

/// Records information for a lexer peek operation.
///
/// See the [Lexer::peek] method.
#[derive(Debug, Clone, Copy)]
struct Peeked<T> {
    /// The result of the peek operation.
    result: LexerResult<T>,
    /// The span of the result.
    span: Span,
    /// The offset *before* the peek.
    ///
    /// This is used to discard the peek for morphing lexers.
    offset: usize,
}

/// Implements a WDL lexer.
///
/// A lexer produces a stream of tokens from a WDL source string.
#[allow(missing_debug_implementations)]
#[derive(Clone)]
pub struct Lexer<'a, T>
where
    T: Logos<'a, Extras = ()>,
{
    /// The underlying logos lexer.
    lexer: logos::Lexer<'a, T>,
    /// The peeked token.
    peeked: Option<Peeked<T>>,
}

impl<'a, T> Lexer<'a, T>
where
    T: Logos<'a, Source = str, Error = (), Extras = ()> + Copy,
{
    /// Creates a new lexer for the given source string.
    pub fn new(source: &'a str) -> Self
    where
        T::Extras: Default,
    {
        Self {
            lexer: T::lexer(source),
            peeked: None,
        }
    }

    /// Gets the source string of the given span.
    pub fn source(&self, span: Span) -> &'a str {
        &self.lexer.source()[span.start()..span.end()]
    }

    /// Gets the length of the source.
    pub fn source_len(&self) -> usize {
        self.lexer.source().len()
    }

    /// Gets the current span of the lexer.
    pub fn span(&self) -> Span {
        self.lexer.span().into()
    }

    /// Peeks at the next token.
    pub fn peek(&mut self) -> Option<(LexerResult<T>, Span)> {
        if self.peeked.is_none() {
            let offset = self.lexer.span().start;
            self.peeked = self.lexer.next().map(|r| Peeked {
                result: r,
                span: self.lexer.span().into(),
                offset,
            });
        }

        self.peeked.map(|p| (p.result, p.span))
    }

    /// Morph this lexer into a lexer for a new token type.
    ///
    /// The returned lexer continues to point at the same span
    /// as the current lexer.
    pub fn morph<T2>(self) -> Lexer<'a, T2>
    where
        T2: Logos<'a, Source = str, Error = (), Extras = ()> + Copy,
    {
        // If the lexer has peeked, we need to "reset" the lexer so that it is no longer
        // peeked; this allows the morphed lexer to lex the previously peeked
        // span
        let lexer = match self.peeked {
            Some(peeked) => {
                let mut lexer = T2::lexer(self.lexer.source());
                if peeked.offset > 0 {
                    lexer.bump(peeked.offset);
                    lexer.next();
                }

                lexer
            }
            None => self.lexer.morph(),
        };

        Lexer {
            lexer,
            peeked: None,
        }
    }

    /// Consumes the remainder of the source, returning the span
    /// of the consumed text.
    pub fn consume_remainder(&mut self) -> Option<Span> {
        // Reset the lexer if we've peeked
        if let Some(peeked) = self.peeked.take() {
            self.lexer = T::lexer(self.lexer.source());
            if peeked.offset > 0 {
                self.lexer.bump(peeked.offset);
                self.lexer.next();
            }
        }

        // Bump the remaining source
        self.lexer.next();
        self.lexer.bump(self.lexer.remainder().len());
        let span = self.lexer.span();
        assert!(self.next().is_none(), "lexer should be completed");
        if span.is_empty() {
            None
        } else {
            Some(span.into())
        }
    }
}

impl<'a, T> Iterator for Lexer<'a, T>
where
    T: Logos<'a, Error = (), Extras = ()> + Copy,
{
    type Item = (LexerResult<T>, Span);

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(peeked) = self.peeked.take() {
            return Some((peeked.result, peeked.span));
        }

        self.lexer.next().map(|r| (r, self.lexer.span().into()))
    }
}

#[cfg(test)]
mod test {
    use pretty_assertions::assert_eq;

    use super::*;

    pub(crate) fn map<T>(
        (t, s): (LexerResult<T>, Span),
    ) -> (LexerResult<T>, std::ops::Range<usize>) {
        (t, s.start()..s.end())
    }

    #[test]
    fn test_version_1_0() {
        let mut lexer = Lexer::<PreambleToken>::new(
            "
# Test for 1.0 documents
version 1.0",
        );
        assert_eq!(
            lexer.next().map(map).unwrap(),
            (Ok(PreambleToken::Whitespace), 0..1)
        );
        assert_eq!(
            lexer.next().map(map).unwrap(),
            (Ok(PreambleToken::Comment), 1..25),
        );
        assert_eq!(
            lexer.next().map(map).unwrap(),
            (Ok(PreambleToken::Whitespace), 25..26),
        );
        assert_eq!(
            lexer.next().map(map).unwrap(),
            (Ok(PreambleToken::VersionKeyword), 26..33),
        );

        let mut lexer: Lexer<'_, VersionStatementToken> = lexer.morph();
        assert_eq!(
            lexer.next().map(map).unwrap(),
            (Ok(VersionStatementToken::Whitespace), 33..34),
        );
        assert_eq!(
            lexer.next().map(map).unwrap(),
            (Ok(VersionStatementToken::Version), 34..37)
        );
    }

    #[test]
    fn test_version_1_1() {
        let mut lexer = Lexer::<PreambleToken>::new(
            "
# Test for 1.1 documents
version 1.1",
        );
        assert_eq!(
            lexer.next().map(map).unwrap(),
            (Ok(PreambleToken::Whitespace), 0..1)
        );
        assert_eq!(
            lexer.next().map(map).unwrap(),
            (Ok(PreambleToken::Comment), 1..25)
        );
        assert_eq!(
            lexer.next().map(map).unwrap(),
            (Ok(PreambleToken::Whitespace), 25..26)
        );
        assert_eq!(
            lexer.next().map(map).unwrap(),
            (Ok(PreambleToken::VersionKeyword), 26..33)
        );

        let mut lexer: Lexer<'_, VersionStatementToken> = lexer.morph();
        assert_eq!(
            lexer.next().map(map).unwrap(),
            (Ok(VersionStatementToken::Whitespace), 33..34)
        );
        assert_eq!(
            lexer.next().map(map).unwrap(),
            (Ok(VersionStatementToken::Version), 34..37)
        );
    }

    #[test]
    fn test_version_draft3() {
        // Note: draft-3 documents aren't supported by `wdl`, but
        // the lexer needs to ensure it can lex any valid version
        // token so that the parser may gracefully reject parsing
        // the document.
        let mut lexer = Lexer::<PreambleToken>::new(
            "
# Test for draft-3 documents
version draft-3",
        );
        assert_eq!(
            lexer.next().map(map).unwrap(),
            (Ok(PreambleToken::Whitespace), 0..1)
        );
        assert_eq!(
            lexer.next().map(map).unwrap(),
            (Ok(PreambleToken::Comment), 1..29)
        );
        assert_eq!(
            lexer.next().map(map).unwrap(),
            (Ok(PreambleToken::Whitespace), 29..30)
        );
        assert_eq!(
            lexer.next().map(map).unwrap(),
            (Ok(PreambleToken::VersionKeyword), 30..37)
        );

        let mut lexer: Lexer<'_, VersionStatementToken> = lexer.morph();
        assert_eq!(
            lexer.next().map(map).unwrap(),
            (Ok(VersionStatementToken::Whitespace), 37..38)
        );
        assert_eq!(
            lexer.next().map(map).unwrap(),
            (Ok(VersionStatementToken::Version), 38..45)
        );
    }
}