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

Function parsePratt

src/parser/syntaxes/expressions.ts:710–762  ·  view source on GitHub ↗
(s: ITokenStream, minBp: number)

Source from the content-addressed store, hash-verified

708type OpInfo = PrefixInfo | InfixInfo | PostfixInfo;
709
710function parsePratt(s: ITokenStream, minBp: number): Ast.Expression {
711 // pratt parsing
712 // https://matklad.github.io/2020/04/13/simple-but-powerful-pratt-parsing.html
713
714 let left: Ast.Expression;
715
716 const tokenKind = s.getTokenKind();
717 const prefix = operators.find((x): x is PrefixInfo => x.opKind === 'prefix' && x.kind === tokenKind);
718 if (prefix != null) {
719 left = parsePrefix(s, prefix.bp);
720 } else {
721 left = parseAtom(s, false);
722 }
723
724 while (true) {
725 // 改行のエスケープ
726 if (s.is(TokenKind.BackSlash)) {
727 s.next();
728 s.expect(TokenKind.NewLine);
729 s.next();
730 }
731
732 const tokenKind = s.getTokenKind();
733
734 const postfix = operators.find((x): x is PostfixInfo => x.opKind === 'postfix' && x.kind === tokenKind);
735 if (postfix != null) {
736 if (postfix.bp < minBp) {
737 break;
738 }
739
740 if ([TokenKind.OpenBracket, TokenKind.OpenParen].includes(tokenKind) && s.getToken().hasLeftSpacing) {
741 // 前にスペースがある場合は後置演算子として処理しない
742 } else {
743 left = parsePostfix(s, left);
744 continue;
745 }
746 }
747
748 const infix = operators.find((x): x is InfixInfo => x.opKind === 'infix' && x.kind === tokenKind);
749 if (infix != null) {
750 if (infix.lbp < minBp) {
751 break;
752 }
753
754 left = parseInfix(s, left, infix.rbp);
755 continue;
756 }
757
758 break;
759 }
760
761 return left;
762}
763
764//#endregion Pratt parsing

Callers 3

parseExprFunction · 0.85
parsePrefixFunction · 0.85
parseInfixFunction · 0.85

Calls 9

parsePrefixFunction · 0.85
parseAtomFunction · 0.85
parsePostfixFunction · 0.85
parseInfixFunction · 0.85
getTokenKindMethod · 0.65
isMethod · 0.65
nextMethod · 0.65
expectMethod · 0.65
getTokenMethod · 0.65

Tested by

no test coverage detected