Skip to content

Commit 7cfb056

Browse files
aviateskpfitzseb
andauthored
fix and optimize erroneous code paths (#58)
Co-authored-by: Sebastian Pfitzner <pfitzseb@gmail.com>
1 parent 2e1e8e7 commit 7cfb056

7 files changed

Lines changed: 28 additions & 27 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ There's arguably a few downsides:
471471
Practically the flisp parser is not quite a classic [recursive descent
472472
parser](https://en.wikipedia.org/wiki/Recursive_descent_parser), because it
473473
often looks back and modifies the output tree it has already produced. We've
474-
tried to eliminate this pattern it favor of lookahead where possible because
474+
tried to eliminate this pattern in favor of lookahead where possible because
475475

476476
* It works poorly when the parser is emitting a stream of node spans with
477477
strict source ordering constraints.

src/diagnostics.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ end
3232
function Diagnostic(first_byte, last_byte; error=nothing, warning=nothing)
3333
message = !isnothing(error) ? error :
3434
!isnothing(warning) ? warning :
35-
error("No message in diagnostic")
35+
Base.error("No message in diagnostic")
3636
level = !isnothing(error) ? :error : :warning
3737
Diagnostic(first_byte, last_byte, level, message)
3838
end
@@ -48,11 +48,12 @@ function show_diagnostic(io::IO, diagnostic::Diagnostic, source::SourceFile)
4848
(:normal, "Info")
4949
line, col = source_location(source, first_byte(diagnostic))
5050
linecol = "$line:$col"
51-
if !isnothing(source.filename)
52-
locstr = "$(source.filename):$linecol"
51+
filename = source.filename
52+
if !isnothing(filename)
53+
locstr = "$filename:$linecol"
5354
if get(io, :color, false)
5455
# Also add hyperlinks in color terminals
55-
url = "file://$(abspath(source.filename))#$linecol"
56+
url = "file://$(abspath(filename))#$linecol"
5657
locstr = "\e]8;;$url\e\\$locstr\e]8;;\e\\"
5758
end
5859
else

src/expr.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,20 @@ end
88

99
function _to_expr(node::SyntaxNode, iteration_spec=false, need_linenodes=true)
1010
if !haschildren(node)
11-
if node.val isa Union{Int128,UInt128,BigInt}
11+
val = node.val
12+
if val isa Union{Int128,UInt128,BigInt}
1213
# Ignore the values of large integers and convert them back to
1314
# symbolic/textural form for compatibility with the Expr
1415
# representation of these.
1516
str = replace(sourcetext(node), '_'=>"")
1617
headsym = :macrocall
1718
k = kind(node)
18-
macname = node.val isa Int128 ? Symbol("@int128_str") :
19-
node.val isa UInt128 ? Symbol("@uint128_str") :
19+
macname = val isa Int128 ? Symbol("@int128_str") :
20+
val isa UInt128 ? Symbol("@uint128_str") :
2021
Symbol("@big_str")
2122
return Expr(:macrocall, GlobalRef(Core, macname), nothing, str)
2223
else
23-
return node.val
24+
return val
2425
end
2526
end
2627
headstr = untokenize(head(node), include_flag_suff=false)
@@ -165,7 +166,8 @@ function _to_expr(node::SyntaxNode, iteration_spec=false, need_linenodes=true)
165166
args[1] = Expr(:block, loc, args[1])
166167
elseif headsym == :(->)
167168
if Meta.isexpr(args[2], :block)
168-
if node.parent isa SyntaxNode && kind(node.parent) != K"do"
169+
parent = node.parent
170+
if parent isa SyntaxNode && kind(parent) != K"do"
169171
pushfirst!(args[2].args, loc)
170172
end
171173
else
@@ -207,4 +209,3 @@ Base.Expr(node::SyntaxNode) = _to_expr(node)
207209
function build_tree(::Type{Expr}, stream::ParseStream; kws...)
208210
Expr(build_tree(SyntaxNode, stream; kws...))
209211
end
210-

src/hooks.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ else
99
# incremental compilation. (Ideally we'd just arrange for Core._parse to be
1010
# set to the JuliaSyntax parser. But how do we signal that to the dumping
1111
# code outside of the initial creation of Core?)
12-
i = findfirst(==(:incremental), fieldnames(Base.JLOptions))
12+
i = Base.fieldindex(Base.JLOptions, :incremental)
1313
ptr = convert(Ptr{fieldtype(Base.JLOptions, i)},
1414
cglobal(:jl_options, Base.JLOptions) + fieldoffset(Base.JLOptions, i))
1515
incremental = unsafe_load(ptr)
@@ -173,4 +173,3 @@ function enable_in_core!(enable=true; freeze_world_age = true,
173173
_set_core_parse_hook(enable ? core_parser_hook : _default_parser)
174174
nothing
175175
end
176-

src/parse_stream.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function Base.summary(head::SyntaxHead)
6161
end
6262

6363
function untokenize(head::SyntaxHead; unique=true, include_flag_suff=true)
64-
str = is_error(kind(head)) ? "error" : untokenize(kind(head); unique=unique)
64+
str = is_error(kind(head)) ? "error" : untokenize(kind(head); unique=unique)::String
6565
if is_dotted(head)
6666
str = "."*str
6767
end
@@ -281,7 +281,7 @@ function release_positions(stream, positions)
281281
end
282282

283283
#-------------------------------------------------------------------------------
284-
# Return true when a token was emitted last at stream position `pos`
284+
# Return true when a token was emitted last at stream position `pos`
285285
function token_is_last(stream, pos)
286286
return pos.range_index == 0 ||
287287
pos.token_index > stream.ranges[pos.range_index].last_token
@@ -872,8 +872,9 @@ stream's text buffer. Note that this leaves the `ParseStream` in an invalid
872872
state for further parsing.
873873
"""
874874
function sourcetext(stream::ParseStream; steal_textbuf=false)
875-
if stream.text_root isa AbstractString && codeunit(stream.text_root) == UInt8
876-
return stream.text_root
875+
root = stream.text_root
876+
if root isa AbstractString && codeunit(root) == UInt8
877+
return root
877878
elseif steal_textbuf
878879
return String(stream.textbuf)
879880
else

src/syntax_tree.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ function interpolate_literal(node::SyntaxNode, val)
118118
SyntaxNode(node.source, node.raw, node.position, node.parent, true, val)
119119
end
120120

121-
function _show_syntax_node(io, current_filename, node, indent)
121+
function _show_syntax_node(io, current_filename, node::SyntaxNode, indent)
122122
fname = node.source.filename
123123
line, col = source_location(node.source, node.position)
124124
posstr = "$(lpad(line, 4)):$(rpad(col,3))$(lpad(node.position,6)):$(rpad(node.position+span(node)-1,6))"
125-
nodestr = haschildren(node) ? "[$(untokenize(head(node)))]" :
126-
node.val isa Symbol ? string(node.val) :
127-
repr(node.val)
125+
val = node.val
126+
nodestr = haschildren(node) ? "[$(untokenize(head(node)))]" :
127+
isa(val, Symbol) ? string(val) : repr(val)
128128
treestr = string(indent, nodestr)
129129
# Add filename if it's changed from the previous node
130130
if fname != current_filename[]
@@ -141,12 +141,13 @@ function _show_syntax_node(io, current_filename, node, indent)
141141
end
142142
end
143143

144-
function _show_syntax_node_sexpr(io, node)
144+
function _show_syntax_node_sexpr(io, node::SyntaxNode)
145145
if !haschildren(node)
146146
if is_error(node)
147147
print(io, "(", untokenize(head(node)), ")")
148148
else
149-
print(io, node.val isa Symbol ? string(node.val) : repr(node.val))
149+
val = node.val
150+
print(io, val isa Symbol ? string(val) : repr(val))
150151
end
151152
else
152153
print(io, "(", untokenize(head(node)))
@@ -256,4 +257,3 @@ function highlight(code::String, node, path::Int...; color=(40,40,70))
256257
_printstyled(stdout, code[p:q-1]; bgcolor=color)
257258
print(stdout, code[q:end])
258259
end
259-

src/value_parsing.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ function utf8proc_decompose(str, options, buffer, nwords)
228228
ret = ccall(:utf8proc_decompose_custom, Int, (Ptr{UInt8}, Int, Ptr{UInt8}, Int, Cint, Ptr{Cvoid}, Ptr{Cvoid}),
229229
str, sizeof(str), buffer, nwords, options,
230230
@cfunction(utf8proc_custom_func, UInt32, (UInt32, Ptr{Cvoid})), C_NULL)
231-
ret < 0 && utf8proc_error(ret)
231+
ret < 0 && Base.Unicode.utf8proc_error(ret)
232232
return ret
233233
end
234234

@@ -237,12 +237,11 @@ function utf8proc_map(str::Union{String,SubString{String}}, options::Integer)
237237
buffer = Base.StringVector(nwords*4)
238238
nwords = utf8proc_decompose(str, options, buffer, nwords)
239239
nbytes = ccall(:utf8proc_reencode, Int, (Ptr{UInt8}, Int, Cint), buffer, nwords, options)
240-
nbytes < 0 && utf8proc_error(nbytes)
240+
nbytes < 0 && Base.Unicode.utf8proc_error(nbytes)
241241
return String(resize!(buffer, nbytes))
242242
end
243243

244244
function normalize_identifier(str)
245245
flags = Base.Unicode.UTF8PROC_STABLE | Base.Unicode.UTF8PROC_COMPOSE
246246
utf8proc_map(str, flags)
247247
end
248-

0 commit comments

Comments
 (0)