Last active
August 29, 2015 14:25
-
-
Save tiffany352/3ce177bf6df80ea5192d to your computer and use it in GitHub Desktop.
Template Pretty-Printer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env lua | |
local args = {...} | |
local indent = 0 | |
local skip = 0 | |
local ignore = false | |
function print_nl() | |
io.stdout:write('\n'..(' '):rep(indent)) | |
end | |
local f = io.open(args[1], 'r'):read('a') | |
f = f:gsub('basic_string%s*%b<>', 'string') | |
f = f:gsub('basic_ptree%s*%b<>', 'ptree') | |
f = f:gsub('sequenced%s*%b<>', 'sequenced') | |
f = f:gsub('[,]?[ \t\n]*mpl_::na', '') | |
f = f:gsub('[,]?[ \t\n]*std::allocator%s*%b<>', '') | |
f = f:gsub('[,]?[ \t\n]*std::less%s*%b<>', '') | |
f = f:gsub('[ \t\n]+', ' ') | |
for i = 1,#f do | |
local c = f:sub(i,i) | |
if c == '<' and skip == 0 then | |
local endpos = f:sub(i,-1):gmatch('%b<>()')() | |
--print('endpos {'..endpos..'}') | |
if f:sub(i,i+endpos):find(',') then | |
io.stdout:write ' <' | |
indent = indent + 1 | |
print_nl() | |
else | |
io.stdout:write '<' | |
skip = endpos - 1 | |
end | |
elseif c == '>' and skip == 0 then | |
indent = indent - 1 | |
print_nl() | |
io.stdout:write '>' | |
elseif c == ',' then | |
io.stdout:write ',' | |
ignore = true | |
print_nl() | |
elseif (c == ' ' and ignore) or c == '\n' then | |
-- do nothing | |
else | |
ignore = false | |
io.stdout:write(c) | |
if skip > 0 then skip = skip - 1 end | |
end | |
end | |
io.stdout:write '\n' | |
io.stdout:flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment