Skip to content

Commit

Permalink
disallow keywords as function names
Browse files Browse the repository at this point in the history
macros are unchanged, for now (to accomodate @catch)
  • Loading branch information
stevenhao committed Apr 21, 2016
1 parent 72ba87a commit 3b65411
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,19 @@
(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 '(begin end while if else for try catch finally return break continue
stagedfunction function macro quote let local global const
abstract typealias type bitstype immutable ccall do
module baremodule using import export importall true false))
;; todo: make this more complete

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

(define (dict-literal? l)
Expand Down Expand Up @@ -829,7 +837,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 +940,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 +1157,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

0 comments on commit 3b65411

Please sign in to comment.