Added a grammar param to the parser.

This commit is contained in:
Dave Halter
2014-11-24 01:10:39 +01:00
parent c152a1c58b
commit 9f45f18ad1
14 changed files with 60 additions and 44 deletions
+2 -1
View File
@@ -91,7 +91,8 @@ from jedi.evaluate.helpers import FakeStatement, deep_ast_copy, call_of_name
class Evaluator(object):
def __init__(self):
def __init__(self, grammar):
self._grammar = grammar
self.memoize_cache = {} # for memoize decorators
self.import_cache = {} # like `sys.modules`.
self.compiled_cache = {} # see `compiled.create()`
+3 -3
View File
@@ -8,8 +8,7 @@ import os
import inspect
from jedi._compatibility import is_py3, builtins, unicode
from jedi.parser import Parser
from jedi.parser import tokenize
from jedi.parser import Parser, load_grammar
from jedi.parser.tree import Class
from jedi.evaluate.helpers import FakeName
@@ -31,7 +30,8 @@ def _load_faked_module(module):
except IOError:
modules[module_name] = None
return
module = Parser(unicode(source), module_name).module
grammar = load_grammar('grammar3.4')
module = Parser(grammar, unicode(source), module_name).module
modules[module_name] = module
if module_name == 'builtins' and not is_py3:
+1 -1
View File
@@ -127,7 +127,7 @@ def _evaluate_for_statement_string(evaluator, string, module):
# (e.g., 'threading' in 'threading.Thread').
string = 'import %s\n' % element + string
p = Parser(code % indent_block(string), no_docstr=True)
p = Parser(evaluator.grammar, code % indent_block(string), no_docstr=True)
pseudo_cls = p.module.subscopes[0]
try:
stmt = pseudo_cls.statements[-1]
+1 -1
View File
@@ -174,7 +174,7 @@ def search_params(evaluator, param):
try:
result = []
# This is like backtracking: Get the first possible result.
for mod in imports.get_modules_containing_name([current_module], func_name):
for mod in imports.get_modules_containing_name(evaluator, [current_module], func_name):
result = get_params_for_module(mod)
if result:
break
+2 -2
View File
@@ -492,8 +492,8 @@ def get_names_of_scope(evaluator, scope, position=None, star_search=True, includ
the current scope is function:
>>> from jedi._compatibility import u
>>> from jedi.parser import Parser
>>> parser = Parser(u('''
>>> from jedi.parser import Parser, load_grammar
>>> parser = Parser(load_grammar('python3.4'), u('''
... x = ['a', 'b', 'c']
... def func():
... y = None
+7 -7
View File
@@ -184,7 +184,7 @@ class ImportWrapper2(pr.Base):
rel_path = os.path.join(self._importer.get_relative_path(),
'__init__.py')
if os.path.exists(rel_path):
m = _load_module(rel_path)
m = _load_module(self.evaluator, rel_path)
names += m.get_defined_names()
else:
# flask
@@ -590,9 +590,9 @@ class _Importer(object):
else:
source = current_namespace[0].read()
current_namespace[0].close()
return _load_module(path, source, sys_path=sys_path), rest
return _load_module(self.evaluator, path, source, sys_path=sys_path), rest
else:
return _load_module(name=path, sys_path=sys_path), rest
return _load_module(self.evaluator, name=path, sys_path=sys_path), rest
def follow_imports(evaluator, scopes):
@@ -633,7 +633,7 @@ def remove_star_imports(evaluator, scope, ignored_modules=()):
return set(modules)
def _load_module(path=None, source=None, name=None, sys_path=None):
def _load_module(evaluator, path=None, source=None, name=None, sys_path=None):
def load(source):
dotted_path = path and compiled.dotted_from_fs_path(path, sys_path)
if path is not None and path.endswith('.py') \
@@ -644,7 +644,7 @@ def _load_module(path=None, source=None, name=None, sys_path=None):
else:
return compiled.load_module(path, name)
p = path or name
p = fast.FastParser(common.source_to_unicode(source), p)
p = fast.FastParser(evaluator.grammar, common.source_to_unicode(source), p)
cache.save_parser(path, name, p)
return p.module
@@ -652,7 +652,7 @@ def _load_module(path=None, source=None, name=None, sys_path=None):
return load(source) if cached is None else cached.module
def get_modules_containing_name(mods, name):
def get_modules_containing_name(evaluator, mods, name):
"""
Search a name in the directories of modules.
"""
@@ -669,7 +669,7 @@ def get_modules_containing_name(mods, name):
with open(path, 'rb') as f:
source = source_to_unicode(f.read())
if name in source:
return _load_module(path, source)
return _load_module(evaluator, path, source)
# skip non python modules
mods = set(m for m in mods if not isinstance(m, compiled.CompiledObject))
+1 -1
View File
@@ -227,7 +227,7 @@ def collections_namedtuple(evaluator, obj, params):
)
# Parse source
generated_class = Parser(unicode(source)).module.subscopes[0]
generated_class = Parser(evaluator.grammar, unicode(source)).module.subscopes[0]
return [er.Class(evaluator, generated_class)]
+1 -1
View File
@@ -140,7 +140,7 @@ def sys_path_with_modifications(evaluator, module):
except IOError:
pass
else:
p = Parser(common.source_to_unicode(source), module_path)
p = Parser(evaluator.grammar, common.source_to_unicode(source), module_path)
for path in _check_module(p.module):
if path not in buildout_paths:
buildout_paths.add(path)