diff options
| author | bjculkin <bjculkin@mix.wvu.edu> | 2017-04-05 15:35:13 -0400 |
|---|---|---|
| committer | bjculkin <bjculkin@mix.wvu.edu> | 2017-04-05 15:35:13 -0400 |
| commit | c82452e59b1547392c3e89d08d9173cc6dc79e23 (patch) | |
| tree | 1927051f7cab7a64eef3f53a78b77780db6cf281 /JPratt/src/main/java/bjc/utils/parserutils/pratt/blocks | |
| parent | b61c66d5e0c18faee68eb91881d5dfe760818856 (diff) | |
Reorganize
Diffstat (limited to 'JPratt/src/main/java/bjc/utils/parserutils/pratt/blocks')
4 files changed, 0 insertions, 342 deletions
diff --git a/JPratt/src/main/java/bjc/utils/parserutils/pratt/blocks/ParseBlocks.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/blocks/ParseBlocks.java deleted file mode 100644 index 9df8355..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/blocks/ParseBlocks.java +++ /dev/null @@ -1,84 +0,0 @@ -package bjc.utils.parserutils.pratt.blocks; - -import java.util.function.Predicate; -import java.util.function.UnaryOperator; - -import bjc.utils.data.ITree; -import bjc.utils.parserutils.pratt.ParseBlock; -import bjc.utils.parserutils.pratt.Token; - -/** - * Utility class for creating common implementations of {@link ParseBlock} - * - * @author bjculkin - * - */ -public class ParseBlocks { - /** - * Create a new repeating parse block. - * - * @param inner - * The parse block to repeat. - * - * @param delim - * The token type that seperates repetitions. - * - * @param term - * The token type that terminates repititions. - * - * @param mark - * The token to use as the node in the AST. - * - * @param action - * The action to perform on the state after every - * repitition. - * - * @return A configured repeating parse block. - */ - public static <K, V, C> ParseBlock<K, V, C> repeating(ParseBlock<K, V, C> inner, K delim, K term, - Token<K, V> mark, UnaryOperator<C> action) { - return new RepeatingParseBlock<>(inner, delim, term, mark, action); - } - - /** - * Create a new triggered parse block. - * - * @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 <K, V, C> ParseBlock<K, V, C> trigger(ParseBlock<K, V, C> source, UnaryOperator<C> onEnter, - UnaryOperator<C> onExit) { - return new TriggeredParseBlock<>(onEnter, onExit, source); - } - - /** - * Create a new simple parse block. - * - * @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 <K, V, C> ParseBlock<K, V, C> simple(int precedence, K terminator, - Predicate<ITree<Token<K, V>>> validator) { - return new SimpleParseBlock<>(precedence, terminator, validator); - } -}
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/utils/parserutils/pratt/blocks/RepeatingParseBlock.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/blocks/RepeatingParseBlock.java deleted file mode 100644 index 08a4bae..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/blocks/RepeatingParseBlock.java +++ /dev/null @@ -1,96 +0,0 @@ -package bjc.utils.parserutils.pratt.blocks; - -import java.util.function.UnaryOperator; - -import bjc.utils.data.ITree; -import bjc.utils.data.Tree; -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 parse a sequnce of zero or more occurances of another - * block. - * - * @author bjculkin - * - * @param <K> - * The key type of the tokens. - * - * @param <V> - * The value type of the tokens. - * - * @param <C> - * The state type of the parser. - */ -public class RepeatingParseBlock<K, V, C> implements ParseBlock<K, V, C> { - private ParseBlock<K, V, C> innerBlock; - - private K delim; - private K term; - - private UnaryOperator<C> onDelim; - - private Token<K, V> 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(ParseBlock<K, V, C> inner, K delimiter, K terminator, Token<K, V> marker, - UnaryOperator<C> 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 ITree<Token<K, V>> parse(ParserContext<K, V, C> ctx) throws ParserException { - ITree<Token<K, V>> ret = new Tree<>(mark); - - Token<K, V> tok = ctx.tokens.current(); - - while (!tok.getKey().equals(term)) { - ITree<Token<K, V>> kid = innerBlock.parse(ctx); - ret.addChild(kid); - - tok = ctx.tokens.current(); - - ctx.tokens.expect(delim, term); - - if (onDelim != null) ctx.state = onDelim.apply(ctx.state); - } - - return ret; - } - -} diff --git a/JPratt/src/main/java/bjc/utils/parserutils/pratt/blocks/SimpleParseBlock.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/blocks/SimpleParseBlock.java deleted file mode 100644 index c2e9e54..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/blocks/SimpleParseBlock.java +++ /dev/null @@ -1,101 +0,0 @@ -package bjc.utils.parserutils.pratt.blocks; - -import java.util.function.Predicate; - -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; - -/** - * Simple implementation of {@link ParseBlock} - * - * @author bjculkin - * - * @param <K> - * The key type of the tokens. - * - * @param <V> - * The value type of the tokens. - * - * @param <C> - * The state type of the parser. - */ -public class SimpleParseBlock<K, V, C> implements ParseBlock<K, V, C> { - private int pow; - - private K term; - - private Predicate<ITree<Token<K, V>>> validatr; - - /** - * Create a new block. - * - * @param precedence - * The precedence of this block. - * - * @param terminator - * The token type that terminates the block. If this is - * null, don't check for a terminator. - * - * @param validator - * The predicate to apply to blocks. - */ - public SimpleParseBlock(int precedence, K terminator, Predicate<ITree<Token<K, V>>> validator) { - if (precedence < 0) throw new IllegalArgumentException("Precedence must be non-negative"); - - pow = precedence; - term = terminator; - validatr = validator; - } - - @Override - public ITree<Token<K, V>> parse(ParserContext<K, V, C> ctx) throws ParserException { - ITree<Token<K, V>> res = ctx.parse.parseExpression(pow, ctx.tokens, ctx.state, false); - - if (term != null) { - ctx.tokens.expect(term); - } - - if (validatr == null || validatr.test(res)) { - return res; - } - - 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(Object obj) { - if (this == obj) return true; - if (obj == null) return false; - if (!(obj instanceof SimpleParseBlock)) return false; - - 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; - } - - @Override - public String toString() { - return String.format("ParseBlock [pow=%s, term='%s']", pow, term); - } -}
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/utils/parserutils/pratt/blocks/TriggeredParseBlock.java b/JPratt/src/main/java/bjc/utils/parserutils/pratt/blocks/TriggeredParseBlock.java deleted file mode 100644 index fbfc61b..0000000 --- a/JPratt/src/main/java/bjc/utils/parserutils/pratt/blocks/TriggeredParseBlock.java +++ /dev/null @@ -1,61 +0,0 @@ -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 <K> - * The key type of the tokens. - * @param <V> - * The value type of the tokens. - * @param <C> - * The state type of the parser. - */ -public class TriggeredParseBlock<K, V, C> implements ParseBlock<K, V, C> { - private UnaryOperator<C> onEnter; - private UnaryOperator<C> onExit; - - private ParseBlock<K, V, C> 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<C> onEnter, UnaryOperator<C> onExit, ParseBlock<K, V, C> source) { - super(); - this.onEnter = onEnter; - this.onExit = onExit; - this.source = source; - } - - @Override - public ITree<Token<K, V>> parse(ParserContext<K, V, C> ctx) throws ParserException { - C newState = onEnter.apply(ctx.state); - - ParserContext<K, V, C> newCtx = new ParserContext<>(ctx.tokens, ctx.parse, newState); - - ITree<Token<K, V>> res = source.parse(newCtx); - - ctx.state = onExit.apply(newState); - - return res; - } - -} |
