Add keep-syntax
and keep-syntax!
functions
#1095
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
These functions are designed to make it easier to properly preserve the sourcemap and tuple type in macros. This commit also modifies the threading macros to make use of these functions.
I've been really enjoying Janet and while working on a DSL I've come across the problem that this pull request attempts to improve. Here is the explanation of the problem and my proposed solution.
Various macros in boot.janet lose the sourcemap information of their arguments due to creating a new modified tuple. This simple program shows an example of this:
The error message points to line 1, where the threading macro starts, instead of line 3, where the error actually occurs. When the threading macro is modified to preserve the sourcemaps, the error message is corrected:
While this bug can be fixed for these macros individually, I think it's worth providing easy-to-use functions to handle sourcemaps correctly, because this problem arises for any user of Janet writing any macro that modifies an ast. So I'd like to propose the following two functions to make it easier to handle this.
The common case is when you have an
ast
and a functionf
transforming the ast, and you want(f ast)
to have the same sourcemap asast
. Then you can use(keep-syntax ast (f ast))
to automatically handle this, without having to add conditionals for cases whereast
or(f ast)
might not be a tuple. And whenf
is a function that accepts a tuple but returns an array, then you can usekeep-syntax!
instead to automatically coerce.This pull request includes implementations of these functions in boot.janet, uses them to preserve sourcemaps in the threading macros, and adds tests for some of these changes.