Appearance
| 1 | namespace Syntax.Process is | |
| 2 | use Logging | |
| 3 | ||
| 4 | // Reports what a comprehension's element and conditions may not | |
| 5 | // contain. Its loops are internal, so a break or continue would leave | |
| 6 | // them half way through filling the result; yield and await would | |
| 7 | // suspend with the result under construction; and a try needs an | |
| 8 | // empty evaluation stack, which the enclosing expression may not | |
| 9 | // leave it. A function literal inside the comprehension is a body of | |
| 10 | // its own and is not scanned. | |
| 11 | class COMPREHENSION_RESTRICTIONS: Visitor is | |
| 12 | _logger: Logger | |
| 13 | _loop_depth: int | |
| 14 | _inside_loop: bool | |
| 15 | ||
| 16 | // What the comprehension is called in a diagnostic: a list | |
| 17 | // comprehension, or a comprehension for the lazy form. | |
| 18 | _what: string | |
| 19 | ||
| 20 | init(logger: Logger) is | |
| 21 | super.init() | |
| 22 | ||
| 23 | _logger = logger | |
| 24 | _what = "list comprehension" | |
| 25 | si | |
| 26 | ||
| 27 | // `inside_loop` says whether a loop encloses the comprehension. | |
| 28 | // Without one, a break or continue is already reported as outside | |
| 29 | // any loop. | |
| 30 | check(expression: Trees.Expressions.Expression, inside_loop: bool) is | |
| 31 | _loop_depth = 0 | |
| 32 | _inside_loop = inside_loop | |
| 33 | _what = expression.description | |
| 34 | expression.walk(self) | |
| 35 | si | |
| 36 | ||
| 37 | pre(function: Trees.Expressions.FUNCTION) -> bool => true | |
| 38 | ||
| 39 | pre(`for: Trees.Statements.FOR) -> bool is | |
| 40 | _loop_depth = _loop_depth + 1 | |
| 41 | return false | |
| 42 | si | |
| 43 | ||
| 44 | visit(`for: Trees.Statements.FOR) is | |
| 45 | _loop_depth = _loop_depth - 1 | |
| 46 | si | |
| 47 | ||
| 48 | pre(`do: Trees.Statements.DO) -> bool is | |
| 49 | _loop_depth = _loop_depth + 1 | |
| 50 | return false | |
| 51 | si | |
| 52 | ||
| 53 | visit(`do: Trees.Statements.DO) is | |
| 54 | _loop_depth = _loop_depth - 1 | |
| 55 | si | |
| 56 | ||
| 57 | visit(`break: Trees.Statements.BREAK) is | |
| 58 | if _loop_depth == 0 /\ _inside_loop then | |
| 59 | _logger.error(`break.location, "break cannot leave a {_what}") | |
| 60 | fi | |
| 61 | si | |
| 62 | ||
| 63 | visit(`continue: Trees.Statements.CONTINUE) is | |
| 64 | if _loop_depth == 0 /\ _inside_loop then | |
| 65 | _logger.error(`continue.location, "continue cannot leave a {_what}") | |
| 66 | fi | |
| 67 | si | |
| 68 | ||
| 69 | visit(`yield: Trees.Statements.YIELD) is | |
| 70 | _logger.error(`yield.location, "cannot yield inside a {_what}") | |
| 71 | si | |
| 72 | ||
| 73 | visit(`yield: Trees.Statements.YIELD_ALL) is | |
| 74 | _logger.error(`yield.location, "cannot yield inside a {_what}") | |
| 75 | si | |
| 76 | ||
| 77 | visit(`await: Trees.Expressions.AWAIT) is | |
| 78 | _logger.error(`await.location, "cannot await inside a {_what}") | |
| 79 | si | |
| 80 | ||
| 81 | visit(`try: Trees.Statements.TRY) is | |
| 82 | _logger.error(`try.location, "try is not allowed inside a {_what}") | |
| 83 | si | |
| 84 | si | |
| 85 | si |