(self, parser: Parser)
| 87 | """Specialization of the ScriptMacro class for Relax.""" |
| 88 | |
| 89 | def parse_macro(self, parser: Parser) -> Expr: |
| 90 | macro_def = self.get_macro_def() |
| 91 | ret_value = None |
| 92 | |
| 93 | with R.SeqExpr() as seq: |
| 94 | for idx, stmt in enumerate(macro_def.body): |
| 95 | # Normally, a "return" statement is only allowed in a R.function. We don't |
| 96 | # want to parse the macro's body as if it was a body of a function, because |
| 97 | # the latter imposes some constraints that we want to avoid. |
| 98 | # At the same time, we want to use "return" to indicate the value of the |
| 99 | # macro (since in Relax everything is an expression), so add special handling |
| 100 | # of "return". |
| 101 | if isinstance(stmt, doc.Return): |
| 102 | ret_value = parser.eval_expr(stmt.value) |
| 103 | if idx + 1 != len(macro_def.body): |
| 104 | parser.report_error(macro_def, "'return' should be the last statement") |
| 105 | break |
| 106 | parser.visit(stmt) |
| 107 | |
| 108 | if ret_value is None: |
| 109 | parser.report_error(macro_def, "Macros must end with a return statement") |
| 110 | |
| 111 | return SeqExpr(seq.binding_blocks, ret_value) |
| 112 | |
| 113 | |
| 114 | def macro(*args, hygienic: bool = True) -> _Callable: |
nothing calls this directly
no test coverage detected