From 15a2b29e48f134bc93cfd0a3d8512001e9242f3d Mon Sep 17 00:00:00 2001 From: Benjamin Culkin Date: Mon, 3 Jun 2024 17:33:53 -0400 Subject: Rename package to new domain Rename the package to the new domain --- .../ashardalon/pratt/blocks/GrammarParseBlock.java | 92 ++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 JPratt/src/main/java/com/ashardalon/pratt/blocks/GrammarParseBlock.java (limited to 'JPratt/src/main/java/com/ashardalon/pratt/blocks/GrammarParseBlock.java') diff --git a/JPratt/src/main/java/com/ashardalon/pratt/blocks/GrammarParseBlock.java b/JPratt/src/main/java/com/ashardalon/pratt/blocks/GrammarParseBlock.java new file mode 100644 index 0000000..048e9f6 --- /dev/null +++ b/JPratt/src/main/java/com/ashardalon/pratt/blocks/GrammarParseBlock.java @@ -0,0 +1,92 @@ +package com.ashardalon.pratt.blocks; + +import java.util.function.Function; + +import com.ashardalon.pratt.ParserContext; +import com.ashardalon.pratt.PrattParser; +import com.ashardalon.pratt.commands.CommandResult; +import com.ashardalon.pratt.tokens.Token; +import com.ashardalon.pratt.tokens.TokenStream; + +import bjc.data.Tree; +import bjc.typeclasses.Isomorphism; +import bjc.data.Tree; +import bjc.functypes.*; +import bjc.utils.parserutils.ParserException; + +/** + * A {@link ParseBlock} that parses an expression from a 'inner' grammar. + * + * @author bjculkin + * + * @param The key type of the outer tokens. + * + * @param The value type of the outer tokens. + * + * @param The state type of the outer parser. + * + * @param The key type of the inner tokens. + * + * @param The value type of the inner tokens. + * + * @param The state type of the outer parser. + */ +public class GrammarParseBlock implements ParseBlock { + private final PrattParser innr; + + private final int prcedence; + private final boolean isStatemnt; + + private final Function, TokenStream> tkenTransform; + private final Isomorphism stteTransform; + private final Function>, Tree>> xpressionTransform; + + /** + * Create a new grammar parser block. + * + * @param inner The inner grammar to parse. + * @param precedence The precedence of the expression to parse. + * @param isStatement Is the expression being parsed in statement + * context? + * @param tokenTransform Function to transform to the new token type. + * @param stateTransform Function to toggle between state types. + * @param expressionTransform Function to transform back to the normal token + * type. + */ + public GrammarParseBlock(final PrattParser inner, final int precedence, final boolean isStatement, + final Function, TokenStream> tokenTransform, + final Isomorphism stateTransform, + final Function>, Tree>> expressionTransform) { + innr = inner; + prcedence = precedence; + isStatemnt = isStatement; + tkenTransform = tokenTransform; + stteTransform = stateTransform; + xpressionTransform = expressionTransform; + } + + @Override + public CommandResult parse(final ParserContext ctx) throws ParserException { + final C2 newState = stteTransform.to(ctx.state); + + final TokenStream newTokens = tkenTransform.apply(ctx.tokens); + + final CommandResult res = innr.parseExpression(prcedence, newTokens, newState, isStatemnt); + switch (res.status) { + case SUCCESS: + break; + case FAIL: + return CommandResult.fail(); + case BACKTRACK: + return CommandResult.backtrack(); + default: + throw new IllegalStateException("Unhandled status " + res.status); + } + + Tree> expression = res.success(); + + ctx.state = stteTransform.from(newState); + + return CommandResult.success(xpressionTransform.apply(expression)); + } +} \ No newline at end of file -- cgit v1.2.3