break38_check.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. # Copyright (c) 2020, 2022 Rocky Bernstein
  2. # This program is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. def break_invalid(
  15. self, lhs: str, n: int, rule, ast, tokens: list, first: int, last: int
  16. ) -> bool:
  17. if rule[1] != ("POP_EXCEPT", "JUMP_FORWARD"):
  18. return False
  19. # Look for a JUMP_LOOP instruction either after
  20. # the end of this rule or before the place where
  21. # we JUMP_FORWARD to
  22. if last + 1 < n and tokens[last + 1] == "JUMP_LOOP":
  23. return False
  24. # FIXME: put jump_loop classification in a subroutine. Preferably in xdis.
  25. jump_target_prev = self.insts[self.offset2inst_index[tokens[first + 1].attr] - 1]
  26. is_jump_loop = (
  27. jump_target_prev.is_jump() and jump_target_prev.arg < jump_target_prev.offset
  28. )
  29. return not is_jump_loop