Skip to content

Instantly share code, notes, and snippets.

@KristofferC
Last active May 17, 2017 07:45
Show Gist options
  • Save KristofferC/24c269727d914ffb88bfc21624dad891 to your computer and use it in GitHub Desktop.
Save KristofferC/24c269727d914ffb88bfc21624dad891 to your computer and use it in GitHub Desktop.
using Tokenize
import Tokenize.Tokens: exactkind, kind
const REPLACEMENTS = Dict{Tokenize.Tokens.Kind, String}(
Tokens.TYPE => "mutable struct",
Tokens.IMMUTABLE => "struct",
)
function update_folder!(path, visual_only::Bool = true)
_update_folder!(path, visual_only)
if visual_only
warn("No modifications done")
end
end
function _update_folder!(path, visual_only)
for (root, dirs, files) in walkdir(path)
for file in files
if split(basename(file), '.')[end] == "jl"
filepath = joinpath(root, file)
if visual_only
info("Would update: $filepath")
else
info("Updating file: $filepath")
update_file!(filepath)
end
end
end
end
end
function update_file!(filepath)
str = open(readstring, filepath)
tokens = tokenize(str)
io = IOBuffer()
prev_token = Tokens.Token()
for token in tokens
if exactkind(token) == Tokens.ERROR
warn("Found error token, aborting")
return
end
if (exactkind(prev_token) != Tokens.ABSTRACT &&
exactkind(prev_token) != Tokens.PRIMITIVE)
write(io, get(REPLACEMENTS, exactkind(token), untokenize(token)))
else
write(io, untokenize(token))
end
if kind(token) != Tokens.WHITESPACE
prev_token = token
end
end
open(filepath, "w") do f
print(f, String(take!(io)))
end
return
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment