From 251419e1f0ab8eb04d21287b708b06a552f4c58a Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 10 Apr 2017 16:49:31 -0400 Subject: Warning resolution --- JPratt/src/main/java/bjc/pratt/PrattParser.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'JPratt/src/main/java/bjc/pratt/PrattParser.java') diff --git a/JPratt/src/main/java/bjc/pratt/PrattParser.java b/JPratt/src/main/java/bjc/pratt/PrattParser.java index e08a67c..c36cabc 100644 --- a/JPratt/src/main/java/bjc/pratt/PrattParser.java +++ b/JPratt/src/main/java/bjc/pratt/PrattParser.java @@ -174,7 +174,16 @@ public class PrattParser { } /** - * Add a dependant non-initial command to this parser. + * 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 comm) { if (dependantLeftCommands.containsKey(dependant)) { -- cgit v1.2.3 From 56f07e9a3aaa873fe385d224f088f048dbafa8f7 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 10 Apr 2017 16:49:54 -0400 Subject: Cleanup --- JPratt/src/main/java/bjc/pratt/PrattParser.java | 86 ++++++++++++------------- 1 file changed, 42 insertions(+), 44 deletions(-) (limited to 'JPratt/src/main/java/bjc/pratt/PrattParser.java') 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 * The key type for the tokens. - * + * * @param * The value type for the tokens. - * + * * @param * The state type of the parser. - * + * * */ public class PrattParser { @@ -35,24 +35,24 @@ public class PrattParser { /* * Left-commands that depend on what the null command was. */ - private Map>> dependantLeftCommands; + private final Map>> dependantLeftCommands; /* * The left commands. */ - private Map> leftCommands; + private final Map> leftCommands; /* * The initial commands. */ - private Map> nullCommands; + private final Map> nullCommands; /* * Initial commands only checked for statements. */ - private Map> statementCommands; + private final Map> statementCommands; /** * Create a new Pratt parser. - * + * */ public PrattParser() { dependantLeftCommands = new HashMap<>(); @@ -64,34 +64,32 @@ public class PrattParser { /** * 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> parseExpression(int precedence, TokenStream tokens, C state, - boolean isStatement) throws ParserException { - if (precedence < 0) { - throw new IllegalArgumentException("Precedence must be greater than zero"); - } + public ITree> parseExpression(final int precedence, final TokenStream tokens, final C state, + final boolean isStatement) throws ParserException { + if (precedence < 0) throw new IllegalArgumentException("Precedence must be greater than zero"); - Token initToken = tokens.current(); + final Token initToken = tokens.current(); tokens.next(); - K initKey = initToken.getKey(); + final K initKey = initToken.getKey(); ITree> ast; @@ -106,9 +104,9 @@ public class PrattParser { int rightPrec = Integer.MAX_VALUE; while (true) { - Token tok = tokens.current(); + final Token tok = tokens.current(); - K key = tok.getKey(); + final K key = tok.getKey(); NonInitialCommand command = leftCommands.getOrDefault(key, DEFAULT_LEFT_COMMAND); @@ -116,7 +114,7 @@ public class PrattParser { 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 { /** * 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 comm) { + public void addNonInitialCommand(final K marker, final NonInitialCommand 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 comm) { + public void addInitialCommand(final K marker, final InitialCommand 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 comm) { + public void addStatementCommand(final K marker, final InitialCommand 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 comm) { + public void addDependantCommand(final K dependant, final K marker, final NonInitialCommand comm) { if (dependantLeftCommands.containsKey(dependant)) { dependantLeftCommands.get(dependant).put(marker, comm); } else { - Map> comms = new HashMap<>(); + final Map> comms = new HashMap<>(); comms.put(marker, comm); -- cgit v1.2.3 From 694bed833470393ee00eae0a85bff0c6c90e692a Mon Sep 17 00:00:00 2001 From: bjculkin Date: Wed, 12 Apr 2017 10:45:46 -0400 Subject: Add support for meta-commands Meta-commands allow you to implement meta-operators, whose behavior changes based off of tokens that follow. --- JPratt/src/main/java/bjc/pratt/PrattParser.java | 119 ++++++++++++++++++------ 1 file changed, 91 insertions(+), 28 deletions(-) (limited to 'JPratt/src/main/java/bjc/pratt/PrattParser.java') diff --git a/JPratt/src/main/java/bjc/pratt/PrattParser.java b/JPratt/src/main/java/bjc/pratt/PrattParser.java index b3ce4a7..e2654f7 100644 --- a/JPratt/src/main/java/bjc/pratt/PrattParser.java +++ b/JPratt/src/main/java/bjc/pratt/PrattParser.java @@ -35,20 +35,25 @@ public class PrattParser { /* * Left-commands that depend on what the null command was. */ - private final Map>> dependantLeftCommands; - + private final Map>> dependantLeftCommands; + private final Map>> dependantMetaLeftCommands; /* * The left commands. */ - private final Map> leftCommands; + private final Map> leftCommands; + private final Map> metaLeftCommands; + /* * The initial commands. */ - private final Map> nullCommands; + private final Map> nullCommands; + private final Map> metaNullCommands; + /* * Initial commands only checked for statements. */ - private final Map> statementCommands; + private final Map> statementCommands; + private final Map> metaStatementCommands; /** * Create a new Pratt parser. @@ -56,10 +61,16 @@ public class PrattParser { */ public PrattParser() { dependantLeftCommands = new HashMap<>(); + dependantMetaLeftCommands = new HashMap<>(); leftCommands = new HashMap<>(); + metaLeftCommands = new HashMap<>(); + nullCommands = new HashMap<>(); + metaNullCommands = new HashMap<>(); + statementCommands = new HashMap<>(); + metaStatementCommands = new HashMap<>(); } /** @@ -84,22 +95,20 @@ public class PrattParser { */ public ITree> parseExpression(final int precedence, final TokenStream tokens, final C state, final boolean isStatement) throws ParserException { - if (precedence < 0) throw new IllegalArgumentException("Precedence must be greater than zero"); + if (precedence < 0) + throw new IllegalArgumentException("Precedence must be greater than zero"); + + ParserContext parserContext = new ParserContext<>(tokens, this, state); final Token initToken = tokens.current(); tokens.next(); final K initKey = initToken.getKey(); - ITree> ast; + InitialCommand nullCommand = getInitialCommand(isStatement, initKey, parserContext); + ITree> ast = nullCommand.denote(initToken, parserContext); - if (isStatement && statementCommands.containsKey(initKey)) { - ast = statementCommands.getOrDefault(initKey, DEFAULT_NULL_COMMAND).denote(initToken, - new ParserContext<>(tokens, this, state)); - } else { - ast = nullCommands.getOrDefault(initKey, DEFAULT_NULL_COMMAND).denote(initToken, - new ParserContext<>(tokens, this, state)); - } + parserContext.initial = initKey; int rightPrec = Integer.MAX_VALUE; @@ -108,19 +117,15 @@ public class PrattParser { final K key = tok.getKey(); - NonInitialCommand command = leftCommands.getOrDefault(key, DEFAULT_LEFT_COMMAND); + NonInitialCommand leftCommand = getNonInitialCommand(key, parserContext); - if (dependantLeftCommands.containsKey(initKey)) { - command = dependantLeftCommands.get(initKey).getOrDefault(key, command); - } - - final int leftBind = command.leftBinding(); + final int leftBind = leftCommand.leftBinding(); if (NumberUtils.between(precedence, rightPrec, leftBind)) { tokens.next(); - ast = command.denote(ast, tok, new ParserContext<>(tokens, this, state)); - rightPrec = command.nextBinding(); + ast = leftCommand.denote(ast, tok, parserContext); + rightPrec = leftCommand.nextBinding(); } else { break; } @@ -184,14 +189,72 @@ public class PrattParser { * The command. */ public void addDependantCommand(final K dependant, final K marker, final NonInitialCommand comm) { - if (dependantLeftCommands.containsKey(dependant)) { - dependantLeftCommands.get(dependant).put(marker, comm); - } else { - final Map> comms = new HashMap<>(); + Map> dependantMap = dependantLeftCommands.getOrDefault(dependant, + new HashMap<>()); + + dependantMap.put(marker, comm); + } + + /** + * Lookup an initial command. + * + * @param isStatement + * Whether to look for statement commands or not. + * + * @param key + * The key of the command. + * + * @param ctx + * The context for meta-commands. + * + * @return A command attached to that key, or a default implementation. + */ + public InitialCommand getInitialCommand(boolean isStatement, K key, ParserContext ctx) { + if (isStatement) { + if (metaStatementCommands.containsKey(key)) + return metaStatementCommands.get(key).getCommand(ctx); + else if (statementCommands.containsKey(key)) + return statementCommands.get(key); + } - comms.put(marker, comm); + if (metaNullCommands.containsKey(key)) + return metaNullCommands.get(key).getCommand(ctx); + else + return nullCommands.getOrDefault(key, DEFAULT_NULL_COMMAND); + } + + /** + * Lookup a non-initial command. + * + * @param key + * The key of the command. + * + * @param ctx + * The context for meta-commands. + * + * @return A command attached to that key, or a default implementation. + */ + public NonInitialCommand getNonInitialCommand(K key, ParserContext ctx) { + if (dependantMetaLeftCommands.containsKey(ctx.initial)) { + Map> dependantCommands = dependantMetaLeftCommands + .get(ctx.initial); - dependantLeftCommands.put(dependant, comms); + if (dependantCommands.containsKey(key)) { + return dependantCommands.get(key).getCommand(ctx); + } + } + + if (dependantLeftCommands.containsKey(ctx.initial)) { + Map> dependantCommands = dependantLeftCommands.get(ctx.initial); + + if (dependantCommands.containsKey(key)) { + return dependantCommands.getOrDefault(key, DEFAULT_LEFT_COMMAND); + } } + + if (metaLeftCommands.containsKey(key)) { + return metaLeftCommands.get(key).getCommand(ctx); + } else + return leftCommands.getOrDefault(key, DEFAULT_LEFT_COMMAND); } } \ No newline at end of file -- cgit v1.2.3 From f394306a4b65a3328551f9f6b8d4abff8bfd5b27 Mon Sep 17 00:00:00 2001 From: bjculkin Date: Wed, 12 Apr 2017 10:46:51 -0400 Subject: Package reorganization --- JPratt/src/main/java/bjc/pratt/PrattParser.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'JPratt/src/main/java/bjc/pratt/PrattParser.java') diff --git a/JPratt/src/main/java/bjc/pratt/PrattParser.java b/JPratt/src/main/java/bjc/pratt/PrattParser.java index e2654f7..7783736 100644 --- a/JPratt/src/main/java/bjc/pratt/PrattParser.java +++ b/JPratt/src/main/java/bjc/pratt/PrattParser.java @@ -3,8 +3,14 @@ package bjc.pratt; import java.util.HashMap; import java.util.Map; -import bjc.pratt.commands.DefaultInitialCommand; -import bjc.pratt.commands.DefaultNonInitialCommand; +import bjc.pratt.commands.InitialCommand; +import bjc.pratt.commands.MetaInitialCommand; +import bjc.pratt.commands.MetaNonInitialCommand; +import bjc.pratt.commands.NonInitialCommand; +import bjc.pratt.commands.impls.DefaultInitialCommand; +import bjc.pratt.commands.impls.DefaultNonInitialCommand; +import bjc.pratt.tokens.Token; +import bjc.pratt.tokens.TokenStream; import bjc.utils.data.ITree; import bjc.utils.funcutils.NumberUtils; import bjc.utils.parserutils.ParserException; -- cgit v1.2.3