Skip to content

Commit

Permalink
fix some parsing issues with end and blocks inside expressions
Browse files Browse the repository at this point in the history
- `end` should only be special inside indexing expressions, not cat
- Blocks introduced by words (e.g. `if`, `begin`) should revert to
  normal parsing context, so `for` is no longer special. Fixes JuliaLang#18935
  • Loading branch information
JeffBezanson committed Jul 22, 2017
1 parent 17496a4 commit f0a8c8c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@
(where-enabled #t))
,@body))

(define-macro (with-normal-context . body)
`(with-bindings ((range-colon-enabled #t)
(space-sensitive #f)
(where-enabled #t)
(inside-vec #f)
(end-symbol #f)
(whitespace-newline #f))
,@body))

(define-macro (without-range-colon . body)
`(with-bindings ((range-colon-enabled #f))
,@body))
Expand Down Expand Up @@ -1263,8 +1272,8 @@
(define (parse-resword s word)
(with-bindings
((expect-end-current-line (input-port-line (ts:port s))))
(with-normal-ops
(without-whitespace-newline
(with-normal-context
(begin
(case word
((begin quote)
(let ((loc (begin (skip-ws-and-comments (ts:port s))
Expand Down Expand Up @@ -1734,6 +1743,8 @@
((null? (cdr v)) (cons (car v) outer))
(else (cons (fix 'row v) outer))))
(define semicolon (eqv? (peek-token s) #\;))
;; if a [ ] expression is a cat expression, `end` is not special
(with-bindings ((end-symbol #f))
(let loop ((vec (list first))
(outer '()))
(let ((t (if (or (eqv? (peek-token s) #\newline) gotnewline)
Expand Down Expand Up @@ -1767,7 +1778,7 @@
(if (and (pair? vec) (not (ts:space? s)))
(error (string "expected separator between arguments to \"[ ]\"; got \""
(deparse (car vec)) t "\"")))
(loop (cons (parse-eq* s) vec) outer)))))))
(loop (cons (parse-eq* s) vec) outer))))))))

(define (peek-non-newline-token s)
(let loop ((t (peek-token s)))
Expand Down
10 changes: 10 additions & 0 deletions test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1254,3 +1254,13 @@ end === (3, String)

# issue #7479
@test expand(Main, parse("(true &&& false)")) == Expr(:error, "misplaced \"&\" expression")

# if an indexing expression becomes a cat expression, `end` is not special
@test_throws ParseError parse("a[end end]")
@test_throws ParseError parse("a[end;end]")
#@test_throws ParseError parse("a[end;]") # this is difficult to fix

# issue #18935
@test [begin
@inbounds for i = 1:10 end
end for i = 1:5] == fill(nothing, 5)

0 comments on commit f0a8c8c

Please sign in to comment.