driver-lang 1.0.0

Compiler driver and session orchestration.
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
//! Typed composition of stages into an end-to-end driver.

use core::marker::PhantomData;

use crate::error::DriverError;
use crate::session::Session;
use crate::stage::Stage;

/// Two stages run back to back — the output of [`Then`] is the composition of its
/// parts.
///
/// `Then<A, B>` is itself a [`Stage`]: it feeds an input through `A`, then feeds
/// `A`'s output through `B`, and its own `Input`/`Output` are `A::Input` and
/// `B::Output`. It is the node [`Pipeline::then`] builds, and you rarely name it
/// directly — a three-stage pipeline has type `Pipeline<Then<Then<A, B>, C>>`,
/// assembled for you by chaining `.then(...)`. It is `pub` only because it appears
/// in those inferred types.
///
/// Composition is monomorphized: dispatch to each stage is a direct, inlinable
/// call with no boxing and no virtual dispatch, so a pipeline of any length is as
/// cheap as calling the stages by hand.
#[derive(Debug, Clone)]
pub struct Then<A, B> {
    first: A,
    second: B,
}

impl<C, A, B> Stage<C> for Then<A, B>
where
    A: Stage<C>,
    B: Stage<C, Input = A::Output>,
{
    type Input = A::Input;
    type Output = B::Output;

    /// The name of the final stage in the composition — the stage whose output the
    /// pipeline ultimately produces.
    fn name(&self) -> &'static str {
        self.second.name()
    }

    fn run(
        &mut self,
        input: Self::Input,
        session: &mut Session<C>,
    ) -> Result<Self::Output, DriverError> {
        let mid = self
            .first
            .run(input, session)
            .map_err(|e| e.in_stage(self.first.name()))?;
        self.second
            .run(mid, session)
            .map_err(|e| e.in_stage(self.second.name()))
    }
}

/// An end-to-end driver: a chain of [`Stage`]s whose types line up, run against a
/// [`Session`].
///
/// A `Pipeline` is how stages become a compiler. You start from one stage with
/// [`Pipeline::new`] and extend it with [`then`](Pipeline::then), which appends a
/// stage whose [`Input`](Stage::Input) matches the current
/// [`Output`](Stage::Output). That match is checked *at compile time*: a pipeline
/// that tries to feed tokens into a stage expecting an AST does not build. The
/// finished pipeline is driven with [`run`](Pipeline::run), which threads an input
/// through every stage in order and returns the final artifact.
///
/// On the first stage that returns a [`DriverError`], the pipeline stops — later
/// stages do not run — and the error names the stage that failed. Diagnostics a
/// stage emitted before failing remain in the session.
///
/// The whole pipeline is a single monomorphized type, so composing stages adds no
/// runtime indirection over calling them directly.
///
/// # Examples
///
/// A two-stage calculator driver — tokenize, then sum:
///
/// ```
/// use driver_lang::{DriverError, Pipeline, Session, Stage};
///
/// struct Lex;
/// impl Stage<()> for Lex {
///     type Input = &'static str;
///     type Output = Vec<i64>;
///     fn name(&self) -> &'static str { "lex" }
///     fn run(&mut self, input: &'static str, _s: &mut Session<()>)
///         -> Result<Vec<i64>, DriverError>
///     {
///         input.split_whitespace()
///             .map(|w| w.parse::<i64>().map_err(|_| DriverError::new("not an integer")))
///             .collect()
///     }
/// }
///
/// struct Sum;
/// impl Stage<()> for Sum {
///     type Input = Vec<i64>;
///     type Output = i64;
///     fn name(&self) -> &'static str { "sum" }
///     fn run(&mut self, input: Vec<i64>, _s: &mut Session<()>)
///         -> Result<i64, DriverError>
///     {
///         Ok(input.iter().sum())
///     }
/// }
///
/// let mut driver = Pipeline::new(Lex).then(Sum);
/// let mut session = Session::new(());
///
/// assert_eq!(driver.run("1 2 3 4", &mut session).unwrap(), 10);
/// ```
///
/// The configuration type `C` is a phantom parameter: a pipeline threads a
/// `&mut Session<C>` through its stages but stores no `C` of its own. It is
/// inferred from the stages, so you write `Pipeline::new(stage)` and never name
/// `C` explicitly.
pub struct Pipeline<C, S> {
    stage: S,
    _config: PhantomData<fn() -> C>,
}

impl<C, S> Pipeline<C, S> {
    /// Start a pipeline from a single stage.
    ///
    /// # Examples
    ///
    /// ```
    /// use driver_lang::{DriverError, Pipeline, Session, Stage};
    ///
    /// struct Identity;
    /// impl Stage<()> for Identity {
    ///     type Input = i64;
    ///     type Output = i64;
    ///     fn name(&self) -> &'static str { "identity" }
    ///     fn run(&mut self, input: i64, _s: &mut Session<()>) -> Result<i64, DriverError> {
    ///         Ok(input)
    ///     }
    /// }
    ///
    /// let driver = Pipeline::new(Identity);
    /// assert_eq!(driver.name(), "identity");
    /// ```
    #[must_use]
    pub fn new(stage: S) -> Self {
        Self {
            stage,
            _config: PhantomData,
        }
    }
}

