ion-rs 1.0.0

Implementation of Amazon Ion
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
use crate::element::iterators::SymbolsIterator;
use crate::lazy::decoder::{Decoder, LazyRawSequence, LazyRawValueExpr};
use crate::lazy::expanded::macro_evaluator::{MacroEvaluator, RawEExpression, ValueExpr};
use crate::lazy::expanded::template::{TemplateElement, TemplateSequenceIterator};
use crate::lazy::expanded::{
    EncodingContextRef, ExpandedAnnotationsIterator, ExpandedAnnotationsSource, LazyExpandedValue,
};
use crate::{try_or_some_err, IonResult, IonType};
use std::fmt::Debug;

/// A sequence of not-yet-evaluated expressions passed as arguments to a macro invocation.
///
/// The number of expressions is required to match the number of parameters in the macro's signature,
/// and the order of the expressions corresponds to the order of the parameters.
///
/// For example, given this macro definition:
/// ```ion_1_1
///     (macro foo (x y z) [x, y, z])
/// ```
/// and this invocation:
/// ```ion_1_1
///     (:foo 1 2 (:values 3))
/// ```
/// The `Environment` would contain the expressions `1`, `2` and `(:values 3)`, corresponding to parameters
/// `x`, `y`, and `z` respectively.
#[derive(Copy, Clone)]
pub struct Environment<'top, D: Decoder> {
    expressions: &'top [ValueExpr<'top, D>],
}

impl<D: Decoder> Debug for Environment<'_, D> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Environment::[")?;
        for expr in self.expressions {
            writeln!(f, "{expr:?}")?;
        }
        write!(f, "]")
    }
}

impl<'top, D: Decoder> Environment<'top, D> {
    pub(crate) fn new(args: &'top [ValueExpr<'top, D>]) -> Self {
        Environment { expressions: args }
    }

    /// Returns the expression for the corresponding signature index -- the variable's offset within
    /// the template's signature.
    ///
    /// Panics if the requested index is out of bounds, as the rules of the template compiler
    /// should make that impossible.
    #[inline]
    pub fn require_expr(&self, signature_index: usize) -> ValueExpr<'top, D> {
        if let Some(expr) = self.expressions().get(signature_index).copied() {
            return expr;
        }
        unreachable!("found a macro signature index reference that was out of bounds")
    }

    /// Returns an empty environment without performing any allocations. This is used for evaluating
    /// e-expressions, which never have named parameters.
    pub const fn empty() -> Environment<'top, D> {
        Environment { expressions: &[] }
    }

    pub fn expressions(&self) -> &'top [ValueExpr<'top, D>] {
        self.expressions
    }

    /// Constructs a new environment with the expressions found in the provided EExp.
    pub fn for_eexp(context: EncodingContextRef<'top>, eexp: D::EExp<'top>) -> IonResult<Self> {
        use bumpalo::collections::Vec as BumpVec;
        let allocator = context.allocator();
        let raw_args = eexp.raw_arguments();
        let capacity_hint = raw_args.size_hint().0;
        let mut env_exprs = BumpVec::with_capacity_in(capacity_hint, allocator);
        // Populate the environment by parsing the arguments from input
        for expr in raw_args {
            env_exprs.push(expr?.resolve(context)?);
        }

        Ok(Environment::new(env_exprs.into_bump_slice()))
    }
}

impl<D: Decoder> Default for Environment<'_, D> {
    fn default() -> Self {
        Self::empty()
    }
}

/// The data source for a [`LazyExpandedList`].
#[derive(Clone, Copy)]
pub enum ExpandedListSource<'top, D: Decoder> {
    /// The list was a value literal in the input stream.
    ValueLiteral(D::List<'top>),
    /// The list was part of a template definition.
    Template(Environment<'top, D>, TemplateElement<'top>),
    // TODO: Constructed
}

/// A list that may have come from either a value literal in the input stream or from evaluating
/// a template.
#[derive(Clone, Copy)]
pub struct LazyExpandedList<'top, D: Decoder> {
    pub(crate) context: EncodingContextRef<'top>,
    pub(crate) source: ExpandedListSource<'top, D>,
}

