mirror of
https://github.com/davidhalter/parso.git
synced 2026-05-24 09:18:52 +08:00
Use keyword only arguments in grammar.py
This commit is contained in:
+5
-19
@@ -40,7 +40,9 @@ class Grammar(object):
|
|||||||
self._diff_parser = diff_parser
|
self._diff_parser = diff_parser
|
||||||
self._hashed = hashlib.sha256(text.encode("utf-8")).hexdigest()
|
self._hashed = hashlib.sha256(text.encode("utf-8")).hexdigest()
|
||||||
|
|
||||||
def parse(self, code=None, **kwargs):
|
def parse(self, code=None, *, error_recovery=True, path=None,
|
||||||
|
start_symbol=None, cache=False, diff_cache=False,
|
||||||
|
cache_path=None, file_io=None):
|
||||||
"""
|
"""
|
||||||
If you want to parse a Python file you want to start here, most likely.
|
If you want to parse a Python file you want to start here, most likely.
|
||||||
|
|
||||||
@@ -75,19 +77,6 @@ class Grammar(object):
|
|||||||
:return: A subclass of :py:class:`parso.tree.NodeOrLeaf`. Typically a
|
:return: A subclass of :py:class:`parso.tree.NodeOrLeaf`. Typically a
|
||||||
:py:class:`parso.python.tree.Module`.
|
:py:class:`parso.python.tree.Module`.
|
||||||
"""
|
"""
|
||||||
if 'start_pos' in kwargs:
|
|
||||||
raise TypeError("parse() got an unexpected keyword argument.")
|
|
||||||
return self._parse(code=code, **kwargs)
|
|
||||||
|
|
||||||
def _parse(self, code=None, error_recovery=True, path=None,
|
|
||||||
start_symbol=None, cache=False, diff_cache=False,
|
|
||||||
cache_path=None, file_io=None, start_pos=(1, 0)):
|
|
||||||
"""
|
|
||||||
Wanted python3.5 * operator and keyword only arguments. Therefore just
|
|
||||||
wrap it all.
|
|
||||||
start_pos here is just a parameter internally used. Might be public
|
|
||||||
sometime in the future.
|
|
||||||
"""
|
|
||||||
if code is None and path is None and file_io is None:
|
if code is None and path is None and file_io is None:
|
||||||
raise TypeError("Please provide either code or a path.")
|
raise TypeError("Please provide either code or a path.")
|
||||||
|
|
||||||
@@ -139,7 +128,7 @@ class Grammar(object):
|
|||||||
cache_path=cache_path)
|
cache_path=cache_path)
|
||||||
return new_node
|
return new_node
|
||||||
|
|
||||||
tokens = self._tokenizer(lines, start_pos=start_pos)
|
tokens = self._tokenizer(lines)
|
||||||
|
|
||||||
p = self._parser(
|
p = self._parser(
|
||||||
self._pgen_grammar,
|
self._pgen_grammar,
|
||||||
@@ -224,7 +213,7 @@ class PythonGrammar(Grammar):
|
|||||||
return tokenize(code, self.version_info)
|
return tokenize(code, self.version_info)
|
||||||
|
|
||||||
|
|
||||||
def load_grammar(**kwargs):
|
def load_grammar(*, language='python', version=None, path=None):
|
||||||
"""
|
"""
|
||||||
Loads a :py:class:`parso.Grammar`. The default version is the current Python
|
Loads a :py:class:`parso.Grammar`. The default version is the current Python
|
||||||
version.
|
version.
|
||||||
@@ -232,7 +221,6 @@ def load_grammar(**kwargs):
|
|||||||
:param str version: A python version string, e.g. ``version='3.8'``.
|
:param str version: A python version string, e.g. ``version='3.8'``.
|
||||||
:param str path: A path to a grammar file
|
:param str path: A path to a grammar file
|
||||||
"""
|
"""
|
||||||
def load_grammar(language='python', version=None, path=None):
|
|
||||||
if language == 'python':
|
if language == 'python':
|
||||||
version_info = parse_version_string(version)
|
version_info = parse_version_string(version)
|
||||||
|
|
||||||
@@ -259,5 +247,3 @@ def load_grammar(**kwargs):
|
|||||||
raise NotImplementedError(message)
|
raise NotImplementedError(message)
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError("No support for language %s." % language)
|
raise NotImplementedError("No support for language %s." % language)
|
||||||
|
|
||||||
return load_grammar(**kwargs)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user