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

Function parseMatch

src/parser/syntaxes/expressions.ts:446–488  ·  view source on GitHub ↗

* ```abnf * Match = "match" Expr "{" [(MatchCase *(SEP MatchCase) [SEP DefaultCase] [SEP]) / DefaultCase [SEP]] "}" * ```

(s: ITokenStream)

Source from the content-addressed store, hash-verified

444 * ```
445*/
446function parseMatch(s: ITokenStream): Ast.Match {
447 const startPos = s.getPos();
448
449 s.expect(TokenKind.MatchKeyword);
450 s.next();
451 const about = parseExpr(s, false);
452
453 s.expect(TokenKind.OpenBrace);
454 s.next();
455
456 if (s.is(TokenKind.NewLine)) {
457 s.next();
458 }
459
460 const qs: Ast.Match['qs'] = [];
461 let x: Ast.Match['default'];
462 if (s.is(TokenKind.CaseKeyword)) {
463 qs.push(parseMatchCase(s));
464 let sep = parseOptionalSeparator(s);
465 while (s.is(TokenKind.CaseKeyword)) {
466 if (!sep) {
467 throw new AiScriptSyntaxError('separator expected', s.getPos());
468 }
469 qs.push(parseMatchCase(s));
470 sep = parseOptionalSeparator(s);
471 }
472 if (s.is(TokenKind.DefaultKeyword)) {
473 if (!sep) {
474 throw new AiScriptSyntaxError('separator expected', s.getPos());
475 }
476 x = parseDefaultCase(s);
477 parseOptionalSeparator(s);
478 }
479 } else if (s.is(TokenKind.DefaultKeyword)) {
480 x = parseDefaultCase(s);
481 parseOptionalSeparator(s);
482 }
483
484 s.expect(TokenKind.CloseBrace);
485 s.next();
486
487 return NODE('match', { about, qs, default: x }, startPos, s.getPos());
488}
489
490/**
491 * ```abnf

Callers 1

parseAtomFunction · 0.85

Calls 9

parseExprFunction · 0.85
parseMatchCaseFunction · 0.85
parseOptionalSeparatorFunction · 0.85
parseDefaultCaseFunction · 0.85
NODEFunction · 0.85
getPosMethod · 0.65
expectMethod · 0.65
nextMethod · 0.65
isMethod · 0.65

Tested by

no test coverage detected