ifStmtBranch method
Implementation
IfStmt ifStmtBranch(Token token) {
final lexeme = token.lexeme;
bool isElseBranch = true;
MathExpr? condition;
if (token.type != TokenType.kElse) {
isElseBranch = false;
condition = math();
consume(TokenType.kThen, 'Expected "then" keyword before body.');
}
const ifTailList = [TokenType.kEnd, TokenType.kElseIf, TokenType.kElse];
final body = bodyStmt(terminals: ifTailList);
// Sanity check. We expect to consume one of the items
// in the terminal list.
IfStmt? next;
final p = peek();
if (!ifTailList.contains(p.type)) {
throw '${p.pos} Expected end of "$lexeme" block to match with "elseif", "else", or "end".';
} else {
final nextToken = advance();
if (nextToken.type != TokenType.kEnd) {
// Add elseif and else chains to this node.
next = ifStmtBranch(nextToken);
}
}
if (isElseBranch) {
return IfStmt.terminalElse(token, body: body);
}
return IfStmt(token, expr: condition!, body: body, nextIfStmt: next);
}