From fabe9ee393890fd7932b3fae8efef94fa20229ce Mon Sep 17 00:00:00 2001 From: Carlo Baldassi Date: Sat, 8 Jul 2017 06:43:43 +0200 Subject: [PATCH] Deprecate the use of `?` as an identifier Ref #6286 --- base/docs/helpdb/Base.jl | 4 ++-- src/julia-parser.scm | 21 +++++++++++++++------ test/enums.jl | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/base/docs/helpdb/Base.jl b/base/docs/helpdb/Base.jl index 1dcd95a8a6870..783c959340391 100644 --- a/base/docs/helpdb/Base.jl +++ b/base/docs/helpdb/Base.jl @@ -250,7 +250,7 @@ the byte representation is different. This would create a 25-by-30000 `BitArray`, linked to the file associated with stream `s`. """ -Mmap.mmap(io, ::BitArray, dims = ?, offset = ?) +Mmap.mmap(io, ::BitArray, dims, offset) """ sizeof(T) @@ -2151,7 +2151,7 @@ julia> pop!(d, "e", 4) 4 ``` """ -pop!(collection,key,?) +pop!(collection,key,default) """ pop!(collection) -> item diff --git a/src/julia-parser.scm b/src/julia-parser.scm index a5c4e665633b9..791fb133e714c 100644 --- a/src/julia-parser.scm +++ b/src/julia-parser.scm @@ -151,6 +151,8 @@ (define whitespace-newline #f) ; enable parsing `where` with high precedence (define where-enabled #t) +; the question mark is only allowed as a token in ternary expressions +(define allow-question-mark #f) (define current-filename 'none) @@ -187,6 +189,10 @@ `(with-bindings ((whitespace-newline #f)) ,@body)) +(define-macro (with-allow-question-mark . body) + `(with-bindings ((allow-question-mark #t)) + ,@body)) + ;; --- lexer --- (define (newline? c) (eqv? c #\newline)) @@ -541,11 +547,14 @@ t))))) (define (take-token s) - (or - (begin0 (ts:pbtok s) - (aset! s 3 #f)) - (begin0 (ts:last-tok s) - (ts:set-tok! s #f)))) + (let ((t (or + (begin0 (ts:pbtok s) + (aset! s 3 #f)) + (begin0 (ts:last-tok s) + (ts:set-tok! s #f))))) + (if (and (not allow-question-mark) (eq? t '?)) + (syntax-deprecation s "`?` used as an identifier" "")) + t)) ;; --- misc --- @@ -736,7 +745,7 @@ (cond ((eq? (peek-token s) '?) (begin (if (not (ts:space? s)) (syntax-deprecation s (string (deparse ex) "?") (string (deparse ex) " ?"))) - (take-token s) ; take the ? + (with-allow-question-mark (take-token s)) ; take the ? (let ((t (with-whitespace-newline (without-range-colon (peek-token s))))) (if (not (ts:space? s)) (syntax-deprecation s (string (deparse ex) " ?" (deparse t)) (string (deparse ex) " ? " (deparse t))))) diff --git a/test/enums.jl b/test/enums.jl index 243650b27c643..ea4169af7b8aa 100644 --- a/test/enums.jl +++ b/test/enums.jl @@ -78,7 +78,7 @@ end @test Integer(_zerobi) == 1 # can't use non-identifiers as enum members -@test_throws ArgumentError eval(:(@enum(Test2, ?))) +@test_throws ArgumentError eval(:(@enum Test2 x ? 1 : 2)) @test_throws ArgumentError eval(:(@enum Test22 1=2)) # other Integer types of enum members