Skip to content

Commit

Permalink
Tweaks to allow vendoring into Base (#119)
Browse files Browse the repository at this point in the history
* Simplify `@doc` usage for use in Base bootstrap.
* Separate sysimage precompile into part which can be used in Base - 
  when vendoring into base, pkgdir doesn't exist - we need to pass a path
  to where it's vendored instead.
* Relative include for JuliaSyntax tests. This allows these files to be run
  during precompilation even when JuliaSyntax isn't a standalone module.
  • Loading branch information
c42f authored Oct 6, 2022
1 parent 6465cce commit 7b23532
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
8 changes: 5 additions & 3 deletions src/parser_api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function _parse(rule::Symbol, need_eof::Bool, ::Type{T}, text, index=1; version=
tree, last_byte(stream) + 1
end

"""
_parse_docs = """
parse(TreeType, text, [index];
version=VERSION,
ignore_trivia=true,
Expand Down Expand Up @@ -130,12 +130,14 @@ source file name.
A `ParseError` will be thrown if any errors or warnings occurred during
parsing. To avoid exceptions due to warnings, use `ignore_warnings=true`.
"""

parse(::Type{T}, text::AbstractString; kws...) where {T} = _parse(:statement, true, T, text; kws...)[1]
parseall(::Type{T}, text::AbstractString; kws...) where {T} = _parse(:toplevel, true, T, text; kws...)[1]
parseatom(::Type{T}, text::AbstractString; kws...) where {T} = _parse(:atom, true, T, text; kws...)[1]

@eval @doc $(@doc parse) parseall
@eval @doc $(@doc parse) parseatom
@eval @doc $_parse_docs parse
@eval @doc $_parse_docs parseall
@eval @doc $_parse_docs parseatom

parse(::Type{T}, text::AbstractString, index::Integer; kws...) where {T} = _parse(:statement, false, T, text, index; kws...)
parseall(::Type{T}, text::AbstractString, index::Integer; kws...) where {T} = _parse(:toplevel, false, T, text, index; kws...)
Expand Down
6 changes: 6 additions & 0 deletions sysimage/precompile.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function precompile_JuliaSyntax(mod, juliasyntax_path)
Base.include(mod, joinpath(juliasyntax_path, "test", "test_utils.jl"))
Base.include(mod, joinpath(juliasyntax_path, "test", "parser.jl"))
JuliaSyntax.enable_in_core!()
Meta.parse("x+y+z-w .+ [a b c]")
end
6 changes: 2 additions & 4 deletions sysimage/precompile_exec.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import JuliaSyntax
Base.include(@__MODULE__(), joinpath(pkgdir(JuliaSyntax), "test", "test_utils.jl"))
Base.include(@__MODULE__(), joinpath(pkgdir(JuliaSyntax), "test", "parser.jl"))
JuliaSyntax.enable_in_core!()
Meta.parse("x+y+z-w .+ [a b c]")
include("precompile.jl")
precompile_JuliaSyntax(@__MODULE__(), pkgdir(JuliaSyntax))
6 changes: 3 additions & 3 deletions test/parser.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
function test_parse(production, code; v=v"1.6")
stream = ParseStream(code, version=v)
production(JuliaSyntax.ParseState(stream))
t = JuliaSyntax.build_tree(GreenNode, stream, wrap_toplevel_as_kind=K"None")
production(ParseState(stream))
t = build_tree(GreenNode, stream, wrap_toplevel_as_kind=K"None")
source = SourceFile(code)
s = SyntaxNode(source, t)
if JuliaSyntax.kind(s) == K"None"
if kind(s) == K"None"
join([sprint(show, MIME("text/x.sexpression"), c) for c in children(s)], ' ')
else
sprint(show, MIME("text/x.sexpression"), s)
Expand Down
7 changes: 3 additions & 4 deletions test/test_utils.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using Test
using JuliaSyntax

using Base.Meta: @dump

using JuliaSyntax:
# We need a relative include here as JuliaSyntax my come from Base.
using .JuliaSyntax:
# Parsing
ParseStream,
ParseState,
SourceFile,
parse!,
parse,
Expand Down

0 comments on commit 7b23532

Please sign in to comment.