summaryrefslogtreecommitdiff
path: root/JPratt/src/main/java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2017-04-10 22:55:22 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2017-04-10 22:55:22 -0400
commit7a510ceb37780a7d0da37117a5cfce23c2919257 (patch)
treee5f7a796cc6555cdcc5f09d969408f80c79b10e8 /JPratt/src/main/java
parent56f07e9a3aaa873fe385d224f088f048dbafa8f7 (diff)
More work on parse blocks
Diffstat (limited to 'JPratt/src/main/java')
-rw-r--r--JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java81
-rw-r--r--JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java16
-rw-r--r--JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java15
-rw-r--r--JPratt/src/main/java/bjc/pratt/commands/BlockInitialCommand.java41
-rw-r--r--JPratt/src/main/java/bjc/pratt/commands/BlockNonInitialCommand.java74
-rw-r--r--JPratt/src/main/java/bjc/pratt/commands/InitialCommands.java2
-rw-r--r--JPratt/src/main/java/bjc/pratt/commands/NonInitialCommands.java4
7 files changed, 215 insertions, 18 deletions
diff --git a/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java
new file mode 100644
index 0000000..459f83d
--- /dev/null
+++ b/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java
@@ -0,0 +1,81 @@
+package bjc.pratt.blocks;
+
+import java.util.function.Function;
+
+import bjc.pratt.ParseBlock;
+import bjc.pratt.ParserContext;
+import bjc.pratt.PrattParser;
+import bjc.pratt.Token;
+import bjc.pratt.TokenStream;
+import bjc.utils.data.ITree;
+import bjc.utils.funcutils.Isomorphism;
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * A {@link ParseBlock} that parses an expression from a 'inner' grammar.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The key type of the outer tokens.
+ *
+ * @param <V>
+ * The value type of the outer tokens.
+ *
+ * @param <C>
+ * The state type of the outer parser.
+ *
+ * @param <K2>
+ * The key type of the inner tokens.
+ *
+ * @param <V2>
+ * The value type of the inner tokens.
+ *
+ * @param <C2>
+ * The state type of the outer parser.
+ */
+public class GrammarParseBlock<K, V, C, K2, V2, C2> implements ParseBlock<K, V, C> {
+ private PrattParser<K2, V2, C2> inner;
+
+ private int precedence;
+ private boolean isStatement;
+
+ private Function<TokenStream<K, V>, TokenStream<K2, V2>> tokenTransform;
+ private Isomorphism<C, C2> stateTransform;
+ private Function<ITree<Token<K2, V2>>, ITree<Token<K, V>>> expressionTransform;
+
+ /**
+ * Create a new grammar parser block.
+ *
+ * @param inner
+ * @param precedence
+ * @param isStatement
+ * @param tokenTransform
+ * @param stateTransform
+ * @param expressionTransform
+ */
+ public GrammarParseBlock(PrattParser<K2, V2, C2> inner, int precedence, boolean isStatement,
+ Function<TokenStream<K, V>, TokenStream<K2, V2>> tokenTransform,
+ Isomorphism<C, C2> stateTransform,
+ Function<ITree<Token<K2, V2>>, ITree<Token<K, V>>> expressionTransform) {
+ this.inner = inner;
+ this.precedence = precedence;
+ this.isStatement = isStatement;
+ this.tokenTransform = tokenTransform;
+ this.stateTransform = stateTransform;
+ this.expressionTransform = expressionTransform;
+ }
+
+ @Override
+ public ITree<Token<K, V>> parse(ParserContext<K, V, C> ctx) throws ParserException {
+ C2 newState = stateTransform.to(ctx.state);
+
+ TokenStream<K2, V2> newTokens = tokenTransform.apply(ctx.tokens);
+
+ ITree<Token<K2, V2>> expression = inner.parseExpression(precedence, newTokens, newState, isStatement);
+
+ ctx.state = stateTransform.from(newState);
+
+ return expressionTransform.apply(expression);
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java
index d236a71..a3e3147 100644
--- a/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java
+++ b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java
@@ -1,11 +1,15 @@
package bjc.pratt.blocks;
+import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import bjc.pratt.ParseBlock;
+import bjc.pratt.PrattParser;
import bjc.pratt.Token;
+import bjc.pratt.TokenStream;
import bjc.utils.data.ITree;
+import bjc.utils.funcutils.Isomorphism;
/**
* Utility class for creating common implementations of {@link ParseBlock}
@@ -14,6 +18,10 @@ import bjc.utils.data.ITree;
*
*/
public class ParseBlocks {
+ /*
+ * Grammar parse blocks are complex enough to not get a builder method.
+ */
+
/**
* Create a new repeating parse block.
*
@@ -21,17 +29,17 @@ public class ParseBlocks {
* The parse block to repeat.
*
* @param delim
- * The token type that seperates repetitions.
+ * The token type that separates repetitions.
*
* @param term
- * The token type that terminates repititions.
+ * The token type that terminates repetitions.
*
* @param mark
* The token to use as the node in the AST.
*
* @param action
* The action to perform on the state after every
- * repitition.
+ * repetition.
*
* @return A configured repeating parse block.
*/
@@ -79,6 +87,6 @@ public class ParseBlocks {
*/
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);
+ return new SimpleParseBlock<>(precedence, validator, terminator);
}
} \ No newline at end of file
diff --git a/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java
index db94034..0fb5097 100644
--- a/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java
+++ b/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java
@@ -34,16 +34,14 @@ public class SimpleParseBlock<K, V, C> implements ParseBlock<K, V, C> {
*
* @param precedence
* The precedence of this block.
- *
+ * @param validator
+ * The predicate to apply to blocks.
* @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(final int precedence, final K terminator,
- final Predicate<ITree<Token<K, V>>> validator) {
+ public SimpleParseBlock(final int precedence, final Predicate<ITree<Token<K, V>>> validator,
+ final K terminator) {
if (precedence < 0) throw new IllegalArgumentException("Precedence must be non-negative");
pow = precedence;
@@ -92,9 +90,4 @@ public class SimpleParseBlock<K, V, C> implements ParseBlock<K, V, C> {
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/pratt/commands/BlockInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/BlockInitialCommand.java
new file mode 100644
index 0000000..f0448f7
--- /dev/null
+++ b/JPratt/src/main/java/bjc/pratt/commands/BlockInitialCommand.java
@@ -0,0 +1,41 @@
+package bjc.pratt.commands;
+
+import bjc.pratt.ParseBlock;
+import bjc.pratt.ParserContext;
+import bjc.pratt.Token;
+import bjc.utils.data.ITree;
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * An initial command that delegates all the work to a {@link ParseBlock}
+ *
+ * @author bjculkin
+ * @param <K>
+ * The token key type.
+ *
+ * @param <V>
+ * The token value type.
+ *
+ * @param <C>
+ * The parser state type.
+ *
+ */
+public class BlockInitialCommand<K, V, C> extends AbstractInitialCommand<K, V, C> {
+ private ParseBlock<K, V, C> blck;
+
+ /**
+ * Create a new block initial command.
+ *
+ * @param block
+ * The block to delegate to.
+ */
+ public BlockInitialCommand(ParseBlock<K, V, C> block) {
+ blck = block;
+ }
+
+ @Override
+ protected ITree<Token<K, V>> intNullDenotation(Token<K, V> operator, ParserContext<K, V, C> ctx)
+ throws ParserException {
+ return blck.parse(ctx);
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/bjc/pratt/commands/BlockNonInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/BlockNonInitialCommand.java
new file mode 100644
index 0000000..9a5ffc9
--- /dev/null
+++ b/JPratt/src/main/java/bjc/pratt/commands/BlockNonInitialCommand.java
@@ -0,0 +1,74 @@
+package bjc.pratt.commands;
+
+import bjc.pratt.NonInitialCommand;
+import bjc.pratt.ParseBlock;
+import bjc.pratt.ParserContext;
+import bjc.pratt.Token;
+import bjc.utils.data.ITree;
+import bjc.utils.data.Tree;
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * A non-initial command that delegates all of the work to a {@link ParseBlock}
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The token key type.
+ *
+ * @param <V>
+ * The token value type.
+ *
+ * @param <C>
+ * The parser state type.
+ */
+public class BlockNonInitialCommand<K, V, C> extends NonInitialCommand<K, V, C> {
+ private ParseBlock<K, V, C> innr;
+
+ private int lftBind;
+ private int nxtBind;
+
+ private Token<K, V> trm;
+
+ /**
+ * Create a new non-initial command that delegates to a parse block.
+ *
+ * @param inner
+ * The parse block to delegate to.
+ *
+ * @param leftBind
+ * The left binding power (precedence).
+ *
+ * @param rightBind
+ * The right binding power (associativity control).
+ *
+ * @param term
+ * The token to use as the node in the AST.
+ */
+ public BlockNonInitialCommand(ParseBlock<K, V, C> inner, int leftBind, int rightBind, Token<K, V> term) {
+ innr = inner;
+
+ lftBind = leftBind;
+ nxtBind = rightBind;
+
+ trm = term;
+ }
+
+ @Override
+ public ITree<Token<K, V>> denote(ITree<Token<K, V>> operand, Token<K, V> operator, ParserContext<K, V, C> ctx)
+ throws ParserException {
+ ITree<Token<K, V>> expression = innr.parse(ctx);
+
+ return new Tree<>(trm, expression);
+ }
+
+ @Override
+ public int leftBinding() {
+ return lftBind;
+ }
+
+ @Override
+ public int nextBinding() {
+ return nxtBind;
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/bjc/pratt/commands/InitialCommands.java b/JPratt/src/main/java/bjc/pratt/commands/InitialCommands.java
index 5710277..b373a7c 100644
--- a/JPratt/src/main/java/bjc/pratt/commands/InitialCommands.java
+++ b/JPratt/src/main/java/bjc/pratt/commands/InitialCommands.java
@@ -168,4 +168,4 @@ public class InitialCommands {
public static <K, V, C> InitialCommand<K, V, C> denest(final InitialCommand<K, V, C> comm) {
return new DenestingCommand<>(comm);
}
-}
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommands.java b/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommands.java
index 48922b7..39baf1f 100644
--- a/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommands.java
+++ b/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommands.java
@@ -104,7 +104,7 @@ public class NonInitialCommands {
*/
public static <K, V, C> NonInitialCommand<K, V, C> postCircumfix(final int precedence,
final int insidePrecedence, final K closer, final Token<K, V> marker) {
- final ParseBlock<K, V, C> innerBlock = new SimpleParseBlock<>(insidePrecedence, closer, null);
+ final ParseBlock<K, V, C> innerBlock = new SimpleParseBlock<>(insidePrecedence, null, closer);
return new PostCircumfixCommand<>(precedence, innerBlock, marker);
}
@@ -134,7 +134,7 @@ public class NonInitialCommands {
*/
public static <K, V, C> NonInitialCommand<K, V, C> ternary(final int precedence, final int insidePrecedence,
final K closer, final Token<K, V> marker, final boolean nonassoc) {
- final ParseBlock<K, V, C> innerBlock = new SimpleParseBlock<>(insidePrecedence, closer, null);
+ final ParseBlock<K, V, C> innerBlock = new SimpleParseBlock<>(insidePrecedence, null, closer);
return new TernaryCommand<>(precedence, innerBlock, marker, nonassoc);
}