impl<'top, D: Decoder> LazyExpandedList<'top, D> {
    pub fn from_literal(
        context: EncodingContextRef<'top>,
        list: D::List<'top>,
    ) -> LazyExpandedList<'top, D> {
        let source = ExpandedListSource::ValueLiteral(list);
        Self { source, context }
    }

    pub fn from_template(
        context: EncodingContextRef<'top>,
        environment: Environment<'top, D>,
        element: TemplateElement<'top>,
    ) -> LazyExpandedList<'top, D> {
        let source = ExpandedListSource::Template(environment, element);
        Self { source, context }
    }

    pub fn source(&self) -> ExpandedListSource<'top, D> {
        self.source
    }

    pub fn ion_type(&self) -> IonType {
        IonType::List
    }

    pub fn annotations(&self) -> ExpandedAnnotationsIterator<'top, D> {
        match &self.source {
            ExpandedListSource::ValueLiteral(value) => ExpandedAnnotationsIterator {
                source: ExpandedAnnotationsSource::ValueLiteral(value.annotations()),
            },
            ExpandedListSource::Template(_environment, element) => {
                let annotations = element.annotations();
                ExpandedAnnotationsIterator {
                    source: ExpandedAnnotationsSource::Template(SymbolsIterator::new(annotations)),
                }
            }
        }
    }

    pub fn iter(&self) -> ExpandedListIterator<'top, D> {
        let source = match &self.source {
            ExpandedListSource::ValueLiteral(list) => {
                ExpandedListIteratorSource::ValueLiteral(MacroEvaluator::new(), list.iter())
            }
            ExpandedListSource::Template(environment, element) => {
                let nested_expressions = element.nested_expressions();
                let evaluator = MacroEvaluator::new_with_environment(*environment);
                ExpandedListIteratorSource::Template(TemplateSequenceIterator::new(
                    self.context,
                    evaluator,
                    element.template(),
                    nested_expressions,
                ))
            }
        };
        ExpandedListIterator {
            context: self.context,
            source,
        }
    }

    #[cfg(feature = "experimental-tooling-apis")]
    pub fn value_exprs(&self) -> ListValueExprIterator<'top, D> {
        let ExpandedListIterator { context, source } = self.iter();
        ListValueExprIterator { context, source }
    }
}

/// The source of child values iterated over by an [`ExpandedListIterator`].
#[derive(Debug)]
pub enum ExpandedListIteratorSource<'top, D: Decoder> {
    ValueLiteral(
        // Giving the list iterator its own evaluator means that we can abandon the iterator
        // at any time without impacting the evaluation state of its parent container.
        MacroEvaluator<'top, D>,
        <D::List<'top> as LazyRawSequence<'top, D>>::Iterator,
    ),
    Template(TemplateSequenceIterator<'top, D>),
}

/// Iterates over the child values of a [`LazyExpandedList`].
#[derive(Debug)]
pub struct ExpandedListIterator<'top, D: Decoder> {
    context: EncodingContextRef<'top>,
    source: ExpandedListIteratorSource<'top, D>,
}

impl<'top, D: Decoder> Iterator for ExpandedListIterator<'top, D> {
    type Item = IonResult<LazyExpandedValue<'top, D>>;

    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.source {
            ExpandedListIteratorSource::ValueLiteral(evaluator, iter) => {
                expand_next_sequence_value(self.context, evaluator, iter)
            }
            ExpandedListIteratorSource::Template(iter) => iter.next(),
        }
    }
}

/// Like an [`ExpandedListIterator`], but will yield macro invocations before also yielding
/// that macro's expansion.
#[derive(Debug)]
#[cfg(feature = "experimental-tooling-apis")]
pub struct ListValueExprIterator<'top, D: Decoder> {
    context: EncodingContextRef<'top>,
    source: ExpandedListIteratorSource<'top, D>,
}

#[cfg(feature = "experimental-tooling-apis")]
impl<'top, D: Decoder> Iterator for ListValueExprIterator<'top, D> {
    type Item = IonResult<ValueExpr<'top, D>>;

