Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function names cannot be reserved words #15991

Merged
merged 1 commit into from
Apr 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,15 @@
(define (dot-opchar? c) (and (char? c) (string.find ".*^/\\+-'<>!=%≥≤≠÷" c)))
(define operator? (Set operators))

(define reserved-words '(begin while if for try return break continue
(define initial-reserved-words '(begin while if for try return break continue
stagedfunction function macro quote let local global const
abstract typealias type bitstype immutable ccall do
module baremodule using import export importall))

(define initial-reserved-word? (Set initial-reserved-words))

(define reserved-words (append initial-reserved-words '(end else catch finally true false))) ;; todo: make this more complete

(define reserved-word? (Set reserved-words))

(define (dict-literal? l)
Expand Down Expand Up @@ -829,7 +833,7 @@
(not (memv t '(#\( #\[ #\{))))
)
(not (operator? t))
(not (reserved-word? t))
(not (initial-reserved-word? t))
(not (closing-token? t))
(not (newline? t))
(not (and (pair? expr) (syntactic-unary-op? (car expr))))))
Expand Down Expand Up @@ -932,10 +936,17 @@
;; also handles looking for syntactic reserved words
(define (parse-call s)
(let ((ex (parse-unary-prefix s)))
(if (reserved-word? ex)
(if (initial-reserved-word? ex)
(parse-resword s ex)
(parse-call-chain s ex #f))))

(define (parse-def s strict)
(let ((ex (parse-unary-prefix s)))
(if (or (and strict (reserved-word? ex)) (initial-reserved-word? ex))
(error (string "invalid name \"" ex "\""))
(parse-call-chain s ex #f))))


(define (deprecated-dict-replacement ex)
(if (dict-literal? ex)
(string "Dict{" (deparse (cadr ex)) #\, (deparse (caddr ex)) "}")
Expand Down Expand Up @@ -1142,7 +1153,7 @@
((stagedfunction function macro)
(if (eq? word 'stagedfunction) (syntax-deprecation s "stagedfunction" "@generated function"))
(let* ((paren (eqv? (require-token s) #\())
(sig (parse-call s)))
(sig (parse-def s (not (eq? word 'macro)))))
(if (and (eq? word 'function) (not paren) (symbol-or-interpolate? sig))
(begin (if (not (eq? (require-token s) 'end))
(error (string "expected \"end\" in definition of function \"" sig "\"")))
Expand Down
5 changes: 5 additions & 0 deletions test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,8 @@ add_method_to_glob_fn!()
@test g15844(Int32(1)) == 2
@test f15844(Int32(1)) == 1
@test f15844(Int64(1)) == 3

# issue #15661
@test_throws ParseError parse("function catch() end")
@test_throws ParseError parse("function end() end")
@test_throws ParseError parse("function finally() end")