forked from VimPlug/jedi
fix position problems which came with lambdas
This commit is contained in:
@@ -358,7 +358,7 @@ class Parser(object):
|
|||||||
elif tok in ['lambda', 'for', 'in']:
|
elif tok in ['lambda', 'for', 'in']:
|
||||||
# don't parse these keywords, parse later in stmt.
|
# don't parse these keywords, parse later in stmt.
|
||||||
if tok == 'lambda':
|
if tok == 'lambda':
|
||||||
breaks.remove(':')
|
breaks.discard(':')
|
||||||
elif token_type == tokenize.NAME:
|
elif token_type == tokenize.NAME:
|
||||||
n, token_type, tok = self._parse_dot_name(self.current)
|
n, token_type, tok = self._parse_dot_name(self.current)
|
||||||
# removed last entry, because we add Name
|
# removed last entry, because we add Name
|
||||||
|
|||||||
@@ -427,7 +427,7 @@ class Lambda(Function):
|
|||||||
|
|
||||||
def get_code(self, first_indent=False, indention=' '):
|
def get_code(self, first_indent=False, indention=' '):
|
||||||
params = ','.join([stmt.get_code() for stmt in self.params])
|
params = ','.join([stmt.get_code() for stmt in self.params])
|
||||||
string = "lambda %s:" % params
|
string = "lambda %s: " % params
|
||||||
return string + super(Function, self).get_code(indention=indention)
|
return string + super(Function, self).get_code(indention=indention)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
@@ -827,7 +827,7 @@ class Statement(Simple):
|
|||||||
start_pos = start_tok_pos
|
start_pos = start_tok_pos
|
||||||
|
|
||||||
if tok == 'lambda':
|
if tok == 'lambda':
|
||||||
lambd = parse_lambda(token_iterator)
|
lambd, tok = parse_lambda(token_iterator)
|
||||||
if lambd is not None:
|
if lambd is not None:
|
||||||
token_list.append(lambd)
|
token_list.append(lambd)
|
||||||
elif tok == 'for':
|
elif tok == 'for':
|
||||||
@@ -870,7 +870,7 @@ class Statement(Simple):
|
|||||||
if tok == ':':
|
if tok == ':':
|
||||||
break
|
break
|
||||||
if tok != ':':
|
if tok != ':':
|
||||||
return None
|
return None, tok
|
||||||
|
|
||||||
# since lambda is a Function scope, it needs Scope parents
|
# since lambda is a Function scope, it needs Scope parents
|
||||||
parent = self.get_parent_until(IsScope)
|
parent = self.get_parent_until(IsScope)
|
||||||
@@ -881,7 +881,7 @@ class Statement(Simple):
|
|||||||
ret.parent = lambd
|
ret.parent = lambd
|
||||||
lambd.returns.append(ret)
|
lambd.returns.append(ret)
|
||||||
lambd.end_pos = self.end_pos
|
lambd.end_pos = self.end_pos
|
||||||
return lambd
|
return lambd, tok
|
||||||
|
|
||||||
def parse_list_comp(token_iterator, token_list, start_pos, end_pos):
|
def parse_list_comp(token_iterator, token_list, start_pos, end_pos):
|
||||||
def parse_stmt_or_arr(token_iterator, added_breaks=()):
|
def parse_stmt_or_arr(token_iterator, added_breaks=()):
|
||||||
@@ -959,9 +959,8 @@ class Statement(Simple):
|
|||||||
is_chain = False
|
is_chain = False
|
||||||
brackets = {'(': Array.TUPLE, '[': Array.LIST, '{': Array.SET}
|
brackets = {'(': Array.TUPLE, '[': Array.LIST, '{': Array.SET}
|
||||||
closing_brackets = ')', '}', ']'
|
closing_brackets = ')', '}', ']'
|
||||||
start = 0
|
|
||||||
|
|
||||||
token_iterator = enumerate(self.token_list)
|
token_iterator = common.PushBackIterator(enumerate(self.token_list))
|
||||||
for i, tok_temp in token_iterator:
|
for i, tok_temp in token_iterator:
|
||||||
if isinstance(tok_temp, Base):
|
if isinstance(tok_temp, Base):
|
||||||
# the token is a Name, which has already been parsed
|
# the token is a Name, which has already been parsed
|
||||||
@@ -976,18 +975,18 @@ class Statement(Simple):
|
|||||||
self._assignment_details.append((result, tok))
|
self._assignment_details.append((result, tok))
|
||||||
result = []
|
result = []
|
||||||
is_chain = False
|
is_chain = False
|
||||||
start = i + 1
|
|
||||||
continue
|
continue
|
||||||
elif tok == 'as': # just ignore as, because it sets values
|
elif tok == 'as': # just ignore as, because it sets values
|
||||||
next(token_iterator, None)
|
next(token_iterator, None)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
is_literal = token_type in [tokenize.STRING, tokenize.NUMBER]
|
|
||||||
if tok == 'lambda':
|
if tok == 'lambda':
|
||||||
lambd = parse_lambda(token_iterator)
|
lambd, tok = parse_lambda(token_iterator)
|
||||||
if lambd is not None:
|
if lambd is not None:
|
||||||
result.append(lambd)
|
result.append(lambd)
|
||||||
elif isinstance(tok, Name) or is_literal:
|
|
||||||
|
is_literal = token_type in [tokenize.STRING, tokenize.NUMBER]
|
||||||
|
if isinstance(tok, Name) or is_literal:
|
||||||
c_type = Call.NAME
|
c_type = Call.NAME
|
||||||
if is_literal:
|
if is_literal:
|
||||||
tok = literal_eval(tok)
|
tok = literal_eval(tok)
|
||||||
@@ -1014,16 +1013,32 @@ class Statement(Simple):
|
|||||||
if result and isinstance(result[-1], Call):
|
if result and isinstance(result[-1], Call):
|
||||||
is_chain = True
|
is_chain = True
|
||||||
elif tok == ',': # implies a tuple
|
elif tok == ',': # implies a tuple
|
||||||
# rewrite `result`, because now the whole thing is a tuple
|
# commands is now an array not a statement anymore
|
||||||
add_el, t = parse_stmt(enumerate(self.token_list[start:i]))
|
t = result[0]
|
||||||
|
start_pos = t[2] if isinstance(t, tuple) else t.start_pos
|
||||||
|
|
||||||
|
# get the correct index
|
||||||
|
i, tok = next(token_iterator, (len(self.token_list), None))
|
||||||
|
if tok is not None:
|
||||||
|
token_iterator.push_back((i, tok))
|
||||||
|
t = self.token_list[i - 1]
|
||||||
|
try:
|
||||||
|
end_pos = t.end_pos
|
||||||
|
except AttributeError:
|
||||||
|
end_pos = (t[2][0], t[2][1] + len(t[1])) \
|
||||||
|
if isinstance(t, tuple) else t.start_pos
|
||||||
|
|
||||||
|
stmt = Statement(self._sub_module, [], [], result,
|
||||||
|
start_pos, end_pos, self.parent)
|
||||||
|
stmt._commands = result
|
||||||
|
#add_el, t = parse_stmt(enumerate(self.token_list[start:i - 1]))
|
||||||
arr, break_tok = parse_array(token_iterator, Array.TUPLE,
|
arr, break_tok = parse_array(token_iterator, Array.TUPLE,
|
||||||
add_el.start_pos, add_el)
|
stmt.start_pos, stmt)
|
||||||
result = [arr]
|
result = [arr]
|
||||||
if is_assignment(break_tok):
|
if is_assignment(break_tok):
|
||||||
self._assignment_details.append((result, break_tok))
|
self._assignment_details.append((result, break_tok))
|
||||||
result = []
|
result = []
|
||||||
is_chain = False
|
is_chain = False
|
||||||
start = i + 1
|
|
||||||
else:
|
else:
|
||||||
if tok != '\n':
|
if tok != '\n':
|
||||||
result.append(tok)
|
result.append(tok)
|
||||||
|
|||||||
Reference in New Issue
Block a user