Skip to content

Commit 1d4c067

Browse files
author
Ian
committed
[!@] Updated to 2017-10 version.
[+] Command-line parameters support. [.] Cross-platform shell runner (due luarocks feature). Peter Melnichenko pointed to this feature. (https://github.com/mpeterv) [+] [scm.rockspec] Latest-version rockspec moved to this repository.
1 parent 7938787 commit 1d4c067

33 files changed

+722
-115
lines changed

get_ast.lua

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
local get_params = request('get_ast.get_params')
12
local convert = request('!.file.convert')
23
local get_ast = request('!.lua.code.get_ast')
34

45
return
5-
function(f_in_name, f_out_name)
6+
function(args)
7+
local f_in_name, f_out_name = get_params(args)
8+
if not f_in_name then
9+
return
10+
end
611
convert(
712
{
813
f_in_name = f_in_name,

get_ast/get_params.lua

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
--[[
2+
Parse table with command line and return
3+
4+
<f_in_name> <f_out_name>
5+
6+
of fail and return nil.
7+
]]
8+
9+
local usage_text =
10+
[[
11+
Get annotated syntax tree (AST) for Lua 5.3 code.
12+
13+
lua.get_ast <f_in> [<f_out>]
14+
15+
-- Martin, 2017-10-14
16+
]]
17+
18+
local cmdline_processor =
19+
new(request('!.mechs.command_line_processor.interface'))
20+
cmdline_processor.allowed_params =
21+
{
22+
{name = 'f_in_name', type = 'string'},
23+
{name = 'f_out_name', type = 'string'},
24+
}
25+
26+
return
27+
function(args)
28+
assert_table(args)
29+
if not args[1] or (args[1] == '--help') then
30+
print(usage_text)
31+
return
32+
end
33+
34+
local params = cmdline_processor:run(args)
35+
if not params.f_in_name then
36+
print(usage_text)
37+
return
38+
end
39+
40+
local f_in_name, f_out_name
41+
f_in_name = params.f_in_name
42+
f_out_name = params.f_out_name or (f_in_name .. '.ast')
43+
44+
return f_in_name, f_out_name
45+
end

get_formatter_ast.lua

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
local get_params = request('get_formatter_ast.get_params')
12
local convert = request('!.file.convert')
2-
33
local get_ast = request('!.lua.code.get_ast')
44
local formatter_preprocess = request('!.formats.lua.save.formatter.preprocess')
55

@@ -12,7 +12,11 @@ local parse =
1212
end
1313

1414
return
15-
function(f_in_name, f_out_name)
15+
function(args)
16+
local f_in_name, f_out_name = get_params(args)
17+
if not f_in_name then
18+
return
19+
end
1620
convert(
1721
{
1822
f_in_name = f_in_name,

get_formatter_ast/get_params.lua

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
--[[
2+
Parse table with command line and return
3+
4+
<f_in_name> <f_out_name>
5+
6+
or fail and return nil.
7+
]]
8+
9+
local usage_text =
10+
[[
11+
Get transformed annotated syntax tree (AST) for Lua 5.3 code.
12+
13+
lua.get_formatter_ast <f_in> [<f_out>]
14+
15+
-- Martin, 2017-10-14
16+
]]
17+
18+
local cmdline_processor =
19+
new(request('!.mechs.command_line_processor.interface'))
20+
cmdline_processor.allowed_params =
21+
{
22+
{name = 'f_in_name', type = 'string'},
23+
{name = 'f_out_name', type = 'string'},
24+
}
25+
26+
return
27+
function(args)
28+
assert_table(args)
29+
if not args[1] or (args[1] == '--help') then
30+
print(usage_text)
31+
return
32+
end
33+
34+
local params = cmdline_processor:run(args)
35+
if not params.f_in_name then
36+
print(usage_text)
37+
return
38+
end
39+
40+
local f_in_name, f_out_name
41+
f_in_name = params.f_in_name
42+
f_out_name = params.f_out_name or (f_in_name .. '.fast')
43+
44+
return f_in_name, f_out_name
45+
end

lcf-scm-1.rockspec

+313
Large diffs are not rendered by default.

lua_get_ast.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
require('lcf.workshop.base')
2-
local run = request('lcf.get_ast')
3-
run(arg[1], arg[2])
2+
local f = request('lcf.get_ast')
3+
f(_G.arg)

lua_get_formatter_ast.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
require('lcf.workshop.base')
2-
local run = request('lcf.get_formatter_ast')
3-
run(arg[1], arg[2])
2+
local f = request('lcf.get_formatter_ast')
3+
f(_G.arg)

lua_reformat.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
require('lcf.workshop.base')
2-
local run = request('lcf.reformat')
3-
run(arg[1], arg[2])
2+
local f = request('lcf.reformat')
3+
f(_G.arg)

readme.txt

+9-5
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,24 @@ Requirements
1919
(I've made a backport for Lua v5.1. See "5.1" branch. It is not
2020
affected by recent changes and probably will be dropped someday.)
2121

22+
It may or may not work under Windows. I've not tested it there.
23+
2224

2325
Installation
2426

25-
sudo luarocks install lcf
27+
> sudo luarocks make lcf-scm-1.rockspec
28+
29+
(also works "sudo luarocks install lcf")
2630

2731

2832
Usage
2933

3034
From command-line
3135

32-
lua.reformat <f_in> <f_out>
36+
> lua.reformat <f_in>
37+
38+
You can pass formatter parameters in command line. For
39+
syntax call "lua.reformat" without parameters.
3340

3441

3542
From Lua interpreter
@@ -106,8 +113,5 @@ Usage
106113

107114
--
108115
2016-08-16
109-
2017-01-03
110-
2017-01-18
111-
2017-01-22
112116
2017-01-28
113117
2017-09-26

reformat.lua

+11-17
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
1-
local pretty_lua_options =
2-
{
3-
indent_chunk = ' ',
4-
right_margin = 100,
5-
max_text_width = 65,
6-
keep_comments = true,
7-
}
8-
9-
local serialize_ast = request('!.formats.lua.save')
10-
local pretty_lua =
11-
function(struc)
12-
return serialize_ast(struc, pretty_lua_options)
13-
end
14-
1+
local get_params = request('reformat.get_params')
152
local get_ast = request('!.lua.code.get_ast')
3+
local serialize_ast = request('!.formats.lua.save')
164
local convert = request('!.file.convert')
175

186
return
19-
function(f_in_name, f_out_name)
7+
function(args)
8+
local f_in_name, f_out_name, formatter_options = get_params(args)
9+
if not f_in_name then
10+
return
11+
end
2012
convert(
2113
{
2214
f_in_name = f_in_name,
2315
f_out_name = f_out_name,
24-
description = 'Reformat Lua 5.3 code.',
2516
parse = get_ast,
26-
compile = pretty_lua,
17+
compile =
18+
function(struc)
19+
return serialize_ast(struc, formatter_options)
20+
end,
2721
}
2822
)
2923
end

reformat/get_params.lua

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
--[[
2+
Parse given command line and return three values:
3+
4+
<f_in_name> <f_out_name> <formatter_options>
5+
6+
Alternatively may print usage help and return nothing.
7+
]]
8+
9+
local usage_text = request('usage_text')
10+
11+
local cmdline_processor =
12+
new(request('!.mechs.command_line_processor.interface'))
13+
cmdline_processor.allowed_params =
14+
{
15+
{name = 'f_in_name', type = 'string'},
16+
{name = 'f_out_name', type = 'string'},
17+
{name = 'right-margin', type = 'key_int'},
18+
{name = 'max-text-width', type = 'key_int'},
19+
{name = 'keep-comments', type = 'flag'},
20+
{name = 'indent', type = 'key_str'},
21+
}
22+
23+
--[[
24+
Although original default parameters are stored in
25+
[lua.save.formatter.interface] I override it here.
26+
]]
27+
local default_formatter_options =
28+
{
29+
right_margin = 120,
30+
max_text_width = 100,
31+
indent_chunk = ' ',
32+
keep_comments = true,
33+
}
34+
35+
local table_to_str = request('!.formats.lua_table.save')
36+
37+
return
38+
function(args)
39+
assert_table(args)
40+
if not args[1] or (args[1] == '--help') then
41+
print(usage_text)
42+
print('Default options:')
43+
print(table_to_str(default_formatter_options))
44+
return
45+
end
46+
47+
local params = cmdline_processor:run(args)
48+
49+
local f_in_name, f_out_name
50+
f_in_name = params.f_in_name
51+
if not f_in_name then
52+
print(usage_text)
53+
return
54+
end
55+
f_out_name = params.f_out_name or (f_in_name .. '.formatted')
56+
57+
params['max-text-width'] = params['max-text-width'] or params['right-margin']
58+
params['right-margin'] = params['right-margin'] or params['max-text-width']
59+
60+
local formatter_options =
61+
new(
62+
default_formatter_options,
63+
{
64+
indent_chunk = params.indent,
65+
right_margin = params['right-margin'],
66+
max_text_width = params['max-text-width'],
67+
keep_comments = params['keep-comments'],
68+
}
69+
)
70+
71+
return f_in_name, f_out_name, formatter_options
72+
end

reformat/usage_text.lua

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
return
2+
[[
3+
Reformat Lua 5.3 code.
4+
5+
lua.reformat <f_in> [<f_out>]
6+
^ ^ ^
7+
| | |
8+
+-- [<options>] -----+
9+
10+
<options>:
11+
--right-margin=<int> -- Right margin, indent chars counts.
12+
--max-text-width=<int> -- Maximum text length, ignoring indent chars.
13+
--keep-comments -- Keep comments.
14+
--~keep-comments -- Remove comments.
15+
--indent=<str> -- Use given string as indent chunk
16+
17+
Notes
18+
* In case when only one parameter of (right-margin, max-text-width)
19+
is given, another parameter is set to same value. This is done
20+
because these parameters have similar effect and in other case
21+
you wish to set another parameter to near value manually.
22+
23+
* If <f_out> name is not given, it's generated using <f_in> name.
24+
25+
-- Martin, 2017-10-14
26+
]]

workshop/file/convert.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
--[[
2-
Simplified interface for [generic_file_converter].
2+
Action-call of [generic_file_converter].
33
]]
44

5-
local c_converter = request('!.frontend.text.generic_file_converter.interface')
5+
local c_converter = request('!.mechs.generic_file_converter.interface')
66

77
return
88
function(params)

workshop/file/safe_open.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
return
2-
function(file_name, mode, no_errors)
2+
function(file_name, mode)
33
assert_string(file_name)
44
mode = mode or 'rb'
55
assert_string(mode)
66
local f_object, err_msg = io.open(file_name, mode)
7-
if not f_object and not no_errors then
7+
if not f_object then
88
error(err_msg, 2)
99
end
1010
return f_object

workshop/formats/lua_table/save.lua

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- Serialize lua table as string with lua table definition.
2+
3+
-- Not suitable for tables with cross-links in keys or values.
4+
5+
local c_table_serializer = request('save.interface')
6+
local compile = request('!.struc.compile')
7+
8+
return
9+
function(t, options)
10+
assert_table(t)
11+
local table_serializer = new(c_table_serializer, options)
12+
table_serializer:init()
13+
local ast = table_serializer:get_ast(t)
14+
local result = table_serializer:serialize_ast(ast)
15+
return result
16+
end

workshop/frontend/text/critical_print.lua

-5
This file was deleted.

workshop/frontend/text/generic_file_converter/error.lua

-10
This file was deleted.

workshop/frontend/text/generic_file_converter/get_state_str.lua

-13
This file was deleted.

0 commit comments

Comments
 (0)