コード例 #1
0
ファイル: fast.py プロジェクト: Ademan/jedi
    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 = 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
コード例 #2
0
ファイル: test_parser.py プロジェクト: MatiasNAmendola/jedi
def test_carriage_return_statements():
    source = u(dedent('''
        foo = 'ns1!'

        # this is a namespace package
    '''))
    source = source.replace('\n', '\r\n')
    stmt = Parser(source).module.statements[0]
    assert '#' not in stmt.get_code()
コード例 #3
0
ファイル: test_parsing.py プロジェクト: CoreyKumpOA/vimconfig
def test_user_statement_on_import():
    """github #285"""
    s = "from datetime import (\n" \
        "    time)"

    for pos in [(2, 1), (2, 4)]:
        u = Parser(s, user_position=pos).user_stmt
        assert isinstance(u, pr.Import)
        assert u.defunct is False
        assert [str(n) for n in u.get_defined_names()] == ['time']
コード例 #4
0
ファイル: test_param_splitting.py プロジェクト: bstaint/jedi
def assert_params(param_string, **wanted_dct):
    source = dedent('''
    def x(%s):
        pass
    ''') % param_string

    parser = Parser(load_grammar(), dedent(source))
    funcdef = parser.get_parsed_node().subscopes[0]
    dct = dict((p.name.value, p.default and p.default.get_code())
               for p in funcdef.params)
    assert dct == wanted_dct
    assert parser.get_parsed_node().get_code() == source
コード例 #5
0
def test_carriage_return_statements():
    source = u(
        dedent(
            """
        foo = 'ns1!'

        # this is a namespace package
    """
        )
    )
    source = source.replace("\n", "\r\n")
    stmt = Parser(load_grammar(), source).module.statements[0]
    assert "#" not in stmt.get_code()
コード例 #6
0
ファイル: run.py プロジェクト: bstaint/jedi
        def definition(correct, correct_start, path):
            should_be = set()
            for match in re.finditer('(?:[^ ]+)', correct):
                string = match.group(0)
                parser = Parser(load_grammar(), string, start_symbol='eval_input')
                parser.position_modifier.line = self.line_nr
                element = parser.get_parsed_node()
                element.parent = jedi.api.completion.get_user_scope(
                    script._get_module(),
                    (self.line_nr, self.column)
                )
                results = evaluator.eval_element(element)
                if not results:
                    raise Exception('Could not resolve %s on line %s'
                                    % (match.string, self.line_nr - 1))

                should_be |= set(Definition(evaluator, r) for r in results)

            # Because the objects have different ids, `repr`, then compare.
            should = set(comparison(r) for r in should_be)
            return should
コード例 #7
0
ファイル: pep0484.py プロジェクト: andrewmw94/configs
def _fix_forward_reference(context, node):
    evaled_nodes = context.eval_node(node)
    if len(evaled_nodes) != 1:
        debug.warning("Eval'ed typing index %s should lead to 1 object, "
                      " not %s" % (node, evaled_nodes))
        return node
    evaled_node = list(evaled_nodes)[0]
    if isinstance(evaled_node, compiled.CompiledObject) and \
            isinstance(evaled_node.obj, str):
        try:
            p = Parser(load_grammar(), _compatibility.unicode(evaled_node.obj),
                       start_symbol='eval_input')
            new_node = p.get_parsed_node()
        except ParseError:
            debug.warning('Annotation not parsed: %s' % evaled_node.obj)
            return node
        else:
            module = node.get_parent_until()
            new_node.move(module.end_pos[0])
            new_node.parent = context.tree_node
            return new_node
    else:
        return node
コード例 #8
0
ファイル: test_parser.py プロジェクト: MatiasNAmendola/jedi
 def get_call(self, source):
     stmt = Parser(u(source), no_docstr=True).module.statements[0]
     return stmt.expression_list()[0]
コード例 #9
0
ファイル: test_parsing.py プロジェクト: CoreyKumpOA/vimconfig
 def get_call(self, source):
     stmt = Parser(source, no_docstr=True).module.statements[0]
     return stmt.get_commands()[0]
コード例 #10
0
ファイル: test_parser.py プロジェクト: cpavlina/c4dot
def test_newline_positions():
    endmarker = Parser(load_grammar(), u('a\n')).module.children[-1]
    assert endmarker.end_pos == (2, 0)
    new_line = endmarker.get_previous()
    assert new_line.start_pos == (1, 1)
    assert new_line.end_pos == (2, 0)
コード例 #11
0
ファイル: test_get_code.py プロジェクト: ABob/vim
 def test(source, end_pos):
     module = Parser(load_grammar(), u(source)).module
     assert module.get_code() == source
     assert module.end_pos == end_pos
コード例 #12
0
ファイル: test_parser.py プロジェクト: Hylen/jedi
def test_incomplete_list_comprehension():
    """ Shouldn't raise an error, same bug as #418. """
    s = Parser(u('(1 for def')).module.statements[0]
    assert s.expression_list()