/
representation.py
1515 lines (1264 loc) · 50 KB
/
representation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
If you know what an abstract syntax tree (ast) is, you'll see that this module
is pretty much that. The classes represent syntax elements: ``Import``,
``Function``.
A very central class is ``Scope``. It is not used directly by the parser, but
inherited. It's used by ``Function``, ``Class``, ``Flow``, etc. A ``Scope`` may
have ``subscopes``, ``imports`` and ``statements``. The entire parser is based
on scopes, because they also stand for indentation.
One special thing:
``Array`` values are statements. But if you think about it, this makes sense.
``[1, 2+33]`` for example would be an Array with two ``Statement`` inside. This
is the easiest way to write a parser. The same behaviour applies to ``Param``,
which is being used in a function definition.
The easiest way to play with this module is to use :class:`parsing.Parser`.
:attr:`parsing.Parser.module` holds an instance of :class:`SubModule`:
>>> from jedi._compatibility import u
>>> from jedi.parser import Parser
>>> parser = Parser(u('import os'), 'example.py')
>>> submodule = parser.module
>>> submodule
<SubModule: example.py@1-1>
Any subclasses of :class:`Scope`, including :class:`SubModule` has
attribute :attr:`imports <Scope.imports>`. This attribute has import
statements in this scope. Check this out:
>>> submodule.imports
[<Import: import os @1,0>]
See also :attr:`Scope.subscopes` and :attr:`Scope.statements`.
"""
import os
import re
from inspect import cleandoc
from jedi._compatibility import (next, Python3Method, encoding, unicode,
is_py3, u, literal_eval)
from jedi import common
from jedi import debug
from jedi import cache
from jedi.parser import tokenize
SCOPE_CONTENTS = 'asserts', 'subscopes', 'imports', 'statements', 'returns'
class GetCodeState(object):
"""A helper class for passing the state of get_code in a thread-safe
manner."""
__slots__ = ("last_pos",)
def __init__(self):
self.last_pos = (0, 0)
class DocstringMixin(object):
__slots__ = ()
def add_docstr(self, token):
""" Clean up a docstring """
self._doc_token = token
@property
def raw_doc(self):
""" Returns a cleaned version of the docstring token. """
try:
# Returns a literal cleaned version of the ``Token``.
return unicode(cleandoc(literal_eval(self._doc_token.string)))
except AttributeError:
return u('')
class Base(object):
"""
This is just here to have an isinstance check, which is also used on
evaluate classes. But since they have sometimes a special type of
delegation, it is important for those classes to override this method.
I know that there is a chance to do such things with __instancecheck__, but
since Python 2.5 doesn't support it, I decided to do it this way.
"""
__slots__ = ()
def isinstance(self, *cls):
return isinstance(self, cls)
@property
def newline(self):
"""Returns the newline type for the current code."""
#TODO: we need newline detection
return "\n"
@property
def whitespace(self):
"""Returns the whitespace type for the current code: tab or space."""
#TODO: we need tab detection
return " "
@Python3Method
def get_parent_until(self, classes=(), reverse=False,
include_current=True):
"""
Searches the parent "chain" until the object is an instance of
classes. If classes is empty return the last parent in the chain
(is without a parent).
"""
if type(classes) not in (tuple, list):
classes = (classes,)
scope = self if include_current else self.parent
while scope.parent is not None:
if classes and reverse != scope.isinstance(*classes):
break
scope = scope.parent
return scope
def is_callable(self):
"""
By default parser objects are not callable, we make them callable by
the ``evaluate.representation`` objects.
"""
return False
def space(self, from_pos, to_pos):
"""Return the space between two tokens"""
linecount = to_pos[0] - from_pos[0]
if linecount == 0:
return self.whitespace * (to_pos[1] - from_pos[1])
else:
return "%s%s" % (
self.newline * linecount,
self.whitespace * to_pos[1],
)
class Simple(Base):
"""
The super class for Scope, Import, Name and Statement. Every object in
the parser tree inherits from this class.
"""
__slots__ = ('parent', '_sub_module', '_start_pos', 'use_as_parent',
'_end_pos')
def __init__(self, module, start_pos, end_pos=(None, None)):
"""
Initialize :class:`Simple`.
:type module: :class:`SubModule`
:param module: The module in which this Python object locates.
:type start_pos: 2-tuple of int
:param start_pos: Position (line, column) of the Statement.
:type end_pos: 2-tuple of int
:param end_pos: Same as `start_pos`.
"""
self._sub_module = module
self._start_pos = start_pos
self._end_pos = end_pos
self.parent = None
# use this attribute if parent should be something else than self.
self.use_as_parent = self
@property
def start_pos(self):
return self._sub_module.line_offset + self._start_pos[0], \
self._start_pos[1]
@start_pos.setter
def start_pos(self, value):
self._start_pos = value
@property
def end_pos(self):
if None in self._end_pos:
return self._end_pos
return self._sub_module.line_offset + self._end_pos[0], \
self._end_pos[1]
@end_pos.setter
def end_pos(self, value):
self._end_pos = value
def __repr__(self):
code = self.get_code().replace('\n', ' ')
if not is_py3:
code = code.encode(encoding, 'replace')
return "<%s: %s@%s,%s>" % \
(type(self).__name__, code, self.start_pos[0], self.start_pos[1])
class IsScope(Base):
__slots__ = ()
class Scope(Simple, IsScope, DocstringMixin):
"""
Super class for the parser tree, which represents the state of a python
text file.
A Scope manages and owns its subscopes, which are classes and functions, as
well as variables and imports. It is used to access the structure of python
files.
:param start_pos: The position (line and column) of the scope.
:type start_pos: tuple(int, int)
"""
__slots__ = ('subscopes', 'imports', 'statements', '_doc_token', 'asserts',
'returns', 'is_generator')
def __init__(self, module, start_pos):
super(Scope, self).__init__(module, start_pos)
self.subscopes = []
self.imports = []
self.statements = []
self._doc_token = None
self.asserts = []
# Needed here for fast_parser, because the fast_parser splits and
# returns will be in "normal" modules.
self.returns = []
self.is_generator = False
def add_scope(self, sub, decorators):
sub.parent = self.use_as_parent
sub.decorators = decorators
for d in decorators:
# the parent is the same, because the decorator has not the scope
# of the function
d.parent = self.use_as_parent
self.subscopes.append(sub)
return sub
def add_statement(self, stmt):
"""
Used to add a Statement or a Scope.
A statement would be a normal command (Statement) or a Scope (Flow).
"""
stmt.parent = self.use_as_parent
self.statements.append(stmt)
return stmt
def add_import(self, imp):
self.imports.append(imp)
imp.parent = self.use_as_parent
def get_imports(self):
""" Gets also the imports within flow statements """
i = [] + self.imports
for s in self.statements:
if isinstance(s, Scope):
i += s.get_imports()
return i
def get_code2(self, state=GetCodeState()):
string = []
return "".join(string)
def get_code(self, first_indent=False, indention=' '):
"""
:return: Returns the code of the current scope.
:rtype: str
"""
string = ""
if self._doc_token is not None:
string += '"""' + self.raw_doc + '"""\n'
objs = self.subscopes + self.imports + self.statements + self.returns
for obj in sorted(objs, key=lambda x: x.start_pos):
if isinstance(obj, Scope):
string += obj.get_code(first_indent=True, indention=indention)
else:
if obj in self.returns and not isinstance(self, Lambda):
string += 'yield ' if self.is_generator else 'return '
string += obj.get_code()
if first_indent:
string = common.indent_block(string, indention=indention)
return string
@Python3Method
def get_defined_names(self):
"""
Get all defined names in this scope.
>>> from jedi._compatibility import u
>>> from jedi.parser import Parser
>>> parser = Parser(u('''
... a = x
... b = y
... b.c = z
... '''))
>>> parser.module.get_defined_names()
[<Name: a@2,0>, <Name: b@3,0>, <Name: b.c@4,0>]
"""
n = []
for stmt in self.statements:
try:
n += stmt.get_defined_names(True)
except TypeError:
n += stmt.get_defined_names()
# function and class names
n += [s.name for s in self.subscopes]
for i in self.imports:
if not i.star:
n += i.get_defined_names()
return n
@Python3Method
def get_statement_for_position(self, pos, include_imports=False):
checks = self.statements + self.asserts
if include_imports:
checks += self.imports
if self.isinstance(Function):
checks += self.params + self.decorators
checks += [r for r in self.returns if r is not None]
if self.isinstance(Flow):
checks += self.inputs
if self.isinstance(ForFlow) and self.set_stmt is not None:
checks.append(self.set_stmt)
for s in checks:
if isinstance(s, Flow):
p = s.get_statement_for_position(pos, include_imports)
while s.next and not p:
s = s.next
p = s.get_statement_for_position(pos, include_imports)
if p:
return p
elif s.start_pos <= pos <= s.end_pos:
return s
for s in self.subscopes:
if s.start_pos <= pos <= s.end_pos:
p = s.get_statement_for_position(pos, include_imports)
if p:
return p
def __repr__(self):
try:
name = self.path
except AttributeError:
try:
name = self.name
except AttributeError:
name = self.command
return "<%s: %s@%s-%s>" % (type(self).__name__, name,
self.start_pos[0], self.end_pos[0])
class Module(IsScope):
"""
For isinstance checks. fast_parser.Module also inherits from this.
"""
class SubModule(Scope, Module):
"""
The top scope, which is always a module.
Depending on the underlying parser this may be a full module or just a part
of a module.
"""
__slots__ = ('path', 'global_vars', 'used_names', 'temp_used_names',
'line_offset', 'use_as_parent')
def __init__(self, path, start_pos=(1, 0), top_module=None):
"""
Initialize :class:`SubModule`.
:type path: str
:arg path: File path to this module.
.. todo:: Document `top_module`.
"""
super(SubModule, self).__init__(self, start_pos)
self.path = path
self.global_vars = []
self.used_names = {}
self.temp_used_names = []
# this may be changed depending on fast_parser
self.line_offset = 0
self.use_as_parent = top_module or self
def add_global(self, name):
"""
Global means in these context a function (subscope) which has a global
statement.
This is only relevant for the top scope.
:param name: The name of the global.
:type name: Name
"""
# set no parent here, because globals are not defined in this scope.
self.global_vars.append(name)
def get_defined_names(self):
n = super(SubModule, self).get_defined_names()
n += self.global_vars
return n
@property
@cache.underscore_memoization
def name(self):
""" This is used for the goto functions. """
if self.path is None:
string = '' # no path -> empty name
else:
sep = (re.escape(os.path.sep),) * 2
r = re.search(r'([^%s]*?)(%s__init__)?(\.py|\.so)?$' % sep, self.path)
# remove PEP 3149 names
string = re.sub('\.[a-z]+-\d{2}[mud]{0,3}$', '', r.group(1))
# positions are not real therefore choose (0, 0)
names = [(string, (0, 0))]
return Name(self, names, (0, 0), (0, 0), self.use_as_parent)
@property
def has_explicit_absolute_import(self):
"""
Checks if imports in this module are explicitly absolute, i.e. there
is a ``__future__`` import.
"""
for imp in self.imports:
if imp.from_ns is None or imp.namespace is None:
continue
namespace, feature = imp.from_ns.names[0], imp.namespace.names[0]
if unicode(namespace) == "__future__" and unicode(feature) == "absolute_import":
return True
return False
class Class(Scope):
"""
Used to store the parsed contents of a python class.
:param name: The Class name.
:type name: str
:param supers: The super classes of a Class.
:type supers: list
:param start_pos: The start position (line, column) of the class.
:type start_pos: tuple(int, int)
"""
__slots__ = ('name', 'supers', 'decorators')
def __init__(self, module, name, supers, start_pos):
super(Class, self).__init__(module, start_pos)
self.name = name
name.parent = self.use_as_parent
self.supers = supers
for s in self.supers:
s.parent = self.use_as_parent
self.decorators = []
def get_code(self, first_indent=False, indention=' '):
string = "\n".join('@' + stmt.get_code() for stmt in self.decorators)
string += 'class %s' % (self.name)
if len(self.supers) > 0:
sup = ', '.join(stmt.get_code(False) for stmt in self.supers)
string += '(%s)' % sup
string += ':\n'
string += super(Class, self).get_code(True, indention)
return string
@property
def doc(self):
"""
Return a document string including call signature of __init__.
"""
docstr = ""
if self._doc_token is not None:
docstr = self.raw_doc
for sub in self.subscopes:
if unicode(sub.name.names[-1]) == '__init__':
return '%s\n\n%s' % (
sub.get_call_signature(funcname=self.name.names[-1]), docstr)
return docstr
class Function(Scope):
"""
Used to store the parsed contents of a python function.
:param name: The Function name.
:type name: str
:param params: The parameters (Statement) of a Function.
:type params: list
:param start_pos: The start position (line, column) the Function.
:type start_pos: tuple(int, int)
"""
__slots__ = ('name', 'params', 'decorators', 'listeners', 'annotation')
def __init__(self, module, name, params, start_pos, annotation):
super(Function, self).__init__(module, start_pos)
self.name = name
if name is not None:
name.parent = self.use_as_parent
self.params = params
for p in params:
p.parent = self.use_as_parent
p.parent_function = self.use_as_parent
self.decorators = []
self.listeners = set() # not used here, but in evaluation.
if annotation is not None:
annotation.parent = self.use_as_parent
self.annotation = annotation
def get_code(self, first_indent=False, indention=' '):
string = "\n".join('@' + stmt.get_code() for stmt in self.decorators)
params = ', '.join([stmt.get_code(False) for stmt in self.params])
string += "def %s(%s):\n" % (self.name, params)
string += super(Function, self).get_code(True, indention)
return string
def get_defined_names(self):
n = super(Function, self).get_defined_names()
for p in self.params:
try:
n.append(p.get_name())
except IndexError:
debug.warning("multiple names in param %s", n)
return n
def get_call_signature(self, width=72, funcname=None):
"""
Generate call signature of this function.
:param width: Fold lines if a line is longer than this value.
:type width: int
:arg funcname: Override function name when given.
:type funcname: str
:rtype: str
"""
l = unicode(funcname or self.name.names[-1]) + '('
lines = []
for (i, p) in enumerate(self.params):
code = p.get_code(False)
if i != len(self.params) - 1:
code += ', '
if len(l + code) > width:
lines.append(l[:-1] if l[-1] == ' ' else l)
l = code
else:
l += code
if l:
lines.append(l)
lines[-1] += ')'
return '\n'.join(lines)
@property
def doc(self):
""" Return a document string including call signature. """
docstr = ""
if self._doc_token is not None:
docstr = self.raw_doc
return '%s\n\n%s' % (self.get_call_signature(), docstr)
class Lambda(Function):
def __init__(self, module, params, start_pos, parent):
super(Lambda, self).__init__(module, None, params, start_pos, None)
self.parent = parent
def get_code(self, first_indent=False, indention=' '):
params = ','.join([stmt.get_code() for stmt in self.params])
string = "lambda %s: " % params
return string + super(Function, self).get_code(indention=indention)
def __repr__(self):
return "<%s @%s (%s-%s)>" % (type(self).__name__, self.start_pos[0],
self.start_pos[1], self.end_pos[1])
class Flow(Scope):
"""
Used to describe programming structure - flow statements,
which indent code, but are not classes or functions:
- for
- while
- if
- try
- with
Therefore statements like else, except and finally are also here,
they are now saved in the root flow elements, but in the next variable.
:param command: The flow command, if, while, else, etc.
:type command: str
:param inputs: The initializations of a flow -> while 'statement'.
:type inputs: list(Statement)
:param start_pos: Position (line, column) of the Flow statement.
:type start_pos: tuple(int, int)
"""
__slots__ = ('next', 'command', '_parent', 'inputs', 'set_vars')
def __init__(self, module, command, inputs, start_pos):
self.next = None
self.command = command
super(Flow, self).__init__(module, start_pos)
self._parent = None
# These have to be statements, because of with, which takes multiple.
self.inputs = inputs
for s in inputs:
s.parent = self.use_as_parent
self.set_vars = []
@property
def parent(self):
return self._parent
@parent.setter
def parent(self, value):
self._parent = value
try:
self.next.parent = value
except AttributeError:
return
def get_code(self, first_indent=False, indention=' '):
stmts = []
for s in self.inputs:
stmts.append(s.get_code(new_line=False))
stmt = ', '.join(stmts)
string = "%s %s:\n" % (self.command, stmt)
string += super(Flow, self).get_code(True, indention)
if self.next:
string += self.next.get_code()
return string
def get_defined_names(self, is_internal_call=False):
"""
Get the names for the flow. This includes also a call to the super
class.
:param is_internal_call: defines an option for internal files to crawl
through this class. Normally it will just call its superiors, to
generate the output.
"""
if is_internal_call:
n = list(self.set_vars)
for s in self.inputs:
n += s.get_defined_names()
if self.next:
n += self.next.get_defined_names(is_internal_call)
n += super(Flow, self).get_defined_names()
return n
else:
return self.get_parent_until((Class, Function)).get_defined_names()
def get_imports(self):
i = super(Flow, self).get_imports()
if self.next:
i += self.next.get_imports()
return i
def set_next(self, next):
"""Set the next element in the flow, those are else, except, etc."""
if self.next:
return self.next.set_next(next)
else:
self.next = next
self.next.parent = self.parent
return next
class ForFlow(Flow):
"""
Used for the for loop, because there are two statement parts.
"""
def __init__(self, module, inputs, start_pos, set_stmt, is_list_comp=False):
super(ForFlow, self).__init__(module, 'for', inputs, start_pos)
self.set_stmt = set_stmt
self.is_list_comp = is_list_comp
if set_stmt is not None:
set_stmt.parent = self.use_as_parent
self.set_vars = set_stmt.get_defined_names()
for s in self.set_vars:
s.parent.parent = self.use_as_parent
s.parent = self.use_as_parent
def get_code(self, first_indent=False, indention=" " * 4):
vars = ",".join(x.get_code() for x in self.set_vars)
stmts = []
for s in self.inputs:
stmts.append(s.get_code(new_line=False))
stmt = ', '.join(stmts)
s = "for %s in %s:\n" % (vars, stmt)
return s + super(Flow, self).get_code(True, indention)
class Import(Simple):
"""
Stores the imports of any Scopes.
:param start_pos: Position (line, column) of the Import.
:type start_pos: tuple(int, int)
:param namespace: The import, can be empty if a star is given
:type namespace: Name
:param alias: The alias of a namespace(valid in the current namespace).
:type alias: Name
:param from_ns: Like the namespace, can be equally used.
:type from_ns: Name
:param star: If a star is used -> from time import *.
:type star: bool
:param defunct: An Import is valid or not.
:type defunct: bool
"""
def __init__(self, module, start_pos, end_pos, namespace, alias=None,
from_ns=None, star=False, relative_count=0, defunct=False):
super(Import, self).__init__(module, start_pos, end_pos)
self.namespace = namespace
self.alias = alias
self.from_ns = from_ns
for n in namespace, alias, from_ns:
if n:
n.parent = self.use_as_parent
self.star = star
self.relative_count = relative_count
self.defunct = defunct
def get_code(self, new_line=True):
# in case one of the names is None
alias = self.alias or ''
namespace = self.namespace or ''
from_ns = self.from_ns or ''
if self.alias:
ns_str = "%s as %s" % (namespace, alias)
else:
ns_str = unicode(namespace)
nl = '\n' if new_line else ''
if self.from_ns or self.relative_count:
if self.star:
ns_str = '*'
dots = '.' * self.relative_count
return "from %s%s import %s%s" % (dots, from_ns, ns_str, nl)
else:
return "import %s%s" % (ns_str, nl)
def get_defined_names(self):
if self.defunct:
return []
if self.star:
return [self]
if self.alias:
return [self.alias]
if len(self.namespace) > 1:
o = self.namespace
n = Name(self._sub_module, [(unicode(o.names[0]), o.start_pos)],
o.start_pos, o.end_pos, parent=o.parent)
return [n]
else:
return [self.namespace]
def get_all_import_names(self):
n = []
if self.from_ns:
n.append(self.from_ns)
if self.namespace:
n.append(self.namespace)
if self.alias:
n.append(self.alias)
return n
class KeywordStatement(Base):
"""
For the following statements: `assert`, `del`, `global`, `nonlocal`,
`raise`, `return`, `yield`, `pass`, `continue`, `break`, `return`, `yield`.
"""
__slots__ = ('name', 'start_pos', '_stmt', 'parent')
def __init__(self, name, start_pos, parent, stmt=None):
self.name = name
self.start_pos = start_pos
self._stmt = stmt
self.parent = parent
if stmt is not None:
stmt.parent = self
def get_code(self):
if self._stmt is None:
return "%s\n" % self.name
else:
return '%s %s\n' % (self.name, self._stmt)
def get_defined_names(self):
return []
@property
def end_pos(self):
try:
return self._stmt.end_pos
except AttributeError:
return self.start_pos[0], self.start_pos[1] + len(self.name)
class Statement(Simple, DocstringMixin):
"""
This is the class for all the possible statements. Which means, this class
stores pretty much all the Python code, except functions, classes, imports,
and flow functions like if, for, etc.
:type token_list: list
:param token_list:
List of tokens or names. Each element is either an instance
of :class:`Name` or a tuple of token type value (e.g.,
:data:`tokenize.NUMBER`), token string (e.g., ``'='``), and
start position (e.g., ``(1, 0)``).
:type start_pos: 2-tuple of int
:param start_pos: Position (line, column) of the Statement.
"""
__slots__ = ('_token_list', '_set_vars', 'as_names', '_expression_list',
'_assignment_details', '_names_are_set_vars', '_doc_token')
def __init__(self, module, token_list, start_pos, end_pos, parent=None,
as_names=(), names_are_set_vars=False, set_name_parents=True):
super(Statement, self).__init__(module, start_pos, end_pos)
self._token_list = token_list
self._names_are_set_vars = names_are_set_vars
if set_name_parents:
for t in token_list:
if isinstance(t, Name):
t.parent = self.use_as_parent
for n in as_names:
n.parent = self.use_as_parent
self.parent = parent
self._doc_token = None
self._set_vars = None
self.as_names = list(as_names)
# cache
self._assignment_details = []
@property
def end_pos(self):
return self._token_list[-1].end_pos
def get_code(self, new_line=True):
def assemble(command_list, assignment=None):
pieces = [c.get_code() if isinstance(c, Simple) else c.string if
isinstance(c, (tokenize.Token, Operator)) else unicode(c)
for c in command_list]
if assignment is None:
return ''.join(pieces)
return '%s %s ' % (''.join(pieces), assignment)
code = ''.join(assemble(*a) for a in self.assignment_details)
code += assemble(self.expression_list())
if self._doc_token:
code += '\n"""%s"""' % self.raw_doc
if new_line:
return code + '\n'
else:
return code
def get_defined_names(self):
""" Get the names for the statement. """
if self._set_vars is None:
def search_calls(calls):
for call in calls:
if isinstance(call, Array):
for stmt in call:
search_calls(stmt.expression_list())
elif isinstance(call, Call):
c = call
# Check if there's an execution in it, if so this is
# not a set_var.
is_execution = False
while c:
if Array.is_type(c.execution, Array.TUPLE):
is_execution = True
c = c.next
if is_execution:
continue
self._set_vars.append(call.name)
self._set_vars = []
for calls, operation in self.assignment_details:
search_calls(calls)
if not self.assignment_details and self._names_are_set_vars:
# In the case of Param, it's also a defining name without ``=``
search_calls(self.expression_list())
return self._set_vars + self.as_names
def is_global(self):
p = self.parent
return isinstance(p, KeywordStatement) and p.name == 'global'
@property
def assignment_details(self):
"""
Returns an array of tuples of the elements before the assignment.
For example the following code::
x = (y, z) = 2, ''
would result in ``[(Name(x), '='), (Array([Name(y), Name(z)]), '=')]``.
"""
# parse statement which creates the assignment details.
self.expression_list()
return self._assignment_details
@cache.underscore_memoization
def expression_list(self):
"""
Parse a statement.
This is not done in the main parser, because it might be slow and
most of the statements won't need this data anyway. This is something
'like' a lazy execution.
This is not really nice written, sorry for that. If you plan to replace
it and make it nicer, that would be cool :-)
"""
def is_assignment(tok):
return isinstance(tok, Operator) and tok.string.endswith('=') \
and not tok.string in ('>=', '<=', '==', '!=')
def parse_array(token_iterator, array_type, start_pos, add_el=None):
arr = Array(self._sub_module, start_pos, array_type, self)
if add_el is not None:
arr.add_statement(add_el)
old_stmt = add_el
maybe_dict = array_type == Array.SET
break_tok = None
is_array = None
while True:
stmt, break_tok = parse_stmt(token_iterator, maybe_dict,
break_on_assignment=bool(add_el))
if stmt is None:
break
else:
if break_tok == ',':
is_array = True
arr.add_statement(stmt, is_key=maybe_dict and break_tok == ':')
if break_tok in closing_brackets \
or is_assignment(break_tok):
break
old_stmt = stmt
if arr.type == Array.TUPLE and len(arr) == 1 and not is_array:
arr.type = Array.NOARRAY
if not arr.values and maybe_dict:
# this is a really special case - empty brackets {} are
# always dictionaries and not sets.
arr.type = Array.DICT
arr.end_pos = (break_tok or stmt or old_stmt).end_pos
return arr, break_tok
def parse_stmt(token_iterator, maybe_dict=False, added_breaks=(),
break_on_assignment=False, stmt_class=Statement,
allow_comma=False):
token_list = []
level = 0
first = True
end_pos = None, None
tok = None
for tok in token_iterator:
end_pos = tok.end_pos
if first:
start_pos = tok.start_pos
first = False
if isinstance(tok, Base):
# the token is a Name, which has already been parsed
if isinstance(tok, ListComprehension):
# it's not possible to set it earlier
tok.parent = self
elif tok == 'lambda':
lambd, tok = parse_lambda(token_iterator)
if lambd is not None:
token_list.append(lambd)
elif tok == 'for':
list_comp, tok = parse_list_comp(token_iterator, token_list,
start_pos, tok.end_pos)
if list_comp is not None:
token_list = [list_comp]
if tok in closing_brackets: