src/eric7/Utilities/ModuleParser.py

branch
eric7
changeset 9733
c5c2a74e9382
parent 9693
a7e9fd398e5a
child 9739
d527cfe919ae
equal deleted inserted replaced
9732:3985549d7d36 9733:c5c2a74e9382
591 m = self._getnext(src, i) 591 m = self._getnext(src, i)
592 if not m.hasMatch(): 592 if not m.hasMatch():
593 break 593 break
594 start, i = m.capturedStart(), m.capturedEnd() 594 start, i = m.capturedStart(), m.capturedEnd()
595 595
596 if m.hasCaptured("MethodModifier"): 596 if m.captured("MethodModifier"):
597 modifierIndent = _indent(m.captured("MethodModifierIndent")) 597 modifierIndent = _indent(m.captured("MethodModifierIndent"))
598 modifierType = m.captured("MethodModifierType") 598 modifierType = m.captured("MethodModifierType")
599 599
600 elif m.hasCaptured("Method"): 600 elif m.captured("Method"):
601 # found a method definition or function 601 # found a method definition or function
602 thisindent = _indent(m.captured("MethodIndent")) 602 thisindent = _indent(m.captured("MethodIndent"))
603 meth_name = m.captured("MethodName") 603 meth_name = m.captured("MethodName")
604 meth_sig = m.captured("MethodSignature") 604 meth_sig = m.captured("MethodSignature")
605 meth_sig = meth_sig.replace("\\\n", "") 605 meth_sig = meth_sig.replace("\\\n", "")
702 702
703 # reset the modifier settings 703 # reset the modifier settings
704 modifierType = Function.General 704 modifierType = Function.General
705 modifierIndent = -1 705 modifierIndent = -1
706 706
707 elif m.hasCaptured("Docstring"): 707 elif m.captured("Docstring"):
708 contents = m.captured("DocstringContents3") 708 contents = m.captured("DocstringContents3")
709 if contents: 709 if contents:
710 contents = _hashsub(r"\1", contents) 710 contents = _hashsub(r"\1", contents)
711 else: 711 else:
712 if self.file.lower().endswith(".ptl"): 712 if self.file.lower().endswith(".ptl"):
716 "DocstringContents2" 716 "DocstringContents2"
717 ) 717 )
718 if cur_obj: 718 if cur_obj:
719 cur_obj.addDescription(contents) 719 cur_obj.addDescription(contents)
720 720
721 elif m.hasCaptured("String"): 721 elif m.captured("String"):
722 if modulelevel and ( 722 if modulelevel and (
723 src[start - len("\r\n") : start] == "\r\n" 723 src[start - len("\r\n") : start] == "\r\n"
724 or src[start - len("\n") : start] == "\n" 724 or src[start - len("\n") : start] == "\n"
725 or src[start - len("\r") : start] == "\r" 725 or src[start - len("\r") : start] == "\r"
726 ): 726 ):
735 "StringContents2" 735 "StringContents2"
736 ) 736 )
737 if cur_obj: 737 if cur_obj:
738 cur_obj.addDescription(contents) 738 cur_obj.addDescription(contents)
739 739
740 elif m.hasCaptured("Class"): 740 elif m.captured("Class"):
741 # we found a class definition 741 # we found a class definition
742 thisindent = _indent(m.captured("ClassIndent")) 742 thisindent = _indent(m.captured("ClassIndent"))
743 lineno += src.count("\n", last_lineno_pos, start) 743 lineno += src.count("\n", last_lineno_pos, start)
744 last_lineno_pos = start 744 last_lineno_pos = start
745 # close all classes indented at least as much 745 # close all classes indented at least as much
793 cur_obj = cur_class 793 cur_obj = cur_class
794 self.addClass(class_name, cur_class) 794 self.addClass(class_name, cur_class)
795 # add nested classes to the module 795 # add nested classes to the module
796 classstack.append((cur_class, thisindent)) 796 classstack.append((cur_class, thisindent))
797 797
798 elif m.hasCaptured("Attribute"): 798 elif m.captured("Attribute"):
799 lineno += src.count("\n", last_lineno_pos, start) 799 lineno += src.count("\n", last_lineno_pos, start)
800 last_lineno_pos = start 800 last_lineno_pos = start
801 index = -1 801 index = -1
802 while index >= -len(classstack): 802 while index >= -len(classstack):
803 if classstack[index][0] is not None: 803 if classstack[index][0] is not None:
807 classstack[index][0].addAttribute(attrName, attr) 807 classstack[index][0].addAttribute(attrName, attr)
808 break 808 break
809 else: 809 else:
810 index -= 1 810 index -= 1
811 811
812 elif m.hasCaptured("Main"): 812 elif m.captured("Main"):
813 # 'main' part of the script, reset class stack 813 # 'main' part of the script, reset class stack
814 lineno += src.count("\n", last_lineno_pos, start) 814 lineno += src.count("\n", last_lineno_pos, start)
815 last_lineno_pos = start 815 last_lineno_pos = start
816 classstack = [] 816 classstack = []
817 817
818 elif m.hasCaptured("Variable"): 818 elif m.captured("Variable"):
819 thisindent = _indent(m.captured("VariableIndent")) 819 thisindent = _indent(m.captured("VariableIndent"))
820 variable_name = m.captured("VariableName") 820 variable_name = m.captured("VariableName")
821 isSignal = m.captured("VariableSignal") != "" 821 isSignal = m.captured("VariableSignal") != ""
822 lineno += src.count("\n", last_lineno_pos, start) 822 lineno += src.count("\n", last_lineno_pos, start)
823 last_lineno_pos = start 823 last_lineno_pos = start
846 ) 846 )
847 self.__py_setVisibility(attr) 847 self.__py_setVisibility(attr)
848 classstack[index][0].addGlobal(variable_name, attr) 848 classstack[index][0].addGlobal(variable_name, attr)
849 break 849 break
850 850
851 elif m.hasCaptured("Import"): 851 elif m.captured("Import"):
852 # - import module 852 # - import module
853 names = [ 853 names = [
854 n.strip() 854 n.strip()
855 for n in "".join(m.captured("ImportList").splitlines()) 855 for n in "".join(m.captured("ImportList").splitlines())
856 .replace("\\", "") 856 .replace("\\", "")
858 ] 858 ]
859 self.imports.extend( 859 self.imports.extend(
860 [name for name in names if name not in self.imports] 860 [name for name in names if name not in self.imports]
861 ) 861 )
862 862
863 elif m.hasCaptured("ImportFrom"): 863 elif m.captured("ImportFrom"):
864 # - from module import stuff 864 # - from module import stuff
865 mod = m.captured("ImportFromPath") 865 mod = m.captured("ImportFromPath")
866 namesLines = ( 866 namesLines = (
867 m.captured("ImportFromList") 867 m.captured("ImportFromList")
868 .replace("(", "") 868 .replace("(", "")
877 self.from_imports[mod] = [] 877 self.from_imports[mod] = []
878 self.from_imports[mod].extend( 878 self.from_imports[mod].extend(
879 [name for name in names if name not in self.from_imports[mod]] 879 [name for name in names if name not in self.from_imports[mod]]
880 ) 880 )
881 881
882 elif m.hasCaptured("ConditionalDefine"): 882 elif m.captured("ConditionalDefine"):
883 # a conditional function/method definition 883 # a conditional function/method definition
884 thisindent = _indent(m.captured("ConditionalDefineIndent")) 884 thisindent = _indent(m.captured("ConditionalDefineIndent"))
885 while conditionalsstack and conditionalsstack[-1] >= thisindent: 885 while conditionalsstack and conditionalsstack[-1] >= thisindent:
886 del conditionalsstack[-1] 886 del conditionalsstack[-1]
887 if deltastack: 887 if deltastack:
888 del deltastack[-1] 888 del deltastack[-1]
889 conditionalsstack.append(thisindent) 889 conditionalsstack.append(thisindent)
890 deltaindentcalculated = 0 890 deltaindentcalculated = 0
891 891
892 elif m.hasCaptured("Comment") and modulelevel: 892 elif m.captured("Comment") and modulelevel:
893 continue 893 continue
894 894
895 modulelevel = False 895 modulelevel = False
896 896
897 def __rb_scan(self, src): 897 def __rb_scan(self, src):
913 m = self._getnext(src, i) 913 m = self._getnext(src, i)
914 if not m.hasMatch(): 914 if not m.hasMatch():
915 break 915 break
916 start, i = m.capturedStart(), m.capturedEnd() 916 start, i = m.capturedStart(), m.capturedEnd()
917 917
918 if m.hasCaptured("Method"): 918 if m.captured("Method"):
919 # found a method definition or function 919 # found a method definition or function
920 thisindent = indent 920 thisindent = indent
921 indent += 1 921 indent += 1
922 meth_name = ( 922 meth_name = (
923 m.captured("MethodName") 923 m.captured("MethodName")
980 if cur_obj and isinstance(cur_obj, Function): 980 if cur_obj and isinstance(cur_obj, Function):
981 cur_obj.setEndLine(lineno - 1) 981 cur_obj.setEndLine(lineno - 1)
982 cur_obj = f 982 cur_obj = f
983 classstack.append((None, thisindent)) # Marker for nested fns 983 classstack.append((None, thisindent)) # Marker for nested fns
984 984
985 elif m.hasCaptured("Docstring"): 985 elif m.captured("Docstring"):
986 contents = m.captured("DocstringContents") 986 contents = m.captured("DocstringContents")
987 if contents: 987 if contents:
988 contents = _hashsub(r"\1", contents) 988 contents = _hashsub(r"\1", contents)
989 if cur_obj: 989 if cur_obj:
990 cur_obj.addDescription(contents) 990 cur_obj.addDescription(contents)
991 991
992 elif m.hasCaptured("Class"): 992 elif m.captured("Class"):
993 # we found a class definition 993 # we found a class definition
994 thisindent = indent 994 thisindent = indent
995 indent += 1 995 indent += 1
996 lineno += src.count("\n", last_lineno_pos, start) 996 lineno += src.count("\n", last_lineno_pos, start)
997 last_lineno_pos = start 997 last_lineno_pos = start
1035 while acstack and acstack[-1][1] >= thisindent: 1035 while acstack and acstack[-1][1] >= thisindent:
1036 del acstack[-1] 1036 del acstack[-1]
1037 acstack.append(["public", thisindent]) 1037 acstack.append(["public", thisindent])
1038 # default access control is 'public' 1038 # default access control is 'public'
1039 1039
1040 elif m.hasCaptured("Module"): 1040 elif m.captured("Module"):
1041 # we found a module definition 1041 # we found a module definition
1042 thisindent = indent 1042 thisindent = indent
1043 indent += 1 1043 indent += 1
1044 lineno += src.count("\n", last_lineno_pos, start) 1044 lineno += src.count("\n", last_lineno_pos, start)
1045 last_lineno_pos = start 1045 last_lineno_pos = start
1068 while acstack and acstack[-1][1] >= thisindent: 1068 while acstack and acstack[-1][1] >= thisindent:
1069 del acstack[-1] 1069 del acstack[-1]
1070 acstack.append(["public", thisindent]) 1070 acstack.append(["public", thisindent])
1071 # default access control is 'public' 1071 # default access control is 'public'
1072 1072
1073 elif m.hasCaptured("AccessControl"): 1073 elif m.captured("AccessControl"):
1074 aclist = m.captured("AccessControlList") 1074 aclist = m.captured("AccessControlList")
1075 if not aclist: 1075 if not aclist:
1076 index = -1 1076 index = -1
1077 while index >= -len(acstack): 1077 while index >= -len(acstack):
1078 if acstack[index][1] < indent: 1078 if acstack[index][1] < indent:
1112 acmeth.setPublic() 1112 acmeth.setPublic()
1113 break 1113 break
1114 else: 1114 else:
1115 index -= 1 1115 index -= 1
1116 1116
1117 elif m.hasCaptured("Attribute"): 1117 elif m.captured("Attribute"):
1118 lineno += src.count("\n", last_lineno_pos, start) 1118 lineno += src.count("\n", last_lineno_pos, start)
1119 last_lineno_pos = start 1119 last_lineno_pos = start
1120 index = -1 1120 index = -1
1121 while index >= -len(classstack): 1121 while index >= -len(classstack):
1122 if ( 1122 if (
1140 self.addGlobal(attrName, attr) 1140 self.addGlobal(attrName, attr)
1141 if lastGlobalEntry: 1141 if lastGlobalEntry:
1142 lastGlobalEntry.setEndLine(lineno - 1) 1142 lastGlobalEntry.setEndLine(lineno - 1)
1143 lastGlobalEntry = None 1143 lastGlobalEntry = None
1144 1144
1145 elif m.hasCaptured("Attr"): 1145 elif m.captured("Attr"):
1146 lineno += src.count("\n", last_lineno_pos, start) 1146 lineno += src.count("\n", last_lineno_pos, start)
1147 last_lineno_pos = start 1147 last_lineno_pos = start
1148 index = -1 1148 index = -1
1149 while index >= -len(classstack): 1149 while index >= -len(classstack):
1150 if ( 1150 if (
1191 parent.addAttribute(attr.name, attr) 1191 parent.addAttribute(attr.name, attr)
1192 break 1192 break
1193 else: 1193 else:
1194 index -= 1 1194 index -= 1
1195 1195
1196 elif m.hasCaptured("Begin"): 1196 elif m.captured("Begin"):
1197 # a begin of a block we are not interested in 1197 # a begin of a block we are not interested in
1198 indent += 1 1198 indent += 1
1199 1199
1200 elif m.hasCaptured("End"): 1200 elif m.captured("End"):
1201 # an end of a block 1201 # an end of a block
1202 indent -= 1 1202 indent -= 1
1203 if indent < 0: 1203 if indent < 0:
1204 # no negative indent allowed 1204 # no negative indent allowed
1205 if classstack: 1205 if classstack:
1207 indent = classstack[-1][1] 1207 indent = classstack[-1][1]
1208 else: 1208 else:
1209 indent = 0 1209 indent = 0
1210 1210
1211 elif ( 1211 elif (
1212 m.hasCaptured("String") 1212 m.captured("String")
1213 or m.hasCaptured("Comment") 1213 or m.captured("Comment")
1214 or m.hasCaptured("ClassIgnored") 1214 or m.captured("ClassIgnored")
1215 or m.hasCaptured("BeginEnd") 1215 or m.captured("BeginEnd")
1216 ): 1216 ):
1217 pass 1217 pass
1218 1218
1219 def createHierarchy(self): 1219 def createHierarchy(self):
1220 """ 1220 """

eric ide

mercurial