From 251419e1f0ab8eb04d21287b708b06a552f4c58a Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 10 Apr 2017 16:49:31 -0400 Subject: Warning resolution --- .../java/bjc/pratt/blocks/TriggeredParseBlock.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'JPratt/src/main/java/bjc/pratt/blocks') diff --git a/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java index 5e561fc..76d99de 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java @@ -21,10 +21,10 @@ import bjc.utils.parserutils.ParserException; * The state type of the parser. */ public class TriggeredParseBlock implements ParseBlock { - private UnaryOperator onEnter; - private UnaryOperator onExit; + private UnaryOperator onEntr; + private UnaryOperator onExt; - private ParseBlock source; + private ParseBlock sourc; /** * Create a new triggered parse block. @@ -39,21 +39,20 @@ public class TriggeredParseBlock implements ParseBlock { * The block to use for parsing. */ public TriggeredParseBlock(UnaryOperator onEnter, UnaryOperator onExit, ParseBlock source) { - super(); - this.onEnter = onEnter; - this.onExit = onExit; - this.source = source; + onEntr = onEnter; + onExt = onExit; + sourc = source; } @Override public ITree> parse(ParserContext ctx) throws ParserException { - C newState = onEnter.apply(ctx.state); + C newState = onEntr.apply(ctx.state); ParserContext newCtx = new ParserContext<>(ctx.tokens, ctx.parse, newState); - ITree> res = source.parse(newCtx); + ITree> res = sourc.parse(newCtx); - ctx.state = onExit.apply(newState); + ctx.state = onExt.apply(newState); return res; } -- 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 --- .../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 From 7a510ceb37780a7d0da37117a5cfce23c2919257 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 10 Apr 2017 22:55:22 -0400 Subject: More work on parse blocks --- .../java/bjc/pratt/blocks/GrammarParseBlock.java | 81 ++++++++++++++++++++++ .../main/java/bjc/pratt/blocks/ParseBlocks.java | 16 +++-- .../java/bjc/pratt/blocks/SimpleParseBlock.java | 15 ++-- 3 files changed, 97 insertions(+), 15 deletions(-) create mode 100644 JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java (limited to 'JPratt/src/main/java/bjc/pratt/blocks') 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 + * The key type of the outer tokens. + * + * @param + * The value type of the outer tokens. + * + * @param + * The state type of the outer parser. + * + * @param + * The key type of the inner tokens. + * + * @param + * The value type of the inner tokens. + * + * @param + * The state type of the outer parser. + */ +public class GrammarParseBlock implements ParseBlock { + private PrattParser inner; + + private int precedence; + private boolean isStatement; + + private Function, TokenStream> tokenTransform; + private Isomorphism stateTransform; + private Function>, ITree>> expressionTransform; + + /** + * Create a new grammar parser block. + * + * @param inner + * @param precedence + * @param isStatement + * @param tokenTransform + * @param stateTransform + * @param expressionTransform + */ + public GrammarParseBlock(PrattParser inner, int precedence, boolean isStatement, + Function, TokenStream> tokenTransform, + Isomorphism stateTransform, + Function>, ITree>> expressionTransform) { + this.inner = inner; + this.precedence = precedence; + this.isStatement = isStatement; + this.tokenTransform = tokenTransform; + this.stateTransform = stateTransform; + this.expressionTransform = expressionTransform; + } + + @Override + public ITree> parse(ParserContext ctx) throws ParserException { + C2 newState = stateTransform.to(ctx.state); + + TokenStream newTokens = tokenTransform.apply(ctx.tokens); + + ITree> 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 ParseBlock simple(final int precedence, final K terminator, final Predicate>> 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 implements ParseBlock { * * @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>> validator) { + public SimpleParseBlock(final int precedence, final Predicate>> validator, + final K terminator) { if (precedence < 0) throw new IllegalArgumentException("Precedence must be non-negative"); pow = precedence; @@ -92,9 +90,4 @@ public class SimpleParseBlock implements ParseBlock { return true; } - - @Override - public String toString() { - return String.format("ParseBlock [pow=%s, term='%s']", pow, term); - } } \ No newline at end of file -- cgit v1.2.3 From 3f74e1e25fd572adab34e53eb90edcf49404fbe5 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Tue, 11 Apr 2017 21:57:37 -0400 Subject: Cleanup --- .../java/bjc/pratt/blocks/GrammarParseBlock.java | 59 +++++++++++----------- .../main/java/bjc/pratt/blocks/ParseBlocks.java | 4 -- 2 files changed, 30 insertions(+), 33 deletions(-) (limited to 'JPratt/src/main/java/bjc/pratt/blocks') diff --git a/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java index 459f83d..b714940 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java @@ -13,40 +13,40 @@ import bjc.utils.parserutils.ParserException; /** * A {@link ParseBlock} that parses an expression from a 'inner' grammar. - * + * * @author bjculkin * * @param * The key type of the outer tokens. - * + * * @param * The value type of the outer tokens. - * + * * @param * The state type of the outer parser. - * + * * @param * The key type of the inner tokens. - * + * * @param * The value type of the inner tokens. - * + * * @param * The state type of the outer parser. */ public class GrammarParseBlock implements ParseBlock { - private PrattParser inner; + private final PrattParser innr; - private int precedence; - private boolean isStatement; + private final int prcedence; + private final boolean isStatemnt; - private Function, TokenStream> tokenTransform; - private Isomorphism stateTransform; - private Function>, ITree>> expressionTransform; + private final Function, TokenStream> tkenTransform; + private final Isomorphism stteTransform; + private final Function>, ITree>> xpressionTransform; /** * Create a new grammar parser block. - * + * * @param inner * @param precedence * @param isStatement @@ -54,28 +54,29 @@ public class GrammarParseBlock implements ParseBlock inner, int precedence, boolean isStatement, - Function, TokenStream> tokenTransform, - Isomorphism stateTransform, - Function>, ITree>> expressionTransform) { - this.inner = inner; - this.precedence = precedence; - this.isStatement = isStatement; - this.tokenTransform = tokenTransform; - this.stateTransform = stateTransform; - this.expressionTransform = expressionTransform; + public GrammarParseBlock(final PrattParser inner, final int precedence, final boolean isStatement, + final Function, TokenStream> tokenTransform, + final Isomorphism stateTransform, + final Function>, ITree>> expressionTransform) { + innr = inner; + prcedence = precedence; + isStatemnt = isStatement; + tkenTransform = tokenTransform; + stteTransform = stateTransform; + xpressionTransform = expressionTransform; } @Override - public ITree> parse(ParserContext ctx) throws ParserException { - C2 newState = stateTransform.to(ctx.state); + public ITree> parse(final ParserContext ctx) throws ParserException { + final C2 newState = stteTransform.to(ctx.state); - TokenStream newTokens = tokenTransform.apply(ctx.tokens); + final TokenStream newTokens = tkenTransform.apply(ctx.tokens); - ITree> expression = inner.parseExpression(precedence, newTokens, newState, isStatement); + final ITree> expression = innr.parseExpression(prcedence, newTokens, newState, + isStatemnt); - ctx.state = stateTransform.from(newState); + ctx.state = stteTransform.from(newState); - return expressionTransform.apply(expression); + return xpressionTransform.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 a3e3147..44288d2 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java @@ -1,15 +1,11 @@ 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} -- cgit v1.2.3 From 6b881e8833596d669fdee9525e064aea0c8946dc Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Tue, 11 Apr 2017 23:12:34 -0400 Subject: Add exceptions to sample lang. --- .../java/bjc/pratt/blocks/ChainParseBlock.java | 79 ++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java (limited to 'JPratt/src/main/java/bjc/pratt/blocks') diff --git a/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java new file mode 100644 index 0000000..1758c17 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java @@ -0,0 +1,79 @@ +package bjc.pratt.blocks; + +import java.util.Set; + +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 {@link ParseBlock} for a series of parse blocks, linked by a set of tokens. + * + * Roughly analogous to Perl 6s list associative operators. + * + * @author bjculkin + * + * @param + * The token key type. + * + * @param + * The token value type. + * + * @param + * The parser state type. + * + */ +public class ChainParseBlock implements ParseBlock { + private ParseBlock iner; + + private Set indicators; + + private Token trm; + + /** + * Create a new chain parser block. + * + * @param inner + * The block for the chains interior. + * + * @param chainIndicators + * The set of markers that indicate continuing the chain + * + * @param term + * The node in the AST for the expression. + */ + public ChainParseBlock(ParseBlock inner, Set chainIndicators, Token term) { + iner = inner; + indicators = chainIndicators; + trm = term; + } + + @Override + public ITree> parse(ParserContext ctx) throws ParserException { + ITree> expression = iner.parse(ctx); + + Token currentToken = ctx.tokens.current(); + if (indicators.contains(currentToken.getKey())) { + ITree> res = new Tree<>(trm); + res.addChild(expression); + + while (indicators.contains(currentToken.getKey())) { + res.addChild(new Tree<>(currentToken)); + ctx.tokens.next(); + + ITree> innerExpression = iner.parse(ctx); + res.addChild(innerExpression); + + currentToken = ctx.tokens.current(); + } + + return res; + } + + return expression; + } + +} -- 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 --- .../java/bjc/pratt/blocks/ChainParseBlock.java | 3 +- .../java/bjc/pratt/blocks/GrammarParseBlock.java | 5 ++- .../src/main/java/bjc/pratt/blocks/ParseBlock.java | 39 ++++++++++++++++++++++ .../main/java/bjc/pratt/blocks/ParseBlocks.java | 3 +- .../java/bjc/pratt/blocks/RepeatingParseBlock.java | 3 +- .../java/bjc/pratt/blocks/SimpleParseBlock.java | 3 +- .../java/bjc/pratt/blocks/TriggeredParseBlock.java | 3 +- 7 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 JPratt/src/main/java/bjc/pratt/blocks/ParseBlock.java (limited to 'JPratt/src/main/java/bjc/pratt/blocks') diff --git a/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java index 1758c17..5c728d9 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java @@ -2,9 +2,8 @@ package bjc.pratt.blocks; import java.util.Set; -import bjc.pratt.ParseBlock; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.data.Tree; import bjc.utils.parserutils.ParserException; diff --git a/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java index b714940..6bf5582 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java @@ -2,11 +2,10 @@ 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.pratt.tokens.Token; +import bjc.pratt.tokens.TokenStream; import bjc.utils.data.ITree; import bjc.utils.funcutils.Isomorphism; import bjc.utils.parserutils.ParserException; diff --git a/JPratt/src/main/java/bjc/pratt/blocks/ParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlock.java new file mode 100644 index 0000000..2fddac0 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlock.java @@ -0,0 +1,39 @@ +package bjc.pratt.blocks; + +import bjc.pratt.ParserContext; +import bjc.pratt.tokens.Token; +import bjc.utils.data.ITree; +import bjc.utils.parserutils.ParserException; + +/** + * Represents a embedded block in an expression. + * + * @author bjculkin + * + * @param + * The key type of the token. + * + * @param + * The value type of the token. + * + * @param + * The state type of the parser. + */ +@FunctionalInterface +public interface ParseBlock { + + /** + * Parse the block this represents. + * + * @param ctx + * The context for parsing. + * + * @return A AST for this block. + * + * @throws ParserException + * If something goes wrong during parsing, or the block + * fails validation. + */ + ITree> parse(ParserContext ctx) throws ParserException; + +} \ 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 44288d2..21fa7e1 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java @@ -3,8 +3,7 @@ package bjc.pratt.blocks; import java.util.function.Predicate; import java.util.function.UnaryOperator; -import bjc.pratt.ParseBlock; -import bjc.pratt.Token; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; /** diff --git a/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java index 7791ea9..4146648 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java @@ -2,9 +2,8 @@ package bjc.pratt.blocks; import java.util.function.UnaryOperator; -import bjc.pratt.ParseBlock; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.data.Tree; import bjc.utils.parserutils.ParserException; diff --git a/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java index 0fb5097..1ff561c 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java @@ -2,9 +2,8 @@ package bjc.pratt.blocks; import java.util.function.Predicate; -import bjc.pratt.ParseBlock; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.parserutils.ParserException; diff --git a/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java index bb3d6f7..844a4f8 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java @@ -2,9 +2,8 @@ package bjc.pratt.blocks; import java.util.function.UnaryOperator; -import bjc.pratt.ParseBlock; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.parserutils.ParserException; -- cgit v1.2.3