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