Skip to content

Commit

Permalink
Refactoring the CLI of process, parse and generate
Browse files Browse the repository at this point in the history
All list-like arguments (nargs='+' and nargs='*') are eliminated
except the file list argument of parse. The rationale behind this
change is to make the usage of the tools less error-prone by
making clear where the argument boundaries are.
  • Loading branch information
renatahodovan committed Mar 5, 2020
1 parent 68f32dd commit 1915fe8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Example usage of ``grammarinator-generate``::

grammarinator-generate <generator> -r <start-rule> -d <max-depth> \
-o <output-pattern> -n <number-of-tests> \
-t <one-or-more-transformer>
-t <transformer1> -t <transformer2>

**Notes**

Expand Down
35 changes: 17 additions & 18 deletions grammarinator/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ def import_entity(name):
steps = name.split('.')
return getattr(importlib.import_module('.'.join(steps[0:-1])), steps[-1])

def import_list(lst):
lst = lst or []
if isinstance(lst, str):
lst = json.loads(lst)
return [import_entity(item) for item in lst]

def get_boolean(value):
return value in ['True', True, 1]

Expand Down Expand Up @@ -83,15 +89,8 @@ def get_boolean(value):
self.cleanup = get_boolean(cleanup)
self.encoding = encoding

tree_transformers = tree_transformers or []
if isinstance(tree_transformers, str):
tree_transformers = json.loads(tree_transformers)
self.tree_transformers = [import_entity(transformer) for transformer in tree_transformers]

test_transformers = test_transformers or []
if isinstance(test_transformers, str):
test_transformers = json.loads(test_transformers)
self.test_transformers = [import_entity(transformer) for transformer in test_transformers]
self.tree_transformers = import_list(tree_transformers)
self.test_transformers = import_list(test_transformers)

