mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-17 18:06:02 +08:00
Refactor the parse function to be more readable.
This commit is contained in:
@@ -68,77 +68,69 @@ def parse(code=None, **kwargs):
|
|||||||
|
|
||||||
:return: A syntax tree node. Typically the module.
|
:return: A syntax tree node. Typically the module.
|
||||||
"""
|
"""
|
||||||
# Wanted python3.5 * operator and keyword only arguments.
|
# Wanted python3.5 * operator and keyword only arguments. Therefore just
|
||||||
path = kwargs.pop('path', None)
|
# wrap it all.
|
||||||
grammar = kwargs.pop('grammar', None)
|
def _parse(code=None, path=None, grammar=None, error_recovery=True,
|
||||||
error_recovery = kwargs.pop('error_recovery', True)
|
start_symbol='file_input', cache=False, diff_cache=False,
|
||||||
start_symbol = kwargs.pop('start_symbol', 'file_input')
|
cache_path=None):
|
||||||
cache = kwargs.pop('cache', False)
|
|
||||||
diff_cache = kwargs.pop('diff_cache', False)
|
|
||||||
cache_path = kwargs.pop('cache_path', None)
|
|
||||||
|
|
||||||
if kwargs:
|
if code is None and path is None:
|
||||||
raise TypeError(
|
raise TypeError("Please provide either code or a path.")
|
||||||
"parse() got an unexpected keyword argument '%s'"
|
|
||||||
% next(iter(kwargs)))
|
|
||||||
|
|
||||||
# Start with actual code.
|
if grammar is None:
|
||||||
if code is None and path is None:
|
grammar = load_grammar()
|
||||||
raise TypeError("Please provide either code or a path.")
|
|
||||||
|
|
||||||
if grammar is None:
|
if cache and code is None and path is not None:
|
||||||
grammar = load_grammar()
|
# With the current architecture we cannot load from cache if the
|
||||||
|
# code is given, because we just load from cache if it's not older than
|
||||||
if cache and code is None and path is not None:
|
# the latest change (file last modified).
|
||||||
# With the current architecture we cannot load from cache if the
|
module_node = load_module(grammar, path, cache_path=cache_path)
|
||||||
# code is given, because we just load from cache if it's not older than
|
if module_node is not None:
|
||||||
# the latest change (file last modified).
|
|
||||||
module_node = load_module(grammar, path, cache_path=cache_path)
|
|
||||||
if module_node is not None:
|
|
||||||
return module_node
|
|
||||||
|
|
||||||
if code is None:
|
|
||||||
with open(path, 'rb') as f:
|
|
||||||
code = source_to_unicode(f.read())
|
|
||||||
|
|
||||||
lines = tokenize_lines = splitlines(code, keepends=True)
|
|
||||||
if diff_cache:
|
|
||||||
try:
|
|
||||||
module_cache_item = parser_cache[path]
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
module_node = module_cache_item.node
|
|
||||||
old_lines = module_cache_item.lines
|
|
||||||
if old_lines == lines:
|
|
||||||
# TODO remove this line? I think it's not needed. (dave)
|
|
||||||
save_module(grammar, path, module_node, lines, pickling=False,
|
|
||||||
cache_path=cache_path)
|
|
||||||
return module_node
|
return module_node
|
||||||
|
|
||||||
new_node = DiffParser(grammar, module_node).update(
|
if code is None:
|
||||||
old_lines=old_lines,
|
with open(path, 'rb') as f:
|
||||||
new_lines=lines
|
code = source_to_unicode(f.read())
|
||||||
)
|
|
||||||
save_module(grammar, path, new_node, lines, pickling=cache,
|
lines = tokenize_lines = splitlines(code, keepends=True)
|
||||||
|
if diff_cache:
|
||||||
|
try:
|
||||||
|
module_cache_item = parser_cache[path]
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
module_node = module_cache_item.node
|
||||||
|
old_lines = module_cache_item.lines
|
||||||
|
if old_lines == lines:
|
||||||
|
# TODO remove this line? I think it's not needed. (dave)
|
||||||
|
save_module(grammar, path, module_node, lines, pickling=False,
|
||||||
|
cache_path=cache_path)
|
||||||
|
return module_node
|
||||||
|
|
||||||
|
new_node = DiffParser(grammar, module_node).update(
|
||||||
|
old_lines=old_lines,
|
||||||
|
new_lines=lines
|
||||||
|
)
|
||||||
|
save_module(grammar, path, new_node, lines, pickling=cache,
|
||||||
|
cache_path=cache_path)
|
||||||
|
return new_node
|
||||||
|
|
||||||
|
added_newline = not code.endswith('\n')
|
||||||
|
if added_newline:
|
||||||
|
code += '\n'
|
||||||
|
tokenize_lines = list(tokenize_lines)
|
||||||
|
tokenize_lines[-1] += '\n'
|
||||||
|
tokenize_lines.append('')
|
||||||
|
|
||||||
|
tokens = generate_tokens(tokenize_lines, use_exact_op_types=True)
|
||||||
|
|
||||||
|
p = Parser(grammar, error_recovery=error_recovery, start_symbol=start_symbol)
|
||||||
|
root_node = p.parse(tokens=tokens)
|
||||||
|
if added_newline:
|
||||||
|
remove_last_newline(root_node)
|
||||||
|
|
||||||
|
if cache or diff_cache:
|
||||||
|
save_module(grammar, path, root_node, lines, pickling=cache,
|
||||||
cache_path=cache_path)
|
cache_path=cache_path)
|
||||||
return new_node
|
return root_node
|
||||||
|
return _parse(code=code, **kwargs)
|
||||||
added_newline = not code.endswith('\n')
|
|
||||||
if added_newline:
|
|
||||||
code += '\n'
|
|
||||||
tokenize_lines = list(tokenize_lines)
|
|
||||||
tokenize_lines[-1] += '\n'
|
|
||||||
tokenize_lines.append('')
|
|
||||||
|
|
||||||
tokens = generate_tokens(tokenize_lines, use_exact_op_types=True)
|
|
||||||
|
|
||||||
p = Parser(grammar, error_recovery=error_recovery, start_symbol=start_symbol)
|
|
||||||
root_node = p.parse(tokens=tokens)
|
|
||||||
if added_newline:
|
|
||||||
remove_last_newline(root_node)
|
|
||||||
|
|
||||||
if cache or diff_cache:
|
|
||||||
save_module(grammar, path, root_node, lines, pickling=cache,
|
|
||||||
cache_path=cache_path)
|
|
||||||
return root_node
|
|
||||||
|
|||||||
Reference in New Issue
Block a user