src/eric7/Utilities/ClassBrowsers/pyclbr.py

branch
eric7
changeset 10477
11b5d533e4a2
parent 10471
28d4780d6a66
child 10592
2bada76be1a6
child 10677
6ee2e475490c
equal deleted inserted replaced
10476:5182a7c5a9f3 10477:11b5d533e4a2
69 69
70 | (?P<Method> 70 | (?P<Method>
71 ^ 71 ^
72 (?P<MethodIndent> [ \t]* ) 72 (?P<MethodIndent> [ \t]* )
73 (?: async [ \t]+ )? (?: cdef | cpdef | def) [ \t]+ 73 (?: async [ \t]+ )? (?: cdef | cpdef | def) [ \t]+
74 (?P<MethodName> \w+ ) 74 (?P<MethodName> [\pL_] \w* )
75 (?: [ \t]* \[ [^\]]+ \] )? 75 (?: [ \t]* \[ [^\]]+ \] )?
76 [ \t]* \( 76 [ \t]* \(
77 (?P<MethodSignature> (?: [^)] | \)[ \t]*,? )*? ) 77 (?P<MethodSignature> (?: [^)] | \)[ \t]*,? )*? )
78 \) [ \t]* 78 \) [ \t]*
79 (?P<MethodReturnAnnotation> (?: -> [ \t]* [^:]+ )? ) 79 (?P<MethodReturnAnnotation> (?: -> [ \t]* [^:]+ )? )
83 | (?P<Class> 83 | (?P<Class>
84 ^ 84 ^
85 (?P<ClassIndent> [ \t]* ) 85 (?P<ClassIndent> [ \t]* )
86 (?: cdef [ \t]+ )? 86 (?: cdef [ \t]+ )?
87 class [ \t]+ 87 class [ \t]+
88 (?P<ClassName> \w+ ) 88 (?P<ClassName> [\pL_] \w* )
89 (?: [ \t]* \[ [^\]]+ \] )? 89 (?: [ \t]* \[ [^\]]+ \] )?
90 [ \t]* 90 [ \t]*
91 (?P<ClassSupers> \( [^)]* \) )? 91 (?P<ClassSupers> \( [^)]* \) )?
92 [ \t]* : 92 [ \t]* :
93 ) 93 )
94 94
95 | (?P<Attribute> 95 | (?P<Attribute>
96 ^ 96 ^
97 (?P<AttributeIndent> [ \t]* ) 97 (?P<AttributeIndent> [ \t]* )
98 self [ \t]* \. [ \t]* 98 self [ \t]* \. [ \t]*
99 (?P<AttributeName> \w+ ) 99 (?P<AttributeName> [\pL_] \w* )
100 (?: [ \t]* : [^=\n]+ )?
101 [ \t]* = 100 [ \t]* =
102 ) 101 )
103 102
103 | (?P<TypedAttribute>
104 ^
105 (?P<TypedAttributeIndent> [ \t]* )
106 self [ \t]* \. [ \t]*
107 (?P<TypedAttributeName> [\pL_] \w* )
108 [ \t]* : [ \t]+
109 )
110
104 | (?P<Variable> 111 | (?P<Variable>
105 ^ 112 ^
106 (?P<VariableIndent> [ \t]* ) 113 (?P<VariableIndent> [ \t]* )
107 (?P<VariableName> \w+ ) 114 (?P<VariableName> [\pL_] \w* )
108 [ \t]* = 115 [ \t]* =
109 ) 116 )
110 117
111 | (?P<TypedVariable> 118 | (?P<TypedVariable>
112 ^ 119 ^
113 (?P<TypedVariableIndent> [ \t]* ) 120 (?P<TypedVariableIndent> [ \t]* )
114 (?P<TypedVariableName> \w+ ) 121 (?P<TypedVariableName> [\pL_] \w* )
115 [ \t]* : 122 [ \t]* :
116 ) 123 )
117 124
118 | (?P<Main> 125 | (?P<Main>
119 ^ 126 ^
644 dictionary[class_name] = cur_class 651 dictionary[class_name] = cur_class
645 else: 652 else:
646 classstack[-1][0]._addclass(class_name, cur_class) 653 classstack[-1][0]._addclass(class_name, cur_class)
647 classstack.append((cur_class, thisindent)) 654 classstack.append((cur_class, thisindent))
648 655
649 elif m.captured("Attribute"): 656 elif m.captured("Attribute") or m.captured("TypedAttribute"):
657 if m.captured("Attribute"):
658 attribute_name = m.captured("AttributeName")
659 else:
660 attribute_name = m.captured("TypedAttributeName")
650 lineno += src.count("\n", last_lineno_pos, start) 661 lineno += src.count("\n", last_lineno_pos, start)
651 last_lineno_pos = start 662 last_lineno_pos = start
652 index = -1 663 index = -1
653 while index >= -len(classstack): 664 while index >= -len(classstack):
654 if classstack[index][0] is not None and not isinstance( 665 if classstack[index][0] is not None and not isinstance(
655 classstack[index][0], Function 666 classstack[index][0], Function
656 ): 667 ):
657 attr = Attribute(module, m.captured("AttributeName"), file, lineno) 668 attr = Attribute(module, attribute_name, file, lineno)
658 classstack[index][0]._addattribute(attr) 669 classstack[index][0]._addattribute(attr)
659 break 670 break
660 else: 671 else:
661 index -= 1 672 index -= 1
662 673
664 # 'main' part of the script, reset class stack 675 # 'main' part of the script, reset class stack
665 lineno += src.count("\n", last_lineno_pos, start) 676 lineno += src.count("\n", last_lineno_pos, start)
666 last_lineno_pos = start 677 last_lineno_pos = start
667 classstack = [] 678 classstack = []
668 679
669 elif m.captured("Variable"): 680 elif m.captured("Variable") or m.captured("TypedVariable"):
670 thisindent = _indent(m.captured("VariableIndent")) 681 if m.captured("Variable"):
671 variable_name = m.captured("VariableName") 682 thisindent = _indent(m.captured("VariableIndent"))
683 variable_name = m.captured("VariableName")
684 else:
685 thisindent = _indent(m.captured("TypedVariableIndent"))
686 variable_name = m.captured("TypedVariableName")
687 if keyword.iskeyword(variable_name):
688 # only if the determined name is not a keyword (e.g. else, except)
689 continue
672 lineno += src.count("\n", last_lineno_pos, start) 690 lineno += src.count("\n", last_lineno_pos, start)
673 last_lineno_pos = start 691 last_lineno_pos = start
674 if thisindent == 0 or not classstack: 692 if thisindent == 0 or not classstack:
675 # global variable, reset class stack first 693 # global variable, reset class stack first
676 classstack = [] 694 classstack = []
696 classstack[index][0]._addattribute( 714 classstack[index][0]._addattribute(
697 Attribute(module, variable_name, file, lineno) 715 Attribute(module, variable_name, file, lineno)
698 ) 716 )
699 break 717 break
700 718
701 elif m.captured("TypedVariable"):
702 thisindent = _indent(m.captured("TypedVariableIndent"))
703 variable_name = m.captured("TypedVariableName")
704 if not keyword.iskeyword(variable_name):
705 # only if the determined name is not a keyword (e.g. else, except)
706 lineno += src.count("\n", last_lineno_pos, start)
707 last_lineno_pos = start
708 if thisindent == 0 or not classstack:
709 # global variable, reset class stack first
710 classstack = []
711
712 if "@@Globals@@" not in dictionary:
713 dictionary["@@Globals@@"] = ClbrBaseClasses.ClbrBase(
714 module, "Globals", file, lineno
715 )
716 dictionary["@@Globals@@"]._addglobal(
717 Attribute(module, variable_name, file, lineno)
718 )
719 else:
720 index = -1
721 while index >= -len(classstack):
722 if classstack[index][1] >= thisindent:
723 index -= 1
724 else:
725 if isinstance(classstack[index][0], Class):
726 classstack[index][0]._addglobal(
727 Attribute(module, variable_name, file, lineno)
728 )
729 elif isinstance(classstack[index][0], Function):
730 classstack[index][0]._addattribute(
731 Attribute(module, variable_name, file, lineno)
732 )
733 break
734
735 elif m.captured("Publics"): 719 elif m.captured("Publics"):
736 idents = m.captured("Identifiers") 720 idents = m.captured("Identifiers")
737 lineno += src.count("\n", last_lineno_pos, start) 721 lineno += src.count("\n", last_lineno_pos, start)
738 last_lineno_pos = start 722 last_lineno_pos = start
739 pubs = Publics( 723 pubs = Publics(

eric ide

mercurial