mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-07 05:14:29 +08:00
load_python_grammar -> load_grammar.
This commit is contained in:
@@ -2,8 +2,8 @@
|
|||||||
parso is a Python parser. It's really easy to use and supports multiple Python
|
parso is a Python parser. It's really easy to use and supports multiple Python
|
||||||
versions, file caching, round-trips and other stuff:
|
versions, file caching, round-trips and other stuff:
|
||||||
|
|
||||||
>>> from parso import load_python_grammar
|
>>> from parso import load_grammar
|
||||||
>>> grammar = load_python_grammar(version='2.7')
|
>>> grammar = load_grammar(version='2.7')
|
||||||
>>> module = grammar.parse('hello + 1')
|
>>> module = grammar.parse('hello + 1')
|
||||||
>>> stmt = module.children[0]
|
>>> stmt = module.children[0]
|
||||||
>>> stmt
|
>>> stmt
|
||||||
@@ -18,7 +18,7 @@ PythonNode(simple_stmt, [PythonNode(arith_expr, [...]), <Newline: ''>])
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from parso.parser import ParserSyntaxError
|
from parso.parser import ParserSyntaxError
|
||||||
from parso.grammar import create_grammar, load_python_grammar
|
from parso.grammar import create_grammar, load_grammar
|
||||||
|
|
||||||
|
|
||||||
__version__ = '0.0.3'
|
__version__ = '0.0.3'
|
||||||
@@ -29,5 +29,5 @@ def parse(code=None, **kwargs):
|
|||||||
A utility function to parse Python with the current Python version. Params
|
A utility function to parse Python with the current Python version. Params
|
||||||
are documented in ``Grammar.parse``.
|
are documented in ``Grammar.parse``.
|
||||||
"""
|
"""
|
||||||
grammar = load_python_grammar()
|
grammar = load_grammar()
|
||||||
return grammar.parse(code, **kwargs)
|
return grammar.parse(code, **kwargs)
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ def create_grammar(text, tokenizer=generate_tokens, parser=BaseParser):
|
|||||||
return Grammar(text, tokenizer=tokenizer, parser=parser)
|
return Grammar(text, tokenizer=tokenizer, parser=parser)
|
||||||
|
|
||||||
|
|
||||||
def load_python_grammar(version=None):
|
def load_grammar(version=None):
|
||||||
"""
|
"""
|
||||||
Loads a Python grammar. The default version is always the latest.
|
Loads a Python grammar. The default version is always the latest.
|
||||||
|
|
||||||
@@ -160,4 +160,4 @@ def load_python_grammar(version=None):
|
|||||||
return _loaded_grammars.setdefault(path, grammar)
|
return _loaded_grammars.setdefault(path, grammar)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
# Just load the default if the file does not exist.
|
# Just load the default if the file does not exist.
|
||||||
return load_python_grammar()
|
return load_grammar()
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import pytest
|
|||||||
|
|
||||||
from parso.cache import _NodeCacheItem, save_module, load_module, \
|
from parso.cache import _NodeCacheItem, save_module, load_module, \
|
||||||
_get_hashed_path, parser_cache, _load_from_file_system, _save_to_file_system
|
_get_hashed_path, parser_cache, _load_from_file_system, _save_to_file_system
|
||||||
from parso import load_python_grammar
|
from parso import load_grammar
|
||||||
from parso import cache
|
from parso import cache
|
||||||
|
|
||||||
|
|
||||||
@@ -37,7 +37,7 @@ def test_modulepickling_change_cache_dir(tmpdir):
|
|||||||
path_1 = 'fake path 1'
|
path_1 = 'fake path 1'
|
||||||
path_2 = 'fake path 2'
|
path_2 = 'fake path 2'
|
||||||
|
|
||||||
grammar = load_python_grammar()
|
grammar = load_grammar()
|
||||||
_save_to_file_system(grammar, path_1, item_1, cache_path=dir_1)
|
_save_to_file_system(grammar, path_1, item_1, cache_path=dir_1)
|
||||||
parser_cache.clear()
|
parser_cache.clear()
|
||||||
cached = load_stored_item(grammar, path_1, item_1, cache_path=dir_1)
|
cached = load_stored_item(grammar, path_1, item_1, cache_path=dir_1)
|
||||||
@@ -69,7 +69,7 @@ def test_modulepickling_simulate_deleted_cache(tmpdir):
|
|||||||
|
|
||||||
__ https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
|
__ https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
|
||||||
"""
|
"""
|
||||||
grammar = load_python_grammar()
|
grammar = load_grammar()
|
||||||
module = 'fake parser'
|
module = 'fake parser'
|
||||||
|
|
||||||
# Create the file
|
# Create the file
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import pytest
|
|||||||
|
|
||||||
from parso.utils import splitlines
|
from parso.utils import splitlines
|
||||||
from parso import cache
|
from parso import cache
|
||||||
from parso import load_python_grammar
|
from parso import load_grammar
|
||||||
from parso.python.diff import DiffParser
|
from parso.python.diff import DiffParser
|
||||||
from parso import parse
|
from parso import parse
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@ def test_simple():
|
|||||||
"""
|
"""
|
||||||
The diff parser reuses modules. So check for that.
|
The diff parser reuses modules. So check for that.
|
||||||
"""
|
"""
|
||||||
grammar = load_python_grammar()
|
grammar = load_grammar()
|
||||||
module_a = grammar.parse('a', diff_cache=True)
|
module_a = grammar.parse('a', diff_cache=True)
|
||||||
assert grammar.parse('b', diff_cache=True) == module_a
|
assert grammar.parse('b', diff_cache=True) == module_a
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ def _assert_valid_graph(node):
|
|||||||
|
|
||||||
|
|
||||||
class Differ(object):
|
class Differ(object):
|
||||||
grammar = load_python_grammar()
|
grammar = load_grammar()
|
||||||
|
|
||||||
def initialize(self, code):
|
def initialize(self, code):
|
||||||
logging.debug('differ: initialize')
|
logging.debug('differ: initialize')
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import pytest
|
|||||||
|
|
||||||
from parso._compatibility import u, py_version
|
from parso._compatibility import u, py_version
|
||||||
from parso import parse
|
from parso import parse
|
||||||
from parso import load_python_grammar
|
from parso import load_grammar
|
||||||
from parso.python import tree
|
from parso.python import tree
|
||||||
from parso.utils import splitlines
|
from parso.utils import splitlines
|
||||||
|
|
||||||
@@ -113,7 +113,7 @@ def test_param_splitting():
|
|||||||
"""
|
"""
|
||||||
def check(src, result):
|
def check(src, result):
|
||||||
# Python 2 tuple params should be ignored for now.
|
# Python 2 tuple params should be ignored for now.
|
||||||
grammar = load_python_grammar('%s.%s' % sys.version_info[:2])
|
grammar = load_grammar('%s.%s' % sys.version_info[:2])
|
||||||
m = grammar.parse(src)
|
m = grammar.parse(src)
|
||||||
if py_version >= 30:
|
if py_version >= 30:
|
||||||
assert not list(m.iter_funcdefs())
|
assert not list(m.iter_funcdefs())
|
||||||
@@ -161,10 +161,10 @@ def test_python3_octal():
|
|||||||
def test_load_newer_grammar():
|
def test_load_newer_grammar():
|
||||||
# This version shouldn't be out for a while, but if we somehow get this it
|
# This version shouldn't be out for a while, but if we somehow get this it
|
||||||
# should just take the latest Python grammar.
|
# should just take the latest Python grammar.
|
||||||
load_python_grammar('15.8')
|
load_grammar('15.8')
|
||||||
# The same is true for very old grammars (even though this is probably not
|
# The same is true for very old grammars (even though this is probably not
|
||||||
# going to be an issue.
|
# going to be an issue.
|
||||||
load_python_grammar('1.5')
|
load_grammar('1.5')
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('code', ['foo "', 'foo """\n', 'foo """\nbar'])
|
@pytest.mark.parametrize('code', ['foo "', 'foo """\n', 'foo """\nbar'])
|
||||||
|
|||||||
@@ -9,14 +9,14 @@ test_grammar.py files from both Python 2 and Python 3.
|
|||||||
from textwrap import dedent
|
from textwrap import dedent
|
||||||
|
|
||||||
from parso._compatibility import py_version
|
from parso._compatibility import py_version
|
||||||
from parso import load_python_grammar
|
from parso import load_grammar
|
||||||
from parso import ParserSyntaxError
|
from parso import ParserSyntaxError
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
def parse(code, version='3.4'):
|
def parse(code, version='3.4'):
|
||||||
code = dedent(code) + "\n\n"
|
code = dedent(code) + "\n\n"
|
||||||
grammar = load_python_grammar(version=version)
|
grammar = load_grammar(version=version)
|
||||||
return grammar.parse(code, error_recovery=False)
|
return grammar.parse(code, error_recovery=False)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user