mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-06 21:04: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
|
||||
versions, file caching, round-trips and other stuff:
|
||||
|
||||
>>> from parso import load_python_grammar
|
||||
>>> grammar = load_python_grammar(version='2.7')
|
||||
>>> from parso import load_grammar
|
||||
>>> grammar = load_grammar(version='2.7')
|
||||
>>> module = grammar.parse('hello + 1')
|
||||
>>> stmt = module.children[0]
|
||||
>>> stmt
|
||||
@@ -18,7 +18,7 @@ PythonNode(simple_stmt, [PythonNode(arith_expr, [...]), <Newline: ''>])
|
||||
"""
|
||||
|
||||
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'
|
||||
@@ -29,5 +29,5 @@ def parse(code=None, **kwargs):
|
||||
A utility function to parse Python with the current Python version. Params
|
||||
are documented in ``Grammar.parse``.
|
||||
"""
|
||||
grammar = load_python_grammar()
|
||||
grammar = load_grammar()
|
||||
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)
|
||||
|
||||
|
||||
def load_python_grammar(version=None):
|
||||
def load_grammar(version=None):
|
||||
"""
|
||||
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)
|
||||
except FileNotFoundError:
|
||||
# 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, \
|
||||
_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
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ def test_modulepickling_change_cache_dir(tmpdir):
|
||||
path_1 = 'fake path 1'
|
||||
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)
|
||||
parser_cache.clear()
|
||||
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
|
||||
"""
|
||||
grammar = load_python_grammar()
|
||||
grammar = load_grammar()
|
||||
module = 'fake parser'
|
||||
|
||||
# Create the file
|
||||
|
||||
@@ -5,7 +5,7 @@ import pytest
|
||||
|
||||
from parso.utils import splitlines
|
||||
from parso import cache
|
||||
from parso import load_python_grammar
|
||||
from parso import load_grammar
|
||||
from parso.python.diff import DiffParser
|
||||
from parso import parse
|
||||
|
||||
@@ -14,7 +14,7 @@ def test_simple():
|
||||
"""
|
||||
The diff parser reuses modules. So check for that.
|
||||
"""
|
||||
grammar = load_python_grammar()
|
||||
grammar = load_grammar()
|
||||
module_a = grammar.parse('a', diff_cache=True)
|
||||
assert grammar.parse('b', diff_cache=True) == module_a
|
||||
|
||||
@@ -49,7 +49,7 @@ def _assert_valid_graph(node):
|
||||
|
||||
|
||||
class Differ(object):
|
||||
grammar = load_python_grammar()
|
||||
grammar = load_grammar()
|
||||
|
||||
def initialize(self, code):
|
||||
logging.debug('differ: initialize')
|
||||
|
||||
@@ -6,7 +6,7 @@ import pytest
|
||||
|
||||
from parso._compatibility import u, py_version
|
||||
from parso import parse
|
||||
from parso import load_python_grammar
|
||||
from parso import load_grammar
|
||||
from parso.python import tree
|
||||
from parso.utils import splitlines
|
||||
|
||||
@@ -113,7 +113,7 @@ def test_param_splitting():
|
||||
"""
|
||||
def check(src, result):
|
||||
# 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)
|
||||
if py_version >= 30:
|
||||
assert not list(m.iter_funcdefs())
|
||||
@@ -161,10 +161,10 @@ def test_python3_octal():
|
||||
def test_load_newer_grammar():
|
||||
# This version shouldn't be out for a while, but if we somehow get this it
|
||||
# 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
|
||||
# going to be an issue.
|
||||
load_python_grammar('1.5')
|
||||
load_grammar('1.5')
|
||||
|
||||
|
||||
@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 parso._compatibility import py_version
|
||||
from parso import load_python_grammar
|
||||
from parso import load_grammar
|
||||
from parso import ParserSyntaxError
|
||||
import pytest
|
||||
|
||||
|
||||
def parse(code, version='3.4'):
|
||||
code = dedent(code) + "\n\n"
|
||||
grammar = load_python_grammar(version=version)
|
||||
grammar = load_grammar(version=version)
|
||||
return grammar.parse(code, error_recovery=False)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user