Commit 8f23c550 authored by Jamie A. Jennings's avatar Jamie A. Jennings

Improved the CLI help (can now use --help everywhere)

parent 07581b65
......@@ -2,7 +2,7 @@
--
-- cli-parser.lua
--
-- © Copyright Jamie A. Jennings 2018.
-- © Copyright Jamie A. Jennings 2018, 2019.
-- © Copyright IBM Corporation 2017.
-- LICENSE: MIT License (https://opensource.org/licenses/mit-license.html)
-- AUTHORS: Jamie A. Jennings, Kevin Zander
......@@ -17,7 +17,7 @@ local p = {}
function p.create(rosie)
local argparse = assert(rosie.import("argparse"), "failed to load argparse package")
local parser = argparse("rosie", "Rosie " .. rosie.attributes.ROSIE_VERSION)
parser:add_help(false)
parser:add_help("--help")
parser:require_command(false)
parser:flag("--verbose", "Output additional messages")
:default(false)
......@@ -83,69 +83,74 @@ function p.create(rosie)
-- target variable for commands
parser:command_target("command")
local commands = {}
-- version command
local cmd_version = parser:command("version")
commands.version = parser:command("version")
:description("Print rosie version")
-- help command
local cmd_help = parser:command("help")
commands.help = parser:command("help")
:description("Print this help message")
-- config command
local cmd_info = parser:command("config")
commands.info = parser:command("config")
:description("Print rosie configuration information")
-- list command
local cmd_patterns = parser:command("list")
commands.patterns = parser:command("list")
:description("List patterns, packages, and macros")
cmd_patterns:argument("filter")
commands.patterns:argument("filter")
:description("List all names that have substring 'filter'")
:default("*")
:args(1)
-- grep command
local cmd_grep = parser:command("grep")
commands.grep = parser:command("grep")
:description("In the style of Unix grep, match the pattern anywhere in each input line")
cmd_grep:option("-o --output", "Output style, one of: " .. output_choices_string)
commands.grep:option("-o --output", "Output style, one of: " .. output_choices_string)
:convert(validate_output_encoder_choice)
:args(1) -- consume argument after option
:target("encoder")
-- match command
local cmd_match = parser:command("match")
commands.match = parser:command("match")
:description("Match the given RPL pattern against the input")
cmd_match:option("-o --output", "Output style, one of: " .. output_choices_string)
commands.match:option("-o --output", "Output style, one of: " .. output_choices_string)
:convert(validate_output_encoder_choice)
:args(1) -- consume argument after option
:target("encoder")
-- repl command
local cmd_repl = parser:command("repl")
commands.repl = parser:command("repl")
:description("Start the read-eval-print loop for interactive pattern development and debugging")
-- test command
local cmd_test = parser:command("test")
commands.test = parser:command("test")
:description("Execute pattern tests written within the target rpl file(s)")
cmd_test:argument("filenames", "RPL filenames")
commands.test:argument("filenames", "RPL filenames")
:args("+")
-- expand command
local cmd_expand = parser:command("expand")
commands.expand = parser:command("expand")
:description("Expand an rpl expression to see the input to the rpl compiler")
:argument("expression")
commands.expand:argument("expression")
:args(1)
-- trace command
local cmd_trace = parser:command("trace")
commands.trace = parser:command("trace")
:description("Match while tracing all steps (generates MUCH output)")
cmd_trace:option("-o --output", "Output style, one of: " .. trace_style_choices_string)
commands.trace:option("-o --output", "Output style, one of: " .. trace_style_choices_string)
:convert(validate_trace_style_choice)
:args(1) -- consume argument after option
:default("condensed")
:target("encoder")
-- TEMPORARY match command that loads the pattern from a (compiled rplx) file
local cmd_rplxmatch = parser:command("rplxmatch")
commands.rplxmatch = parser:command("rplxmatch")
:description("Match the using the compiled pattern stored in the argument (an rplx file)")
cmd_rplxmatch:option("-o --output", "Output style, one of: " .. output_choices_string)
commands.rplxmatch:option("-o --output", "Output style, one of: " .. output_choices_string)
:convert(validate_output_encoder_choice)
:args(1) -- consume argument after option
:target("encoder")
for name, cmd in pairs(commands) do
cmd:add_help("--help")
end
-- Common flags and arguments across match, trace, and grep
for _, cmd in ipairs{cmd_match, cmd_trace, cmd_grep, cmd_rplxmatch} do
for _, cmd in ipairs{commands.match, commands.trace, commands.grep, commands.rplxmatch} do
-- match/trace/grep flags (true/false)
cmd:flag("-w --wholefile", "Read the whole input file as single string")
:default(false)
......@@ -166,7 +171,7 @@ function p.create(rosie)
end
-- Common flags across match, and grep
for _, cmd in ipairs{cmd_match, cmd_grep, cmd_rplxmatch} do
for _, cmd in ipairs{commands.match, commands.grep, commands.rplxmatch} do
-- match/trace/grep flags (true/false)
cmd:flag("--time", "Time each match, writing to stderr after each output")
:default(false)
......
......@@ -75,6 +75,7 @@ local function run(args)
if not args.command then
print("Usage: rosie command [options] pattern file [...]")
print("Use the 'help' command to see all commands.")
return cli_common.ERROR_USAGE
end
......
......@@ -74,8 +74,6 @@ local function new_stack()
setmetatable(data, stack_mt)
return data
end
-- TEMP:
STACK = new_stack
local function is_atmosphere(pt)
return not common.not_atmosphere(pt)
......@@ -318,12 +316,11 @@ function shunting_yard_explist(subs, attribute_table)
exps:push(e)
elseif e.type == "operator" then
while not should_push(e.data, opstack:peek(), attribute_table) do
-- To produce a postfix sequence of exps and ops, as original
-- shunting yard algorithm did:
-- exps:push(opstack:pop())
-- Instead, we will convert to prefix in tree form. Would be
-- simpler except that there's atmosphere on the exps stack.
-- Convert to prefix in tree form.
exps:push(make_prefix_tree(opstack:pop(), exps))
-- To produce a postfix sequence of exps and ops, as the original
-- shunting yard algorithm did, do this instead of the above:
-- exps:push(opstack:pop())
end
opstack:push(e)
else
......@@ -577,3 +574,38 @@ function setup()
end
return infix
-- Demonstration of how the infix grammar in rpl_1_3 (combined with shunting
-- yard) eliminates the exponential behavior of the original grammar from
-- rpl_1_2:
-- e is an engine set up with rpl_1_3 and shunting yard
-- orig is a plain rpl_1_2 engine
--
-- Performance is roughly equal for no parens and 2 parens, and significantly
-- better when there are more.
--
-- > -- 6 sets of parens
-- > collectgarbage('collect'); t0=os.clock(); for i=1,10000 do x = e:compile('(((((("a"))))))'); end; t1=os.clock(); print((t1-t0)/10)
-- 0.4165246
-- > collectgarbage('collect'); t0=os.clock(); for i=1,10000 do x = orig:compile('(((((("a"))))))'); end; t1=os.clock(); print((t1-t0)/10)
-- 3.097605
-- >
-- > -- 4 sets of parens
-- > collectgarbage('collect'); t0=os.clock(); for i=1,10000 do x = e:compile('(((("a"))))'); end; t1=os.clock(); print((t1-t0)/10)
-- 0.3444756
-- > collectgarbage('collect'); t0=os.clock(); for i=1,10000 do x = orig:compile('(((("a"))))'); end; t1=os.clock(); print((t1-t0)/10)
-- 0.5154367
-- >
-- > -- 2 sets of parens
-- > collectgarbage('collect'); t0=os.clock(); for i=1,10000 do x = e:compile('(("a"))'); end; t1=os.clock(); print((t1-t0)/10)
-- 0.2667446
-- > collectgarbage('collect'); t0=os.clock(); for i=1,10000 do x = orig:compile('(("a"))'); end; t1=os.clock(); print((t1-t0)/10)
-- 0.2808682
-- >
-- > -- 0 sets of parens
-- > collectgarbage('collect'); t0=os.clock(); for i=1,10000 do x = e:compile('"a"'); end; t1=os.clock(); print((t1-t0)/10)
-- 0.181641
-- > collectgarbage('collect'); t0=os.clock(); for i=1,10000 do x = orig:compile('"a"'); end; t1=os.clock(); print((t1-t0)/10)
-- 0.1787887
-- >
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment