summaryrefslogtreecommitdiff
path: root/JPratt/src/main/java/bjc/pratt/blocks
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2017-04-10 16:49:54 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2017-04-10 16:49:54 -0400
commit56f07e9a3aaa873fe385d224f088f048dbafa8f7 (patch)
tree64fae78f95fd1c233689ecf3dda2e2b645bb8d33 /JPratt/src/main/java/bjc/pratt/blocks
parent251419e1f0ab8eb04d21287b708b06a552f4c58a (diff)
Cleanup
Diffstat (limited to 'JPratt/src/main/java/bjc/pratt/blocks')
-rw-r--r--JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java42
-rw-r--r--JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java40
-rw-r--r--JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java35
-rw-r--r--JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java25
4 files changed, 72 insertions, 70 deletions
diff --git a/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java
index e0dea48..d236a71 100644
--- a/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java
+++ b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java
@@ -9,76 +9,76 @@ import bjc.utils.data.ITree;
/**
* 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) {
+ public static <K, V, C> ParseBlock<K, V, C> repeating(final ParseBlock<K, V, C> inner, final K delim,
+ final K term, final Token<K, V> mark, final 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) {
+ public static <K, V, C> ParseBlock<K, V, C> trigger(final ParseBlock<K, V, C> source,
+ final UnaryOperator<C> onEnter, final 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) {
+ public static <K, V, C> ParseBlock<K, V, C> simple(final int precedence, final K terminator,
+ final 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/pratt/blocks/RepeatingParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java
index 1c82b36..7791ea9 100644
--- a/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java
+++ b/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java
@@ -12,49 +12,49 @@ import bjc.utils.parserutils.ParserException;
/**
* 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 final ParseBlock<K, V, C> innerBlock;
- private K delim;
- private K term;
+ private final K delim;
+ private final K term;
- private UnaryOperator<C> onDelim;
+ private final UnaryOperator<C> onDelim;
- private Token<K, V> mark;
+ private final 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) {
+ public RepeatingParseBlock(final ParseBlock<K, V, C> inner, final K delimiter, final K terminator,
+ final Token<K, V> marker, final UnaryOperator<C> action) {
super();
if (inner == null)
@@ -74,20 +74,22 @@ public class RepeatingParseBlock<K, V, C> implements ParseBlock<K, V, C> {
}
@Override
- public ITree<Token<K, V>> parse(ParserContext<K, V, C> ctx) throws ParserException {
- ITree<Token<K, V>> ret = new Tree<>(mark);
+ public ITree<Token<K, V>> parse(final ParserContext<K, V, C> ctx) throws ParserException {
+ final 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);
+ final 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);
+ if (onDelim != null) {
+ ctx.state = onDelim.apply(ctx.state);
+ }
}
return ret;
diff --git a/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java
index acddd3b..db94034 100644
--- a/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java
+++ b/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java
@@ -10,39 +10,40 @@ import bjc.utils.parserutils.ParserException;
/**
* 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 final int pow;
- private K term;
+ private final K term;
- private Predicate<ITree<Token<K, V>>> validatr;
+ private final 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) {
+ public SimpleParseBlock(final int precedence, final K terminator,
+ final Predicate<ITree<Token<K, V>>> validator) {
if (precedence < 0) throw new IllegalArgumentException("Precedence must be non-negative");
pow = precedence;
@@ -51,16 +52,14 @@ public class SimpleParseBlock<K, V, C> implements ParseBlock<K, V, C> {
}
@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);
+ public ITree<Token<K, V>> parse(final ParserContext<K, V, C> ctx) throws ParserException {
+ final 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;
- }
+ if (validatr == null || validatr.test(res)) return res;
throw new ParserException("Block failed validation");
}
@@ -72,18 +71,18 @@ public class SimpleParseBlock<K, V, C> implements ParseBlock<K, V, C> {
int result = 1;
result = prime * result + pow;
- result = prime * result + ((term == null) ? 0 : term.hashCode());
+ result = prime * result + (term == null ? 0 : term.hashCode());
return result;
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(final Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (!(obj instanceof SimpleParseBlock)) return false;
- SimpleParseBlock<?, ?, ?> other = (SimpleParseBlock<?, ?, ?>) obj;
+ final SimpleParseBlock<?, ?, ?> other = (SimpleParseBlock<?, ?, ?>) obj;
if (pow != other.pow) return false;
diff --git a/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java
index 76d99de..bb3d6f7 100644
--- a/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java
+++ b/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java
@@ -10,7 +10,7 @@ import bjc.utils.parserutils.ParserException;
/**
* A parse block that can adjust the state before handling its context.
- *
+ *
* @author bjculkin
*
* @param <K>
@@ -21,36 +21,37 @@ import bjc.utils.parserutils.ParserException;
* The state type of the parser.
*/
public class TriggeredParseBlock<K, V, C> implements ParseBlock<K, V, C> {
- private UnaryOperator<C> onEntr;
- private UnaryOperator<C> onExt;
+ private final UnaryOperator<C> onEntr;
+ private final UnaryOperator<C> onExt;
- private ParseBlock<K, V, C> sourc;
+ private final ParseBlock<K, V, C> 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<C> onEnter, UnaryOperator<C> onExit, ParseBlock<K, V, C> source) {
+ public TriggeredParseBlock(final UnaryOperator<C> onEnter, final UnaryOperator<C> onExit,
+ final ParseBlock<K, V, C> source) {
onEntr = onEnter;
onExt = onExit;
sourc = source;
}
@Override
- public ITree<Token<K, V>> parse(ParserContext<K, V, C> ctx) throws ParserException {
- C newState = onEntr.apply(ctx.state);
+ public ITree<Token<K, V>> parse(final ParserContext<K, V, C> ctx) throws ParserException {
+ final C newState = onEntr.apply(ctx.state);
- ParserContext<K, V, C> newCtx = new ParserContext<>(ctx.tokens, ctx.parse, newState);
+ final ParserContext<K, V, C> newCtx = new ParserContext<>(ctx.tokens, ctx.parse, newState);
- ITree<Token<K, V>> res = sourc.parse(newCtx);
+ final ITree<Token<K, V>> res = sourc.parse(newCtx);
ctx.state = onExt.apply(newState);