    fn next(&mut self) -> Option<Self::Item> {
        use ExpandedListIteratorSource::*;
        match &mut self.source {
            ValueLiteral(evaluator, iter) => {
                expand_next_sequence_value_expr(self.context, evaluator, iter)
            }
            Template(iter) => iter.next_value_expr(),
        }
    }
}

/// Like an [`ExpandedSExpIterator`], but will yield macro invocations before also yielding
/// that macro's expansion.
#[derive(Debug)]
#[cfg(feature = "experimental-tooling-apis")]
pub struct SExpValueExprIterator<'top, D: Decoder> {
    context: EncodingContextRef<'top>,
    source: ExpandedSExpIteratorSource<'top, D>,
}

#[cfg(feature = "experimental-tooling-apis")]
impl<'top, D: Decoder> Iterator for SExpValueExprIterator<'top, D> {
    type Item = IonResult<ValueExpr<'top, D>>;

    fn next(&mut self) -> Option<Self::Item> {
        use ExpandedSExpIteratorSource::*;
        match &mut self.source {
            ValueLiteral(evaluator, iter) => {
                expand_next_sequence_value_expr(self.context, evaluator, iter)
            }
            Template(iter) => iter.next_value_expr(),
        }
    }
}

/// The data source for a [`LazyExpandedSExp`].
#[derive(Clone, Copy)]
pub enum ExpandedSExpSource<'top, D: Decoder> {
    /// The SExp was a value literal in the input stream.
    ValueLiteral(D::SExp<'top>),
    /// The SExp was part of a template definition.
    /// Because the TDL treats s-expressions as literals, this variant only applies when the
    /// s-expression appeared within a `literal` operation.
    Template(Environment<'top, D>, TemplateElement<'top>),
}

/// An s-expression that may have come from either a value literal in the input stream or from
/// evaluating a template.
#[derive(Clone, Copy)]
pub struct LazyExpandedSExp<'top, D: Decoder> {
    pub(crate) source: ExpandedSExpSource<'top, D>,
    pub(crate) context: EncodingContextRef<'top>,
}

impl<'top, D: Decoder> LazyExpandedSExp<'top, D> {
    pub fn source(&self) -> ExpandedSExpSource<'top, D> {
        self.source
    }

    pub fn ion_type(&self) -> IonType {
        IonType::SExp
    }

    pub fn annotations(&self) -> ExpandedAnnotationsIterator<'top, D> {
        match &self.source {
            ExpandedSExpSource::ValueLiteral(value) => ExpandedAnnotationsIterator {
                source: ExpandedAnnotationsSource::ValueLiteral(value.annotations()),
            },
            ExpandedSExpSource::Template(_environment, element) => {
                let annotations = element.annotations();
                ExpandedAnnotationsIterator {
                    source: ExpandedAnnotationsSource::Template(SymbolsIterator::new(annotations)),
                }
            }
        }
    }

    pub fn iter(&self) -> ExpandedSExpIterator<'top, D> {
        let source = match &self.source {
            ExpandedSExpSource::ValueLiteral(sexp) => {
                ExpandedSExpIteratorSource::ValueLiteral(MacroEvaluator::new(), sexp.iter())
            }
            ExpandedSExpSource::Template(environment, element) => {
                let nested_expressions = element.nested_expressions();
                let evaluator = MacroEvaluator::new_with_environment(*environment);
                ExpandedSExpIteratorSource::Template(TemplateSequenceIterator::new(
                    self.context,
                    evaluator,
                    element.template(),
                    nested_expressions,
                ))
            }
        };
        ExpandedSExpIterator {
            context: self.context,
            source,
        }
    }

    #[cfg(feature = "experimental-tooling-apis")]
    pub fn value_exprs(&self) -> SExpValueExprIterator<'top, D> {
        let ExpandedSExpIterator { context, source } = self.iter();
        SExpValueExprIterator { context, source }
    }

    pub fn from_literal(
        context: EncodingContextRef<'top>,
        sexp: D::SExp<'top>,
    ) -> LazyExpandedSExp<'top, D> {
        let source = ExpandedSExpSource::ValueLiteral(sexp);
        Self { source, context }
    }

    pub fn from_template(
        context: EncodingContextRef<'top>,
        environment: Environment<'top, D>,
        element: TemplateElement<'top>,
    ) -> LazyExpandedSExp<'top, D> {
        let source = ExpandedSExpSource::Template(environment, element);
        Self { source, context }
    }
}

