eric6/Utilities/ModuleParser.py

changeset 7259
7c017076c12e
parent 7249
0bf517e60f54
child 7360
9190402e4505
equal deleted inserted replaced
7258:aff39db4dacc 7259:7c017076c12e
553 meth_sig = m.group("MethodSignature") 553 meth_sig = m.group("MethodSignature")
554 meth_sig = meth_sig.replace('\\\n', '') 554 meth_sig = meth_sig.replace('\\\n', '')
555 meth_ret = m.group("MethodReturnAnnotation") 555 meth_ret = m.group("MethodReturnAnnotation")
556 meth_ret = meth_ret.replace('\\\n', '') 556 meth_ret = meth_ret.replace('\\\n', '')
557 if m.group("MethodPyQtSignature") is not None: 557 if m.group("MethodPyQtSignature") is not None:
558 meth_pyqtSig = m.group("MethodPyQtSignature")\ 558 meth_pyqtSig = (
559 .replace('\\\n', '')\ 559 m.group("MethodPyQtSignature")
560 .split('result')[0]\ 560 .replace('\\\n', '')
561 .split('name')[0]\ 561 .split('result')[0]
562 .strip("\"', \t") 562 .split('name')[0]
563 .strip("\"', \t")
564 )
563 else: 565 else:
564 meth_pyqtSig = None 566 meth_pyqtSig = None
565 lineno = lineno + src.count('\n', last_lineno_pos, start) 567 lineno = lineno + src.count('\n', last_lineno_pos, start)
566 last_lineno_pos = start 568 last_lineno_pos = start
567 if modifierType and modifierIndent == thisindent: 569 if modifierType and modifierIndent == thisindent:
582 deltaindent = reduce( 584 deltaindent = reduce(
583 lambda x, y: x + y, deltastack) 585 lambda x, y: x + y, deltastack)
584 deltaindentcalculated = 1 586 deltaindentcalculated = 1
585 thisindent -= deltaindent 587 thisindent -= deltaindent
586 else: 588 else:
587 while conditionalsstack and \ 589 while (
588 conditionalsstack[-1] >= thisindent: 590 conditionalsstack and
591 conditionalsstack[-1] >= thisindent
592 ):
589 del conditionalsstack[-1] 593 del conditionalsstack[-1]
590 if deltastack: 594 if deltastack:
591 del deltastack[-1] 595 del deltastack[-1]
592 deltaindentcalculated = 0 596 deltaindentcalculated = 0
593 # close all classes indented at least as much 597 # close all classes indented at least as much
594 while classstack and \ 598 while classstack and classstack[-1][1] >= thisindent:
595 classstack[-1][1] >= thisindent: 599 if (
596 if classstack[-1][0] is not None and \ 600 classstack[-1][0] is not None and
597 isinstance(classstack[-1][0], (Class, Function)): 601 isinstance(classstack[-1][0], (Class, Function))
602 ):
598 # record the end line of this class or function 603 # record the end line of this class or function
599 classstack[-1][0].setEndLine(lineno - 1) 604 classstack[-1][0].setEndLine(lineno - 1)
600 del classstack[-1] 605 del classstack[-1]
601 if classstack: 606 if classstack:
602 csi = -1 607 csi = -1
650 contents = _hashsub(r"\1", contents) 655 contents = _hashsub(r"\1", contents)
651 else: 656 else:
652 if self.file.lower().endswith('.ptl'): 657 if self.file.lower().endswith('.ptl'):
653 contents = "" 658 contents = ""
654 else: 659 else:
655 contents = m.group("DocstringContents1") \ 660 contents = (
656 or m.group("DocstringContents2") 661 m.group("DocstringContents1") or
662 m.group("DocstringContents2")
663 )
657 if cur_obj: 664 if cur_obj:
658 cur_obj.addDescription(contents) 665 cur_obj.addDescription(contents)
659 666
660 elif m.start("String") >= 0: 667 elif m.start("String") >= 0:
661 if modulelevel and \ 668 if (
662 (src[start - len('\r\n'):start] == '\r\n' or 669 modulelevel and (
663 src[start - len('\n'):start] == '\n' or 670 src[start - len('\r\n'):start] == '\r\n' or
664 src[start - len('\r'):start] == '\r'): 671 src[start - len('\n'):start] == '\n' or
672 src[start - len('\r'):start] == '\r'
673 )
674 ):
665 contents = m.group("StringContents3") 675 contents = m.group("StringContents3")
666 if contents is not None: 676 if contents is not None:
667 contents = _hashsub(r"\1", contents) 677 contents = _hashsub(r"\1", contents)
668 else: 678 else:
669 if self.file.lower().endswith('.ptl'): 679 if self.file.lower().endswith('.ptl'):
670 contents = "" 680 contents = ""
671 else: 681 else:
672 contents = m.group("StringContents1") \ 682 contents = (
673 or m.group("StringContents2") 683 m.group("StringContents1") or
684 m.group("StringContents2")
685 )
674 if cur_obj: 686 if cur_obj:
675 cur_obj.addDescription(contents) 687 cur_obj.addDescription(contents)
676 688
677 elif m.start("Class") >= 0: 689 elif m.start("Class") >= 0:
678 # we found a class definition 690 # we found a class definition
679 thisindent = _indent(m.group("ClassIndent")) 691 thisindent = _indent(m.group("ClassIndent"))
680 lineno = lineno + src.count('\n', last_lineno_pos, start) 692 lineno = lineno + src.count('\n', last_lineno_pos, start)
681 last_lineno_pos = start 693 last_lineno_pos = start
682 # close all classes indented at least as much 694 # close all classes indented at least as much
683 while classstack and \ 695 while classstack and classstack[-1][1] >= thisindent:
684 classstack[-1][1] >= thisindent: 696 if (
685 if classstack[-1][0] is not None and \ 697 classstack[-1][0] is not None and
686 isinstance(classstack[-1][0], (Class, Function)): 698 isinstance(classstack[-1][0], (Class, Function))
699 ):
687 # record the end line of this class or function 700 # record the end line of this class or function
688 classstack[-1][0].setEndLine(lineno - 1) 701 classstack[-1][0].setEndLine(lineno - 1)
689 del classstack[-1] 702 del classstack[-1]
690 class_name = m.group("ClassName") 703 class_name = m.group("ClassName")
691 inherit = m.group("ClassSupers") 704 inherit = m.group("ClassSupers")
767 index = -1 780 index = -1
768 while index >= -len(classstack): 781 while index >= -len(classstack):
769 if classstack[index][1] >= thisindent: 782 if classstack[index][1] >= thisindent:
770 index -= 1 783 index -= 1
771 else: 784 else:
772 if classstack[index][0] is not None and \ 785 if (
773 isinstance(classstack[index][0], Class): 786 classstack[index][0] is not None and
787 isinstance(classstack[index][0], Class)
788 ):
774 attr = Attribute( 789 attr = Attribute(
775 self.name, variable_name, self.file, 790 self.name, variable_name, self.file,
776 lineno, isSignal=isSignal) 791 lineno, isSignal=isSignal)
777 self.__py_setVisibility(attr) 792 self.__py_setVisibility(attr)
778 classstack[index][0].addGlobal( 793 classstack[index][0].addGlobal(
807 if name not in self.from_imports[mod]]) 822 if name not in self.from_imports[mod]])
808 823
809 elif m.start("ConditionalDefine") >= 0: 824 elif m.start("ConditionalDefine") >= 0:
810 # a conditional function/method definition 825 # a conditional function/method definition
811 thisindent = _indent(m.group("ConditionalDefineIndent")) 826 thisindent = _indent(m.group("ConditionalDefineIndent"))
812 while conditionalsstack and \ 827 while (
813 conditionalsstack[-1] >= thisindent: 828 conditionalsstack and
829 conditionalsstack[-1] >= thisindent
830 ):
814 del conditionalsstack[-1] 831 del conditionalsstack[-1]
815 if deltastack: 832 if deltastack:
816 del deltastack[-1] 833 del deltastack[-1]
817 conditionalsstack.append(thisindent) 834 conditionalsstack.append(thisindent)
818 deltaindentcalculated = 0 835 deltaindentcalculated = 0
848 865
849 if m.start("Method") >= 0: 866 if m.start("Method") >= 0:
850 # found a method definition or function 867 # found a method definition or function
851 thisindent = indent 868 thisindent = indent
852 indent += 1 869 indent += 1
853 meth_name = m.group("MethodName") or \ 870 meth_name = (
854 m.group("MethodName2") or \ 871 m.group("MethodName") or
872 m.group("MethodName2") or
855 m.group("MethodName3") 873 m.group("MethodName3")
874 )
856 meth_sig = m.group("MethodSignature") 875 meth_sig = m.group("MethodSignature")
857 meth_sig = meth_sig and meth_sig.replace('\\\n', '') or '' 876 meth_sig = meth_sig and meth_sig.replace('\\\n', '') or ''
858 lineno = lineno + src.count('\n', last_lineno_pos, start) 877 lineno = lineno + src.count('\n', last_lineno_pos, start)
859 last_lineno_pos = start 878 last_lineno_pos = start
860 if meth_name.startswith('self.'): 879 if meth_name.startswith('self.'):
861 meth_name = meth_name[5:] 880 meth_name = meth_name[5:]
862 elif meth_name.startswith('self::'): 881 elif meth_name.startswith('self::'):
863 meth_name = meth_name[6:] 882 meth_name = meth_name[6:]
864 # close all classes/modules indented at least as much 883 # close all classes/modules indented at least as much
865 while classstack and \ 884 while classstack and classstack[-1][1] >= thisindent:
866 classstack[-1][1] >= thisindent: 885 if (
867 if classstack[-1][0] is not None and \ 886 classstack[-1][0] is not None and
868 isinstance(classstack[-1][0], 887 isinstance(classstack[-1][0],
869 (Class, Function, RbModule)): 888 (Class, Function, RbModule))
889 ):
870 # record the end line of this class, function or module 890 # record the end line of this class, function or module
871 classstack[-1][0].setEndLine(lineno - 1) 891 classstack[-1][0].setEndLine(lineno - 1)
872 del classstack[-1] 892 del classstack[-1]
873 while acstack and \ 893 while acstack and acstack[-1][1] >= thisindent:
874 acstack[-1][1] >= thisindent:
875 del acstack[-1] 894 del acstack[-1]
876 if classstack: 895 if classstack:
877 csi = -1 896 csi = -1
878 while csi >= -len(classstack): 897 while csi >= -len(classstack):
879 # nested defs are added to the class 898 # nested defs are added to the class
880 cur_class = classstack[csi][0] 899 cur_class = classstack[csi][0]
881 csi -= 1 900 csi -= 1
882 if cur_class is None: 901 if cur_class is None:
883 continue 902 continue
884 903
885 if isinstance(cur_class, Class) or \ 904 if (
886 isinstance(cur_class, RbModule): 905 isinstance(cur_class, Class) or
906 isinstance(cur_class, RbModule)
907 ):
887 # it's a class/module method 908 # it's a class/module method
888 f = Function(None, meth_name, 909 f = Function(None, meth_name,
889 None, lineno, meth_sig) 910 None, lineno, meth_sig)
890 cur_class.addMethod(meth_name, f) 911 cur_class.addMethod(meth_name, f)
891 break 912 break
938 thisindent = indent 959 thisindent = indent
939 indent += 1 960 indent += 1
940 lineno = lineno + src.count('\n', last_lineno_pos, start) 961 lineno = lineno + src.count('\n', last_lineno_pos, start)
941 last_lineno_pos = start 962 last_lineno_pos = start
942 # close all classes/modules indented at least as much 963 # close all classes/modules indented at least as much
943 while classstack and \ 964 while classstack and classstack[-1][1] >= thisindent:
944 classstack[-1][1] >= thisindent: 965 if (
945 if classstack[-1][0] is not None and \ 966 classstack[-1][0] is not None and
946 isinstance(classstack[-1][0], 967 isinstance(classstack[-1][0],
947 (Class, Function, RbModule)): 968 (Class, Function, RbModule))
969 ):
948 # record the end line of this class, function or module 970 # record the end line of this class, function or module
949 classstack[-1][0].setEndLine(lineno - 1) 971 classstack[-1][0].setEndLine(lineno - 1)
950 del classstack[-1] 972 del classstack[-1]
951 class_name = m.group("ClassName") or m.group("ClassName2") 973 class_name = m.group("ClassName") or m.group("ClassName2")
952 inherit = m.group("ClassSupers") 974 inherit = m.group("ClassSupers")
962 parent_obj = classstack[-1][0] 984 parent_obj = classstack[-1][0]
963 else: 985 else:
964 parent_obj = self 986 parent_obj = self
965 if class_name in parent_obj.classes: 987 if class_name in parent_obj.classes:
966 cur_class = parent_obj.classes[class_name] 988 cur_class = parent_obj.classes[class_name]
967 elif classstack and \ 989 elif (
968 isinstance(classstack[-1][0], Class) and \ 990 classstack and
969 class_name == "self": 991 isinstance(classstack[-1][0], Class) and
992 class_name == "self"
993 ):
970 cur_class = classstack[-1][0] 994 cur_class = classstack[-1][0]
971 else: 995 else:
972 parent_obj.addClass(class_name, cur_class) 996 parent_obj.addClass(class_name, cur_class)
973 if not classstack: 997 if not classstack:
974 if lastGlobalEntry: 998 if lastGlobalEntry:
975 lastGlobalEntry.setEndLine(lineno - 1) 999 lastGlobalEntry.setEndLine(lineno - 1)
976 lastGlobalEntry = cur_class 1000 lastGlobalEntry = cur_class
977 cur_obj = cur_class 1001 cur_obj = cur_class
978 classstack.append((cur_class, thisindent)) 1002 classstack.append((cur_class, thisindent))
979 while acstack and \ 1003 while acstack and acstack[-1][1] >= thisindent:
980 acstack[-1][1] >= thisindent:
981 del acstack[-1] 1004 del acstack[-1]
982 acstack.append(["public", thisindent]) 1005 acstack.append(["public", thisindent])
983 # default access control is 'public' 1006 # default access control is 'public'
984 1007
985 elif m.start("Module") >= 0: 1008 elif m.start("Module") >= 0:
987 thisindent = indent 1010 thisindent = indent
988 indent += 1 1011 indent += 1
989 lineno = lineno + src.count('\n', last_lineno_pos, start) 1012 lineno = lineno + src.count('\n', last_lineno_pos, start)
990 last_lineno_pos = start 1013 last_lineno_pos = start
991 # close all classes/modules indented at least as much 1014 # close all classes/modules indented at least as much
992 while classstack and \ 1015 while classstack and classstack[-1][1] >= thisindent:
993 classstack[-1][1] >= thisindent: 1016 if (
994 if classstack[-1][0] is not None and \ 1017 classstack[-1][0] is not None and
995 isinstance(classstack[-1][0], 1018 isinstance(classstack[-1][0],
996 (Class, Function, RbModule)): 1019 (Class, Function, RbModule))
1020 ):
997 # record the end line of this class, function or module 1021 # record the end line of this class, function or module
998 classstack[-1][0].setEndLine(lineno - 1) 1022 classstack[-1][0].setEndLine(lineno - 1)
999 del classstack[-1] 1023 del classstack[-1]
1000 module_name = m.group("ModuleName") 1024 module_name = m.group("ModuleName")
1001 # remember this class 1025 # remember this class
1010 if lastGlobalEntry: 1034 if lastGlobalEntry:
1011 lastGlobalEntry.setEndLine(lineno - 1) 1035 lastGlobalEntry.setEndLine(lineno - 1)
1012 lastGlobalEntry = cur_class 1036 lastGlobalEntry = cur_class
1013 cur_obj = cur_class 1037 cur_obj = cur_class
1014 classstack.append((cur_class, thisindent)) 1038 classstack.append((cur_class, thisindent))
1015 while acstack and \ 1039 while acstack and acstack[-1][1] >= thisindent:
1016 acstack[-1][1] >= thisindent:
1017 del acstack[-1] 1040 del acstack[-1]
1018 acstack.append(["public", thisindent]) 1041 acstack.append(["public", thisindent])
1019 # default access control is 'public' 1042 # default access control is 'public'
1020 1043
1021 elif m.start("AccessControl") >= 0: 1044 elif m.start("AccessControl") >= 0:
1022 aclist = m.group("AccessControlList") 1045 aclist = m.group("AccessControlList")
1023 if aclist is None: 1046 if aclist is None:
1024 index = -1 1047 index = -1
1025 while index >= -len(acstack): 1048 while index >= -len(acstack):
1026 if acstack[index][1] < indent: 1049 if acstack[index][1] < indent:
1027 actype = \ 1050 actype = (
1028 m.group("AccessControlType") or \ 1051 m.group("AccessControlType") or
1029 m.group("AccessControlType2").split('_')[0] 1052 m.group("AccessControlType2").split('_')[0]
1053 )
1030 acstack[index][0] = actype.lower() 1054 acstack[index][0] = actype.lower()
1031 break 1055 break
1032 else: 1056 else:
1033 index -= 1 1057 index -= 1
1034 else: 1058 else:
1035 index = -1 1059 index = -1
1036 while index >= -len(classstack): 1060 while index >= -len(classstack):
1037 if classstack[index][0] is not None and \ 1061 if (
1038 not isinstance(classstack[index][0], Function) and \ 1062 classstack[index][0] is not None and
1039 not classstack[index][1] >= indent: 1063 not isinstance(classstack[index][0], Function) and
1064 not classstack[index][1] >= indent
1065 ):
1040 parent = classstack[index][0] 1066 parent = classstack[index][0]
1041 actype = \ 1067 actype = (
1042 m.group("AccessControlType") or \ 1068 m.group("AccessControlType") or
1043 m.group("AccessControlType2").split('_')[0] 1069 m.group("AccessControlType2").split('_')[0]
1070 )
1044 actype = actype.lower() 1071 actype = actype.lower()
1045 for name in aclist.split(","): 1072 for name in aclist.split(","):
1046 # get rid of leading ':' 1073 # get rid of leading ':'
1047 name = name.strip()[1:] 1074 name = name.strip()[1:]
1048 acmeth = parent.getMethod(name) 1075 acmeth = parent.getMethod(name)
1061 elif m.start("Attribute") >= 0: 1088 elif m.start("Attribute") >= 0:
1062 lineno = lineno + src.count('\n', last_lineno_pos, start) 1089 lineno = lineno + src.count('\n', last_lineno_pos, start)
1063 last_lineno_pos = start 1090 last_lineno_pos = start
1064 index = -1 1091 index = -1
1065 while index >= -len(classstack): 1092 while index >= -len(classstack):
1066 if classstack[index][0] is not None and \ 1093 if (
1067 not isinstance(classstack[index][0], Function) and \ 1094 classstack[index][0] is not None and
1068 not classstack[index][1] >= indent: 1095 not isinstance(classstack[index][0], Function) and
1096 not classstack[index][1] >= indent
1097 ):
1069 attrName = m.group("AttributeName") 1098 attrName = m.group("AttributeName")
1070 attr = Attribute( 1099 attr = Attribute(
1071 self.name, attrName, self.file, lineno) 1100 self.name, attrName, self.file, lineno)
1072 if attrName.startswith("@@") or attrName[0].isupper(): 1101 if attrName.startswith("@@") or attrName[0].isupper():
1073 classstack[index][0].addGlobal(attrName, attr) 1102 classstack[index][0].addGlobal(attrName, attr)
1089 elif m.start("Attr") >= 0: 1118 elif m.start("Attr") >= 0:
1090 lineno = lineno + src.count('\n', last_lineno_pos, start) 1119 lineno = lineno + src.count('\n', last_lineno_pos, start)
1091 last_lineno_pos = start 1120 last_lineno_pos = start
1092 index = -1 1121 index = -1
1093 while index >= -len(classstack): 1122 while index >= -len(classstack):
1094 if classstack[index][0] is not None and \ 1123 if (
1095 not isinstance(classstack[index][0], Function) and \ 1124 classstack[index][0] is not None and
1096 not classstack[index][1] >= indent: 1125 not isinstance(classstack[index][0], Function) and
1126 not classstack[index][1] >= indent
1127 ):
1097 parent = classstack[index][0] 1128 parent = classstack[index][0]
1098 if m.group("AttrType") is None: 1129 if m.group("AttrType") is None:
1099 nv = m.group("AttrList").split(",") 1130 nv = m.group("AttrList").split(",")
1100 if not nv: 1131 if not nv:
1101 break 1132 break
1102 # get rid of leading ':' 1133 # get rid of leading ':'
1103 name = nv[0].strip()[1:] 1134 name = nv[0].strip()[1:]
1104 attr = parent.getAttribute("@" + name) or \ 1135 attr = (
1105 parent.getAttribute("@@" + name) or \ 1136 parent.getAttribute("@" + name) or
1137 parent.getAttribute("@@" + name) or
1106 Attribute( 1138 Attribute(
1107 self.name, "@" + name, self.file, lineno) 1139 self.name, "@" + name, self.file, lineno)
1140 )
1108 if len(nv) == 1 or nv[1].strip() == "false": 1141 if len(nv) == 1 or nv[1].strip() == "false":
1109 attr.setProtected() 1142 attr.setProtected()
1110 elif nv[1].strip() == "true": 1143 elif nv[1].strip() == "true":
1111 attr.setPublic() 1144 attr.setPublic()
1112 parent.addAttribute(attr.name, attr) 1145 parent.addAttribute(attr.name, attr)
1113 else: 1146 else:
1114 access = m.group("AttrType") 1147 access = m.group("AttrType")
1115 for name in m.group("AttrList").split(","): 1148 for name in m.group("AttrList").split(","):
1116 # get rid of leading ':' 1149 # get rid of leading ':'
1117 name = name.strip()[1:] 1150 name = name.strip()[1:]
1118 attr = parent.getAttribute("@" + name) or \ 1151 attr = (
1119 parent.getAttribute("@@" + name) or \ 1152 parent.getAttribute("@" + name) or
1153 parent.getAttribute("@@" + name) or
1120 Attribute( 1154 Attribute(
1121 self.name, "@" + name, self.file, 1155 self.name, "@" + name, self.file,
1122 lineno) 1156 lineno)
1157 )
1123 if access == "_accessor": 1158 if access == "_accessor":
1124 attr.setPublic() 1159 attr.setPublic()
1125 elif access == "_reader" or \ 1160 elif (
1126 access == "_writer": 1161 access == "_reader" or
1162 access == "_writer"
1163 ):
1127 if attr.isPrivate(): 1164 if attr.isPrivate():
1128 attr.setProtected() 1165 attr.setProtected()
1129 elif attr.isProtected(): 1166 elif attr.isProtected():
1130 attr.setPublic() 1167 attr.setPublic()
1131 parent.addAttribute(attr.name, attr) 1168 parent.addAttribute(attr.name, attr)
1506 1543
1507 if os.path.exists(module): 1544 if os.path.exists(module):
1508 path = [os.path.dirname(module)] 1545 path = [os.path.dirname(module)]
1509 if module.lower().endswith(".py"): 1546 if module.lower().endswith(".py"):
1510 module = module[:-3] 1547 module = module[:-3]
1511 if os.path.exists(os.path.join(path[0], "__init__.py")) or \ 1548 if (
1512 os.path.exists(os.path.join(path[0], "__init__.rb")) or \ 1549 os.path.exists(os.path.join(path[0], "__init__.py")) or
1513 inpackage: 1550 os.path.exists(os.path.join(path[0], "__init__.rb")) or
1551 inpackage
1552 ):
1514 if basename: 1553 if basename:
1515 module = module.replace(basename, "") 1554 module = module.replace(basename, "")
1516 if os.path.isabs(module): 1555 if os.path.isabs(module):
1517 modname = os.path.splitdrive(module)[1][len(os.sep):] 1556 modname = os.path.splitdrive(module)[1][len(os.sep):]
1518 else: 1557 else:
1646 if basename: 1685 if basename:
1647 module = module.replace(basename, "") 1686 module = module.replace(basename, "")
1648 modname = module.replace(os.sep, '.') 1687 modname = module.replace(os.sep, '.')
1649 else: 1688 else:
1650 modname = os.path.basename(module) 1689 modname = os.path.basename(module)
1651 if modname.lower().endswith(".ptl") or \ 1690 if (
1652 modname.lower().endswith(".pyw"): 1691 modname.lower().endswith(".ptl") or
1692 modname.lower().endswith(".pyw")
1693 ):
1653 modname = modname[:-4] 1694 modname = modname[:-4]
1654 elif modname.lower().endswith(".rb"): 1695 elif modname.lower().endswith(".rb"):
1655 modname = modname[:-3] 1696 modname = modname[:-3]
1656 module = os.path.basename(module) 1697 module = os.path.basename(module)
1657 1698

eric ide

mercurial