From 56f07e9a3aaa873fe385d224f088f048dbafa8f7 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 10 Apr 2017 16:49:54 -0400 Subject: Cleanup --- .../main/java/bjc/pratt/blocks/ParseBlocks.java | 42 +++++++++++----------- .../java/bjc/pratt/blocks/RepeatingParseBlock.java | 40 +++++++++++---------- .../java/bjc/pratt/blocks/SimpleParseBlock.java | 35 +++++++++--------- .../java/bjc/pratt/blocks/TriggeredParseBlock.java | 25 ++++++------- 4 files changed, 72 insertions(+), 70 deletions(-) (limited to 'JPratt/src/main/java/bjc/pratt/blocks') 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 ParseBlock repeating(ParseBlock inner, K delim, K term, - Token mark, UnaryOperator action) { + 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 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(ParseBlock source, UnaryOperator onEnter, - UnaryOperator onExit) { + 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 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(int precedence, K terminator, - Predicate>> validator) { + public static ParseBlock simple(final int precedence, final K terminator, + final Predicate>> 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 * 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 ParseBlock innerBlock; + private final ParseBlock innerBlock; - private K delim; - private K term; + private final K delim; + private final K term; - private UnaryOperator onDelim; + private final UnaryOperator onDelim; - private Token mark; + 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(ParseBlock inner, K delimiter, K terminator, Token marker, - UnaryOperator action) { + public RepeatingParseBlock(final ParseBlock inner, final K delimiter, final K terminator, + final Token marker, final UnaryOperator action) { super(); if (inner == null) @@ -74,20 +74,22 @@ public class RepeatingParseBlock implements ParseBlock { } @Override - public ITree> parse(ParserContext ctx) throws ParserException { - ITree> ret = new Tree<>(mark); + public ITree> parse(final ParserContext ctx) throws ParserException { + final ITree> ret = new Tree<>(mark); Token tok = ctx.tokens.current(); while (!tok.getKey().equals(term)) { - ITree> kid = innerBlock.parse(ctx); + final ITree> 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 * 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 int pow; + private final int pow; - private K term; + private final K term; - private Predicate>> validatr; + private final Predicate>> 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>> validator) { + public SimpleParseBlock(final int precedence, final K terminator, + final Predicate>> validator) { if (precedence < 0) throw new IllegalArgumentException("Precedence must be non-negative"); pow = precedence; @@ -51,16 +52,14 @@ public class SimpleParseBlock implements ParseBlock { } @Override - public ITree> parse(ParserContext ctx) throws ParserException { - ITree> res = ctx.parse.parseExpression(pow, ctx.tokens, ctx.state, false); + public ITree> parse(final ParserContext ctx) throws ParserException { + final ITree> 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 implements ParseBlock { 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 @@ -21,36 +21,37 @@ import bjc.utils.parserutils.ParserException; * The state type of the parser. */ public class TriggeredParseBlock implements ParseBlock { - private UnaryOperator onEntr; - private UnaryOperator onExt; + private final UnaryOperator onEntr; + private final UnaryOperator onExt; - private ParseBlock sourc; + 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(UnaryOperator onEnter, UnaryOperator onExit, ParseBlock source) { + public TriggeredParseBlock(final UnaryOperator onEnter, final UnaryOperator onExit, + final ParseBlock source) { onEntr = onEnter; onExt = onExit; sourc = source; } @Override - public ITree> parse(ParserContext ctx) throws ParserException { - C newState = onEntr.apply(ctx.state); + public ITree> parse(final ParserContext ctx) throws ParserException { + final C newState = onEntr.apply(ctx.state); - ParserContext newCtx = new ParserContext<>(ctx.tokens, ctx.parse, newState); + final ParserContext newCtx = new ParserContext<>(ctx.tokens, ctx.parse, newState); - ITree> res = sourc.parse(newCtx); + final ITree> res = sourc.parse(newCtx); ctx.state = onExt.apply(newState); -- cgit v1.2.3