def _yield_items(stmts):
match_head = None
match_rules = None
for stmt in stmts:
if match_head is not None:
if (isinstance(stmt, ast.With) and
not BlockWithBinding.is_with_binding(stmt)):
if stmt.optional_vars is not None:
raise _errors.TyError("Invalid rule form.", stmt)
match_rules.append(stmt)
continue
else:
if len(match_rules) == 0:
raise _errors.TyError(
"No match rules.", match_head)
yield BlockMatchExpr(match_head, match_rules)
match_head = match_rules = None
# falls through below
if match_head is None:
if isinstance(stmt, ast.Expr):
if BlockMatchExpr.is_match_head(stmt):
match_head = stmt
match_rules = []
else:
yield BlockExprExpr(stmt)
elif isinstance(stmt, ast.With):
if (BlockWithBinding.is_with_binding(stmt)):
yield BlockWithBinding(stmt)
else:
raise _errors.TyError("Invalid with form.", stmt)
elif isinstance(stmt, ast.Assign):
yield BlockLet(stmt)
elif isinstance(stmt, ast.If):
yield BlockIfExpr(stmt)
elif isinstance(stmt, ast.Pass):
yield BlockPassExpr(stmt)
elif isinstance(stmt, ast.FunctionDef):
if BlockFunctionDefExpr.check_valid_function_def(stmt):
yield BlockFunctionDefExpr(stmt)
else:
raise _errors.TyError("Statement form not supported.", stmt)
# finish making match expression at the end of the block if necessary
if match_head is not None:
if len(match_rules) == 0:
raise _errors.TyError(
"No match rules.", match_head)
yield BlockMatchExpr(match_head, match_rules)
评论列表
文章目录