Skip to content

Commit cf04565

Browse files
authored
Merge pull request JuliaLang/JuliaSyntax.jl#60 from JuliaLang/sp/fix-vect-newline
Fix vect parsing with newline before comma
2 parents 7885cec + 3e00073 commit cf04565

3 files changed

Lines changed: 17 additions & 12 deletions

File tree

JuliaSyntax/src/parser.jl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ function parse_Nary(ps::ParseState, down, delimiters, closing_tokens)
398398
break
399399
elseif k in delimiters
400400
# ignore empty delimited sections
401-
# a;;;b ==> (block a b)
401+
# a;;;b ==> (block a b)
402402
continue
403403
end
404404
down(ps)
@@ -433,7 +433,7 @@ function parse_toplevel(ps::ParseState)
433433
nothing
434434
end
435435

436-
# Parse a newline or semicolon-delimited list of expressions.
436+
# Parse a newline or semicolon-delimited list of expressions.
437437
# Repeated delimiters are allowed but ignored
438438
# a;b;c ==> (block a b c)
439439
# a;;;b;; ==> (block a b)
@@ -2246,7 +2246,7 @@ function parse_imports(ps::ParseState)
22462246
bump(ps, TRIVIA_FLAG)
22472247
emark = position(ps)
22482248
initial_as = parse_import(ps, word, false)
2249-
t = peek_token(ps)
2249+
t = peek_token(ps)
22502250
k = kind(t)
22512251
has_import_prefix = false # true if we have `prefix:` in `import prefix: stuff`
22522252
has_comma = false
@@ -2798,7 +2798,7 @@ function parse_cat(ps::ParseState, closer, end_is_symbol)
27982798
if k == K"," || (is_closing_token(ps, k) && k != K";")
27992799
if k == K","
28002800
# [x,] ==> (vect x)
2801-
bump(ps, TRIVIA_FLAG)
2801+
bump(ps, TRIVIA_FLAG; skip_newlines = true)
28022802
end
28032803
# [x] ==> (vect x)
28042804
# [x \n ] ==> (vect x)
@@ -2912,9 +2912,9 @@ end
29122912
# For example, (a=1; b=2) could be seen to parse four different ways!
29132913
#
29142914
# Function args: (kw a 1) (parameters (kw b 2))
2915-
# Tuple-like: (= a 1) (parameters (kw b 2))
2916-
# Block: (= a 1) (= b 2)
2917-
# [] vect-like: (= a 1) (parameters (= b 2))
2915+
# Tuple-like: (= a 1) (parameters (kw b 2))
2916+
# Block: (= a 1) (= b 2)
2917+
# [] vect-like: (= a 1) (parameters (= b 2))
29182918
#
29192919
# Expressions (X; Y; Z) with more semicolons are also allowed by the flisp
29202920
# parser and generally parse as nested parameters blocks. This is invalid Julia
@@ -3384,7 +3384,7 @@ function parse_atom(ps::ParseState, check_identifiers=true)
33843384
# 42 ==> 42
33853385
bump(ps)
33863386
elseif is_closing_token(ps, leading_kind)
3387-
# Leave closing token in place for other productions to
3387+
# Leave closing token in place for other productions to
33883388
# recover with
33893389
# ) ==> error
33903390
msg = leading_kind == K"EndMarker" ?
@@ -3395,4 +3395,3 @@ function parse_atom(ps::ParseState, check_identifiers=true)
33953395
bump(ps, error="invalid syntax atom")
33963396
end
33973397
end
3398-

JuliaSyntax/src/syntax_tree.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ function SyntaxNode(source::SourceFile, raw::GreenNode{SyntaxHead}, position::In
7474
elseif is_syntax_kind(raw)
7575
nothing
7676
else
77-
error("Leaf node of kind $k unknown to SyntaxNode")
78-
val = nothing
77+
# FIXME: this allows us to recover from trivia is_error nodes
78+
# that we insert below
79+
@debug "Leaf node of kind $k unknown to SyntaxNode"
80+
ErrorVal()
7981
end
8082
return SyntaxNode(source, raw, position, nothing, true, val)
8183
else

JuliaSyntax/test/parser.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,10 @@ tests = [
600600
# parse_cat
601601
"[]" => "(vect)"
602602
"[x,]" => "(vect x)"
603+
"[x\n,,]" => "(vect x (error-t ✘))"
603604
"[x]" => "(vect x)"
604605
"[x \n ]" => "(vect x)"
606+
"[x \n, ]" => "(vect x)"
605607
"[x" => "(vect x (error-t))"
606608
"[x \n\n ]" => "(vect x)"
607609
"[x for a in as]" => "(comprehension (generator x (= a as)))"
@@ -616,6 +618,9 @@ tests = [
616618
# parse_vect
617619
"[x, y]" => "(vect x y)"
618620
"[x, y]" => "(vect x y)"
621+
"[x,\n y]" => "(vect x y)"
622+
"[x\n, y]" => "(vect x y)"
623+
"[x\n,, y]" => "(vect x (error-t ✘ y))"
619624
"[x,y ; z]" => "(vect x y (parameters z))"
620625
"[x=1, y=2]" => "(vect (= x 1) (= y 2))"
621626
"[x=1, ; y=2]" => "(vect (= x 1) (parameters (= y 2)))"
@@ -830,4 +835,3 @@ end
830835
@test test_parse(JuliaSyntax.parse_eq, "a \u2212= b") == "(-= a b)"
831836
@test test_parse(JuliaSyntax.parse_eq, "a .\u2212= b") == "(.-= a b)"
832837
end
833-

0 commit comments

Comments
 (0)