-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow
Kind
s to be registered by packages outside JuliaSyntax (#461)
Extensible kinds are quite tricky. We want * To use a small number of bits for them * To have the string representation in the source, but have the compiler able to fully inline the integer representation. * Allow modules with different kinds to cooperate together on the same integer representation. * Not trigger invalidation when new kinds are added * Different `Kind` modules to not require cooperation This is a very hard set of constraints to satisfy. The last one is already impossible in a single flat namespace so in this design we've given up on it and require cooperation between all kind extension modules, including module authors allocating non-colliding id's for their modules, in addition to non-colliding kind names.
- Loading branch information
Showing
3 changed files
with
250 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Only test this once per session, as kind modules must be unique (ugh) | ||
if !isdefined(@__MODULE__, :FooKinds) | ||
@eval module FooKinds | ||
|
||
using JuliaSyntax | ||
|
||
function _init_kinds() | ||
JuliaSyntax.register_kinds!(@__MODULE__, 42, [ | ||
"BEGIN_FOO" | ||
"foo_1" | ||
"foo_2" | ||
"BEGIN_FOOBAR" | ||
"foobar_1" | ||
"foobar_2" | ||
"END_FOOBAR" | ||
"END_FOO" | ||
]) | ||
end | ||
|
||
_init_kinds() | ||
|
||
k_before_init = K"foo_1" | ||
|
||
function __init__() | ||
_init_kinds() | ||
end | ||
|
||
end | ||
|
||
@eval module BarKinds | ||
# Intentionally empty | ||
end | ||
|
||
end | ||
|
||
@testset "Kinds" begin | ||
@test K"foo_1" != K"foo_2" | ||
|
||
@test FooKinds.k_before_init == K"foo_1" | ||
|
||
@test K"BEGIN_FOO" == K"foo_1" | ||
@test K"foo_2" < K"BEGIN_FOOBAR" | ||
@test K"BEGIN_FOOBAR" == K"foobar_1" | ||
@test K"END_FOOBAR" == K"foobar_2" | ||
@test K"END_FOO" == K"foobar_2" | ||
|
||
@test parentmodule(K"foo_1") == FooKinds | ||
@test sprint(show, K"foo_1") == "K\"foo_1\"" | ||
|
||
# Too many kind modules | ||
@test_throws ErrorException JuliaSyntax.register_kinds!(BarKinds, 64, ["hoo?"]) | ||
# Too many kind names per module | ||
@test_throws ErrorException JuliaSyntax.register_kinds!(BarKinds, 42, string.(1:1024)) | ||
# Re-registering or registering new kinds is not supported | ||
@test_throws ErrorException JuliaSyntax.register_kinds!(FooKinds, 42, ["foo_2", "foo_1"]) | ||
@test_throws ErrorException JuliaSyntax.register_kinds!(FooKinds, 42, ["foo_3"]) | ||
# Module ID already taken by FooKinds | ||
@test_throws ErrorException JuliaSyntax.register_kinds!(BarKinds, 42, ["hii?"]) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters