-
Notifications
You must be signed in to change notification settings - Fork 50
/
CommandLineLiveBeautify.lua
47 lines (44 loc) · 1.13 KB
/
CommandLineLiveBeautify.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
--
-- beautify.interactive
--
-- For testing: Lets you enter lines of text to be beautified to verify the
-- correctness of their implementation.
--
local util = require'Util'
local Parser = require'ParseLua'
local Format_Beautify = require'FormatBeautiful'
local ParseLua = Parser.ParseLua
local PrintTable = util.PrintTable
while true do
io.write('> ')
local line = io.read('*line')
local fileFrom, fileTo = line:match("^file (.*) (.*)")
if fileFrom and fileTo then
local file = io.open(fileFrom, 'r')
local fileTo = io.open(fileTo, 'w')
if file and fileTo then
local st, ast = ParseLua(file:read('*all'))
if st then
fileTo:write(Format_Beautify(ast)..'\n')
io.write("Beautification Complete\n")
else
io.write(""..tostring(ast).."\n")
end
file:close()
fileTo:close()
else
io.write("File does not exist\n")
end
else
local st, ast = ParseLua(line)
if st then
io.write("====== AST =======\n")
io.write(PrintTable(ast)..'\n')
io.write("==== BEAUTIFIED ====\n")
io.write(Format_Beautify(ast))
io.write("==================\n")
else
io.write(""..tostring(ast).."\n")
end
end
end