Skip to content

Commit

Permalink
@__END__ macro
Browse files Browse the repository at this point in the history
  • Loading branch information
wookay committed Apr 27, 2019
1 parent be8175e commit e52fdd6
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ script:
- julia --color=yes runtests.jl jive
- julia --color=yes runtests.jl jive/s jive/m start=2
- julia --color=yes -pauto runtests.jl jive/s jive/m start=2
- julia --color=yes -pauto -e 'using Jive; runtests(@__DIR__, skip=["Example", "errors", "jive/onlyonce/heavy.jl", "jive/s"], node1=["jive/m"])'
- julia --color=yes -pauto -e 'using Jive; runtests(@__DIR__, skip=["Example", "errors", "jive/onlyonce/heavy.jl", "jive/__END__/included.jl", "jive/s"], node1=["jive/m"])'
- julia --color=yes -pauto -e 'using Jive; runtests(@__DIR__, targets=["errors", "jive"], node1=["jive/m"])' || true

- cd Example
Expand Down
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ makedocs(
"@If" => "If.md",
"@useinside" => "useinside.md",
"@mockup" => "mockup.md",
"`@__END__`" => "END.md",
],
)
7 changes: 7 additions & 0 deletions docs/src/END.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# `@__END__`

`throw(Jive.EndError())`

```@docs
Jive.@__END__
```
3 changes: 3 additions & 0 deletions src/Jive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ include("If.jl")
export @useinside
include("useinside.jl")

export @__END__
include("__END__.jl")

export runtests
include("runtests.jl")

Expand Down
33 changes: 33 additions & 0 deletions src/__END__.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# module Jive

struct EndError <: Exception
end

function isenderror(ex::LoadError)
if ex.error isa EndError
return true
elseif ex.error isa LoadError
return isenderror(ex.error)
else
return false
end
end

"""
@__END__
`throw(Jive.EndError())`
"""
macro __END__()
@eval function Base.showerror(io::IO, ex::LoadError, bt; backtrace=true)
if isenderror(ex)
printstyled(io, "@__END__", color=:cyan)
print(io, " at ", basename(ex.file), ":", ex.line)
else
print(io, "Error while loading expression starting at ", ex.file, ":", ex.line)
end
end
throw(EndError())
end

# module Jive
4 changes: 4 additions & 0 deletions test/jive/__END__/included.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using Jive
push!(stack, 10)
@__END__
push!(stack, 20)
23 changes: 23 additions & 0 deletions test/jive/__END__/test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module test_jive__END__

using Test

stack = []

try
include("included.jl")
catch
push!(stack, 1)
end

try
include("included.jl")
catch
push!(stack, 2)
end

push!(stack, 3)

@test stack == [10, 1, 10, 2, 3]

end # module test_jive__END__
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
using Jive # runtests
runtests(@__DIR__, skip=["Example", "errors", "jive/onlyonce/heavy.jl"])
runtests(@__DIR__, skip=["Example", "errors", "jive/onlyonce/heavy.jl", "jive/__END__/included.jl"])

0 comments on commit e52fdd6

Please sign in to comment.