diff --git a/src/lazy/any_encoding.rs b/src/lazy/any_encoding.rs index 98423b16..6c908239 100644 --- a/src/lazy/any_encoding.rs +++ b/src/lazy/any_encoding.rs @@ -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; @@ -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> { @@ -77,9 +76,8 @@ impl<'data> From> 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 { @@ -99,7 +97,10 @@ impl<'data> RawMacroInvocation<'data, AnyEncoding> for LazyRawAnyEExpression<'da } } - fn resolve<'top>(self, context: EncodingContext<'top>) -> IonResult> + fn resolve<'top>( + self, + context: EncodingContext<'top>, + ) -> IonResult> where 'data: 'top, { @@ -119,7 +120,7 @@ impl<'data> RawMacroInvocation<'data, AnyEncoding> for LazyRawAnyEExpression<'da pub enum LazyRawAnyMacroArgsIteratorKind<'data> { Text_1_1( - as RawMacroInvocation< + as RawEExpression< 'data, TextEncoding_1_1, >>::RawArgumentsIterator<'data>, diff --git a/src/lazy/decoder.rs b/src/lazy/decoder.rs index 395ccf63..872d00f5 100644 --- a/src/lazy/decoder.rs +++ b/src/lazy/decoder.rs @@ -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; @@ -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>>; /// 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. @@ -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 } @@ -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> + fn resolve<'top>( + self, + context: EncodingContext<'top>, + ) -> IonResult> where 'data: 'top, { diff --git a/src/lazy/encoding.rs b/src/lazy/encoding.rs index 1e366afc..e387073b 100644 --- a/src/lazy/encoding.rs +++ b/src/lazy/encoding.rs @@ -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; @@ -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 { @@ -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 { @@ -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. diff --git a/src/lazy/expanded/e_expression.rs b/src/lazy/expanded/e_expression.rs index fd597e1b..d7b120cd 100644 --- a/src/lazy/expanded/e_expression.rs +++ b/src/lazy/expanded/e_expression.rs @@ -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; @@ -41,21 +38,12 @@ 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(), @@ -63,11 +51,8 @@ impl< } } -impl< - 'top, - 'data: 'top, - D: LazyDecoder<'data, MacroInvocation<'top> = EExpression<'top, 'data, D>>, - > From> for MacroExpr<'top, 'data, D> +impl<'top, 'data: 'top, D: LazyDecoder<'data>> From> + for MacroExpr<'top, 'data, D> { fn from(value: EExpression<'top, 'data, D>) -> Self { MacroExpr::EExp(value) @@ -76,7 +61,7 @@ impl< pub struct EExpressionArgsIterator<'top, 'data, D: LazyDecoder<'data>> { context: EncodingContext<'top>, - raw_args: >::RawArgumentsIterator<'data>, + raw_args: >::RawArgumentsIterator<'data>, } impl<'top, 'data: 'top, D: LazyDecoder<'data>> Iterator diff --git a/src/lazy/expanded/macro_evaluator.rs b/src/lazy/expanded/macro_evaluator.rs index 94c43e76..de6a169e 100644 --- a/src/lazy/expanded/macro_evaluator.rs +++ b/src/lazy/expanded/macro_evaluator.rs @@ -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::{ @@ -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>> 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>; @@ -53,35 +48,22 @@ pub trait RawMacroInvocation<'data, D: LazyDecoder<'data>>: Debug + Copy + Clone fn resolve<'top>( self, context: EncodingContext<'top>, - ) -> IonResult> + ) -> IonResult> 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> -{ - type ArgumentsIterator: Iterator>>; - - 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) => { - as MacroInvocation<'top, 'data, D>>::id(m) - } + MacroExpr::TemplateMacro(m) => m.id(), MacroExpr::EExp(e) => e.id(), } } @@ -94,9 +76,7 @@ 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 } } @@ -104,7 +84,7 @@ impl<'top, 'data: 'top, D: LazyDecoder<'data>> MacroExpr<'top, 'data, D> { pub enum MacroExprArgsKind<'top, 'data: 'top, D: LazyDecoder<'data>> { Macro(TemplateMacroInvocationArgsIterator<'top, 'data, D>), - EExp( as MacroInvocation<'top, 'data, D>>::ArgumentsIterator), + EExp(EExpressionArgsIterator<'top, 'data, D>), } pub struct MacroExprArgsIterator<'top, 'data: 'top, D: LazyDecoder<'data>> { diff --git a/src/lazy/expanded/mod.rs b/src/lazy/expanded/mod.rs index 132e5054..d3e90ab8 100644 --- a/src/lazy/expanded/mod.rs +++ b/src/lazy/expanded/mod.rs @@ -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; diff --git a/src/lazy/expanded/sequence.rs b/src/lazy/expanded/sequence.rs index 77bb9fb8..edc10e99 100644 --- a/src/lazy/expanded/sequence.rs +++ b/src/lazy/expanded/sequence.rs @@ -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, }; diff --git a/src/lazy/expanded/struct.rs b/src/lazy/expanded/struct.rs index de6478fe..28ccc3c8 100644 --- a/src/lazy/expanded/struct.rs +++ b/src/lazy/expanded/struct.rs @@ -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, diff --git a/src/lazy/expanded/template.rs b/src/lazy/expanded/template.rs index 5f167856..b33efd76 100644 --- a/src/lazy/expanded/template.rs +++ b/src/lazy/expanded/template.rs @@ -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::{ @@ -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>, @@ -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 { diff --git a/src/lazy/never.rs b/src/lazy/never.rs index bbe9a79d..12d272ea 100644 --- a/src/lazy/never.rs +++ b/src/lazy/never.rs @@ -1,10 +1,8 @@ use std::fmt::Debug; use crate::lazy::decoder::{LazyDecoder, LazyRawValueExpr}; -use crate::lazy::expanded::macro_evaluator::{ - ArgumentExpr, MacroExpr, MacroInvocation, RawMacroInvocation, -}; -use crate::lazy::expanded::sequence::Environment; +use crate::lazy::expanded::e_expression::EExpression; +use crate::lazy::expanded::macro_evaluator::{MacroExpr, RawEExpression}; use crate::lazy::expanded::EncodingContext; use crate::lazy::text::raw::v1_1::reader::MacroIdRef; use crate::IonResult; @@ -17,10 +15,9 @@ pub enum Never { // Ion 1.0 uses `Never` as a placeholder type for MacroInvocation. // The compiler should optimize these methods away. -impl<'data, D: LazyDecoder<'data>> RawMacroInvocation<'data, D> for Never { +impl<'data, D: LazyDecoder<'data>> RawEExpression<'data, D> for Never { // These use Box to avoid defining yet another placeholder type. type RawArgumentsIterator<'a> = Box>>>; - type MacroInvocation<'top> = Never where 'data: 'top; fn id(&self) -> MacroIdRef<'data> { unreachable!("macro in Ion 1.0 (method: id)") @@ -33,7 +30,7 @@ impl<'data, D: LazyDecoder<'data>> RawMacroInvocation<'data, D> for Never { fn resolve<'top>( self, _context: EncodingContext<'top>, - ) -> IonResult> + ) -> IonResult> where 'data: 'top, { @@ -46,15 +43,3 @@ impl<'top, 'data: 'top, D: LazyDecoder<'data>> From for MacroExpr<'top, ' unreachable!("macro in Ion 1.0 (method: into)") } } - -impl<'top, 'data: 'top, D: LazyDecoder<'data>> MacroInvocation<'top, 'data, D> for Never { - type ArgumentsIterator = Box>>>; - - fn id(&self) -> MacroIdRef<'data> { - unreachable!("macro in Ion 1.0 (method: id)"); - } - - fn arguments(&self, _environment: Environment<'top, 'data, D>) -> Self::ArgumentsIterator { - unreachable!("macro in Ion 1.0 (method: arguments)"); - } -}