Skip to content

Commit

Permalink
Format .jl files
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Jul 27, 2020
1 parent 09e0e6b commit ee2c27f
Show file tree
Hide file tree
Showing 9 changed files with 304 additions and 98 deletions.
10 changes: 6 additions & 4 deletions src/codegen/ast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function codegen(cmd::AbstractCommand)
defs = Dict{Symbol,Any}()
defs[:name] = :command_main
defs[:args] = [Expr(:kw, :(ARGS::Vector{String}), :ARGS)]

ctx = ASTCtx()
defs[:body] = quote
$(codegen_scan_glob(ctx))
Expand Down Expand Up @@ -142,7 +142,9 @@ function codegen_body(ctx::ASTCtx, cmd::LeafCommand)
pushmaybe!(ret, codegen_params(ctx, parameters, cmd))

if nrequires > 0
err = xerror(:("command $($(cmd.name)) expect at least $($nrequires) arguments, got $($n_args)"))
err = xerror(
:("command $($(cmd.name)) expect at least $($nrequires) arguments, got $($n_args)"),
)
push!(validate_ex.args, quote
if $n_args < $nrequires
$err
Expand Down Expand Up @@ -221,7 +223,7 @@ function codegen_call(ctx::ASTCtx, params::Symbol, n_args::Symbol, cmd::LeafComm

for (i, arg) in enumerate(cmd.args)
if arg.require
push!(ex_call.args, xparse_args(arg.type, ctx.ptr+i-1))
push!(ex_call.args, xparse_args(arg.type, ctx.ptr + i - 1))
end
end

Expand All @@ -235,7 +237,7 @@ function codegen_call(ctx::ASTCtx, params::Symbol, n_args::Symbol, cmd::LeafComm

for i in cmd.nrequire+1:length(cmd.args)
call_ex = copy(ex_call)
expanded_call = push!(call_ex.args, xparse_args(cmd.args[i].type, ctx.ptr+i-1))
expanded_call = push!(call_ex.args, xparse_args(cmd.args[i].type, ctx.ptr + i - 1))
push!(ex.args, Expr(:if, :($n_args == $i), expanded_call))
end
return ex
Expand Down
9 changes: 5 additions & 4 deletions src/codegen/utils.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
help_str(x; color=true) = sprint(print_cmd, x; context = :color => color)
help_str(x; color = true) = sprint(print_cmd, x; context = :color => color)

# printing in expression
xprint_help(x; color=true) = :(print($(help_str(x; color=color))))
xprint_version(cmd::EntryCommand; color=true) = :(println($(sprint(show, cmd; context = :color => color))))
xprint_help(x; color = true) = :(print($(help_str(x; color = color))))
xprint_version(cmd::EntryCommand; color = true) =
:(println($(sprint(show, cmd; context = :color => color))))

function xerror(msg::String)
msg = "Error: $msg, use -h or --help to check more detailed help info"
Expand Down Expand Up @@ -99,7 +100,7 @@ function prettify(ex::Expr)

if any(ex.args) do x
x isa Expr && x.head === :block
end
end

return prettify(eat_blocks(ex))
end
Expand Down
42 changes: 28 additions & 14 deletions src/parse/cast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function cast_m(m, ex)
return ret
end

def = splitdef(ex; throw=false)
def = splitdef(ex; throw = false)
if def === nothing
push!(ret.args, parse_module(m, ex))
return ret
Expand All @@ -44,12 +44,17 @@ function parse_function(m, ex, def)
return quote
$ex
Core.@__doc__ $(def[:name])
$(xcall(set_cmd!, casted_commands(m), xcall(command, def[:name], parse_args(def), parse_kwargs(def))))
$(xcall(
set_cmd!,
casted_commands(m),
xcall(command, def[:name], parse_args(def), parse_kwargs(def)),
))
end
end

function parse_module(m, ex::Expr)
ex.head === :module || throw(Meta.ParseError("invalid expression, can only cast functions or modules"))
ex.head === :module ||
throw(Meta.ParseError("invalid expression, can only cast functions or modules"))

return quote
$ex
Expand Down Expand Up @@ -98,8 +103,10 @@ function to_option_or_flag(ex)
Expr(:kw, name::Symbol, value) => (string(name), Any, false)
Expr(:kw, :($name::Bool), false) => (string(name), Bool, true)
Expr(:kw, :($name::$type), value) => (string(name), type, false)
Expr(:kw, :($name::Bool), true) => throw(Meta.ParseError("Boolean options must use false as default value, and will be parsed as flags. got $name"))
::Symbol || :($name::$type) => throw(Meta.ParseError("options should have default values or make it a positional argument"))
Expr(:kw, :($name::Bool), true) =>
throw(Meta.ParseError("Boolean options must use false as default value, and will be parsed as flags. got $name"))
::Symbol || :($name::$type) =>
throw(Meta.ParseError("options should have default values or make it a positional argument"))
_ => throw(Meta.ParseError("invalid syntax: $ex"))
end
end
Expand Down Expand Up @@ -138,17 +145,18 @@ function main_m(m, ex::Expr)
ex.head === :(=) && return create_entry(m, ex)

ret = Expr(:block)
def = splitdef(ex; throw=false)
def = splitdef(ex; throw = false)
var_cmd, var_entry = gensym(:cmd), gensym(:entry)
push!(ret.args, ex)

if def === nothing
ex.head === :module || throw(Meta.ParseError("invalid expression, can only cast functions or modules"))
cmd = xcall(command, ex.args[2]; name="main")
ex.head === :module ||
throw(Meta.ParseError("invalid expression, can only cast functions or modules"))
cmd = xcall(command, ex.args[2]; name = "main")
push!(ret.args, :($var_cmd = $cmd))
else
push!(ret.args, :(Core.@__doc__ $(def[:name])))
cmd = xcall(command, def[:name], parse_args(def), parse_kwargs(def); name="main")
cmd = xcall(command, def[:name], parse_args(def), parse_kwargs(def); name = "main")
push!(ret.args, :($var_cmd = $cmd))
end

Expand All @@ -159,9 +167,9 @@ end

function main_m(m, ex::Symbol)
CACHE_FLAG[] && iscached() && return :(include($(cachefile()[1])))
var_cmd, var_entry =gensym(:cmd), gensym(:entry)
var_cmd, var_entry = gensym(:cmd), gensym(:entry)
quote
$var_cmd = $(xcall(command, ex; name="main"))
$var_cmd = $(xcall(command, ex; name = "main"))
$var_entry = $(xcall(Types, :EntryCommand, var_cmd))
$(precompile_or_exec(m, var_entry))
end
Expand All @@ -184,9 +192,15 @@ function create_entry(m, kwargs...)

ret = Expr(:block)
pushmaybe!(ret.args, create_casted_commands(m))

var_cmd, var_entry =gensym(:cmd), gensym(:entry)
cmd = xcall(Types, :NodeCommand, configs[:name], :(collect(values($m.CASTED_COMMANDS))), configs[:doc])

var_cmd, var_entry = gensym(:cmd), gensym(:entry)
cmd = xcall(
Types,
:NodeCommand,
configs[:name],
:(collect(values($m.CASTED_COMMANDS))),
configs[:doc],
)
entry = xcall(Types, :EntryCommand, var_cmd, configs[:version])

push!(ret.args, :($var_cmd = $cmd))
Expand Down
14 changes: 12 additions & 2 deletions src/parse/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@ using MatchCore
using Comonicon.Types
using Comonicon.CodeGen

export @cast, @main, read_doc, command, rm_lineinfo, default_name,
get_version, iscached, cachefile, create_cache, enable_cache, disable_cache
export @cast,
@main,
read_doc,
command,
rm_lineinfo,
default_name,
get_version,
iscached,
cachefile,
create_cache,
enable_cache,
disable_cache

include("cast.jl")
include("markdown.jl")
Expand Down
6 changes: 3 additions & 3 deletions src/parse/runtime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
register `cmd` in the command registry `cmds`, which usually is
a constant `CASTED_COMMANDS` under given module.
"""
function set_cmd!(cmds::Dict, cmd, name=cmd_name(cmd))
function set_cmd!(cmds::Dict, cmd, name = cmd_name(cmd))
if haskey(cmds, name)
@warn "replacing command $name in the registry"
end
Expand Down Expand Up @@ -88,9 +88,9 @@ end
function create_option(name::String, type, option_docs)
if haskey(option_docs, name)
arg, doc, short = option_docs[name]
return Option(name, Arg(arg; type=type); short=short)
return Option(name, Arg(arg; type = type); short = short)
else
return Option(name, Arg("::$type"; type=type))
return Option(name, Arg("::$type"; type = type))
end
end

Expand Down
14 changes: 7 additions & 7 deletions src/parse/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,30 @@ function get_version(m::Module)
return v"0.0.0"
end

function cachefile(file=Base.PROGRAM_FILE)
function cachefile(file = Base.PROGRAM_FILE)
dir = cachedir(file)
return joinpath(dir, "cmd.jl"), joinpath(dir, "checksum")
end

function cachedir(file=Base.PROGRAM_FILE)
function cachedir(file = Base.PROGRAM_FILE)
name, _ = splitext(basename(file))
dir = joinpath(dirname(file), "." * name * ".cmd")
isabspath(file) || return joinpath(pwd(), dir)
return dir
end

function iscached(file=Base.PROGRAM_FILE)
function iscached(file = Base.PROGRAM_FILE)
cache_file, crc = cachefile(file)
isfile(crc) || return false
isfile(cache_file) || return false
if read(crc, String) == string(checksum(file), base=16)
if read(crc, String) == string(checksum(file), base = 16)
return true
end
return false
end

# taken from Steven G Johnson
function checksum(filename, blocksize=16384)
function checksum(filename, blocksize = 16384)
crc = zero(UInt32)
open(filename, "r") do f
while !eof(f)
Expand All @@ -69,7 +69,7 @@ function checksum(filename, blocksize=16384)
return crc
end

function create_cache(cmd, file=Base.PROGRAM_FILE)
function create_cache(cmd, file = Base.PROGRAM_FILE)
isempty(file) && return
dir = cachedir(file)
if !ispath(dir)
Expand All @@ -78,6 +78,6 @@ function create_cache(cmd, file=Base.PROGRAM_FILE)

cache_file, crc = cachefile(file)
write(cache_file, cmd)
write(crc, string(checksum(file), base=16))
write(crc, string(checksum(file), base = 16))
return
end
Loading

0 comments on commit ee2c27f

Please sign in to comment.