Skip to content

Commit 0f2a1bb

Browse files
committed
Treat block in ifelse conditional as Expr wart
Instead of baking this into the parser output, we treat this extra block as an Expr-specific oddity and add it during Expr conversion.
1 parent 7b80faf commit 0f2a1bb

File tree

6 files changed

+11
-12
lines changed

6 files changed

+11
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ tree", but that term has also been used for the parse tree of the full formal
127127
grammar for a language including any grammar hacks required to solve
128128
ambiguities, etc. So we avoid this term.)
129129

130-
`JuliaSyntax` uses use a mostly recursive descent parser which closely
130+
`JuliaSyntax` uses a mostly recursive descent parser which closely
131131
follows the high level structure of the flisp reference parser. This makes the
132132
code familiar and reduces porting bugs. It also gives a lot of flexibility for
133133
designing the diagnostics, tree data structures, compatibility with different

src/expr.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ function _to_expr(node::SyntaxNode, iteration_spec=false)
144144
args[2] = Expr(:block, loc, args[2])
145145
end
146146
end
147+
elseif headsym == :elseif
148+
# Compat wart: add a block for the elseif conditional
149+
args[1] = Expr(:block, args[1])
147150
elseif headsym == :(->)
148151
if Meta.isexpr(args[2], :block)
149152
pushfirst!(args[2].args, loc)

src/parser.jl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,7 +1852,7 @@ end
18521852

18531853
# Parse if-elseif-else-end expressions
18541854
#
1855-
# if a xx elseif b yy else zz end ==> (if a (block xx) (elseif (block b) (block yy) (block zz)))
1855+
# if a xx elseif b yy else zz end ==> (if a (block xx) (elseif b (block yy) (block zz)))
18561856
function parse_if_elseif(ps, is_elseif=false, is_elseif_whitespace_err=false)
18571857
mark = position(ps)
18581858
word = peek(ps)
@@ -1872,23 +1872,19 @@ function parse_if_elseif(ps, is_elseif=false, is_elseif_whitespace_err=false)
18721872
# if a xx end ==> (if a (block xx))
18731873
parse_cond(ps)
18741874
end
1875-
if is_elseif
1876-
# Wart: `elseif` condition is in a block but not `if` condition
1877-
emit(ps, cond_mark, K"block")
1878-
end
18791875
# if a \n\n xx \n\n end ==> (if a (block xx))
18801876
parse_block(ps)
18811877
bump_trivia(ps)
18821878
k = peek(ps)
18831879
if k == K"elseif"
1884-
# if a xx elseif b yy end ==> (if a (block xx) (elseif (block b) (block yy)))
1880+
# if a xx elseif b yy end ==> (if a (block xx) (elseif b (block yy)))
18851881
parse_if_elseif(ps, true)
18861882
elseif k == K"else"
18871883
emark = position(ps)
18881884
bump(ps, TRIVIA_FLAG)
18891885
if peek(ps) == K"if"
18901886
# Recovery: User wrote `else if` by mistake ?
1891-
# if a xx else if b yy end ==> (if a (block xx) (error-t) (elseif (block b) (block yy)))
1887+
# if a xx else if b yy end ==> (if a (block xx) (error-t) (elseif b (block yy)))
18921888
bump(ps, TRIVIA_FLAG)
18931889
emit(ps, emark, K"error", TRIVIA_FLAG,
18941890
error="use `elseif` instead of `else if`")
File renamed without changes.

test/parser.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,14 +390,14 @@ tests = [
390390
"export (\$f)" => "(export (\$ f))"
391391
],
392392
JuliaSyntax.parse_if_elseif => [
393-
"if a xx elseif b yy else zz end" => "(if a (block xx) (elseif (block b) (block yy) (block zz)))"
393+
"if a xx elseif b yy else zz end" => "(if a (block xx) (elseif b (block yy) (block zz)))"
394394
"if end" => "(if (error) (block))"
395395
"if \n end" => "(if (error) (block))"
396396
"if a end" => "(if a (block))"
397397
"if a xx end" => "(if a (block xx))"
398398
"if a \n\n xx \n\n end" => "(if a (block xx))"
399-
"if a xx elseif b yy end" => "(if a (block xx) (elseif (block b) (block yy)))"
400-
"if a xx else if b yy end" => "(if a (block xx) (error-t) (elseif (block b) (block yy)))"
399+
"if a xx elseif b yy end" => "(if a (block xx) (elseif b (block yy)))"
400+
"if a xx else if b yy end" => "(if a (block xx) (error-t) (elseif b (block yy)))"
401401
"if a xx else yy end" => "(if a (block xx) (block yy))"
402402
],
403403
JuliaSyntax.parse_const_local_global => [

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ include("test_utils.jl")
1818
include("parse_stream.jl")
1919
include("parser.jl")
2020
include("parser_api.jl")
21-
include("syntax_tree.jl")
21+
include("expr.jl")
2222
@testset "Parsing values from strings" begin
2323
include("value_parsing.jl")
2424
end

0 commit comments

Comments
 (0)