| 1065 | self.body_col0 = None |
| 1066 | |
| 1067 | def tokeneater(self, type, token, srowcol, erowcol, line): |
| 1068 | if not self.started and not self.indecorator: |
| 1069 | if type in (tokenize.INDENT, tokenize.COMMENT, tokenize.NL): |
| 1070 | pass |
| 1071 | elif token == "async": |
| 1072 | pass |
| 1073 | # skip any decorators |
| 1074 | elif token == "@": |
| 1075 | self.indecorator = True |
| 1076 | else: |
| 1077 | # For "def" and "class" scan to the end of the block. |
| 1078 | # For "lambda" and generator expression scan to |
| 1079 | # the end of the logical line. |
| 1080 | self.singleline = token not in ("def", "class") |
| 1081 | self.started = True |
| 1082 | self.passline = True # skip to the end of the line |
| 1083 | elif type == tokenize.NEWLINE: |
| 1084 | self.passline = False # stop skipping when a NEWLINE is seen |
| 1085 | self.last = srowcol[0] |
| 1086 | if self.singleline: |
| 1087 | raise EndOfBlock |
| 1088 | # hitting a NEWLINE when in a decorator without args |
| 1089 | # ends the decorator |
| 1090 | if self.indecorator: |
| 1091 | self.indecorator = False |
| 1092 | elif self.passline: |
| 1093 | pass |
| 1094 | elif type == tokenize.INDENT: |
| 1095 | if self.body_col0 is None and self.started: |
| 1096 | self.body_col0 = erowcol[1] |
| 1097 | self.indent = self.indent + 1 |
| 1098 | self.passline = True |
| 1099 | elif type == tokenize.DEDENT: |
| 1100 | self.indent = self.indent - 1 |
| 1101 | # the end of matching indent/dedent pairs end a block |
| 1102 | # (note that this only works for "def"/"class" blocks, |
| 1103 | # not e.g. for "if: else:" or "try: finally:" blocks) |
| 1104 | if self.indent <= 0: |
| 1105 | raise EndOfBlock |
| 1106 | elif type == tokenize.COMMENT: |
| 1107 | if self.body_col0 is not None and srowcol[1] >= self.body_col0: |
| 1108 | # Include comments if indented at least as much as the block |
| 1109 | self.last = srowcol[0] |
| 1110 | elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL): |
| 1111 | # any other token on the same indentation level end the previous |
| 1112 | # block as well, except the pseudo-tokens COMMENT and NL. |
| 1113 | raise EndOfBlock |
| 1114 | |
| 1115 | def getblock(lines): |
| 1116 | """Extract the block of code at the top of the given list of lines.""" |