Refactor the parse function to be more readable.

This commit is contained in:
Dave Halter
2017-05-21 13:50:02 -04:00
parent a5b9177d4f
commit 973f7c5f61

View File

@@ -68,21 +68,12 @@ def parse(code=None, **kwargs):
:return: A syntax tree node. Typically the module.
"""
# Wanted python3.5 * operator and keyword only arguments.
path = kwargs.pop('path', None)
grammar = kwargs.pop('grammar', None)
error_recovery = kwargs.pop('error_recovery', True)
start_symbol = kwargs.pop('start_symbol', 'file_input')
cache = kwargs.pop('cache', False)
diff_cache = kwargs.pop('diff_cache', False)
cache_path = kwargs.pop('cache_path', None)
# Wanted python3.5 * operator and keyword only arguments. Therefore just
# wrap it all.
def _parse(code=None, path=None, grammar=None, error_recovery=True,
start_symbol='file_input', cache=False, diff_cache=False,
cache_path=None):
if kwargs:
raise TypeError(
"parse() got an unexpected keyword argument '%s'"
% next(iter(kwargs)))
# Start with actual code.
if code is None and path is None:
raise TypeError("Please provide either code or a path.")
@@ -142,3 +133,4 @@ def parse(code=None, **kwargs):
save_module(grammar, path, root_node, lines, pickling=cache,
cache_path=cache_path)
return root_node
return _parse(code=code, **kwargs)