Skip to content

Commit

Permalink
Removed MacroInvocation trait, renamed RawMacroInvocation to RawEExpr…
Browse files Browse the repository at this point in the history
…ession
  • Loading branch information
zslayton committed Oct 17, 2023
1 parent 9150196 commit 368cfb4
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 111 deletions.
13 changes: 7 additions & 6 deletions src/lazy/any_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::lazy::decoder::{
};
use crate::lazy::encoding::{BinaryEncoding_1_0, TextEncoding_1_0, TextEncoding_1_1};
use crate::lazy::expanded::e_expression::EExpression;
use crate::lazy::expanded::macro_evaluator::RawMacroInvocation;
use crate::lazy::expanded::macro_evaluator::RawEExpression;
use crate::lazy::expanded::EncodingContext;
use crate::lazy::never::Never;
use crate::lazy::raw_stream_item::RawStreamItem;
Expand Down Expand Up @@ -52,7 +52,6 @@ impl<'data> LazyDecoder<'data> for AnyEncoding {
type Struct = LazyRawAnyStruct<'data>;
type AnnotationsIterator = RawAnyAnnotationsIterator<'data>;
type RawMacroInvocation = LazyRawAnyEExpression<'data>;
type MacroInvocation<'top> = EExpression<'top, 'data, AnyEncoding> where 'data: 'top;
}
#[derive(Debug, Copy, Clone)]
pub struct LazyRawAnyEExpression<'data> {
Expand All @@ -77,9 +76,8 @@ impl<'data> From<RawTextEExpression_1_1<'data>> for LazyRawAnyEExpression<'data>
}
}

impl<'data> RawMacroInvocation<'data, AnyEncoding> for LazyRawAnyEExpression<'data> {
impl<'data> RawEExpression<'data, AnyEncoding> for LazyRawAnyEExpression<'data> {
type RawArgumentsIterator<'a> = LazyRawAnyMacroArgsIterator<'data> where Self: 'a;
type MacroInvocation<'top> = EExpression<'top, 'data, AnyEncoding> where 'data: 'top;

fn id(&self) -> MacroIdRef<'data> {
match self.encoding {
Expand All @@ -99,7 +97,10 @@ impl<'data> RawMacroInvocation<'data, AnyEncoding> for LazyRawAnyEExpression<'da
}
}

