Utilities/ModuleParser.py

branch
Py2 comp.
changeset 3060
5883ce99ee12
parent 3058
0a02c433f52d
parent 3039
8dd0165d805d
child 3145
a9de05d4a22f
equal deleted inserted replaced
3058:0a02c433f52d 3060:5883ce99ee12
188 | (?P<ConditionalDefine> 188 | (?P<ConditionalDefine>
189 ^ 189 ^
190 (?P<ConditionalDefineIndent> [ \t]* ) 190 (?P<ConditionalDefineIndent> [ \t]* )
191 (?: (?: if | elif ) [ \t]+ [^:]* | else [ \t]* ) : (?= \s* def) 191 (?: (?: if | elif ) [ \t]+ [^:]* | else [ \t]* ) : (?= \s* def)
192 ) 192 )
193 """, re.VERBOSE | re.DOTALL | re.MULTILINE).search 193 """, re.VERBOSE | re.DOTALL | re.MULTILINE).search # __IGNORE_WARNING__
194 194
195 _rb_getnext = re.compile( 195 _rb_getnext = re.compile(
196 r""" 196 r"""
197 (?P<Docstring> 197 (?P<Docstring>
198 =begin [ \t]+ edoc (?P<DocstringContents> .*? ) =end 198 =begin [ \t]+ edoc (?P<DocstringContents> .*? ) =end
325 end [ \t]* $ 325 end [ \t]* $
326 | 326 |
327 end \b [^_] 327 end \b [^_]
328 ) 328 )
329 ) 329 )
330 """, re.VERBOSE | re.DOTALL | re.MULTILINE).search 330 """, re.VERBOSE | re.DOTALL | re.MULTILINE).search # __IGNORE_WARNING__
331 331
332 _hashsub = re.compile(r"""^([ \t]*)#[ \t]?""", re.MULTILINE).sub 332 _hashsub = re.compile(r"""^([ \t]*)#[ \t]?""", re.MULTILINE).sub
333 333
334 _commentsub = re.compile(r"""#[^\n]*\n|#[^\n]*$""").sub 334 _commentsub = re.compile(r"""#[^\n]*\n|#[^\n]*$""").sub
335 335
567 lambda x, y: x + y, deltastack) 567 lambda x, y: x + y, deltastack)
568 deltaindentcalculated = 1 568 deltaindentcalculated = 1
569 thisindent -= deltaindent 569 thisindent -= deltaindent
570 else: 570 else:
571 while conditionalsstack and \ 571 while conditionalsstack and \
572 conditionalsstack[-1] >= thisindent: 572 conditionalsstack[-1] >= thisindent:
573 del conditionalsstack[-1] 573 del conditionalsstack[-1]
574 if deltastack: 574 if deltastack:
575 del deltastack[-1] 575 del deltastack[-1]
576 deltaindentcalculated = 0 576 deltaindentcalculated = 0
577 # close all classes indented at least as much 577 # close all classes indented at least as much
578 while classstack and \ 578 while classstack and \
579 classstack[-1][1] >= thisindent: 579 classstack[-1][1] >= thisindent:
580 if classstack[-1][0] is not None and \ 580 if classstack[-1][0] is not None and \
581 isinstance(classstack[-1][0], (Class, Function)): 581 isinstance(classstack[-1][0], (Class, Function)):
582 # record the end line of this class or function 582 # record the end line of this class or function
583 classstack[-1][0].setEndLine(lineno - 1) 583 classstack[-1][0].setEndLine(lineno - 1)
584 del classstack[-1] 584 del classstack[-1]
632 else: 632 else:
633 if self.file.lower().endswith('.ptl'): 633 if self.file.lower().endswith('.ptl'):
634 contents = "" 634 contents = ""
635 else: 635 else:
636 contents = 1 and m.group("DocstringContents1") \ 636 contents = 1 and m.group("DocstringContents1") \
637 or m.group("DocstringContents2") 637 or m.group("DocstringContents2")
638 if cur_obj: 638 if cur_obj:
639 cur_obj.addDescription(contents) 639 cur_obj.addDescription(contents)
640 640
641 elif m.start("String") >= 0: 641 elif m.start("String") >= 0:
642 if modulelevel and \ 642 if modulelevel and \
643 (src[start - len('\r\n'):start] == '\r\n' or \ 643 (src[start - len('\r\n'):start] == '\r\n' or
644 src[start - len('\n'):start] == '\n' or \ 644 src[start - len('\n'):start] == '\n' or
645 src[start - len('\r'):start] == '\r'): 645 src[start - len('\r'):start] == '\r'):
646 contents = m.group("StringContents3") 646 contents = m.group("StringContents3")
647 if contents is not None: 647 if contents is not None:
648 contents = _hashsub(r"\1", contents) 648 contents = _hashsub(r"\1", contents)
649 else: 649 else:
650 if self.file.lower().endswith('.ptl'): 650 if self.file.lower().endswith('.ptl'):
651 contents = "" 651 contents = ""
652 else: 652 else:
653 contents = 1 and m.group("StringContents1") \ 653 contents = 1 and m.group("StringContents1") \
654 or m.group("StringContents2") 654 or m.group("StringContents2")
655 if cur_obj: 655 if cur_obj:
656 cur_obj.addDescription(contents) 656 cur_obj.addDescription(contents)
657 657
658 elif m.start("Class") >= 0: 658 elif m.start("Class") >= 0:
659 # we found a class definition 659 # we found a class definition
660 thisindent = _indent(m.group("ClassIndent")) 660 thisindent = _indent(m.group("ClassIndent"))
661 lineno = lineno + src.count('\n', last_lineno_pos, start) 661 lineno = lineno + src.count('\n', last_lineno_pos, start)
662 last_lineno_pos = start 662 last_lineno_pos = start
663 # close all classes indented at least as much 663 # close all classes indented at least as much
664 while classstack and \ 664 while classstack and \
665 classstack[-1][1] >= thisindent: 665 classstack[-1][1] >= thisindent:
666 if classstack[-1][0] is not None and \ 666 if classstack[-1][0] is not None and \
667 isinstance(classstack[-1][0], (Class, Function)): 667 isinstance(classstack[-1][0], (Class, Function)):
668 # record the end line of this class or function 668 # record the end line of this class or function
669 classstack[-1][0].setEndLine(lineno - 1) 669 classstack[-1][0].setEndLine(lineno - 1)
670 del classstack[-1] 670 del classstack[-1]
754 variable_name, attr) 754 variable_name, attr)
755 break 755 break
756 756
757 elif m.start("Import") >= 0: 757 elif m.start("Import") >= 0:
758 # import module 758 # import module
759 names = [n.strip() for n in 759 names = [n.strip() for n in
760 "".join(m.group("ImportList").splitlines()) 760 "".join(m.group("ImportList").splitlines())
761 .replace("\\", "").split(',')] 761 .replace("\\", "").split(',')]
762 for name in names: 762 for name in names:
763 if not name in self.imports: 763 if not name in self.imports:
764 self.imports.append(name) 764 self.imports.append(name)
765 765
766 elif m.start("ImportFrom") >= 0: 766 elif m.start("ImportFrom") >= 0:
767 # from module import stuff 767 # from module import stuff
768 mod = m.group("ImportFromPath") 768 mod = m.group("ImportFromPath")
769 names = [n.strip() for n in 769 names = [n.strip() for n in
770 "".join(m.group("ImportFromList").splitlines()) 770 "".join(m.group("ImportFromList").splitlines())
771 .replace("\\", "").split(',')] 771 .replace("\\", "").split(',')]
772 if mod not in self.from_imports: 772 if mod not in self.from_imports:
773 self.from_imports[mod] = [] 773 self.from_imports[mod] = []
774 self.from_imports[mod].extend(names) 774 self.from_imports[mod].extend(names)
775 775
776 elif m.start("ConditionalDefine") >= 0: 776 elif m.start("ConditionalDefine") >= 0:
777 # a conditional function/method definition 777 # a conditional function/method definition
778 thisindent = _indent(m.group("ConditionalDefineIndent")) 778 thisindent = _indent(m.group("ConditionalDefineIndent"))
779 while conditionalsstack and \ 779 while conditionalsstack and \
780 conditionalsstack[-1] >= thisindent: 780 conditionalsstack[-1] >= thisindent:
781 del conditionalsstack[-1] 781 del conditionalsstack[-1]
782 if deltastack: 782 if deltastack:
783 del deltastack[-1] 783 del deltastack[-1]
784 conditionalsstack.append(thisindent) 784 conditionalsstack.append(thisindent)
785 deltaindentcalculated = 0 785 deltaindentcalculated = 0
812 if m.start("Method") >= 0: 812 if m.start("Method") >= 0:
813 # found a method definition or function 813 # found a method definition or function
814 thisindent = indent 814 thisindent = indent
815 indent += 1 815 indent += 1
816 meth_name = m.group("MethodName") or \ 816 meth_name = m.group("MethodName") or \
817 m.group("MethodName2") or \ 817 m.group("MethodName2") or \
818 m.group("MethodName3") 818 m.group("MethodName3")
819 meth_sig = m.group("MethodSignature") 819 meth_sig = m.group("MethodSignature")
820 meth_sig = meth_sig and meth_sig.replace('\\\n', '') or '' 820 meth_sig = meth_sig and meth_sig.replace('\\\n', '') or ''
821 lineno = lineno + src.count('\n', last_lineno_pos, start) 821 lineno = lineno + src.count('\n', last_lineno_pos, start)
822 last_lineno_pos = start 822 last_lineno_pos = start
823 if meth_name.startswith('self.'): 823 if meth_name.startswith('self.'):
824 meth_name = meth_name[5:] 824 meth_name = meth_name[5:]
825 elif meth_name.startswith('self::'): 825 elif meth_name.startswith('self::'):
826 meth_name = meth_name[6:] 826 meth_name = meth_name[6:]
827 # close all classes/modules indented at least as much 827 # close all classes/modules indented at least as much
828 while classstack and \ 828 while classstack and \
829 classstack[-1][1] >= thisindent: 829 classstack[-1][1] >= thisindent:
830 if classstack[-1][0] is not None and \ 830 if classstack[-1][0] is not None and \
831 isinstance(classstack[-1][0], 831 isinstance(classstack[-1][0],
832 (Class, Function, RbModule)): 832 (Class, Function, RbModule)):
833 # record the end line of this class, function or module 833 # record the end line of this class, function or module
834 classstack[-1][0].setEndLine(lineno - 1) 834 classstack[-1][0].setEndLine(lineno - 1)
835 del classstack[-1] 835 del classstack[-1]
836 while acstack and \ 836 while acstack and \
837 acstack[-1][1] >= thisindent: 837 acstack[-1][1] >= thisindent:
838 del acstack[-1] 838 del acstack[-1]
839 if classstack: 839 if classstack:
840 csi = -1 840 csi = -1
841 while csi >= -len(classstack): 841 while csi >= -len(classstack):
842 # nested defs are added to the class 842 # nested defs are added to the class
902 indent += 1 902 indent += 1
903 lineno = lineno + src.count('\n', last_lineno_pos, start) 903 lineno = lineno + src.count('\n', last_lineno_pos, start)
904 last_lineno_pos = start 904 last_lineno_pos = start
905 # close all classes/modules indented at least as much 905 # close all classes/modules indented at least as much
906 while classstack and \ 906 while classstack and \
907 classstack[-1][1] >= thisindent: 907 classstack[-1][1] >= thisindent:
908 if classstack[-1][0] is not None and \ 908 if classstack[-1][0] is not None and \
909 isinstance(classstack[-1][0], 909 isinstance(classstack[-1][0],
910 (Class, Function, RbModule)): 910 (Class, Function, RbModule)):
911 # record the end line of this class, function or module 911 # record the end line of this class, function or module
912 classstack[-1][0].setEndLine(lineno - 1) 912 classstack[-1][0].setEndLine(lineno - 1)
926 else: 926 else:
927 parent_obj = self 927 parent_obj = self
928 if class_name in parent_obj.classes: 928 if class_name in parent_obj.classes:
929 cur_class = parent_obj.classes[class_name] 929 cur_class = parent_obj.classes[class_name]
930 elif classstack and \ 930 elif classstack and \
931 isinstance(classstack[-1][0], Class) and \ 931 isinstance(classstack[-1][0], Class) and \
932 class_name == "self": 932 class_name == "self":
933 cur_class = classstack[-1][0] 933 cur_class = classstack[-1][0]
934 else: 934 else:
935 parent_obj.addClass(class_name, cur_class) 935 parent_obj.addClass(class_name, cur_class)
936 if not classstack: 936 if not classstack:
937 if lastGlobalEntry: 937 if lastGlobalEntry:
938 lastGlobalEntry.setEndLine(lineno - 1) 938 lastGlobalEntry.setEndLine(lineno - 1)
939 lastGlobalEntry = cur_class 939 lastGlobalEntry = cur_class
940 cur_obj = cur_class 940 cur_obj = cur_class
941 classstack.append((cur_class, thisindent)) 941 classstack.append((cur_class, thisindent))
942 while acstack and \ 942 while acstack and \
943 acstack[-1][1] >= thisindent: 943 acstack[-1][1] >= thisindent:
944 del acstack[-1] 944 del acstack[-1]
945 acstack.append(["public", thisindent]) 945 acstack.append(["public", thisindent])
946 # default access control is 'public' 946 # default access control is 'public'
947 947
948 elif m.start("Module") >= 0: 948 elif m.start("Module") >= 0:
951 indent += 1 951 indent += 1
952 lineno = lineno + src.count('\n', last_lineno_pos, start) 952 lineno = lineno + src.count('\n', last_lineno_pos, start)
953 last_lineno_pos = start 953 last_lineno_pos = start
954 # close all classes/modules indented at least as much 954 # close all classes/modules indented at least as much
955 while classstack and \ 955 while classstack and \
956 classstack[-1][1] >= thisindent: 956 classstack[-1][1] >= thisindent:
957 if classstack[-1][0] is not None and \ 957 if classstack[-1][0] is not None and \
958 isinstance(classstack[-1][0], 958 isinstance(classstack[-1][0],
959 (Class, Function, RbModule)): 959 (Class, Function, RbModule)):
960 # record the end line of this class, function or module 960 # record the end line of this class, function or module
961 classstack[-1][0].setEndLine(lineno - 1) 961 classstack[-1][0].setEndLine(lineno - 1)
974 lastGlobalEntry.setEndLine(lineno - 1) 974 lastGlobalEntry.setEndLine(lineno - 1)
975 lastGlobalEntry = cur_class 975 lastGlobalEntry = cur_class
976 cur_obj = cur_class 976 cur_obj = cur_class
977 classstack.append((cur_class, thisindent)) 977 classstack.append((cur_class, thisindent))
978 while acstack and \ 978 while acstack and \
979 acstack[-1][1] >= thisindent: 979 acstack[-1][1] >= thisindent:
980 del acstack[-1] 980 del acstack[-1]
981 acstack.append(["public", thisindent]) 981 acstack.append(["public", thisindent])
982 # default access control is 'public' 982 # default access control is 'public'
983 983
984 elif m.start("AccessControl") >= 0: 984 elif m.start("AccessControl") >= 0:
1063 if not nv: 1063 if not nv:
1064 break 1064 break
1065 # get rid of leading ':' 1065 # get rid of leading ':'
1066 name = nv[0].strip()[1:] 1066 name = nv[0].strip()[1:]
1067 attr = parent.getAttribute("@" + name) or \ 1067 attr = parent.getAttribute("@" + name) or \
1068 parent.getAttribute("@@" + name) or \ 1068 parent.getAttribute("@@" + name) or \
1069 Attribute( 1069 Attribute(
1070 self.name, "@" + name, self.file, lineno) 1070 self.name, "@" + name, self.file, lineno)
1071 if len(nv) == 1 or nv[1].strip() == "false": 1071 if len(nv) == 1 or nv[1].strip() == "false":
1072 attr.setProtected() 1072 attr.setProtected()
1073 elif nv[1].strip() == "true": 1073 elif nv[1].strip() == "true":
1074 attr.setPublic() 1074 attr.setPublic()
1077 access = m.group("AttrType") 1077 access = m.group("AttrType")
1078 for name in m.group("AttrList").split(","): 1078 for name in m.group("AttrList").split(","):
1079 # get rid of leading ':' 1079 # get rid of leading ':'
1080 name = name.strip()[1:] 1080 name = name.strip()[1:]
1081 attr = parent.getAttribute("@" + name) or \ 1081 attr = parent.getAttribute("@" + name) or \
1082 parent.getAttribute("@@" + name) or \ 1082 parent.getAttribute("@@" + name) or \
1083 Attribute( 1083 Attribute(
1084 self.name, "@" + name, self.file, 1084 self.name, "@" + name, self.file,
1085 lineno) 1085 lineno)
1086 if access == "_accessor": 1086 if access == "_accessor":
1087 attr.setPublic() 1087 attr.setPublic()
1088 elif access == "_reader" or \ 1088 elif access == "_reader" or \

eric ide

mercurial