def _get_parser(self, code, parser_code, line_offset, nodes, no_docstr): h = hash(code) hashes = [n.hash for n in nodes] node = None try: index = hashes.index(h) if nodes[index].code != code: raise ValueError() except ValueError: p = parsing.Parser(parser_code, self.module_path, self.user_position, offset=(line_offset, 0), is_fast_parser=True, top_module=self.module, no_docstr=no_docstr) p.module.parent = self.module else: if nodes[index] != self.current_node: offset = int(nodes[0] == self.current_node) self.current_node.old_children.pop(index - offset) node = nodes.pop(index) p = node.parser m = p.module m.line_offset += line_offset + 1 - m.start_pos[0] if self.user_position is not None and \ m.start_pos[0] <= self.user_position[0] <= m.end_pos[0]: # It's important to take care of the whole user # positioning stuff, if no reparsing is being done. p.user_stmt = m.get_statement_for_position( self.user_position, include_imports=True) if p.user_stmt: p.user_scope = p.user_stmt.parent else: p.user_scope = self._scan_user_scope(m) or m return p, node
def test_end_pos(self): # jedi issue #150 s = "x()\nx( )\nx( )\nx ( )" parser = parsing.Parser(s) for i, s in enumerate(parser.scope.statements, 3): for c in s.get_commands(): self.assertEqual(c.execution.end_pos[1], i)
def _get_under_cursor_stmt(self, cursor_txt): offset = self._line - 1, self._column r = parsing.Parser(cursor_txt, no_docstr=True, offset=offset) try: stmt = r.module.statements[0] except IndexError: raise NotFoundError() stmt.parent = self._parser.user_scope return stmt
def __call__(self, source, module_path=None, user_position=None): if not settings.fast_parser: return parsing.Parser(source, module_path, user_position) pi = cache.parser_cache.get(module_path, None) if pi is None or isinstance(pi.parser, parsing.Parser): p = super(CachedFastParser, self).__call__(source, module_path, user_position) else: p = pi.parser # pi is a `cache.ParserCacheItem` p.update(source, user_position) return p
def magic_function_scope(self): try: return self._magic_function_scope except AttributeError: # depth = 1 because this is not a module class Container(object): FunctionType = types.FunctionType source = _generate_code(Container, depth=0) parser = parsing.Parser(source, None) module = parser.module module.parent = self.scope typ = evaluate.follow_path(iter(['FunctionType']), module, module) s = self._magic_function_scope = typ.pop() return s
def defined_names(source, path=None, source_encoding='utf-8'): """ Get all definitions in `source` sorted by its position. This functions can be used for listing functions, classes and data defined in a file. This can be useful if you want to list them in "sidebar". Each element in the returned list also has `defined_names` method which can be used to get sub-definitions (e.g., methods in class). :rtype: list of api_classes.Definition """ parser = parsing.Parser( modules.source_to_unicode(source, source_encoding), module_path=path, ) return api_classes._defined_names(parser.module)
def follow_param(param): func = param.parent_function #print func, param, param.parent_function param_str = _search_param_in_docstr(func.docstr, str(param.get_name())) user_position = (1, 0) if param_str is not None: # Try to import module part in dotted name. # (e.g., 'threading' in 'threading.Thread'). if '.' in param_str: param_str = 'import %s\n%s' % (param_str.rsplit('.', 1)[0], param_str) user_position = (2, 0) p = parsing.Parser(param_str, None, user_position, no_docstr=True) return evaluate.follow_statement(p.user_stmt) return []
def find_return_types(func): def search_return_in_docstr(code): for p in DOCSTRING_RETURN_PATTERNS: match = p.search(code) if match: return match.group(1) if isinstance(func, er.InstanceElement): func = func.var if isinstance(func, er.Function): func = func.base_func type_str = search_return_in_docstr(func.docstr) if not type_str: return [] p = parsing.Parser(type_str, None, (1, 0), no_docstr=True) p.user_stmt.parent = func return list(evaluate.follow_statement(p.user_stmt))