fn resolve<'top>(self, context: EncodingContext<'top>) -> IonResult<Self::MacroInvocation<'top>>
fn resolve<'top>(
self,
context: EncodingContext<'top>,
) -> IonResult<EExpression<'top, 'data, AnyEncoding>>
where
'data: 'top,
{
Expand All @@ -119,7 +120,7 @@ impl<'data> RawMacroInvocation<'data, AnyEncoding> for LazyRawAnyEExpression<'da

pub enum LazyRawAnyMacroArgsIteratorKind<'data> {
Text_1_1(
<RawTextEExpression_1_1<'data> as RawMacroInvocation<
<RawTextEExpression_1_1<'data> as RawEExpression<
'data,
TextEncoding_1_1,
>>::RawArgumentsIterator<'data>,
Expand Down
18 changes: 8 additions & 10 deletions src/lazy/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::fmt::Debug;

use crate::lazy::encoding::TextEncoding_1_1;
use crate::lazy::expanded::e_expression::TextEExpression_1_1;
use crate::lazy::expanded::macro_evaluator::{MacroInvocation, RawMacroInvocation};
use crate::lazy::expanded::e_expression::{EExpression, TextEExpression_1_1};
use crate::lazy::expanded::macro_evaluator::RawEExpression;
use crate::lazy::expanded::EncodingContext;
use crate::lazy::raw_stream_item::RawStreamItem;
use crate::lazy::raw_value_ref::RawValueRef;
Expand Down Expand Up @@ -31,10 +31,7 @@ pub trait LazyDecoder<'data>: 'static + Sized + Debug + Clone + Copy {
/// An iterator over the annotations on the input stream's values.
type AnnotationsIterator: Iterator<Item = IonResult<RawSymbolTokenRef<'data>>>;
/// An e-expression invoking a macro. (Ion 1.1+)
type RawMacroInvocation: RawMacroInvocation<'data, Self>;
type MacroInvocation<'top>: MacroInvocation<'top, 'data, Self>
where
'data: 'top;
type RawMacroInvocation: RawEExpression<'data, Self>;
}

/// An expression found in value position in either serialized Ion or a template.
Expand Down Expand Up @@ -182,11 +179,9 @@ pub(crate) mod private {

// TODO: Everything should use LazyRawValueExpr in the RMI impl?

impl<'data> RawMacroInvocation<'data, TextEncoding_1_1> for RawTextEExpression_1_1<'data> {
impl<'data> RawEExpression<'data, TextEncoding_1_1> for RawTextEExpression_1_1<'data> {
type RawArgumentsIterator<'a> = RawTextSExpIterator_1_1<'data> where Self: 'a;

type MacroInvocation<'top> = TextEExpression_1_1<'top, 'data> where 'data: 'top;

fn id(&self) -> MacroIdRef<'data> {
self.id
}
Expand All @@ -195,7 +190,10 @@ impl<'data> RawMacroInvocation<'data, TextEncoding_1_1> for RawTextEExpression_1
RawTextSExpIterator_1_1::new(self.arguments_bytes())
}

fn resolve<'top>(self, context: EncodingContext<'top>) -> IonResult<Self::MacroInvocation<'top>>
fn resolve<'top>(
self,
context: EncodingContext<'top>,
) -> IonResult<EExpression<'top, 'data, TextEncoding_1_1>>
where
'data: 'top,
{
Expand Down
4 changes: 0 additions & 4 deletions src/lazy/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::lazy::binary::raw::reader::LazyRawBinaryReader;
use crate::lazy::binary::raw::sequence::{LazyRawBinaryList, LazyRawBinarySExp};
use crate::lazy::binary::raw::value::LazyRawBinaryValue;
use crate::lazy::decoder::LazyDecoder;
use crate::lazy::expanded::e_expression::TextEExpression_1_1;
use crate::lazy::never::Never;
use crate::lazy::text::raw::r#struct::LazyRawTextStruct_1_0;
use crate::lazy::text::raw::reader::LazyRawTextReader_1_0;
Expand Down Expand Up @@ -99,7 +98,6 @@ impl<'data> LazyDecoder<'data> for BinaryEncoding_1_0 {
type AnnotationsIterator = RawBinaryAnnotationsIterator<'data>;
// Macros are not supported in Ion 1.0
type RawMacroInvocation = Never;
type MacroInvocation<'top> = Never where 'data: 'top;
}

impl<'data> LazyDecoder<'data> for TextEncoding_1_0 {
Expand All @@ -111,7 +109,6 @@ impl<'data> LazyDecoder<'data> for TextEncoding_1_0 {
type AnnotationsIterator = RawTextAnnotationsIterator<'data>;
// Macros are not supported in Ion 1.0
type RawMacroInvocation = Never;
type MacroInvocation<'top> = Never where 'data: 'top;
}

impl<'data> LazyDecoder<'data> for TextEncoding_1_1 {
Expand All @@ -122,7 +119,6 @@ impl<'data> LazyDecoder<'data> for TextEncoding_1_1 {
type Struct = LazyRawTextStruct_1_1<'data>;
type AnnotationsIterator = RawTextAnnotationsIterator<'data>;
type RawMacroInvocation = RawTextEExpression_1_1<'data>;
type MacroInvocation<'top> = TextEExpression_1_1<'top, 'data> where 'data: 'top;
}

/// Marker trait for types that represent value literals in an Ion stream of some encoding.
Expand Down
29 changes: 7 additions & 22 deletions src/lazy/expanded/e_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@

use crate::lazy::decoder::{LazyDecoder, LazyRawValueExpr};
use crate::lazy::encoding::TextEncoding_1_1;
use crate::lazy::expanded::macro_evaluator::{
ArgumentExpr, MacroExpr, MacroInvocation, RawMacroInvocation,
};
use crate::lazy::expanded::macro_evaluator::{ArgumentExpr, MacroExpr, RawEExpression};
use crate::lazy::expanded::macro_table::MacroRef;
use crate::lazy::expanded::sequence::Environment;
use crate::lazy::expanded::{EncodingContext, LazyExpandedValue};
use crate::lazy::text::raw::v1_1::reader::MacroIdRef;
use crate::IonResult;
Expand Down Expand Up @@ -41,33 +38,21 @@ impl<'top, 'data, D: LazyDecoder<'data>> EExpression<'top, 'data, D> {
}
}

impl<
'top,
'data: 'top,
D: LazyDecoder<'data, MacroInvocation<'top> = EExpression<'top, 'data, D>>,
> MacroInvocation<'top, 'data, D> for EExpression<'top, 'data, D>
{
type ArgumentsIterator = EExpressionArgsIterator<'top, 'data, D>;

fn id(&self) -> MacroIdRef<'top> {
impl<'top, 'data: 'top, D: LazyDecoder<'data>> EExpression<'top, 'data, D> {
pub fn id(&self) -> MacroIdRef<'top> {
self.raw_invocation.id()
}

fn arguments(&self, _environment: Environment<'top, 'data, D>) -> Self::ArgumentsIterator {
// E-expressions do not have an environment, so we can ignore/discard that parameter.
// Callers pass `Environment::empty()` in E-expression contexts anyway.
pub fn arguments(&self) -> EExpressionArgsIterator<'top, 'data, D> {
EExpressionArgsIterator {
context: self.context,
raw_args: self.raw_invocation.raw_arguments(),
}
}
}

impl<
'top,
'data: 'top,
D: LazyDecoder<'data, MacroInvocation<'top> = EExpression<'top, 'data, D>>,
> From<EExpression<'top, 'data, D>> for MacroExpr<'top, 'data, D>
impl<'top, 'data: 'top, D: LazyDecoder<'data>> From<EExpression<'top, 'data, D>>
for MacroExpr<'top, 'data, D>
{
fn from(value: EExpression<'top, 'data, D>) -> Self {
MacroExpr::EExp(value)
Expand All @@ -76,7 +61,7 @@ impl<

pub struct EExpressionArgsIterator<'top, 'data, D: LazyDecoder<'data>> {
context: EncodingContext<'top>,
raw_args: <D::RawMacroInvocation as RawMacroInvocation<'data, D>>::RawArgumentsIterator<'data>,
raw_args: <D::RawMacroInvocation as RawEExpression<'data, D>>::RawArgumentsIterator<'data>,
}

impl<'top, 'data: 'top, D: LazyDecoder<'data>> Iterator
Expand Down
38 changes: 9 additions & 29 deletions src/lazy/expanded/macro_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::fmt::{Debug, Formatter};
use std::marker::PhantomData;

use crate::lazy::decoder::{LazyDecoder, LazyRawValueExpr};
use crate::lazy::expanded::e_expression::{EExpression, EExpressionArgsIterator};
use crate::lazy::expanded::macro_table::MacroKind;
use crate::lazy::expanded::sequence::Environment;
use crate::lazy::expanded::template::{
Expand All @@ -30,20 +31,14 @@ use crate::result::IonFailure;
use crate::{IonError, IonResult, RawSymbolTokenRef};
use bumpalo::collections::{String as BumpString, Vec as BumpVec};

/// A syntactic entity that represents the invocation of a macro in some context.
///
/// This entity may be an item from a binary stream, a text stream, or a template definition.
/// Implementors must specify how their type can be mapped to a macro ID and a sequence of arguments.
pub trait RawMacroInvocation<'data, D: LazyDecoder<'data>>: Debug + Copy + Clone {
/// The syntactic entity in format `D` that represents an e-expression. This expression has not
/// yet been resolved in the current encoding context.
pub trait RawEExpression<'data, D: LazyDecoder<'data>>: Debug + Copy + Clone {
/// An iterator that yields the macro invocation's arguments in order.
type RawArgumentsIterator<'a>: Iterator<Item = IonResult<LazyRawValueExpr<'data, D>>>
where
Self: 'a;

type MacroInvocation<'top>: MacroInvocation<'top, 'data, D>
where
'data: 'top;

/// The macro name or address specified at the head of this macro invocation.
fn id(&self) -> MacroIdRef<'data>;

Expand All @@ -53,35 +48,22 @@ pub trait RawMacroInvocation<'data, D: LazyDecoder<'data>>: Debug + Copy + Clone
fn resolve<'top>(
self,
context: EncodingContext<'top>,
) -> IonResult<Self::MacroInvocation<'top>>
) -> IonResult<EExpression<'top, 'data, D>>
where
'data: 'top;
}

/// A macro invocation (either an e-expression or a template macro) that has been resolved
/// in the current encoding context.
pub trait MacroInvocation<'top, 'data: 'top, D: LazyDecoder<'data>>:
Debug + Copy + Clone + Into<MacroExpr<'top, 'data, D>>
{
type ArgumentsIterator: Iterator<Item = IonResult<ArgumentExpr<'top, 'data, D>>>;

fn id(&self) -> MacroIdRef<'top>;
fn arguments(&self, environment: Environment<'top, 'data, D>) -> Self::ArgumentsIterator;
}

// Either
#[derive(Copy, Clone, Debug)]
pub enum MacroExpr<'top, 'data: 'top, D: LazyDecoder<'data>> {
TemplateMacro(TemplateMacroInvocation<'top>),
EExp(D::MacroInvocation<'top>),
EExp(EExpression<'top, 'data, D>),
}

impl<'top, 'data: 'top, D: LazyDecoder<'data>> MacroExpr<'top, 'data, D> {
fn id(&self) -> MacroIdRef {
match &self {
MacroExpr::TemplateMacro(m) => {
<TemplateMacroInvocation<'top> as MacroInvocation<'top, 'data, D>>::id(m)
}
MacroExpr::TemplateMacro(m) => m.id(),
MacroExpr::EExp(e) => e.id(),
}
}
Expand All @@ -94,17 +76,15 @@ impl<'top, 'data: 'top, D: LazyDecoder<'data>> MacroExpr<'top, 'data, D> {
MacroExpr::TemplateMacro(m) => {
MacroExprArgsKind::<'top, 'data, D>::Macro(m.arguments(environment))
}
MacroExpr::EExp(e) => {
MacroExprArgsKind::<'top, 'data, D>::EExp(e.arguments(Environment::empty()))
}
MacroExpr::EExp(e) => MacroExprArgsKind::<'top, 'data, D>::EExp(e.arguments()),
};
MacroExprArgsIterator { source: args_kind }
}
}

pub enum MacroExprArgsKind<'top, 'data: 'top, D: LazyDecoder<'data>> {
Macro(TemplateMacroInvocationArgsIterator<'top, 'data, D>),
EExp(<D::MacroInvocation<'top> as MacroInvocation<'top, 'data, D>>::ArgumentsIterator),
EExp(EExpressionArgsIterator<'top, 'data, D>),
}

pub struct MacroExprArgsIterator<'top, 'data: 'top, D: LazyDecoder<'data>> {
Expand Down
2 changes: 1 addition & 1 deletion src/lazy/expanded/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use crate::element::iterators::SymbolsIterator;
use crate::lazy::bytes_ref::BytesRef;
use crate::lazy::decoder::{LazyDecoder, LazyRawReader, LazyRawValue};
use crate::lazy::encoding::RawValueLiteral;
use crate::lazy::expanded::macro_evaluator::{MacroEvaluator, RawMacroInvocation};
use crate::lazy::expanded::macro_evaluator::{MacroEvaluator, RawEExpression};
use crate::lazy::expanded::macro_table::MacroTable;
use crate::lazy::expanded::r#struct::LazyExpandedStruct;
use crate::lazy::expanded::sequence::Environment;
Expand Down
2 changes: 1 addition & 1 deletion src/lazy/expanded/sequence.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::element::iterators::SymbolsIterator;
use crate::lazy::decoder::{LazyDecoder, LazyRawSequence, LazyRawValueExpr, RawValueExpr};
use crate::lazy::expanded::macro_evaluator::{ArgumentExpr, MacroEvaluator, RawMacroInvocation};
use crate::lazy::expanded::macro_evaluator::{ArgumentExpr, MacroEvaluator, RawEExpression};
use crate::lazy::expanded::template::{
AnnotationsRange, ExprRange, TemplateMacroRef, TemplateSequenceIterator,
};
Expand Down
2 changes: 1 addition & 1 deletion src/lazy/expanded/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::element::iterators::SymbolsIterator;
use crate::lazy::decoder::{
LazyDecoder, LazyRawFieldExpr, LazyRawStruct, RawFieldExpr, RawValueExpr,
};
use crate::lazy::expanded::macro_evaluator::{MacroEvaluator, MacroExpr, RawMacroInvocation};
use crate::lazy::expanded::macro_evaluator::{MacroEvaluator, MacroExpr, RawEExpression};
use crate::lazy::expanded::sequence::Environment;
use crate::lazy::expanded::template::{
AnnotationsRange, ExprRange, TemplateMacroRef, TemplateStructRawFieldsIterator,
Expand Down
23 changes: 5 additions & 18 deletions src/lazy/expanded/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use std::marker::PhantomData;
use std::ops::{Deref, Range};

use crate::lazy::decoder::{LazyDecoder, RawFieldExpr, RawValueExpr};
use crate::lazy::expanded::macro_evaluator::{
ArgumentExpr, MacroEvaluator, MacroExpr, MacroInvocation,
};
use crate::lazy::expanded::macro_evaluator::{ArgumentExpr, MacroEvaluator, MacroExpr};
use crate::lazy::expanded::macro_table::MacroRef;
use crate::lazy::expanded::sequence::Environment;
use crate::lazy::expanded::{
Expand Down Expand Up @@ -620,6 +618,10 @@ impl<'top> TemplateMacroInvocation<'top> {
arg_expressions,
}
}

pub fn id(&self) -> MacroIdRef<'top> {
MacroIdRef::LocalAddress(self.invoked_macro.address())
}
pub fn arguments<'data, D: LazyDecoder<'data>>(
&self,
environment: Environment<'top, 'data, D>,
Expand Down Expand Up @@ -709,21 +711,6 @@ impl<'top, 'data, D: LazyDecoder<'data>> Iterator
}
}

impl<'top, 'data: 'top, D: LazyDecoder<'data>> MacroInvocation<'top, 'data, D>
for TemplateMacroInvocation<'top>
{
type ArgumentsIterator = TemplateMacroInvocationArgsIterator<'top, 'data, D>;

fn id(&self) -> MacroIdRef<'data> {
MacroIdRef::LocalAddress(self.invoked_macro.address())
}

fn arguments(&self, environment: Environment<'top, 'data, D>) -> Self::ArgumentsIterator {
// Delegate to the inherent impl on TemplateMacroInvocation
self.arguments(environment)
}
}

/// A reference to a variable in a template body.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct TemplateBodyVariableReference {
Expand Down
Loading

0 comments on commit 368cfb4

Please sign in to comment.