package bjc.pratt.blocks; import java.util.function.UnaryOperator; import bjc.pratt.ParserContext; import bjc.pratt.tokens.Token; import bjc.data.Tree; import bjc.utils.parserutils.ParserException; /** * 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 final UnaryOperator onEntr; private final UnaryOperator onExt; private final ParseBlock sourc; /** * 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(final UnaryOperator onEnter, final UnaryOperator onExit, final ParseBlock source) { onEntr = onEnter; onExt = onExit; sourc = source; } @Override public Tree> parse(final ParserContext ctx) throws ParserException { final C newState = onEntr.apply(ctx.state); final ParserContext newCtx = new ParserContext<>(ctx.tokens, ctx.parse, newState); final Tree> res = sourc.parse(newCtx); ctx.state = onExt.apply(newState); return res; } }