while1elsestmt.py 809 B

12345678910111213141516171819202122232425
  1. # Copyright (c) 2020-2021 Rocky Bernstein
  2. def while1elsestmt(
  3. self, lhs: str, n: int, rule, ast, tokens: list, first: int, last: int
  4. ) -> bool:
  5. if last == n:
  6. # Adjust for fuzziness in parsing
  7. last -= 1
  8. if tokens[last] == "COME_FROM_LOOP":
  9. last -= 1
  10. elif tokens[last - 1] == "COME_FROM_LOOP":
  11. last -= 2
  12. if tokens[last] in ("JUMP_LOOP", "CONTINUE"):
  13. # These indicate inside a loop, but token[last]
  14. # should not be in a loop.
  15. # FIXME: Not quite right: refine by using target
  16. return True
  17. # if SETUP_LOOP target spans the else part, then this is
  18. # not while1else. Also do for whileTrue?
  19. last += 1
  20. # 3.8+ Doesn't have SETUP_LOOP
  21. return self.version < (3, 8) and tokens[first].attr > tokens[last].off2int()