From eee075ce51e82d26f1df650eb3bf6e565c01fb09 Mon Sep 17 00:00:00 2001 From: Isaiah Norton Date: Fri, 21 Aug 2015 00:46:26 -0400 Subject: [PATCH] Implement __LINE__ magic macros Fixes #8066 --- NEWS.md | 1 + base/docs/helpdb.jl | 16 +++++++++++++--- src/julia-parser.scm | 13 ++++++++----- test/loading.jl | 2 ++ 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/NEWS.md b/NEWS.md index 1940f834ecb72..31463e43cd5ac 100644 --- a/NEWS.md +++ b/NEWS.md @@ -58,6 +58,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 2040f0f80cfc0..813cf929139bf 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) @@ -9537,12 +9539,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/src/julia-parser.scm b/src/julia-parser.scm index c77ab37a4594b..df6bc68df4ff2 100644 --- a/src/julia-parser.scm +++ b/src/julia-parser.scm @@ -2039,14 +2039,17 @@ (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))) + ((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..0e7f05aeeba76 100644 --- a/test/loading.jl +++ b/test/loading.jl @@ -2,6 +2,8 @@ using Base.Test +@test @__LINE__ == 5 + include("test_sourcepath.jl") thefname = "the fname!//\\&\0\1*" @test include_string("include_string_test() = @__FILE__", thefname)() == Base.source_path()