Skip to content

Commit 1c8f2d5

Browse files
committed
fix case with nested class in parser
1 parent d20686a commit 1c8f2d5

1 file changed

Lines changed: 14 additions & 44 deletions

File tree

codegraph/parser.py

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 'cut of' code from pythons pyclrb
1+
# 'cut of' code from pythons pyclrb with some additionals
22
import io
33
import tokenize
44
from token import NAME, DEDENT, NL
@@ -19,6 +19,7 @@ def __init__(self, name: Text, file: Text, lineno: int, parent: object):
1919

2020
def _addchild(self, name, obj):
2121
self.children[name] = obj
22+
obj.main = self
2223

2324
def __repr__(self):
2425
return f"{self.name} <{self.__class__.__name__}: Parent {self.parent}>"
@@ -78,7 +79,7 @@ def _nest_function(ob, func_name, lineno, async_f=False):
7879

7980
def _nest_class(ob, class_name, lineno, super=None):
8081
"Return a Class after nesting within ob."
81-
newclass = Class(ob.module, class_name, super, ob.file, lineno, ob)
82+
newclass = Class(class_name, super, ob.file, lineno, ob)
8283
ob._addchild(class_name, newclass)
8384
return newclass
8485

@@ -94,13 +95,17 @@ def create_objects_array(fname, source):
9495
try:
9596
new_lines = 0
9697
imports = None
98+
cur_func = None
9799
for tokentype, token, start, _end, _line in g:
98100
if tokentype == DEDENT:
99101
lineno, thisindent = start
100102
# Close previous nested classes and defs.
101103
while stack and stack[-1][1] >= thisindent:
102104
if isinstance(stack[-1][0], _Object):
103-
stack[-1][0].endno = lineno -1 - new_lines
105+
if getattr(stack[-1][0], 'main', None):
106+
stack[-1][0].endno = lineno - 1 - new_lines
107+
else:
108+
stack[-1][0].endno = lineno -1 - new_lines
104109
del stack[-1]
105110
else:
106111
if tree:
@@ -152,24 +157,27 @@ def create_objects_array(fname, source):
152157
tokentype, func_name, start = next(g)[0:3]
153158
if tokentype != NAME:
154159
continue # Skip def with syntax error.
155-
cur_func = None
156160
if stack:
157161
cur_obj = stack[-1][0]
158162
cur_func = _nest_function(cur_obj, func_name, lineno)
159163
cur_obj.endno = lineno - new_lines
160164
else:
161-
tree.append(Function(func_name, fname, lineno))
162-
stack.append((cur_func, thisindent))
165+
cur_func = Function(func_name, fname, lineno)
166+
tree.append(cur_func)
167+
if cur_func:
168+
stack.append((cur_func, thisindent))
163169
elif token == 'class':
164170
new_lines = 0
165171
lineno, thisindent = start
166172
# Close previous nested classes and defs.
167173
while stack and stack[-1][1] >= thisindent:
168174
del stack[-1]
169175
tokentype, class_name, start = next(g)[0:3]
176+
170177
if tokentype != NAME:
171178
continue
172179
inherit = None
180+
173181
if stack:
174182
cur_obj = stack[-1][0]
175183
cur_class = _nest_class(
@@ -189,41 +197,3 @@ def create_objects_array(fname, source):
189197
tree.append(imports)
190198
return tree
191199

192-
193-
def _getnamelist(g):
194-
"""Return list of (dotted-name, as-name or None) tuples for token source g.
195-
An as-name is the name that follows 'as' in an as clause.
196-
"""
197-
names = []
198-
while True:
199-
name, token = _getname(g)
200-
if not name:
201-
break
202-
if token == 'as':
203-
name2, token = _getname(g)
204-
else:
205-
name2 = None
206-
names.append((name, name2))
207-
while token != "," and "\n" not in token:
208-
token = next(g)[1]
209-
if token != ",":
210-
break
211-
return names
212-
213-
214-
def _getname(g):
215-
"Return (dotted-name or None, next-token) tuple for token source g."
216-
parts = []
217-
tokentype, token = next(g)[0:2]
218-
if tokentype != NAME and token != '*':
219-
return (None, token)
220-
parts.append(token)
221-
while True:
222-
tokentype, token = next(g)[0:2]
223-
if token != '.':
224-
break
225-
tokentype, token = next(g)[0:2]
226-
if tokentype != NAME:
227-
break
228-
parts.append(token)
229-
return (".".join(parts), token)

0 commit comments

Comments
 (0)