-
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
Various changes in support of system macros #981
Various changes in support of system macros #981
Conversation
* Add a definition of `default` * Use qualified module reference for system macros where relevant * Support use of system macros in TDL * Updating system symbols to match spec as of now * Associate SystemMacro values directly with SystemSymbols_1_1 values * Add SystemSymbol lookup by name
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## ion-11-encoding #981 +/- ##
==================================================
Coverage ? 70.56%
Complexity ? 7083
==================================================
Files ? 201
Lines ? 28198
Branches ? 5037
==================================================
Hits ? 19899
Misses ? 6710
Partials ? 1589 ☔ View full report in Codecov by Sentry. |
return SystemSymbols_1_1.get((int) id).getToken(); | ||
SystemSymbols_1_1 systemSymbol = SystemSymbols_1_1.get((int) id); | ||
return (systemSymbol == null) ? new SymbolTokenImpl(-1) : systemSymbol.getToken(); |
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.
🗺️ This needs attention- is this the right thing to do? Formerly, SystemSymbols_1_1.get(int)
was documented to return null
if the supplied int
did not correspond to a system symbol, but in practice it would throw an ArrayIndexOutOfBoundsException
.
I added a method to lookup a system symbol by text, where ArrayIndexOutOfBoundsException
does not make sense for an unmapped value but yielding null
does, so I brought both lookups to the same contract.
Modifying the method to match the documentation exposed the nullability of the return value in a way that triggered ktlint
to force me to handle the null
case here... is this a sensible response? Should I return null
instead? Throw an exception?
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.
It looks to me like throwing is the right thing here. This is only called in places where the encoding indicates a system symbol is present. It would be invalid Ion for that encoding to occur without a valid system symbol mapping.
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.
We probably need to throw an IonException
here because if we have something that claims to be a system symbol, but the ID is out of range for the system symbol table, it's invalid Ion data.
@JvmStatic | ||
private val BY_NAME = ALL_VALUES.fold(HashMap<String, SystemSymbols_1_1>(ALL_VALUES.size)) { map, s -> | ||
check(map.put(s.text, s) == null) { "Duplicate system symbol text: ${s.id}=${s.text}" } | ||
map | ||
} |
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.
🗺️ I added this validation after duplicating system symbol text with a GitHub issue URL caused a small number of tests in EncodingDirectiveCompilationTest to fail in a way that took me a few minutes to track down.
is SystemMacro -> exportSystemMacro(macro, name) | ||
is SystemMacro -> { | ||
if (name != macro.macroName) { | ||
exportSystemMacro(macro, name) | ||
} | ||
// Else, no need to export the macro since it's already known by the desired name | ||
} |
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.
🗺️ We still don't have support for reading/compiling exported symbols, but that will come later.
private fun startSystemMacro(macro: SystemMacro) { | ||
var macroName = macro.macroName | ||
// TODO: Replace `indexOf` with something that is not O(n) time complexity. | ||
var id = macroNames.indexOf(macroName) | ||
if (id < 0) { | ||
// If the name is not in use, put it into the user macro table | ||
id = getOrAssignMacroAddressAndName(macroName, macro) | ||
} | ||
if (macrosById[id] == macro) { | ||
// The name and id in the local table refers to the system macro we want to invoke, | ||
// so invoke as a local symbol since it's almost always shorter. | ||
startMacro(macroName, id, macro) | ||
} else { | ||
// The name is already being used by something else, so invoke using the system macro syntax. | ||
userData.stepInEExp(macro) | ||
} | ||
} | ||
private fun startSystemMacro(macro: SystemMacro) = userData.stepInEExp(macro) |
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.
🗺️ This removes the behavior when a system macro is invoked, it should be added to the user table if there is no name conflict
, matching test removed below. We can restore this behavior at need, but it's not certain that the user wants to add every invoked system macro to their macro table.
return SystemSymbols_1_1.get((int) id).getToken(); | ||
SystemSymbols_1_1 systemSymbol = SystemSymbols_1_1.get((int) id); | ||
return (systemSymbol == null) ? new SymbolTokenImpl(-1) : systemSymbol.getToken(); |
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.
It looks to me like throwing is the right thing here. This is only called in places where the encoding indicates a system symbol is present. It would be invalid Ion for that encoding to occur without a valid system symbol mapping.
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.
Looks pretty good. Thanks!
return SystemSymbols_1_1.get((int) id).getToken(); | ||
SystemSymbols_1_1 systemSymbol = SystemSymbols_1_1.get((int) id); | ||
return (systemSymbol == null) ? new SymbolTokenImpl(-1) : systemSymbol.getToken(); |
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.
We probably need to throw an IonException
here because if we have something that claims to be a system symbol, but the ID is out of range for the system symbol table, it's invalid Ion data.
Co-authored-by: Matthew Pope <[email protected]>
917bf0a
to
140a83e
Compare
383066b
into
amazon-ion:ion-11-encoding
Issue #, if available: NA
Description of changes:
default
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.