diff --git a/NEWS.md b/NEWS.md index a30a063491bc0..dcd53d85dcdec 100644 --- a/NEWS.md +++ b/NEWS.md @@ -53,6 +53,7 @@ New language features Default continues to be of length 1 ([#12385]). See http://docs.julialang.org/en/latest/manual/parallel-computing/#remoterefs-and-abstractchannels for details. + * `@__LINE__` special macro now available to reflect invocation source line number ([#12727]). Language changes ---------------- diff --git a/base/docs/helpdb.jl b/base/docs/helpdb.jl index a3b1391fb43cd..da388b26437cc 100644 --- a/base/docs/helpdb.jl +++ b/base/docs/helpdb.jl @@ -2,6 +2,8 @@ # Base.LinAlg.BLAS +import .Docs: keywords + doc""" ger!(alpha, x, y, A) @@ -9531,12 +9533,20 @@ Returns an iterator over substrings of `s` that correspond to the extended graph """ graphemes -doc""" +keywords[symbol("@__FILE__")] = doc""" + @__FILE__() -> AbstractString -`@__FILE__` expands to a string with the absolute path and file name of the script being run. Returns `nothing` if run from a REPL or an empty string if evaluated by `julia -e `. +`@__FILE__` expands to a string with the absolute path and file name of the script being run. +Returns `"none"` if run from a REPL or command-line context. +""" + +keywords[symbol("@__LINE__")] = doc""" + + @__LINE__() -> Int + +`@__LINE__` expands to the line number of the call-site. """ -:@__FILE__ doc""" charwidth(c) diff --git a/base/exports.jl b/base/exports.jl index fb91694411578..e2c4a87b5b288 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -1343,7 +1343,6 @@ export # Macros # parser internal - @__FILE__, @int128_str, @uint128_str, @big_str, diff --git a/base/loading.jl b/base/loading.jl index b9b6257146078..12b96c9ce29fc 100644 --- a/base/loading.jl +++ b/base/loading.jl @@ -253,8 +253,6 @@ function source_dir() p === nothing ? p : dirname(p) end -macro __FILE__() source_path() end - function include_from_node1(_path::AbstractString) path, prev = _include_dependency(_path) tls = task_local_storage() diff --git a/src/julia-parser.scm b/src/julia-parser.scm index c77ab37a4594b..bae2b5566cd9b 100644 --- a/src/julia-parser.scm +++ b/src/julia-parser.scm @@ -2039,14 +2039,18 @@ (with-space-sensitive (let* ((head (parse-unary-prefix s)) (t (peek-token s))) - (if (ts:space? s) + (cond + ((eqv? head '__LINE__) (input-port-line (ts:port s))) + ((eqv? head '__FILE__) (string current-filename)) + ((ts:space? s) `(macrocall ,(macroify-name head) - ,@(parse-space-separated-exprs s)) - (let ((call (parse-call-chain s head #t))) - (if (and (pair? call) (eq? (car call) 'call)) + ,@(parse-space-separated-exprs s))) + (else + (let ((call (parse-call-chain s head #t))) + (if (and (pair? call) (eq? (car call) 'call)) `(macrocall ,(macroify-name (cadr call)) ,@(cddr call)) `(macrocall ,(macroify-name call) - ,@(parse-space-separated-exprs s)))))))) + ,@(parse-space-separated-exprs s))))))))) ;; command syntax ((eqv? t #\`) diff --git a/test/loading.jl b/test/loading.jl index cc6be3aa95f34..085cf5938d3df 100644 --- a/test/loading.jl +++ b/test/loading.jl @@ -3,8 +3,10 @@ using Base.Test include("test_sourcepath.jl") -thefname = "the fname!//\\&\0\1*" -@test include_string("include_string_test() = @__FILE__", thefname)() == Base.source_path() +thefname = "the fname!//\\&\1*" +# passing a second argument to include_string should be reflected by @__FILE__ +@test include_string("test_atsign_file() = @__FILE__", thefname)() == thefname +@test include_string("test_atsign_file() = @__FILE__")() == "string" @test include_string("Base.source_path()", thefname) == Base.source_path() @test basename(@__FILE__) == "loading.jl" @test isabspath(@__FILE__)