diff options
Diffstat (limited to 'JPratt/src')
9 files changed, 230 insertions, 42 deletions
diff --git a/JPratt/src/examples/java/bjc/pratt/examples/PrattParserTest.java b/JPratt/src/examples/java/bjc/pratt/examples/PrattParserTest.java index e504048..7b9feea 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/PrattParserTest.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/PrattParserTest.java @@ -65,7 +65,7 @@ public class PrattParserTest { ops.addAll(Arrays.asList("||", "&&")); ops.addAll(Arrays.asList("<=", ">=")); - ops.addAll(Arrays.asList("�")); + ops.addAll(Arrays.asList("\u00B1")); // Unicode plus/minus ops.addAll(Arrays.asList(".", ",", ";", ":")); ops.addAll(Arrays.asList("=", "<", ">")); ops.addAll(Arrays.asList("+", "-", "*", "/")); @@ -95,7 +95,7 @@ public class PrattParserTest { hi.addSimpleDelimiters("||", "&&"); hi.addSimpleDelimiters("<=", ">="); - lo.addSimpleDelimiters("�"); + lo.addSimpleDelimiters("\u00B1"); // Unicode plus/minus lo.addSimpleDelimiters(".", ",", ";", ":"); lo.addSimpleDelimiters("=", "<", ">"); lo.addSimpleDelimiters("+", "-", "*", "/"); @@ -243,7 +243,7 @@ public class PrattParserTest { final NonInitialCommand<String, String, TestContext> addSub = infixLeft(20); parser.addNonInitialCommand("+", addSub); parser.addNonInitialCommand("-", addSub); - parser.addNonInitialCommand("�", addSub); + parser.addNonInitialCommand("\u00B1", addSub); // Unicode plus/minus final NonInitialCommand<String, String, TestContext> mulDiv = infixLeft(30); parser.addNonInitialCommand("*", mulDiv); @@ -293,7 +293,6 @@ public class PrattParserTest { final InitialCommand<String, String, TestContext> leaf = leaf(); parser.addInitialCommand("(literal)", leaf); - parser.addInitialCommand("(vref)", leaf); parser.addInitialCommand("var", new VarCommand()); diff --git a/JPratt/src/examples/java/bjc/pratt/examples/Tokenizer.java b/JPratt/src/examples/java/bjc/pratt/examples/Tokenizer.java index 9b3fa74..4f8da12 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/Tokenizer.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/Tokenizer.java @@ -11,7 +11,9 @@ import bjc.pratt.tokens.StringToken; final class Tokenizer implements Function<String, Token<String, String>> { private final Set<String> ops; private final Set<String> reserved; - private final TestContext ctx; + + @SuppressWarnings("unused") + private final TestContext ctx; public Tokenizer(final Set<String> operators, final Set<String> reservedWords, final TestContext context) { ops = operators; @@ -21,26 +23,16 @@ final class Tokenizer implements Function<String, Token<String, String>> { @Override public Token<String, String> apply(final String strang) { - if (ops.contains(strang) || reserved.contains(strang)) + if (ops.contains(strang) || reserved.contains(strang)) { return litToken(strang); - else if (ctx.scopes.top().containsKey(strang)) - return new StringToken("(vref)", strang); - else if (strang.matches("(?:[\\u00B2\\u00B3\\u00B9\\u2070]|[\\u2074-\\u2079])+")) /* - * This - * regular - * expression - * matches - * series - * of - * unicode - * super - * - - * scripts - * 1 - * - - * 9. - */ + } else if (strang.matches("(?:[\\u00B2\\u00B3\\u00B9\\u2070]|[\\u2074-\\u2079])+")) { + /* + * This regular expression matches series of unicode + * super - scripts 1 - 9. + */ return new StringToken("(superexp)", strang); - else return new StringToken("(literal)", strang); + } else { + return new StringToken("(literal)", strang); + } } } 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); } |
