Skip to content
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

Fix line numbers with source fragments #310

Merged
merged 3 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ JuliaSyntax.highlight
JuliaSyntax.sourcetext
JuliaSyntax.source_line
JuliaSyntax.source_location
JuliaSyntax.source_line_range
```

## Expression heads/kinds
Expand Down
2 changes: 1 addition & 1 deletion prototypes/simple_parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ end
function parse_and_show(production::Function, code)
st = ParseStream(code)
production(st)
t = JuliaSyntax.build_tree(GreenNode, st, wrap_toplevel_as_kind=K"error")
t = JuliaSyntax.build_tree(GreenNode, st)
show(stdout, MIME"text/plain"(), t, code, show_trivia=true)
if !isempty(st.diagnostics)
println()
Expand Down
13 changes: 8 additions & 5 deletions src/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function _strip_parens(ex)
end
end

function _leaf_to_Expr(source, head, srcrange, node)
function _leaf_to_Expr(source, txtbuf, head, srcrange, node)
k = kind(head)
if k == K"core_@cmd"
return GlobalRef(Core, Symbol("@cmd"))
Expand All @@ -79,7 +79,7 @@ function _leaf_to_Expr(source, head, srcrange, node)
Expr(:error) :
Expr(:error, "$(_token_error_descriptions[k]): `$(source[srcrange])`")
else
val = isnothing(node) ? parse_julia_literal(source, head, srcrange) : node.val
val = isnothing(node) ? parse_julia_literal(txtbuf, head, srcrange) : node.val
if val isa Union{Int128,UInt128,BigInt}
# Ignore the values of large integers and convert them back to
# symbolic/textural form for compatibility with the Expr
Expand Down Expand Up @@ -457,7 +457,9 @@ end

function build_tree(::Type{Expr}, stream::ParseStream;
filename=nothing, first_line=1, kws...)
source = SourceFile(sourcetext(stream), filename=filename, first_line=first_line)
source = SourceFile(sourcetext(stream), first_index=first_byte(stream),
filename=filename, first_line=first_line)
txtbuf = textbuf(stream)
args = Any[]
childranges = UnitRange{Int}[]
childheads = SyntaxHead[]
Expand All @@ -467,7 +469,7 @@ function build_tree(::Type{Expr}, stream::ParseStream;
end
k = kind(head)
if isnothing(nodechildren)
ex = _leaf_to_Expr(source, head, srcrange, nothing)
ex = _leaf_to_Expr(source, txtbuf, head, srcrange, nothing)
else
resize!(childranges, length(nodechildren))
resize!(childheads, length(nodechildren))
Expand All @@ -487,7 +489,8 @@ end

function _to_expr(node::SyntaxNode)
if !haschildren(node)
return _leaf_to_Expr(node.source, head(node), range(node), node)
offset, txtbuf = _unsafe_wrap_substring(sourcetext(node.source))
return _leaf_to_Expr(node.source, txtbuf, head(node), range(node) .+ offset, node)
end
cs = children(node)
args = Any[_to_expr(c) for c in cs]
Expand Down
25 changes: 6 additions & 19 deletions src/hooks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,7 @@ function core_parser_hook(code, filename::String, lineno::Int, offset::Int, opti
write(_debug_log[], code)
end

io = IOBuffer(code)
seek(io, offset)

stream = ParseStream(io)
stream = ParseStream(code, offset+1)
if options === :statement || options === :atom
# To copy the flisp parser driver:
# * Parsing atoms consumes leading trivia
Expand All @@ -179,9 +176,7 @@ function core_parser_hook(code, filename::String, lineno::Int, offset::Int, opti
end

if any_error(stream)
tree = build_tree(SyntaxNode, stream,
wrap_toplevel_as_kind=K"None", first_line=lineno,
filename=filename)
tree = build_tree(SyntaxNode, stream, first_line=lineno, filename=filename)
tag = _incomplete_tag(tree, lastindex(code))
if _has_v1_10_hooks
exc = ParseError(stream, filename=filename, first_line=lineno,
Expand Down Expand Up @@ -233,13 +228,7 @@ function core_parser_hook(code, filename::String, lineno::Int, offset::Int, opti
#
# show_diagnostics(stdout, stream.diagnostics, code)
#
ex = build_tree(Expr, stream; filename=filename,
wrap_toplevel_as_kind=K"None", first_line=lineno)
if @isexpr(ex, :None)
# The None wrapping is only to give somewhere for trivia to be
# attached; unwrap!
ex = only(ex.args)
end
ex = build_tree(Expr, stream; filename=filename, first_line=lineno)
end

# Note the next byte in 1-based indexing is `last_byte(stream) + 1` but
Expand Down Expand Up @@ -291,15 +280,13 @@ else
Base.Meta.ParseError(e::JuliaSyntax.ParseError) = e
end

const _default_parser = _has_v1_6_hooks ? Core._parse : nothing

"""
enable_in_core!([enable=true; freeze_world_age=true, debug_filename=nothing])

Connect the JuliaSyntax parser to the Julia runtime so that it replaces the
flisp parser for all parsing work. That is, JuliaSyntax will be used for
`include()` `Meta.parse()`, the REPL, etc. To disable, set use
`enable_in_core!(false)`.
`include()` `Meta.parse()`, the REPL, etc. To reset to the reference parser,
use `enable_in_core!(false)`.

Keyword arguments:
* `freeze_world_age` - Use a fixed world age for the parser to prevent
Expand All @@ -322,7 +309,7 @@ function enable_in_core!(enable=true; freeze_world_age = true,
world_age = freeze_world_age ? Base.get_world_counter() : typemax(UInt)
_set_core_parse_hook(fix_world_age(core_parser_hook, world_age))
else
_set_core_parse_hook(_default_parser)
_set_core_parse_hook(Core.Compiler.fl_parse)
end
nothing
end
Expand Down
2 changes: 2 additions & 0 deletions src/kinds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,8 @@ const _kind_names =
"cartesian_iterator"
"comprehension"
"typed_comprehension"
# Container for a single statement/atom plus any trivia and errors
"wrapper"
"END_SYNTAX_KINDS"
]

Expand Down
Loading