-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implements encoding directive macros and comment macro #973
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,15 +2,27 @@ | |
// SPDX-License-Identifier: Apache-2.0 | ||
package com.amazon.ion.impl.macro | ||
|
||
import com.amazon.ion.impl.* | ||
import com.amazon.ion.impl.SystemSymbols_1_1.* | ||
import com.amazon.ion.impl.macro.ExpressionBuilderDsl.Companion.templateBody | ||
import com.amazon.ion.impl.macro.ParameterFactory.exactlyOneFlexInt | ||
import com.amazon.ion.impl.macro.ParameterFactory.exactlyOneTagged | ||
import com.amazon.ion.impl.macro.ParameterFactory.oneToManyTagged | ||
import com.amazon.ion.impl.macro.ParameterFactory.zeroOrOneTagged | ||
import com.amazon.ion.impl.macro.ParameterFactory.zeroToManyTagged | ||
|
||
/** | ||
* Macros that are built in, rather than being defined by a template. | ||
*/ | ||
enum class SystemMacro(val id: Byte, val macroName: String, override val signature: List<Macro.Parameter>) : Macro { | ||
enum class SystemMacro(val id: Byte, val macroName: String, override val signature: List<Macro.Parameter>, override val body: List<Expression.TemplateBodyExpression>? = null) : Macro { | ||
// Technically not system macros, but special forms. However, it's easier to model them as if they are macros in TDL. | ||
// We give them an ID of -1 to distinguish that they are not addressable outside TDL. | ||
IfNone(-1, "if_none", listOf(zeroToManyTagged("stream"), zeroToManyTagged("true_branch"), zeroToManyTagged("false_branch"))), | ||
IfSome(-1, "if_some", listOf(zeroToManyTagged("stream"), zeroToManyTagged("true_branch"), zeroToManyTagged("false_branch"))), | ||
IfSingle(-1, "if_single", listOf(zeroToManyTagged("stream"), zeroToManyTagged("true_branch"), zeroToManyTagged("false_branch"))), | ||
IfMulti(-1, "if_multi", listOf(zeroToManyTagged("stream"), zeroToManyTagged("true_branch"), zeroToManyTagged("false_branch"))), | ||
Comment on lines
+18
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗺️ These were moved to the beginning of the enum because |
||
|
||
// The real macros | ||
None(0, "none", emptyList()), | ||
Values(1, "values", listOf(zeroToManyTagged("values"))), | ||
Annotate(2, "annotate", listOf(zeroToManyTagged("ann"), exactlyOneTagged("value"))), | ||
|
@@ -19,8 +31,150 @@ | |
MakeBlob(5, "make_blob", listOf(zeroToManyTagged("bytes"))), | ||
MakeDecimal(6, "make_decimal", listOf(exactlyOneFlexInt("coefficient"), exactlyOneFlexInt("exponent"))), | ||
|
||
/** | ||
* ```ion | ||
* (macro set_symbols (symbols*) | ||
* $ion_encoding::( | ||
* (symbol_table [(%symbols)]) | ||
* (macro_table $ion_encoding) | ||
* )) | ||
* ``` | ||
*/ | ||
SetSymbols( | ||
11, "set_symbols", listOf(zeroToManyTagged("symbols")), | ||
templateBody { | ||
annotated(ION_ENCODING, ::sexp) { | ||
sexp { | ||
symbol(SYMBOL_TABLE) | ||
list { variable(0) } | ||
} | ||
sexp { | ||
symbol(MACRO_TABLE) | ||
symbol(ION_ENCODING) | ||
} | ||
} | ||
} | ||
), | ||
|
||
/** | ||
* ```ion | ||
* (macro add_symbols (symbols*) | ||
* $ion_encoding::( | ||
* (symbol_table $ion_encoding [(%symbols)]) | ||
* (macro_table $ion_encoding) | ||
* )) | ||
* ``` | ||
*/ | ||
AddSymbols( | ||
12, "add_symbols", listOf(zeroToManyTagged("symbols")), | ||
templateBody { | ||
annotated(ION_ENCODING, ::sexp) { | ||
sexp { | ||
symbol(SYMBOL_TABLE) | ||
symbol(ION_ENCODING) | ||
list { variable(0) } | ||
} | ||
sexp { | ||
symbol(MACRO_TABLE) | ||
symbol(ION_ENCODING) | ||
} | ||
} | ||
} | ||
), | ||
|
||
/** | ||
* ```ion | ||
* (macro set_macros (macros*) | ||
* $ion_encoding::( | ||
* (symbol_table $ion_encoding) | ||
* (macro_table (%macros)) | ||
* )) | ||
* ``` | ||
*/ | ||
SetMacros( | ||
13, "set_macros", listOf(zeroToManyTagged("macros")), | ||
templateBody { | ||
annotated(ION_ENCODING, ::sexp) { | ||
sexp { | ||
symbol(SYMBOL_TABLE) | ||
symbol(ION_ENCODING) | ||
} | ||
sexp { | ||
symbol(MACRO_TABLE) | ||
variable(0) | ||
} | ||
} | ||
} | ||
), | ||
|
||
/** | ||
* ```ion | ||
* (macro add_macros (macros*) | ||
* $ion_encoding::( | ||
* (symbol_table $ion_encoding) | ||
* (macro_table $ion_encoding (%macros)) | ||
* )) | ||
* ``` | ||
*/ | ||
AddMacros( | ||
14, "add_macros", listOf(zeroToManyTagged("macros")), | ||
templateBody { | ||
annotated(ION_ENCODING, ::sexp) { | ||
sexp { | ||
symbol(SYMBOL_TABLE) | ||
symbol(ION_ENCODING) | ||
} | ||
sexp { | ||
symbol(MACRO_TABLE) | ||
symbol(ION_ENCODING) | ||
variable(0) | ||
} | ||
} | ||
} | ||
), | ||
|
||
/** | ||
* ```ion | ||
* (macro use (catalog_key version?) | ||
* $ion_encoding::( | ||
* (import the_module (%catalog_key) (.if_none (%version) 1 (%version))) | ||
* (symbol_table $ion_encoding the_module) | ||
* (macro_table $ion_encoding the_module) | ||
* )) | ||
* ``` | ||
*/ | ||
Use( | ||
15, "use", listOf(exactlyOneTagged("catalog_key"), zeroOrOneTagged("version")), | ||
templateBody { | ||
val theModule = _Private_Utils.newSymbolToken("the_module") | ||
annotated(ION_ENCODING, ::sexp) { | ||
sexp { | ||
symbol(IMPORT) | ||
symbol(theModule) | ||
variable(0) | ||
macro(IfNone) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we're adding |
||
variable(1) | ||
int(1) | ||
variable(1) | ||
} | ||
} | ||
sexp { | ||
symbol(SYMBOL_TABLE) | ||
symbol(ION_ENCODING) | ||
symbol(theModule) | ||
} | ||
sexp { | ||
symbol(MACRO_TABLE) | ||
symbol(ION_ENCODING) | ||
symbol(theModule) | ||
} | ||
} | ||
} | ||
), | ||
|
||
Repeat(17, "repeat", listOf(exactlyOneTagged("n"), oneToManyTagged("value"))), | ||
|
||
Comment(21, "comment", listOf(zeroToManyTagged("values")), templateBody { macro(None) {} }), | ||
MakeField( | ||
22, "make_field", | ||
listOf( | ||
|
@@ -29,17 +183,14 @@ | |
), | ||
|
||
// TODO: Other system macros | ||
|
||
// Technically not system macros, but special forms. However, it's easier to model them as if they are macros in TDL. | ||
// We give them an ID of -1 to distinguish that they are not addressable outside TDL. | ||
IfNone(-1, "if_none", listOf(zeroToManyTagged("stream"), zeroToManyTagged("true_branch"), zeroToManyTagged("false_branch"))), | ||
IfSome(-1, "if_some", listOf(zeroToManyTagged("stream"), zeroToManyTagged("true_branch"), zeroToManyTagged("false_branch"))), | ||
IfSingle(-1, "if_single", listOf(zeroToManyTagged("stream"), zeroToManyTagged("true_branch"), zeroToManyTagged("false_branch"))), | ||
IfMulti(-1, "if_multi", listOf(zeroToManyTagged("stream"), zeroToManyTagged("true_branch"), zeroToManyTagged("false_branch"))), | ||
; | ||
|
||
override val dependencies: List<Macro> | ||
get() = emptyList() | ||
get() = body | ||
?.filterIsInstance<Expression.MacroInvocation>() | ||
?.map(Expression.MacroInvocation::macro) | ||
?.distinct() | ||
?: emptyList() | ||
|
||
companion object { | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗺️ Rather than throwing exceptions, these now return
null
, which is allowed by the interface. I don't know why I didn't do that to begin with, but now it means we can use e.g._Private_IonSystem.iterate()
on aMacroEvaluatorAsIonReader
instance.