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 various iteration issues #296

Merged
merged 1 commit into from
Jul 26, 2021
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
9 changes: 1 addition & 8 deletions src/components/internals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,7 @@ function parse_parameters(ps::ParseState, args::Vector{EXPR}, args1::Vector{EXPR
isfirst = true
end
if !isempty(args1)
# this happens when multiple semi-colons are used
# Julia will error during lowering, so these shenanigans are just for matching
# kwarg order with Base's parser
if length(args) >= 1 && args[1] isa EXPR && args[1].head == :kw && usekw
insert!(args, max(insert_params_at - 1, 1), EXPR(:parameters, args1, trivia))
else
insert!(args, insert_params_at, EXPR(:parameters, args1, trivia))
end
insert!(args, insert_params_at, EXPR(:parameters, args1, trivia))
end
return
end
Expand Down
2 changes: 1 addition & 1 deletion src/components/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ function parse_unary_colon(ps::ParseState, op::EXPR)
if isoperator(unwrapped) || isidentifier(unwrapped) || isliteral(unwrapped)
ret = EXPR(:quotenode, EXPR[arg], EXPR[op])
elseif arg.head == :tuple && length(arg.args) == 1 && arg.args[1].head == :parameters && length(something(arg.args[1].args, [])) == 0 && arg.span > 3
ret = EXPR(:quote, EXPR[EXPR(:BLOCK, EXPR[], EXPR[])], EXPR[])
ret = EXPR(:quote, EXPR[EXPR(:BLOCK, EXPR[], EXPR[])], EXPR[op])
else
ret = EXPR(:quote, EXPR[arg], EXPR[op])
end
Expand Down
14 changes: 14 additions & 0 deletions src/conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,20 @@ function Expr(x::EXPR)
Expr(:string, Expr.(x.args[2:end])...)
elseif x.args === nothing
Expr(Symbol(lowercase(String(x.head))))
elseif x.head === :parameters
if length(x.args) > 1 && any(a -> a.head === :parameters, x.args)
ordered_args = EXPR[]
for arg in x.args
if arg.head === :parameters
pushfirst!(ordered_args, arg)
else
push!(ordered_args, arg)
end
end
Expr(:parameters, Expr.(ordered_args)...)
else
Expr(:parameters, Expr.(x.args)...)
end
Comment on lines +198 to +211
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a priority right now but we shouldn't need to special case conversion so should look to fix the underlying inconsistency w/ the scheme parser

Copy link
Member Author

@pfitzseb pfitzseb Jul 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's basically what I reverted here. Originally I had the right order in the EXPR, but that failed during iterate, so this was easier for me than wrapping my head around how iteration works :P

elseif x.head === :errortoken
Expr(:error)
else
Expand Down
2 changes: 1 addition & 1 deletion src/iterate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function Base.getindex(x::EXPR, i)
error("indexing error for $(x.head) expression at $i")
end
return a
catch
catch
error("indexing error for $(x.head) expression at $i. args: $(x.args !== nothing ? headof.(x.args) : []) trivia: $(x.trivia !== nothing ? headof.(x.trivia) : [])")
end
end
Expand Down
13 changes: 13 additions & 0 deletions test/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ randop() = rand(["-->", "→",

test_expr_broken(str) = test_expr(str, false)

function traverse(x)
try
for a in x
@test traverse(a)
end
return true
catch err
@error "EXPR traversal failed." expr = x exception = err
return false
end
end

function test_expr(str, show_data=true)
x, ps = CSTParser.parse(ParseState(str))

Expand All @@ -26,6 +38,7 @@ function test_expr(str, show_data=true)
@test x.trivia === nothing || all(x === parentof(a) for a in x.trivia)
@test isempty(check_span(x))
check_parents(x)
@test traverse(x)

if CSTParser.has_error(ps) || x0 != x1
if show_data
Expand Down