Skip to content

Commit 197aa46

Browse files
committed
Add various inline parser test cases
Some of these derive from Base's test/syntax.jl
1 parent 8cf8727 commit 197aa46

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/parser.jl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,8 @@ function parse_stmts(ps::ParseState)
465465
bump(ps)
466466
end
467467
if junk_mark != position(ps)
468-
emit(ps, junk_mark, K"error",
468+
# x y ==> x (error-t y)
469+
emit(ps, junk_mark, K"error", TRIVIA_FLAG,
469470
error="extra tokens after end of expression")
470471
end
471472
if do_emit
@@ -1016,6 +1017,12 @@ end
10161017
function is_juxtapose(ps, prev_k, t)
10171018
k = kind(t)
10181019

1020+
# Not juxtaposition - parse_juxtapose will consume only the first token.
1021+
# x.3 ==> x
1022+
# sqrt(2)2 ==> (call sqrt 2)
1023+
# x' y ==> x
1024+
# x 'y ==> x
1025+
10191026
return !t.had_whitespace &&
10201027
(is_number(prev_k) ||
10211028
(!is_number(k) && # disallow "x.3" and "sqrt(2)2"
@@ -1037,6 +1044,7 @@ end
10371044
# 2(x) ==> (call-i 2 * x)
10381045
# (2)(3)x ==> (call-i 2 * 3 x)
10391046
# (x-1)y ==> (call-i (call-i x - 1) * y)
1047+
# x'y ==> x
10401048
#
10411049
# flisp: parse-juxtapose
10421050
function parse_juxtapose(ps::ParseState)
@@ -2152,7 +2160,8 @@ function parse_catch(ps::ParseState)
21522160
# try x catch \n y end ==> (try (block x) false (block y) false false)
21532161
bump_invisible(ps, K"false")
21542162
else
2155-
# try x catch e y end ==> (try (block x) e (block y) false false)
2163+
# try x catch e y end ==> (try (block x) e (block y) false false)
2164+
# try x catch $e y end ==> (try (block x) ($ e) (block y) false false)
21562165
parse_identifier_or_interpolate(ps)
21572166
end
21582167
parse_block(ps)
@@ -3238,7 +3247,8 @@ function parse_atom(ps::ParseState, check_identifiers=true)
32383247
# todo: Reorder to put most likely tokens first?
32393248
if leading_kind == K":"
32403249
# symbol/expression quote
3241-
# :foo => (quote foo)
3250+
# :foo ==> (quote foo)
3251+
# : foo ==> (quote (error-t) foo)
32423252
t = peek_token(ps, 2)
32433253
k = kind(t)
32443254
if is_closing_token(ps, k) && (!is_keyword(k) || t.had_whitespace)

test/parser.jl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ tests = [
3434
"a;;;b;;" => "(toplevel a b)"
3535
""" "x" a ; "y" b """ =>
3636
"""(toplevel (macrocall :(Core.var"@doc") "x" a) (macrocall :(Core.var"@doc") "y" b))"""
37+
"x y" => "x (error-t y)"
3738
],
3839
JuliaSyntax.parse_eq => [
3940
# parse_assignment
@@ -140,10 +141,16 @@ tests = [
140141
"2(x)" => "(call-i 2 * x)"
141142
"(2)(3)x" => "(call-i 2 * 3 x)"
142143
"(x-1)y" => "(call-i (call-i x - 1) * y)"
143-
"0xenomorph" => "0x0e" # ie, not juxtoposition
144+
"x'y" => "(call-i (' x) * y)"
144145
# errors
145146
"\"a\"\"b\"" => "(call-i \"a\" * (error-t) \"b\")"
146147
"\"a\"x" => "(call-i \"a\" * (error-t) x)"
148+
# Not juxtaposition - parse_juxtapose will consume only the first token.
149+
"x.3" => "x"
150+
"sqrt(2)2" => "(call sqrt 2)"
151+
"x' y" => "(' x)"
152+
"x 'y" => "x"
153+
"0xenomorph" => "0x0e"
147154
],
148155
JuliaSyntax.parse_unary => [
149156
"+2" => "2"
@@ -461,6 +468,7 @@ tests = [
461468
"try x catch ; y end" => "(try (block x) false (block y) false false)"
462469
"try x catch \n y end" => "(try (block x) false (block y) false false)"
463470
"try x catch e y end" => "(try (block x) e (block y) false false)"
471+
"try x catch \$e y end" => "(try (block x) (\$ e) (block y) false false)"
464472
"try x finally y end" => "(try (block x) false false false (block y))"
465473
# v1.8 only
466474
((v=v"1.8",), "try catch ; else end") => "(try (block) false (block) (block) false)"
@@ -549,7 +557,8 @@ tests = [
549557
"(x \n\n for a in as)" => "(generator x (= a as))"
550558
],
551559
JuliaSyntax.parse_atom => [
552-
":foo" => "(quote foo)"
560+
":foo" => "(quote foo)"
561+
": foo" => "(quote (error-t) foo)"
553562
# Literal colons
554563
":)" => ":"
555564
": end" => ":"
@@ -744,6 +753,10 @@ broken_tests = [
744753
"@S[a b]" => "(macrocall S (hcat a b))"
745754
"@S[a; b]" => "(macrocall S (vcat a b))"
746755
"@S[a; b ;; c; d]" => "(macrocall S (ncat-2 (nrow-1 a b) (nrow-1 c d)))"
756+
# Bad character literals
757+
"'\\xff'" => "(error '\\xff')"
758+
"'\\x80'" => "(error '\\x80')"
759+
"'ab'" => "(error 'ab')"
747760
]
748761
JuliaSyntax.parse_call => [
749762
# kw's in ref

0 commit comments

Comments
 (0)