6
6
import tokenize as tk
7
7
from itertools import chain , dropwhile
8
8
from re import compile as re
9
+ from .utils import log
9
10
10
11
try :
11
12
from StringIO import StringIO
@@ -227,13 +228,15 @@ def __init__(self, filelike):
227
228
self ._generator = tk .generate_tokens (filelike .readline )
228
229
self .current = Token (* next (self ._generator , None ))
229
230
self .line = self .current .start [0 ]
230
- self .log = logging .getLogger ()
231
+ self .log = log
232
+ self .got_newline = True
231
233
232
234
def move (self ):
233
235
previous = self .current
234
236
current = self ._next_from_generator ()
235
237
self .current = None if current is None else Token (* current )
236
238
self .line = self .current .start [0 ] if self .current else self .line
239
+ self .got_newline = (previous .kind == tk .NEWLINE )
237
240
return previous
238
241
239
242
def _next_from_generator (self ):
@@ -271,7 +274,7 @@ class Parser(object):
271
274
def parse (self , filelike , filename ):
272
275
"""Parse the given file-like object and return its Module object."""
273
276
# TODO: fix log
274
- self .log = logging . getLogger ()
277
+ self .log = log
275
278
self .source = filelike .readlines ()
276
279
src = '' .join (self .source )
277
280
try :
@@ -336,6 +339,8 @@ def parse_decorators(self):
336
339
at_arguments = False
337
340
338
341
while self .current is not None :
342
+ self .log .debug ("parsing decorators, current token is %r (%s)" ,
343
+ self .current .kind , self .current .value )
339
344
if (self .current .kind == tk .NAME and
340
345
self .current .value in ['def' , 'class' ]):
341
346
# Done with decorators - found function or class proper
@@ -373,9 +378,12 @@ def parse_definitions(self, class_, all=False):
373
378
while self .current is not None :
374
379
self .log .debug ("parsing definition list, current token is %r (%s)" ,
375
380
self .current .kind , self .current .value )
381
+ self .log .debug ('got_newline: %s' , self .stream .got_newline )
376
382
if all and self .current .value == '__all__' :
377
383
self .parse_all ()
378
- elif self .current .kind == tk .OP and self .current .value == '@' :
384
+ elif (self .current .kind == tk .OP and
385
+ self .current .value == '@' and
386
+ self .stream .got_newline ):
379
387
self .consume (tk .OP )
380
388
self .parse_decorators ()
381
389
elif self .current .value in ['def' , 'class' ]:
@@ -392,6 +400,7 @@ def parse_definitions(self, class_, all=False):
392
400
else :
393
401
self .stream .move ()
394
402
403
+
395
404
def parse_all (self ):
396
405
"""Parse the __all__ definition in a module."""
397
406
assert self .current .value == '__all__'
@@ -471,6 +480,7 @@ def parse_definition(self, class_):
471
480
assert self .current .kind != tk .INDENT
472
481
docstring = self .parse_docstring ()
473
482
decorators = self ._accumulated_decorators
483
+ self .log .debug ("current accumulated decorators: %s" , decorators )
474
484
self ._accumulated_decorators = []
475
485
self .log .debug ("parsing nested definitions." )
476
486
children = list (self .parse_definitions (class_ ))
0 commit comments