MCPcopy Create free account
hub / github.com/aiscript-dev/aiscript / parseIf

Function parseIf

src/parser/syntaxes/expressions.ts:378–408  ·  view source on GitHub ↗

* ```abnf * If = "if" Expr BlockOrStatement *("elif" Expr BlockOrStatement) ["else" BlockOrStatement] * ```

(s: ITokenStream)

Source from the content-addressed store, hash-verified

376 * ```
377*/
378function parseIf(s: ITokenStream): Ast.If {
379 const startPos = s.getPos();
380
381 s.expect(TokenKind.IfKeyword);
382 s.next();
383 const cond = parseExpr(s, false);
384 const then = parseBlockOrStatement(s);
385
386 if (s.is(TokenKind.NewLine) && [TokenKind.ElifKeyword, TokenKind.ElseKeyword].includes(s.lookahead(1).kind)) {
387 s.next();
388 }
389
390 const elseif: Ast.If['elseif'] = [];
391 while (s.is(TokenKind.ElifKeyword)) {
392 s.next();
393 const elifCond = parseExpr(s, false);
394 const elifThen = parseBlockOrStatement(s);
395 if (s.is(TokenKind.NewLine) && [TokenKind.ElifKeyword, TokenKind.ElseKeyword].includes(s.lookahead(1).kind)) {
396 s.next();
397 }
398 elseif.push({ cond: elifCond, then: elifThen });
399 }
400
401 let _else = undefined;
402 if (s.is(TokenKind.ElseKeyword)) {
403 s.next();
404 _else = parseBlockOrStatement(s);
405 }
406
407 return NODE('if', { cond, then, elseif, else: _else }, startPos, s.getPos());
408}
409
410/**
411 * ```abnf

Callers 1

parseAtomFunction · 0.85

Calls 8

parseExprFunction · 0.85
parseBlockOrStatementFunction · 0.85
NODEFunction · 0.85
getPosMethod · 0.65
expectMethod · 0.65
nextMethod · 0.65
isMethod · 0.65
lookaheadMethod · 0.65

Tested by

no test coverage detected