Skip to content

Commit

Permalink
Merge pull request #12727 from ihnorton/on-location
Browse files Browse the repository at this point in the history
RFC: Implement @__LINE__ macro
  • Loading branch information
StefanKarpinski committed Aug 28, 2015
2 parents 29d070a + eee075c commit a4c2916
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------------
Expand Down
16 changes: 13 additions & 3 deletions base/docs/helpdb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# Base.LinAlg.BLAS

import .Docs: keywords

doc"""
ger!(alpha, x, y, A)
Expand Down Expand Up @@ -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 <expr>`.
`@__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)
Expand Down
13 changes: 8 additions & 5 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -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 #\`)
Expand Down
2 changes: 2 additions & 0 deletions test/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit a4c2916

Please sign in to comment.