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 --- .../java/bjc/pratt/blocks/ChainParseBlock.java | 85 ------------------ .../java/bjc/pratt/blocks/GrammarParseBlock.java | 91 ------------------- .../src/main/java/bjc/pratt/blocks/ParseBlock.java | 37 -------- .../main/java/bjc/pratt/blocks/ParseBlocks.java | 97 -------------------- .../java/bjc/pratt/blocks/RepeatingParseBlock.java | 100 --------------------- .../java/bjc/pratt/blocks/SimpleParseBlock.java | 97 -------------------- .../java/bjc/pratt/blocks/TriggeredParseBlock.java | 62 ------------- 7 files changed, 569 deletions(-) delete mode 100644 JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java delete mode 100644 JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java delete mode 100644 JPratt/src/main/java/bjc/pratt/blocks/ParseBlock.java delete mode 100644 JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java delete mode 100644 JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java delete mode 100644 JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java delete mode 100644 JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java (limited to 'JPratt/src/main/java/bjc/pratt/blocks') diff --git a/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java deleted file mode 100644 index 2717e42..0000000 --- a/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java +++ /dev/null @@ -1,85 +0,0 @@ -package bjc.pratt.blocks; - -import java.util.Set; - -import bjc.pratt.ParserContext; -import bjc.pratt.commands.CommandResult; -import bjc.pratt.commands.CommandResult.Status; -import bjc.pratt.tokens.Token; -import bjc.data.Tree; -import bjc.data.SimpleTree; -import bjc.utils.parserutils.ParserException; - -/** - * A {@link ParseBlock} for a series of parse blocks, linked by a set of tokens. - * - * Roughly analogous to Perl 6s list associative operators. - * - * @author bjculkin - * - * @param - * The token key type. - * - * @param - * The token value type. - * - * @param - * The parser state type. - * - */ -public class ChainParseBlock implements ParseBlock { - private ParseBlock iner; - - private Set indicators; - - private Token trm; - - /** - * Create a new chain parser block. - * - * @param inner - * The block for the chains interior. - * - * @param chainIndicators - * The set of markers that indicate continuing the chain - * - * @param term - * The node in the AST for the expression. - */ - public ChainParseBlock(ParseBlock inner, Set chainIndicators, Token term) { - iner = inner; - indicators = chainIndicators; - trm = term; - } - - @Override - public CommandResult parse(ParserContext ctx) throws ParserException { - CommandResult resOuter = iner.parse(ctx); - if (resOuter.status != Status.SUCCESS) return resOuter; - - Tree> expression = resOuter.success(); - Token currentToken = ctx.tokens.current(); - if(indicators.contains(currentToken.getKey())) { - Tree> res = new SimpleTree<>(trm); - res.addChild(expression); - - while(indicators.contains(currentToken.getKey())) { - res.addChild(new SimpleTree<>(currentToken)); - ctx.tokens.next(); - - CommandResult resInner = iner.parse(ctx); - if (resInner.status != Status.SUCCESS) return resInner; - - Tree> innerExpression = resInner.success(); - res.addChild(innerExpression); - - currentToken = ctx.tokens.current(); - } - - return CommandResult.success(res); - } - - return resOuter; - } - -} diff --git a/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java deleted file mode 100644 index 446b80b..0000000 --- a/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java +++ /dev/null @@ -1,91 +0,0 @@ -package bjc.pratt.blocks; - -import java.util.function.Function; - -import bjc.data.Tree; -import bjc.pratt.ParserContext; -import bjc.pratt.PrattParser; -import bjc.pratt.commands.CommandResult; -import bjc.pratt.tokens.Token; -import bjc.pratt.tokens.TokenStream; -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 diff --git a/JPratt/src/main/java/bjc/pratt/blocks/ParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlock.java deleted file mode 100644 index aa2fa2e..0000000 --- a/JPratt/src/main/java/bjc/pratt/blocks/ParseBlock.java +++ /dev/null @@ -1,37 +0,0 @@ -package bjc.pratt.blocks; - -import bjc.pratt.ParserContext; -import bjc.pratt.commands.CommandResult; -import bjc.utils.parserutils.ParserException; - -/** - * Represents a embedded block in an expression. - * - * @author bjculkin - * - * @param - * The key type of the token. - * - * @param - * The value type of the token. - * - * @param - * The state type of the parser. - */ -@FunctionalInterface -public interface ParseBlock { - - /** - * Parse the block this represents. - * - * @param ctx - * The context for parsing. - * - * @return A AST for this block. - * - * @throws ParserException - * If something goes wrong during parsing, or the block fails - * validation. - */ - CommandResult parse(ParserContext ctx) throws ParserException; -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java deleted file mode 100644 index 96b9737..0000000 --- a/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java +++ /dev/null @@ -1,97 +0,0 @@ -package bjc.pratt.blocks; - -import java.util.function.Predicate; -import java.util.function.UnaryOperator; - -import bjc.pratt.tokens.Token; -import bjc.data.Tree; - -/** - * Utility class for creating common implementations of {@link ParseBlock} - * - * @author bjculkin - * - */ -public class ParseBlocks { - /* - * Grammar parse blocks are complex enough to not get a builder method. - */ - - /** - * Create a new repeating parse block. - * - * @param The key type for the tokens. - * @param The value type for the tokens. - * @param The context type for the tokens. - * - * @param inner - * The parse block to repeat. - * - * @param delim - * The token type that separates repetitions. - * - * @param term - * The token type that terminates repetitions. - * - * @param mark - * The token to use as the node in the AST. - * - * @param action - * The action to perform on the state after every repetition. - * - * @return A configured repeating parse block. - */ - public static ParseBlock repeating(final ParseBlock inner, final K delim, - final K term, final Token mark, final UnaryOperator action) { - return new RepeatingParseBlock<>(inner, delim, term, mark, action); - } - - /** - * Create a new triggered parse block. - * - * @param The key type for the tokens. - * @param The value type for the tokens. - * @param The context type for the tokens. - * - * @param source - * The block to trigger around. - * - * @param onEnter - * The action to perform upon the state before entering the - * block. - * - * @param onExit - * The action to perform upon the state after exiting the block. - * - * @return A configured trigger parse block. - */ - public static ParseBlock trigger(final ParseBlock source, - final UnaryOperator onEnter, final UnaryOperator onExit) { - return new TriggeredParseBlock<>(onEnter, onExit, source); - } - - /** - * Create a new simple parse block. - * - * @param The key type for the tokens. - * @param The value type for the tokens. - * @param The context type for the tokens. - * - * @param precedence - * The precedence of the expression inside the block. - * - * @param terminator - * The key type of the token expected after this block, or null - * if none is expected. - * - * @param validator - * The predicate to use to validate parsed expressions, or null - * if none is used. - * - * @return A configured simple parse block. - */ - public static ParseBlock simple(final int precedence, final K terminator, - final Predicate>> validator) { - return new SimpleParseBlock<>(precedence, validator, terminator); - } -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java deleted file mode 100644 index 722e395..0000000 --- a/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java +++ /dev/null @@ -1,100 +0,0 @@ -package bjc.pratt.blocks; - -import java.util.function.UnaryOperator; - -import bjc.pratt.ParserContext; -import bjc.pratt.commands.CommandResult; -import bjc.pratt.commands.CommandResult.Status; -import bjc.pratt.tokens.Token; -import bjc.data.Tree; -import bjc.data.SimpleTree; -import bjc.utils.parserutils.ParserException; - -/** - * A parse block that can parse a sequnce of zero or more occurances of another - * block. - * - * @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 RepeatingParseBlock implements ParseBlock { - private final ParseBlock innerBlock; - - private final K delim; - private final K term; - - private final UnaryOperator onDelim; - - private final Token mark; - - /** - * Create a new repeating block. - * - * @param inner - * The inner block for elements. - * - * @param delimiter - * The token that delimits elements in the sequence. - * - * @param terminator - * The token that terminates the sequence. - * - * @param marker - * The token to use as the node in the AST. - * - * @param action - * The action to apply to the state after every delimiter. - */ - public RepeatingParseBlock(final ParseBlock inner, final K delimiter, final K terminator, - final Token marker, final UnaryOperator action) { - super(); - - if(inner == null) - throw new NullPointerException("Inner block must not be null"); - else if(delimiter == null) - throw new NullPointerException("Delimiter must not be null"); - else if(terminator == null) throw new NullPointerException("Terminator must not be null"); - - innerBlock = inner; - - delim = delimiter; - term = terminator; - - mark = marker; - - onDelim = action; - } - - @Override - public CommandResult parse(final ParserContext ctx) throws ParserException { - final Tree> ret = new SimpleTree<>(mark); - - Token tok = ctx.tokens.current(); - - while(!tok.getKey().equals(term)) { - final CommandResult resKid = innerBlock.parse(ctx); - if (resKid.status != Status.SUCCESS) return resKid; - Tree> kid = resKid.success(); - ret.addChild(kid); - - tok = ctx.tokens.current(); - - ctx.tokens.expect(delim, term); - - if(onDelim != null) { - ctx.state = onDelim.apply(ctx.state); - } - } - - return CommandResult.success(ret); - } - -} diff --git a/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java deleted file mode 100644 index b674815..0000000 --- a/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java +++ /dev/null @@ -1,97 +0,0 @@ -package bjc.pratt.blocks; - -import java.util.function.Predicate; - -import bjc.pratt.ParserContext; -import bjc.pratt.commands.CommandResult; -import bjc.pratt.commands.CommandResult.Status; -import bjc.pratt.tokens.Token; -import bjc.data.Tree; -import bjc.utils.parserutils.ParserException; - -/** - * Simple implementation of {@link ParseBlock} - * - * @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 SimpleParseBlock implements ParseBlock { - private final int pow; - - private final K term; - - private final Predicate>> validatr; - - /** - * Create a new block. - * - * @param precedence - * The precedence of this block. - * @param validator - * The predicate to apply to blocks. - * @param terminator - * The token type that terminates the block. If this is null, - * don't check for a terminator. - */ - public SimpleParseBlock(final int precedence, final Predicate>> validator, - final K terminator) { - if(precedence < 0) throw new IllegalArgumentException("Precedence must be non-negative"); - - pow = precedence; - term = terminator; - validatr = validator; - } - - @Override - public CommandResult parse(final ParserContext ctx) throws ParserException { - final CommandResult resBlock = ctx.parse.parseExpression(pow, ctx.tokens, ctx.state, false); - if (resBlock.status != Status.SUCCESS) return resBlock; - - Tree> res = resBlock.success(); - if(term != null) { - ctx.tokens.expect(term); - } - - if(validatr == null || validatr.test(res)) return CommandResult.success(res); - - // TODO: Figure out the right way to handle error context w/ CommandResult - throw new ParserException("Block failed validation"); - } - - @Override - public int hashCode() { - final int prime = 31; - - int result = 1; - - result = prime * result + pow; - result = prime * result + (term == null ? 0 : term.hashCode()); - - return result; - } - - @Override - public boolean equals(final Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(!(obj instanceof SimpleParseBlock)) return false; - - final SimpleParseBlock other = (SimpleParseBlock) obj; - - if(pow != other.pow) return false; - - if(term == null) { - if(other.term != null) return false; - } else if(!term.equals(other.term)) return false; - - return true; - } -} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java deleted file mode 100644 index d404eea..0000000 --- a/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java +++ /dev/null @@ -1,62 +0,0 @@ -package bjc.pratt.blocks; - -import java.util.function.UnaryOperator; - -import bjc.pratt.ParserContext; -import bjc.pratt.commands.CommandResult; -import bjc.pratt.commands.CommandResult.Status; -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 CommandResult parse(final ParserContext ctx) throws ParserException { - final C newState = onEntr.apply(ctx.state); - - final ParserContext newCtx = new ParserContext<>(ctx.tokens, ctx.parse, newState); - - final CommandResult res = sourc.parse(newCtx); - - if (res.status != Status.SUCCESS) return res; - - ctx.state = onExt.apply(newState); - - return res; - } - -} -- cgit v1.2.3