From 42990231fee502552b769b9af4c04ac0dcaeb195 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sat, 25 Mar 2017 19:13:42 -0400 Subject: Update Pratt parser --- .../pratt/blocks/TriggeredParseBlock.java | 61 ++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/blocks/TriggeredParseBlock.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/blocks/TriggeredParseBlock.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/blocks/TriggeredParseBlock.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/blocks/TriggeredParseBlock.java new file mode 100644 index 0000000..fbfc61b --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/blocks/TriggeredParseBlock.java @@ -0,0 +1,61 @@ +package bjc.utils.parserutils.pratt.blocks; + +import java.util.function.UnaryOperator; + +import bjc.utils.data.ITree; +import bjc.utils.parserutils.ParserException; +import bjc.utils.parserutils.pratt.ParseBlock; +import bjc.utils.parserutils.pratt.ParserContext; +import bjc.utils.parserutils.pratt.Token; + +/** + * A parse block that can adjust the state before handling its context. + * + * @author bjculkin + * + * @param + * The key type of the tokens. + * @param + * The value type of the tokens. + * @param + * The state type of the parser. + */ +public class TriggeredParseBlock implements ParseBlock { + private UnaryOperator onEnter; + private UnaryOperator onExit; + + private ParseBlock source; + + /** + * Create a new triggered parse block. + * + * @param onEnter + * The action to fire before parsing the block. + * + * @param onExit + * The action to fire after parsing the block. + * + * @param source + * The block to use for parsing. + */ + public TriggeredParseBlock(UnaryOperator onEnter, UnaryOperator onExit, ParseBlock source) { + super(); + this.onEnter = onEnter; + this.onExit = onExit; + this.source = source; + } + + @Override + public ITree> parse(ParserContext ctx) throws ParserException { + C newState = onEnter.apply(ctx.state); + + ParserContext newCtx = new ParserContext<>(ctx.tokens, ctx.parse, newState); + + ITree> res = source.parse(newCtx); + + ctx.state = onExit.apply(newState); + + return res; + } + +} -- cgit v1.2.3