diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-04-10 16:49:54 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-04-10 16:49:54 -0400 |
| commit | 56f07e9a3aaa873fe385d224f088f048dbafa8f7 (patch) | |
| tree | 64fae78f95fd1c233689ecf3dda2e2b645bb8d33 /JPratt/src/main/java/bjc/pratt/PrattParser.java | |
| parent | 251419e1f0ab8eb04d21287b708b06a552f4c58a (diff) | |
Cleanup
Diffstat (limited to 'JPratt/src/main/java/bjc/pratt/PrattParser.java')
| -rw-r--r-- | JPratt/src/main/java/bjc/pratt/PrattParser.java | 86 |
1 files changed, 42 insertions, 44 deletions
diff --git a/JPratt/src/main/java/bjc/pratt/PrattParser.java b/JPratt/src/main/java/bjc/pratt/PrattParser.java index c36cabc..b3ce4a7 100644 --- a/JPratt/src/main/java/bjc/pratt/PrattParser.java +++ b/JPratt/src/main/java/bjc/pratt/PrattParser.java @@ -1,28 +1,28 @@ package bjc.pratt; +import java.util.HashMap; +import java.util.Map; + import bjc.pratt.commands.DefaultInitialCommand; import bjc.pratt.commands.DefaultNonInitialCommand; import bjc.utils.data.ITree; import bjc.utils.funcutils.NumberUtils; import bjc.utils.parserutils.ParserException; -import java.util.HashMap; -import java.util.Map; - /** * A configurable Pratt parser for expressions. - * + * * @author EVE - * + * * @param <K> * The key type for the tokens. - * + * * @param <V> * The value type for the tokens. - * + * * @param <C> * The state type of the parser. - * + * * */ public class PrattParser<K, V, C> { @@ -35,24 +35,24 @@ public class PrattParser<K, V, C> { /* * Left-commands that depend on what the null command was. */ - private Map<K, Map<K, NonInitialCommand<K, V, C>>> dependantLeftCommands; + private final Map<K, Map<K, NonInitialCommand<K, V, C>>> dependantLeftCommands; /* * The left commands. */ - private Map<K, NonInitialCommand<K, V, C>> leftCommands; + private final Map<K, NonInitialCommand<K, V, C>> leftCommands; /* * The initial commands. */ - private Map<K, InitialCommand<K, V, C>> nullCommands; + private final Map<K, InitialCommand<K, V, C>> nullCommands; /* * Initial commands only checked for statements. */ - private Map<K, InitialCommand<K, V, C>> statementCommands; + private final Map<K, InitialCommand<K, V, C>> statementCommands; /** * Create a new Pratt parser. - * + * */ public PrattParser() { dependantLeftCommands = new HashMap<>(); @@ -64,34 +64,32 @@ public class PrattParser<K, V, C> { /** * Parse an expression. - * + * * @param precedence * The initial precedence for the expression. - * + * * @param tokens * The tokens for the expression. - * + * * @param state * The state of the parser. - * + * * @param isStatement * Whether or not to parse statements. - * + * * @return The expression as an AST. - * + * * @throws ParserException * If something goes wrong during parsing. */ - public ITree<Token<K, V>> parseExpression(int precedence, TokenStream<K, V> tokens, C state, - boolean isStatement) throws ParserException { - if (precedence < 0) { - throw new IllegalArgumentException("Precedence must be greater than zero"); - } + public ITree<Token<K, V>> parseExpression(final int precedence, final TokenStream<K, V> tokens, final C state, + final boolean isStatement) throws ParserException { + if (precedence < 0) throw new IllegalArgumentException("Precedence must be greater than zero"); - Token<K, V> initToken = tokens.current(); + final Token<K, V> initToken = tokens.current(); tokens.next(); - K initKey = initToken.getKey(); + final K initKey = initToken.getKey(); ITree<Token<K, V>> ast; @@ -106,9 +104,9 @@ public class PrattParser<K, V, C> { int rightPrec = Integer.MAX_VALUE; while (true) { - Token<K, V> tok = tokens.current(); + final Token<K, V> tok = tokens.current(); - K key = tok.getKey(); + final K key = tok.getKey(); NonInitialCommand<K, V, C> command = leftCommands.getOrDefault(key, DEFAULT_LEFT_COMMAND); @@ -116,7 +114,7 @@ public class PrattParser<K, V, C> { command = dependantLeftCommands.get(initKey).getOrDefault(key, command); } - int leftBind = command.leftBinding(); + final int leftBind = command.leftBinding(); if (NumberUtils.between(precedence, rightPrec, leftBind)) { tokens.next(); @@ -133,63 +131,63 @@ public class PrattParser<K, V, C> { /** * Add a non-initial command to this parser. - * + * * @param marker * The key that marks the command. - * + * * @param comm * The command. */ - public void addNonInitialCommand(K marker, NonInitialCommand<K, V, C> comm) { + public void addNonInitialCommand(final K marker, final NonInitialCommand<K, V, C> comm) { leftCommands.put(marker, comm); } /** * Add a initial command to this parser. - * + * * @param marker * The key that marks the command. - * + * * @param comm * The command. */ - public void addInitialCommand(K marker, InitialCommand<K, V, C> comm) { + public void addInitialCommand(final K marker, final InitialCommand<K, V, C> comm) { nullCommands.put(marker, comm); } /** * Add a statement command to this parser. - * + * * The difference between statements and initial commands is that * statements can only appear at the start of the expression. - * + * * @param marker * The key that marks the command. - * + * * @param comm * The command. */ - public void addStatementCommand(K marker, InitialCommand<K, V, C> comm) { + public void addStatementCommand(final K marker, final InitialCommand<K, V, C> comm) { statementCommands.put(marker, comm); } /** * Add a dependent non-initial command to this parser. - * + * * @param dependant * The dependent that precedes the command. - * + * * @param marker * The token key that marks the command. - * + * * @param comm * The command. */ - public void addDependantCommand(K dependant, K marker, NonInitialCommand<K, V, C> comm) { + public void addDependantCommand(final K dependant, final K marker, final NonInitialCommand<K, V, C> comm) { if (dependantLeftCommands.containsKey(dependant)) { dependantLeftCommands.get(dependant).put(marker, comm); } else { - Map<K, NonInitialCommand<K, V, C>> comms = new HashMap<>(); + final Map<K, NonInitialCommand<K, V, C>> comms = new HashMap<>(); comms.put(marker, comm); |