/// The source of child values iterated over by an [`ExpandedSExpIterator`].
#[derive(Debug)]
pub enum ExpandedSExpIteratorSource<'top, D: Decoder> {
    /// The SExp was a literal in the data stream
    ValueLiteral(
        // Giving the sexp iterator its own evaluator means that we can abandon the iterator
        // at any time without impacting the evaluation state of its parent container.
        MacroEvaluator<'top, D>,
        <D::SExp<'top> as LazyRawSequence<'top, D>>::Iterator,
    ),
    /// The SExp was in the body of a macro
    Template(TemplateSequenceIterator<'top, D>),
}

/// Iterates over the child values of a [`LazyExpandedSExp`].
#[derive(Debug)]
pub struct ExpandedSExpIterator<'top, D: Decoder> {
    context: EncodingContextRef<'top>,
    source: ExpandedSExpIteratorSource<'top, D>,
}

impl<'top, D: Decoder> Iterator for ExpandedSExpIterator<'top, D> {
    type Item = IonResult<LazyExpandedValue<'top, D>>;

    fn next(&mut self) -> Option<Self::Item> {
        use ExpandedSExpIteratorSource::*;
        match &mut self.source {
            ValueLiteral(evaluator, iter) => {
                expand_next_sequence_value(self.context, evaluator, iter)
            }
            Template(iter) => iter.next(),
        }
    }
}

#[cfg(feature = "experimental-tooling-apis")]
fn expand_next_sequence_value_expr<'top, D: Decoder>(
    context: EncodingContextRef<'top>,
    evaluator: &mut MacroEvaluator<'top, D>,
    iter: &mut impl Iterator<Item = IonResult<LazyRawValueExpr<'top, D>>>,
) -> Option<IonResult<ValueExpr<'top, D>>> {
    if let Some(value) = try_or_some_err!(evaluator.next()) {
        return Some(Ok(ValueExpr::ValueLiteral(value)));
    }

    // At this point, the evaluator is empty.

    let value_expr = try_or_some_err!(iter.next()?.and_then(|raw_expr| raw_expr.resolve(context)));

    if let ValueExpr::MacroInvocation(invocation) = value_expr {
        evaluator.push(try_or_some_err!(invocation.expand()));
    }

    Some(Ok(value_expr))
}

/// For both lists and s-expressions, yields the next sequence value by either continuing a macro
/// evaluation already in progress or reading the next item from the input stream.
fn expand_next_sequence_value<'top, D: Decoder>(
    context: EncodingContextRef<'top>,
    evaluator: &mut MacroEvaluator<'top, D>,
    iter: &mut impl Iterator<Item = IonResult<LazyRawValueExpr<'top, D>>>,
) -> Option<IonResult<LazyExpandedValue<'top, D>>> {
    let mut resolving_iter =
        iter.map(|result| result.and_then(|raw_expr| raw_expr.resolve(context)));
    expand_next_sequence_value_from_resolved(evaluator, &mut resolving_iter)
}

fn expand_next_sequence_value_from_resolved<'top, D: Decoder>(
    evaluator: &mut MacroEvaluator<'top, D>,
    iter: &mut impl Iterator<Item = IonResult<ValueExpr<'top, D>>>,
) -> Option<IonResult<LazyExpandedValue<'top, D>>> {
    loop {
        // If the evaluator's stack is not empty, it's still expanding a macro.
        if !evaluator.is_empty() {
            let value = evaluator.next().transpose();
            if value.is_some() {
                // The `Some` may contain a value or an error; either way, that's the next return value.
                return value;
            }
            // It's possible for a macro to produce zero values. If that happens, we continue on to
            // pull another expression from the list iterator.
        }

        match iter.next() {
            None => return None,
            Some(Ok(ValueExpr::ValueLiteral(value))) => return Some(Ok(value)),
            Some(Ok(ValueExpr::MacroInvocation(invocation))) => {
                evaluator.push(try_or_some_err!(invocation.expand()));
                continue;
            }
            Some(Err(e)) => return Some(Err(e)),
        }
    }
}