def __enter__(self):
return self
Expand Down Expand Up @@ -215,17 +214,17 @@ def restricted_float(value):
created by Grammarinator:Processor.
""")
# Settings for generating from grammar.
parser.add_argument('generator', metavar='FILE',
help='reference to the generator created by grammarinator-process (in package.module.class format).')
parser.add_argument('--model', default='grammarinator.model.DefaultModel',
help='reference to the decision model (in package.module.class format) (default: %(default)s).')
parser.add_argument('generator', metavar='NAME',
help='reference to the generator created by grammarinator-process (in package.module.class format).')
parser.add_argument('-r', '--rule', metavar='NAME',
help='name of the rule to start generation from (default: first parser rule).')
parser.add_argument('-t', '--tree-transformers', metavar='LIST', nargs='+', default=[],
help='list of transformers (in package.module.function format) to postprocess the generated tree '
parser.add_argument('-m', '--model', metavar='NAME', default='grammarinator.model.DefaultModel',
help='reference to the decision model (in package.module.class format) (default: %(default)s).')
parser.add_argument('-t', '--tree-transformer', metavar='NAME', action='append', default=[],
help='reference to a transformer (in package.module.function format) to postprocess the generated tree '
'(the result of these transformers will be saved into the serialized tree, e.g., variable matching).')
parser.add_argument('--test-transformers', metavar='LIST', nargs='+', default=[],
help='list of transformers (in package.module.function format) to postprocess the generated tree '
parser.add_argument('--test-transformer', metavar='NAME', action='append', default=[],
help='reference to a transformer (in package.module.function format) to postprocess the generated tree '
'(the result of these transformers will only affect test serialization but won\'t be saved to the '
'tree representation, e.g., space insertion).')
parser.add_argument('-d', '--max-depth', default=inf, type=int, metavar='NUM',
Expand Down Expand Up @@ -277,7 +276,7 @@ def restricted_float(value):
with Generator(generator=args.generator, rule=args.rule, out_format=args.out,
model=args.model, max_depth=args.max_depth, cooldown=args.cooldown,
population=args.population, generate=args.generate, mutate=args.mutate, recombine=args.recombine, keep_trees=args.keep_trees,
tree_transformers=args.tree_transformers, test_transformers=args.test_transformers,
tree_transformers=args.tree_transformer, test_transformers=args.test_transformer,
cleanup=False, encoding=args.encoding) as generator:
if args.jobs > 1:
with Pool(args.jobs) as pool:
Expand Down
18 changes: 9 additions & 9 deletions grammarinator/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ def execute():
compatible tree representations from them and saves them for further
reuse.
""")
parser.add_argument('files', nargs='+',
help='input files to process.')
parser.add_argument('-g', '--grammars', nargs='+', metavar='FILE', required=True,
parser.add_argument('grammar', metavar='FILE', nargs='+',
help='ANTLR grammar files describing the expected format of input to parse.')
parser.add_argument('-i', '--input', metavar='FILE', nargs='+', required=True,
help='input files to process.')
parser.add_argument('-r', '--rule', metavar='NAME',
help='name of the rule to start parsing with (default: first parser rule)')
parser.add_argument('-t', '--transformers', metavar='LIST', nargs='+', default=[],
help='list of transformers (in package.module.function format) to postprocess the parsed tree.')
parser.add_argument('--hidden', nargs='+', metavar='NAME',
help='name of the rule to start parsing with (default: first parser rule).')
parser.add_argument('-t', '--transformer', metavar='NAME', action='append', default=[],
help='reference to a transformer (in package.module.function format) to postprocess the parsed tree.')
parser.add_argument('--hidden', metavar='NAME', action='append', default=[],
help='list of hidden tokens to be built into the parsed tree.')
parser.add_argument('--encoding', metavar='ENC', default='utf-8',
help='input file encoding (default: %(default)s).')
Expand All @@ -179,7 +179,7 @@ def execute():
add_version_argument(parser)
args = parser.parse_args()

for grammar in args.grammars:
for grammar in args.grammar:
if not exists(grammar):
parser.error('{grammar} does not exist.'.format(grammar=grammar))

Expand All @@ -191,7 +191,7 @@ def execute():
process_sys_recursion_limit_argument(args)
process_antlr_argument(args)

with ParserFactory(grammars=args.grammars, hidden=args.hidden, transformers=args.transformers, parser_dir=args.parser_dir, antlr=args.antlr,
with ParserFactory(grammars=args.grammar, hidden=args.hidden, transformers=args.transformer, parser_dir=args.parser_dir, antlr=args.antlr,
max_depth=args.max_depth, cleanup=args.cleanup) as factory:
if args.jobs > 1:
with Pool(args.jobs) as pool:
Expand Down
6 changes: 3 additions & 3 deletions grammarinator/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ def execute():
creates a fuzzer that can generate randomized content conforming to
the format described by the grammar.
""")
parser.add_argument('grammars', nargs='+', metavar='FILE',
parser.add_argument('grammar', metavar='FILE', nargs='+',
help='ANTLR grammar files describing the expected format to generate.')
parser.add_argument('--no-actions', dest='actions', default=True, action='store_false',
help='do not process inline actions.')
Expand All @@ -648,14 +648,14 @@ def execute():
add_version_argument(parser)
args = parser.parse_args()

for grammar in args.grammars:
for grammar in args.grammar:
if not exists(grammar):
parser.error('{grammar} does not exist.'.format(grammar=grammar))

process_log_level_argument(args)
process_antlr_argument(args)

FuzzerFactory(args.out, args.antlr).generate_fuzzer(args.grammars, encoding=args.encoding, lib_dir=args.lib, actions=args.actions, pep8=args.pep8)
FuzzerFactory(args.out, args.antlr).generate_fuzzer(args.grammar, encoding=args.encoding, lib_dir=args.lib, actions=args.actions, pep8=args.pep8)

if args.cleanup:
rmtree(join(args.out, 'antlr'), ignore_errors=True)
Expand Down

0 comments on commit 1915fe8

Please sign in to comment.