Skip to content

Commit d81eafb

Browse files
authored
Bug fix for macro call square bracket whitespace (#125)
Ensure things like `@S[a,b]` where there's no whitespace between the `@S` and opening `[` are parsed correctly.
1 parent 700101e commit d81eafb

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

src/parser.jl

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,33 +1449,42 @@ function parse_call_chain(ps::ParseState, mark, is_macrocall=false)
14491449
end
14501450
elseif k == K"["
14511451
if is_macrocall
1452-
# a().@x[1] ==> (macrocall (ref (error (. (call a) (quote x))) 1))
1452+
# a().@x[1] ==> (macrocall (error (. (call a) (quote x))) (vect 1))
14531453
finish_macroname(ps, mark, valid_macroname, macro_name_position)
14541454
end
1455+
m = position(ps)
14551456
# a [i] ==> (ref a (error-t) i)
14561457
bump_disallowed_space(ps)
14571458
bump(ps, TRIVIA_FLAG)
14581459
ckind, cflags = parse_cat(ParseState(ps, end_symbol=true),
14591460
K"]", ps.end_symbol)
1460-
# a[i] ==> (ref a i)
1461-
# a[i,j] ==> (ref a i j)
1462-
# (a=1)[] ==> (ref (= a 1))
1463-
# T[x y] ==> (typed_hcat T x y)
1464-
# T[x ; y] ==> (typed_vcat T x y)
1465-
# T[a b; c d] ==> (typed_vcat T (row a b) (row c d))
1466-
# T[x for x in xs] ==> (typed_comprehension T (generator x (= x xs)))
1467-
#v1.8: T[a ; b ;; c ; d] ==> (typed_ncat-2 T (nrow-1 a b) (nrow-1 c d))
1468-
outk = ckind == K"vect" ? K"ref" :
1469-
ckind == K"hcat" ? K"typed_hcat" :
1470-
ckind == K"vcat" ? K"typed_vcat" :
1471-
ckind == K"comprehension" ? K"typed_comprehension" :
1472-
ckind == K"ncat" ? K"typed_ncat" :
1473-
internal_error("unrecognized kind in parse_cat ", ckind)
1474-
emit(ps, mark, outk, cflags)
1475-
check_ncat_compat(ps, mark, ckind)
14761461
if is_macrocall
1462+
# @S[a,b] ==> (macrocall @S (vect a b))
1463+
# @S[a b] ==> (macrocall @S (hcat a b))
1464+
# @S[a; b] ==> (macrocall @S (vcat a b))
1465+
#v1.7: @S[a ;; b] ==> (macrocall @S (ncat-2 a b))
1466+
#v1.6: @S[a ;; b] ==> (macrocall @S (error (ncat-2 a b)))
1467+
emit(ps, m, ckind, cflags)
1468+
check_ncat_compat(ps, m, ckind)
14771469
emit(ps, mark, K"macrocall")
14781470
break
1471+
else
1472+
# a[i] ==> (ref a i)
1473+
# a[i,j] ==> (ref a i j)
1474+
# (a=1)[] ==> (ref (= a 1))
1475+
# T[x y] ==> (typed_hcat T x y)
1476+
# T[x ; y] ==> (typed_vcat T x y)
1477+
# T[a b; c d] ==> (typed_vcat T (row a b) (row c d))
1478+
# T[x for x in xs] ==> (typed_comprehension T (generator x (= x xs)))
1479+
#v1.8: T[a ; b ;; c ; d] ==> (typed_ncat-2 T (nrow-1 a b) (nrow-1 c d))
1480+
outk = ckind == K"vect" ? K"ref" :
1481+
ckind == K"hcat" ? K"typed_hcat" :
1482+
ckind == K"vcat" ? K"typed_vcat" :
1483+
ckind == K"comprehension" ? K"typed_comprehension" :
1484+
ckind == K"ncat" ? K"typed_ncat" :
1485+
internal_error("unrecognized kind in parse_cat ", ckind)
1486+
emit(ps, mark, outk, cflags)
1487+
check_ncat_compat(ps, mark, ckind)
14791488
end
14801489
elseif k == K"."
14811490
# x .y ==> (. x (error-t) (quote y))

test/parser.jl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,17 @@ tests = [
282282
"a().@x(y)" => "(macrocall (error (. (call a) (quote x))) y)"
283283
"a().@x y" => "(macrocall (error (. (call a) (quote x))) y)"
284284
"a().@x{y}" => "(macrocall (error (. (call a) (quote x))) (braces y))"
285-
# array indexing, typed comprehension, etc
286-
"a().@x[1]" => "(macrocall (ref (error (. (call a) (quote x))) 1))"
285+
# square brackets
286+
"a().@x[1]" => "(macrocall (error (. (call a) (quote x))) (vect 1))"
287+
"@S[a,b]" => "(macrocall @S (vect a b))" =>
288+
Expr(:macrocall, Symbol("@S"), LineNumberNode(1), Expr(:vect, :a, :b))
289+
"@S[a b]" => "(macrocall @S (hcat a b))" =>
290+
Expr(:macrocall, Symbol("@S"), LineNumberNode(1), Expr(:hcat, :a, :b))
291+
"@S[a; b]" => "(macrocall @S (vcat a b))" =>
292+
Expr(:macrocall, Symbol("@S"), LineNumberNode(1), Expr(:vcat, :a, :b))
293+
((v=v"1.7",), "@S[a ;; b]") => "(macrocall @S (ncat-2 a b))" =>
294+
Expr(:macrocall, Symbol("@S"), LineNumberNode(1), Expr(:ncat, 2, :a, :b))
295+
((v=v"1.6",), "@S[a ;; b]") => "(macrocall @S (error (ncat-2 a b)))"
287296
"a[i]" => "(ref a i)"
288297
"a [i]" => "(ref a (error-t) i)"
289298
"a[i,j]" => "(ref a i j)"
@@ -805,11 +814,6 @@ broken_tests = [
805814
# Invalid numeric literals, not juxtaposition
806815
"0b12" => "(error \"0b12\")"
807816
"0xex" => "(error \"0xex\")"
808-
# Square brackets without space in macrocall
809-
"@S[a,b]" => "(macrocall S (vect a b))"
810-
"@S[a b]" => "(macrocall S (hcat a b))"
811-
"@S[a; b]" => "(macrocall S (vcat a b))"
812-
"@S[a; b ;; c; d]" => "(macrocall S (ncat-2 (nrow-1 a b) (nrow-1 c d)))"
813817
# Bad character literals
814818
"'\\xff'" => "(error '\\xff')"
815819
"'\\x80'" => "(error '\\x80')"

0 commit comments

Comments
 (0)