/// Represents a sequence (list or sexp) whose contents are being traversed.
/// This is used in `flatten` to store an iterator for each of its
/// sequence arguments in turn.
#[derive(Debug)]
pub enum ExpandedSequenceIterator<'top, D: Decoder> {
    List(ExpandedListIterator<'top, D>),
    SExp(ExpandedSExpIterator<'top, D>),
}

impl<'top, D: Decoder> Iterator for ExpandedSequenceIterator<'top, D> {
    type Item = IonResult<LazyExpandedValue<'top, D>>;

    fn next(&mut self) -> Option<Self::Item> {
        use ExpandedSequenceIterator::*;
        match self {
            List(l) => l.next(),
            SExp(s) => s.next(),
        }
    }
}

#[cfg(test)]
#[cfg(feature = "experimental-tooling-apis")]
mod tests {
    use super::*;
    use crate::{v1_1, ExpandedValueRef, Int, MacroExprKind, Reader};

    fn expect_int<'top, D: Decoder>(
        value_exprs: &mut impl Iterator<Item = IonResult<ValueExpr<'top, D>>>,
        expected_value: impl Into<Int>,
    ) -> IonResult<()> {
        assert!(matches!(
            value_exprs.next().unwrap()?,
            ValueExpr::ValueLiteral(v) if v.read()? == ExpandedValueRef::Int(expected_value.into())
        ));
        Ok(())
    }

    fn expect_eexp<'top, D: Decoder>(
        value_exprs: &mut impl Iterator<Item = IonResult<ValueExpr<'top, D>>>,
        expected_macro_name: &str,
    ) -> IonResult<()> {
        assert!(matches!(
            value_exprs.next().unwrap()?,
            ValueExpr::MacroInvocation(i) if matches!(
                i.kind(),
                MacroExprKind::EExp(e) if e.invoked_macro.name() == Some(expected_macro_name)
            )
        ));
        Ok(())
    }

    #[test]
    fn list_iter_value_expr() -> IonResult<()> {
        let source = r#"
                $ion_1_1
                (:add_macros
                    (macro three_values ()
                        (.values 1 2 3)
                    )
                )
                [0, (:three_values), 4, (:flatten (5 6)), 7]
            "#;
        let mut reader = Reader::new(v1_1::Text, source)?;
        let list = reader.expect_next()?.read()?.expect_list()?;
        let value_exprs = &mut list.expanded_list.value_exprs();

        expect_int(value_exprs, 0)?;
        expect_eexp(value_exprs, "three_values")?;
        expect_int(value_exprs, 1)?;
        expect_int(value_exprs, 2)?;
        expect_int(value_exprs, 3)?;
        expect_int(value_exprs, 4)?;
        expect_eexp(value_exprs, "flatten")?;
        expect_int(value_exprs, 5)?;
        expect_int(value_exprs, 6)?;
        expect_int(value_exprs, 7)?;
        assert!(value_exprs.next().is_none());
        Ok(())
    }

    #[test]
    fn sexp_iter_value_expr() -> IonResult<()> {
        let source = r#"
                $ion_1_1
                (:add_macros
                    (macro three_values ()
                        (.values 1 2 3)
                    )
                )
                (0 (:three_values) 4 (:flatten (5 6)) 7)
            "#;
        let mut reader = Reader::new(v1_1::Text, source)?;
        let sexp = reader.expect_next()?.read()?.expect_sexp()?;
        let value_exprs = &mut sexp.expanded_sexp.value_exprs();

        expect_int(value_exprs, 0)?;
        expect_eexp(value_exprs, "three_values")?;
        expect_int(value_exprs, 1)?;
        expect_int(value_exprs, 2)?;
        expect_int(value_exprs, 3)?;
        expect_int(value_exprs, 4)?;
        expect_eexp(value_exprs, "flatten")?;
        expect_int(value_exprs, 5)?;
        expect_int(value_exprs, 6)?;
        expect_int(value_exprs, 7)?;
        assert!(value_exprs.next().is_none());
        Ok(())
    }
}