impl<C, S: Stage<C>> Pipeline<C, S> {
    /// Append a stage whose [`Input`](Stage::Input) is this pipeline's current
    /// [`Output`](Stage::Output).
    ///
    /// The type bound `N: Stage<C, Input = S::Output>` is the compile-time check
    /// that the phases connect: the appended stage must accept exactly what the
    /// pipeline currently produces. The result is a longer pipeline that produces
    /// `N`'s output.
    ///
    /// # Examples
    ///
    /// ```
    /// use driver_lang::{DriverError, Pipeline, Session, Stage};
    ///
    /// struct Parse;
    /// impl Stage<()> for Parse {
    ///     type Input = &'static str;
    ///     type Output = usize;
    ///     fn name(&self) -> &'static str { "parse" }
    ///     fn run(&mut self, input: &'static str, _s: &mut Session<()>)
    ///         -> Result<usize, DriverError>
    ///     { Ok(input.len()) }
    /// }
    ///
    /// struct Halve;
    /// impl Stage<()> for Halve {
    ///     type Input = usize;
    ///     type Output = usize;
    ///     fn name(&self) -> &'static str { "halve" }
    ///     fn run(&mut self, input: usize, _s: &mut Session<()>)
    ///         -> Result<usize, DriverError>
    ///     { Ok(input / 2) }
    /// }
    ///
    /// let mut driver = Pipeline::new(Parse).then(Halve);
    /// let mut session = Session::new(());
    /// assert_eq!(driver.run("abcdef", &mut session).unwrap(), 3);
    /// ```
    #[must_use]
    pub fn then<N>(self, next: N) -> Pipeline<C, Then<S, N>>
    where
        N: Stage<C, Input = S::Output>,
    {
        Pipeline {
            stage: Then {
                first: self.stage,
                second: next,
            },
            _config: PhantomData,
        }
    }

    /// Run the pipeline: thread `input` through every stage in order and return the
    /// final output.
    ///
    /// Stages run left to right, each receiving the previous stage's output and the
    /// shared [`Session`]. The run stops at the first stage that returns a
    /// [`DriverError`]; that error names the failing stage.
    ///
    /// # Errors
    ///
    /// Returns the [`DriverError`] of the first stage that fails, with the stage's
    /// name stamped in.
    ///
    /// # Examples
    ///
    /// ```
    /// use driver_lang::{DriverError, Pipeline, Session, Stage};
    ///
    /// struct Fail;
    /// impl Stage<()> for Fail {
    ///     type Input = ();
    ///     type Output = ();
    ///     fn name(&self) -> &'static str { "fail" }
    ///     fn run(&mut self, _input: (), _s: &mut Session<()>) -> Result<(), DriverError> {
    ///         Err(DriverError::new("nope"))
    ///     }
    /// }
    ///
    /// let mut driver = Pipeline::new(Fail);
    /// let mut session = Session::new(());
    /// let err = driver.run((), &mut session).unwrap_err();
    /// assert_eq!(err.stage(), "fail");
    /// assert_eq!(err.message(), "nope");
    /// ```
    pub fn run(
        &mut self,
        input: S::Input,
        session: &mut Session<C>,
    ) -> Result<S::Output, DriverError> {
        let name = self.stage.name();
        self.stage.run(input, session).map_err(|e| e.in_stage(name))
    }

    /// The name of the final stage — the stage whose output this pipeline produces.
    #[must_use]
    #[inline]
    pub fn name(&self) -> &'static str {
        self.stage.name()
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use alloc::vec;
    use alloc::vec::Vec;

