Utilities/ModuleParser.py

branch
Py2 comp.
changeset 2791
a9577f248f04
parent 2525
8b507a9a2d40
parent 2768
eab35f6e709f
child 3056
9986ec0e559a
equal deleted inserted replaced
2790:6686a3326df8 2791:a9577f248f04
501 relevant information. 501 relevant information.
502 502
503 @param src the source text to be scanned (string) 503 @param src the source text to be scanned (string)
504 """ 504 """
505 lineno, last_lineno_pos = 1, 0 505 lineno, last_lineno_pos = 1, 0
506 lastGlobalEntry = None
506 classstack = [] # stack of (class, indent) pairs 507 classstack = [] # stack of (class, indent) pairs
507 conditionalsstack = [] # stack of indents of conditional defines 508 conditionalsstack = [] # stack of indents of conditional defines
508 deltastack = [] 509 deltastack = []
509 deltaindent = 0 510 deltaindent = 0
510 deltaindentcalculated = 0 511 deltaindentcalculated = 0
564 del deltastack[-1] 565 del deltastack[-1]
565 deltaindentcalculated = 0 566 deltaindentcalculated = 0
566 # close all classes indented at least as much 567 # close all classes indented at least as much
567 while classstack and \ 568 while classstack and \
568 classstack[-1][1] >= thisindent: 569 classstack[-1][1] >= thisindent:
569 if classstack[-1][1] == thisindent and \ 570 if classstack[-1][0] is not None and \
570 classstack[-1][0] is not None and \ 571 isinstance(classstack[-1][0], (Class, Function)):
571 isinstance(classstack[-1][0], Class): 572 # record the end line of this class or function
572 # we got a class at the same indentation level;
573 # record the end line of this class
574 classstack[-1][0].setEndLine(lineno - 1) 573 classstack[-1][0].setEndLine(lineno - 1)
575 del classstack[-1] 574 del classstack[-1]
576 if classstack: 575 if classstack:
577 csi = -1 576 csi = -1
578 while csi >= -len(classstack): 577 while csi >= -len(classstack):
599 # it's a module function 598 # it's a module function
600 f = Function(self.name, meth_name, self.file, lineno, 599 f = Function(self.name, meth_name, self.file, lineno,
601 meth_sig, meth_pyqtSig, modifierType=modifier) 600 meth_sig, meth_pyqtSig, modifierType=modifier)
602 self.__py_setVisibility(f) 601 self.__py_setVisibility(f)
603 self.addFunction(meth_name, f) 602 self.addFunction(meth_name, f)
603 if not classstack:
604 if lastGlobalEntry:
605 lastGlobalEntry.setEndLine(lineno - 1)
606 lastGlobalEntry = f
607 if cur_obj and isinstance(cur_obj, Function):
608 cur_obj.setEndLine(lineno - 1)
604 cur_obj = f 609 cur_obj = f
605 classstack.append((None, thisindent)) # Marker for nested fns 610 classstack.append((None, thisindent)) # Marker for nested fns
606 611
607 # reset the modifier settings 612 # reset the modifier settings
608 modifierType = Function.General 613 modifierType = Function.General
644 lineno = lineno + src.count('\n', last_lineno_pos, start) 649 lineno = lineno + src.count('\n', last_lineno_pos, start)
645 last_lineno_pos = start 650 last_lineno_pos = start
646 # close all classes indented at least as much 651 # close all classes indented at least as much
647 while classstack and \ 652 while classstack and \
648 classstack[-1][1] >= thisindent: 653 classstack[-1][1] >= thisindent:
649 if classstack[-1][1] == thisindent and \ 654 if classstack[-1][0] is not None and \
650 classstack[-1][0] is not None and \ 655 isinstance(classstack[-1][0], (Class, Function)):
651 isinstance(classstack[-1][0], Class): 656 # record the end line of this class or function
652 # we got a class at the same indentation level;
653 # record the end line of this class
654 classstack[-1][0].setEndLine(lineno - 1) 657 classstack[-1][0].setEndLine(lineno - 1)
655 del classstack[-1] 658 del classstack[-1]
656 class_name = m.group("ClassName") 659 class_name = m.group("ClassName")
657 inherit = m.group("ClassSupers") 660 inherit = m.group("ClassSupers")
658 if inherit: 661 if inherit:
684 self.file, lineno) 687 self.file, lineno)
685 self.__py_setVisibility(cur_class) 688 self.__py_setVisibility(cur_class)
686 cur_obj = cur_class 689 cur_obj = cur_class
687 # add nested classes to the module 690 # add nested classes to the module
688 self.addClass(class_name, cur_class) 691 self.addClass(class_name, cur_class)
692 if not classstack:
693 if lastGlobalEntry:
694 lastGlobalEntry.setEndLine(lineno - 1)
695 lastGlobalEntry = cur_class
689 classstack.append((cur_class, thisindent)) 696 classstack.append((cur_class, thisindent))
690 697
691 elif m.start("Attribute") >= 0: 698 elif m.start("Attribute") >= 0:
692 lineno = lineno + src.count('\n', last_lineno_pos, start) 699 lineno = lineno + src.count('\n', last_lineno_pos, start)
693 last_lineno_pos = start 700 last_lineno_pos = start
712 # global variable 719 # global variable
713 attr = Attribute(self.name, variable_name, self.file, lineno, 720 attr = Attribute(self.name, variable_name, self.file, lineno,
714 isSignal=isSignal) 721 isSignal=isSignal)
715 self.__py_setVisibility(attr) 722 self.__py_setVisibility(attr)
716 self.addGlobal(variable_name, attr) 723 self.addGlobal(variable_name, attr)
724 if lastGlobalEntry:
725 lastGlobalEntry.setEndLine(lineno - 1)
726 lastGlobalEntry = None
717 else: 727 else:
718 index = -1 728 index = -1
719 while index >= -len(classstack): 729 while index >= -len(classstack):
720 if classstack[index][1] >= thisindent: 730 if classstack[index][1] >= thisindent:
721 index -= 1 731 index -= 1
772 classstack = [] # stack of (class, indent) pairs 782 classstack = [] # stack of (class, indent) pairs
773 acstack = [] # stack of (access control, indent) pairs 783 acstack = [] # stack of (access control, indent) pairs
774 indent = 0 784 indent = 0
775 i = 0 785 i = 0
776 cur_obj = self 786 cur_obj = self
787 lastGlobalEntry = None
777 while True: 788 while True:
778 m = self._getnext(src, i) 789 m = self._getnext(src, i)
779 if not m: 790 if not m:
780 break 791 break
781 start, i = m.span() 792 start, i = m.span()
796 elif meth_name.startswith('self::'): 807 elif meth_name.startswith('self::'):
797 meth_name = meth_name[6:] 808 meth_name = meth_name[6:]
798 # close all classes/modules indented at least as much 809 # close all classes/modules indented at least as much
799 while classstack and \ 810 while classstack and \
800 classstack[-1][1] >= thisindent: 811 classstack[-1][1] >= thisindent:
812 if classstack[-1][0] is not None and \
813 isinstance(classstack[-1][0], (Class, Function, RbModule)):
814 # record the end line of this class, function or module
815 classstack[-1][0].setEndLine(lineno - 1)
801 del classstack[-1] 816 del classstack[-1]
802 while acstack and \ 817 while acstack and \
803 acstack[-1][1] >= thisindent: 818 acstack[-1][1] >= thisindent:
804 del acstack[-1] 819 del acstack[-1]
805 if classstack: 820 if classstack:
833 f.setPublic() 848 f.setPublic()
834 else: 849 else:
835 # it's a function 850 # it's a function
836 f = Function(self.name, meth_name, self.file, lineno, meth_sig) 851 f = Function(self.name, meth_name, self.file, lineno, meth_sig)
837 self.addFunction(meth_name, f) 852 self.addFunction(meth_name, f)
853 if not classstack:
854 if lastGlobalEntry:
855 lastGlobalEntry.setEndLine(lineno - 1)
856 lastGlobalEntry = f
857 if cur_obj and isinstance(cur_obj, Function):
858 cur_obj.setEndLine(lineno - 1)
838 cur_obj = f 859 cur_obj = f
839 classstack.append((None, thisindent)) # Marker for nested fns 860 classstack.append((None, thisindent)) # Marker for nested fns
840 861
841 elif m.start("Docstring") >= 0: 862 elif m.start("Docstring") >= 0:
842 contents = m.group("DocstringContents") 863 contents = m.group("DocstringContents")
856 877
857 elif m.start("Class") >= 0: 878 elif m.start("Class") >= 0:
858 # we found a class definition 879 # we found a class definition
859 thisindent = indent 880 thisindent = indent
860 indent += 1 881 indent += 1
882 lineno = lineno + src.count('\n', last_lineno_pos, start)
883 last_lineno_pos = start
861 # close all classes/modules indented at least as much 884 # close all classes/modules indented at least as much
862 while classstack and \ 885 while classstack and \
863 classstack[-1][1] >= thisindent: 886 classstack[-1][1] >= thisindent:
887 if classstack[-1][0] is not None and \
888 isinstance(classstack[-1][0], (Class, Function, RbModule)):
889 # record the end line of this class, function or module
890 classstack[-1][0].setEndLine(lineno - 1)
864 del classstack[-1] 891 del classstack[-1]
865 lineno = lineno + src.count('\n', last_lineno_pos, start)
866 last_lineno_pos = start
867 class_name = m.group("ClassName") or m.group("ClassName2") 892 class_name = m.group("ClassName") or m.group("ClassName2")
868 inherit = m.group("ClassSupers") 893 inherit = m.group("ClassSupers")
869 if inherit: 894 if inherit:
870 # the class inherits from other classes 895 # the class inherits from other classes
871 inherit = inherit[1:].strip() 896 inherit = inherit[1:].strip()
884 isinstance(classstack[-1][0], Class) and \ 909 isinstance(classstack[-1][0], Class) and \
885 class_name == "self": 910 class_name == "self":
886 cur_class = classstack[-1][0] 911 cur_class = classstack[-1][0]
887 else: 912 else:
888 parent_obj.addClass(class_name, cur_class) 913 parent_obj.addClass(class_name, cur_class)
914 if not classstack:
915 if lastGlobalEntry:
916 lastGlobalEntry.setEndLine(lineno - 1)
917 lastGlobalEntry = cur_class
889 cur_obj = cur_class 918 cur_obj = cur_class
890 classstack.append((cur_class, thisindent)) 919 classstack.append((cur_class, thisindent))
891 while acstack and \ 920 while acstack and \
892 acstack[-1][1] >= thisindent: 921 acstack[-1][1] >= thisindent:
893 del acstack[-1] 922 del acstack[-1]
894 acstack.append(["public", thisindent]) # default access control is 'public' 923 acstack.append(["public", thisindent])
924 # default access control is 'public'
895 925
896 elif m.start("Module") >= 0: 926 elif m.start("Module") >= 0:
897 # we found a module definition 927 # we found a module definition
898 thisindent = indent 928 thisindent = indent
899 indent += 1 929 indent += 1
930 lineno = lineno + src.count('\n', last_lineno_pos, start)
931 last_lineno_pos = start
900 # close all classes/modules indented at least as much 932 # close all classes/modules indented at least as much
901 while classstack and \ 933 while classstack and \
902 classstack[-1][1] >= thisindent: 934 classstack[-1][1] >= thisindent:
935 if classstack[-1][0] is not None and \
936 isinstance(classstack[-1][0], (Class, Function, RbModule)):
937 # record the end line of this class, function or module
938 classstack[-1][0].setEndLine(lineno - 1)
903 del classstack[-1] 939 del classstack[-1]
904 lineno = lineno + src.count('\n', last_lineno_pos, start)
905 last_lineno_pos = start
906 module_name = m.group("ModuleName") 940 module_name = m.group("ModuleName")
907 # remember this class 941 # remember this class
908 cur_class = RbModule(self.name, module_name, 942 cur_class = RbModule(self.name, module_name,
909 self.file, lineno) 943 self.file, lineno)
910 # add nested Ruby modules to the file 944 # add nested Ruby modules to the file
911 if module_name in self.modules: 945 if module_name in self.modules:
912 cur_class = self.modules[module_name] 946 cur_class = self.modules[module_name]
913 else: 947 else:
914 self.addModule(module_name, cur_class) 948 self.addModule(module_name, cur_class)
949 if not classstack:
950 if lastGlobalEntry:
951 lastGlobalEntry.setEndLine(lineno - 1)
952 lastGlobalEntry = cur_class
915 cur_obj = cur_class 953 cur_obj = cur_class
916 classstack.append((cur_class, thisindent)) 954 classstack.append((cur_class, thisindent))
917 while acstack and \ 955 while acstack and \
918 acstack[-1][1] >= thisindent: 956 acstack[-1][1] >= thisindent:
919 del acstack[-1] 957 del acstack[-1]
920 acstack.append(["public", thisindent]) # default access control is 'public' 958 acstack.append(["public", thisindent])
959 # default access control is 'public'
921 960
922 elif m.start("AccessControl") >= 0: 961 elif m.start("AccessControl") >= 0:
923 aclist = m.group("AccessControlList") 962 aclist = m.group("AccessControlList")
924 if aclist is None: 963 if aclist is None:
925 index = -1 964 index = -1
976 else: 1015 else:
977 attrName = m.group("AttributeName") 1016 attrName = m.group("AttributeName")
978 if attrName[0] != "@": 1017 if attrName[0] != "@":
979 attr = Attribute(self.name, attrName, self.file, lineno) 1018 attr = Attribute(self.name, attrName, self.file, lineno)
980 self.addGlobal(attrName, attr) 1019 self.addGlobal(attrName, attr)
1020 if lastGlobalEntry:
1021 lastGlobalEntry.setEndLine(lineno - 1)
1022 lastGlobalEntry = None
981 1023
982 elif m.start("Attr") >= 0: 1024 elif m.start("Attr") >= 0:
983 lineno = lineno + src.count('\n', last_lineno_pos, start) 1025 lineno = lineno + src.count('\n', last_lineno_pos, start)
984 last_lineno_pos = start 1026 last_lineno_pos = start
985 index = -1 1027 index = -1
1290 """ 1332 """
1291 self.module = module 1333 self.module = module
1292 self.name = name 1334 self.name = name
1293 self.file = file 1335 self.file = file
1294 self.lineno = lineno 1336 self.lineno = lineno
1337 self.endlineno = -1 # marker for "not set"
1295 signature = _commentsub('', signature) 1338 signature = _commentsub('', signature)
1296 self.parameters = [e.strip() for e in signature.split(',')] 1339 self.parameters = [e.strip() for e in signature.split(',')]
1297 self.description = "" 1340 self.description = ""
1298 self.pyqtSignature = pyqtSignature 1341 self.pyqtSignature = pyqtSignature
1299 self.modifier = modifierType 1342 self.modifier = modifierType
1304 Public method to store the functions docstring. 1347 Public method to store the functions docstring.
1305 1348
1306 @param description the docstring to be stored (string) 1349 @param description the docstring to be stored (string)
1307 """ 1350 """
1308 self.description = description 1351 self.description = description
1352
1353 def setEndLine(self, endLineNo):
1354 """
1355 Public method to record the number of the last line of a class.
1356
1357 @param endLineNo number of the last line (integer)
1358 """
1359 self.endlineno = endLineNo
1309 1360
1310 1361
1311 class Attribute(VisibilityBase): 1362 class Attribute(VisibilityBase):
1312 ''' 1363 '''
1313 Class to represent a Python function or method. 1364 Class to represent a Python function or method.

eric ide

mercurial