Skip to content

Commit

Permalink
fix JuliaLang#36196, failure to give expected "end" error in parser (
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson authored Jun 8, 2020
1 parent cb41bbb commit d57c648
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -662,16 +662,13 @@
(first? #t)
(t (peek-token s)))
(if (not (memv t ops))
(begin
(if (not (or (eof-object? t) (eqv? t #\newline) (closer? t)))
(error (string "extra token \"" t "\" after end of expression")))
(if (or (null? ex) (pair? (cdr ex)) (not first?))
;; () => (head)
;; (ex2 ex1) => (head ex1 ex2)
;; (ex1) if operator appeared => (head ex1) (handles "x;")
(cons head (reverse! ex))
;; (ex1) => ex1
(car ex)))
(if (or (null? ex) (pair? (cdr ex)) (not first?))
;; () => (head)
;; (ex2 ex1) => (head ex1 ex2)
;; (ex1) if operator appeared => (head ex1) (handles "x;")
(cons head (reverse! ex))
;; (ex1) => ex1
(car ex))
(begin (take-token s)
;; allow input to end with the operator, as in a;b;
(if (or (eof-object? (peek-token s))
Expand Down Expand Up @@ -1270,15 +1267,18 @@

(define (expect-end s word)
(let ((t (peek-token s)))
(cond ((eq? t 'end) (take-token s))
((eof-object? t)
(error (string "incomplete: \"" word "\" at " ; NOTE: changing this may affect code in base/client.jl
current-filename ":" expect-end-current-line
" requires end")))
(else
(error (string "\"" word "\" at "
current-filename ":" expect-end-current-line
" expected \"end\", got \"" t "\""))))))
(if (eq? t 'end)
(take-token s)
(expect-end-error t word))))

(define (expect-end-error t word)
(if (eof-object? t)
(error (string "incomplete: \"" word "\" at " ; NOTE: changing this may affect code in base/client.jl
current-filename ":" expect-end-current-line
" requires end"))
(error (string "\"" word "\" at "
current-filename ":" expect-end-current-line
" expected \"end\", got \"" t "\""))))

(define (parse-subtype-spec s)
(parse-comparison s))
Expand Down Expand Up @@ -1469,10 +1469,10 @@
(expect-end (take-lineendings s) "primitive type"))))))

((try)
(let ((try-block (if (memq (require-token s) '(catch finally))
(let ((try-block (if (memq (peek-token s) '(catch finally))
'(block)
(parse-block s))))
(let loop ((nxt (require-token s))
(let loop ((nxt (peek-token s))
(catchb #f)
(catchv #f)
(finalb #f))
Expand Down Expand Up @@ -1519,7 +1519,7 @@
catchb
catchv
fb)))
(else (error (string "unexpected \"" nxt "\"")))))))
(else (expect-end-error nxt 'try))))))
((return) (let ((t (peek-token s)))
(if (or (eqv? t #\newline) (closing-token? t))
(list 'return '(null))
Expand Down
4 changes: 4 additions & 0 deletions test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2264,3 +2264,7 @@ x = let var"'"(x) = 2x
3'
end
@test x == 6

# issue #36196
@test_throws ParseError("\"for\" at none:1 expected \"end\", got \")\"") Meta.parse("(for i=1; println())")
@test_throws ParseError("\"try\" at none:1 expected \"end\", got \")\"") Meta.parse("(try i=1; println())")

0 comments on commit d57c648

Please sign in to comment.