    struct Lex;
    impl Stage<()> for Lex {
        type Input = &'static str;
        type Output = Vec<i64>;
        fn name(&self) -> &'static str {
            "lex"
        }
        fn run(
            &mut self,
            input: &'static str,
            _s: &mut Session<()>,
        ) -> Result<Vec<i64>, DriverError> {
            input
                .split_whitespace()
                .map(|w| {
                    w.parse::<i64>()
                        .map_err(|_| DriverError::new("not an integer"))
                })
                .collect()
        }
    }

    struct Sum;
    impl Stage<()> for Sum {
        type Input = Vec<i64>;
        type Output = i64;
        fn name(&self) -> &'static str {
            "sum"
        }
        fn run(&mut self, input: Vec<i64>, _s: &mut Session<()>) -> Result<i64, DriverError> {
            Ok(input.iter().sum())
        }
    }

    struct Negate;
    impl Stage<()> for Negate {
        type Input = i64;
        type Output = i64;
        fn name(&self) -> &'static str {
            "negate"
        }
        fn run(&mut self, input: i64, _s: &mut Session<()>) -> Result<i64, DriverError> {
            Ok(-input)
        }
    }

    /// A stage that emits an error then aborts, to prove a mid-pipeline abort is
    /// attributed to the stage that made it.
    struct EmitThenAbort;
    impl Stage<()> for EmitThenAbort {
        type Input = i64;
        type Output = i64;
        fn name(&self) -> &'static str {
            "emit-then-abort"
        }
        fn run(&mut self, _input: i64, session: &mut Session<()>) -> Result<i64, DriverError> {
            session.error("boom");
            session.abort_if_errors()?;
            Ok(0)
        }
    }

    #[test]
    fn test_single_stage_pipeline_runs() {
        let mut driver = Pipeline::new(Sum);
        let mut s = Session::new(());
        assert_eq!(driver.run(vec![1, 2, 3], &mut s).unwrap(), 6);
    }

    #[test]
    fn test_two_stage_pipeline_threads_output() {
        let mut driver = Pipeline::new(Lex).then(Sum);
        let mut s = Session::new(());
        assert_eq!(driver.run("1 2 3 4", &mut s).unwrap(), 10);
    }

    #[test]
    fn test_three_stage_pipeline_threads_output() {
        let mut driver = Pipeline::new(Lex).then(Sum).then(Negate);
        let mut s = Session::new(());
        assert_eq!(driver.run("1 2 3", &mut s).unwrap(), -6);
    }

    #[test]
    fn test_pipeline_name_is_final_stage() {
        let driver = Pipeline::new(Lex).then(Sum).then(Negate);
        assert_eq!(driver.name(), "negate");
    }

    #[test]
    fn test_pipeline_stops_at_first_failing_stage() {
        let mut driver = Pipeline::new(Lex).then(Sum);
        let mut s = Session::new(());
        let err = driver.run("1 nope 3", &mut s).unwrap_err();
        assert_eq!(err.stage(), "lex");
        assert_eq!(err.message(), "not an integer");
    }

    #[test]
    fn test_failing_stage_in_the_middle_is_attributed() {
        // Lex succeeds, Sum succeeds, EmitThenAbort aborts — the error names the
        // stage that actually failed, not an outer wrapper.
        let mut driver = Pipeline::new(Lex).then(Sum).then(EmitThenAbort);
        let mut s = Session::new(());
        let err = driver.run("1 2 3", &mut s).unwrap_err();
        assert_eq!(err.stage(), "emit-then-abort");
        assert_eq!(err.message(), "aborting due to 1 previous error");
        // The diagnostic the failing stage emitted survives in the session.
        assert_eq!(s.diagnostics().len(), 1);
        assert_eq!(s.diagnostics()[0].message(), "boom");
    }

    /// A stage generic over a shared string config, to prove every stage in a
    /// pipeline reads and writes the same `Session<C>`.
    struct TagFromConfig;
    impl Stage<&'static str> for TagFromConfig {
        type Input = i64;
        type Output = i64;
        fn name(&self) -> &'static str {
            "tag-from-config"
        }
        fn run(
            &mut self,
            input: i64,
            session: &mut Session<&'static str>,
        ) -> Result<i64, DriverError> {
            session.note(*session.config());
            Ok(input)
        }
    }

    #[test]
    fn test_stages_share_the_session_config() {
        let mut driver = Pipeline::new(TagFromConfig).then(TagFromConfig);
        let mut s = Session::new("shared");
        let out = driver.run(7, &mut s).unwrap();
        assert_eq!(out, 7);
        // Both stages saw the same config and emitted from it.
        let notes: Vec<_> = s.diagnostics().iter().map(|d| d.message()).collect();
        assert_eq!(notes, ["shared", "shared"]);
    }

    #[test]
    fn test_diagnostics_from_earlier_stages_survive_a_later_failure() {
        struct WarnThenPass;
        impl Stage<()> for WarnThenPass {
            type Input = &'static str;
            type Output = &'static str;
            fn name(&self) -> &'static str {
                "warn-then-pass"
            }
            fn run(
                &mut self,
                input: &'static str,
                session: &mut Session<()>,
            ) -> Result<&'static str, DriverError> {
                session.warn("heads up");
                Ok(input)
            }
        }
        struct AlwaysFail;
        impl Stage<()> for AlwaysFail {
            type Input = &'static str;
            type Output = ();
            fn name(&self) -> &'static str {
                "always-fail"
            }
            fn run(
                &mut self,
                _input: &'static str,
                _s: &mut Session<()>,
            ) -> Result<(), DriverError> {
                Err(DriverError::new("stop"))
            }
        }

        let mut driver = Pipeline::new(WarnThenPass).then(AlwaysFail);
        let mut s = Session::new(());
        let err = driver.run("x", &mut s).unwrap_err();
        assert_eq!(err.stage(), "always-fail");
        assert_eq!(s.diagnostics().len(), 1);
        assert_eq!(s.diagnostics()[0].message(), "heads up");
    }
}