diff options
| author | Benjamin Culkin <scorpress@gmail.com> | 2024-06-03 17:33:53 -0400 |
|---|---|---|
| committer | Benjamin Culkin <scorpress@gmail.com> | 2024-06-03 17:33:53 -0400 |
| commit | 15a2b29e48f134bc93cfd0a3d8512001e9242f3d (patch) | |
| tree | b3f5c4c5f0e474479cd47ad0ac0f35770fc44881 /JPratt/src/main/java/com/ashardalon/pratt/blocks | |
| parent | 39ba97edf49270715ec61bedb7d4a62ada819ba0 (diff) | |
Rename the package to the new domain
Diffstat (limited to 'JPratt/src/main/java/com/ashardalon/pratt/blocks')
7 files changed, 576 insertions, 0 deletions
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/blocks/ChainParseBlock.java b/JPratt/src/main/java/com/ashardalon/pratt/blocks/ChainParseBlock.java new file mode 100644 index 0000000..fe04070 --- /dev/null +++ b/JPratt/src/main/java/com/ashardalon/pratt/blocks/ChainParseBlock.java @@ -0,0 +1,86 @@ +package com.ashardalon.pratt.blocks; + +import java.util.Set; + +import com.ashardalon.pratt.ParserContext; +import com.ashardalon.pratt.commands.CommandResult; +import com.ashardalon.pratt.commands.CommandResult.Status; +import com.ashardalon.pratt.tokens.Token; + +import bjc.data.Tree; +import bjc.data.SimpleTree; +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 <K> + * The token key type. + * + * @param <V> + * The token value type. + * + * @param <C> + * The parser state type. + * + */ +public class ChainParseBlock<K, V, C> implements ParseBlock<K, V, C> { + private ParseBlock<K, V, C> iner; + + private Set<K> indicators; + + private Token<K, V> 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<K, V, C> inner, Set<K> chainIndicators, Token<K, V> term) { + iner = inner; + indicators = chainIndicators; + trm = term; + } + + @Override + public CommandResult<K, V> parse(ParserContext<K, V, C> ctx) throws ParserException { + CommandResult<K,V> resOuter = iner.parse(ctx); + if (resOuter.status != Status.SUCCESS) return resOuter; + + Tree<Token<K, V>> expression = resOuter.success(); + Token<K, V> currentToken = ctx.tokens.current(); + if(indicators.contains(currentToken.getKey())) { + Tree<Token<K, V>> res = new SimpleTree<>(trm); + res.addChild(expression); + + while(indicators.contains(currentToken.getKey())) { + res.addChild(new SimpleTree<>(currentToken)); + ctx.tokens.next(); + + CommandResult<K,V> resInner = iner.parse(ctx); + if (resInner.status != Status.SUCCESS) return resInner; + + Tree<Token<K, V>> innerExpression = resInner.success(); + res.addChild(innerExpression); + + currentToken = ctx.tokens.current(); + } + + return CommandResult.success(res); + } + + return resOuter; + } + +} diff --git a/JPratt/src/main/java/com/ashardalon/pratt/blocks/GrammarParseBlock.java b/JPratt/src/main/java/com/ashardalon/pratt/blocks/GrammarParseBlock.java new file mode 100644 index 0000000..048e9f6 --- /dev/null +++ b/JPratt/src/main/java/com/ashardalon/pratt/blocks/GrammarParseBlock.java @@ -0,0 +1,92 @@ +package com.ashardalon.pratt.blocks; + +import java.util.function.Function; + +import com.ashardalon.pratt.ParserContext; +import com.ashardalon.pratt.PrattParser; +import com.ashardalon.pratt.commands.CommandResult; +import com.ashardalon.pratt.tokens.Token; +import com.ashardalon.pratt.tokens.TokenStream; + +import bjc.data.Tree; +import bjc.typeclasses.Isomorphism; +import bjc.data.Tree; +import bjc.functypes.*; +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 final PrattParser<K2, V2, C2> innr; + + private final int prcedence; + private final boolean isStatemnt; + + private final Function<TokenStream<K, V>, TokenStream<K2, V2>> tkenTransform; + private final Isomorphism<C, C2> stteTransform; + private final Function<Tree<Token<K2, V2>>, Tree<Token<K, V>>> xpressionTransform; + + /** + * Create a new grammar parser block. + * + * @param inner The inner grammar to parse. + * @param precedence The precedence of the expression to parse. + * @param isStatement Is the expression being parsed in statement + * context? + * @param tokenTransform Function to transform to the new token type. + * @param stateTransform Function to toggle between state types. + * @param expressionTransform Function to transform back to the normal token + * type. + */ + public GrammarParseBlock(final PrattParser<K2, V2, C2> inner, final int precedence, final boolean isStatement, + final Function<TokenStream<K, V>, TokenStream<K2, V2>> tokenTransform, + final Isomorphism<C, C2> stateTransform, + final Function<Tree<Token<K2, V2>>, Tree<Token<K, V>>> expressionTransform) { + innr = inner; + prcedence = precedence; + isStatemnt = isStatement; + tkenTransform = tokenTransform; + stteTransform = stateTransform; + xpressionTransform = expressionTransform; + } + + @Override + public CommandResult<K, V> parse(final ParserContext<K, V, C> ctx) throws ParserException { + final C2 newState = stteTransform.to(ctx.state); + + final TokenStream<K2, V2> newTokens = tkenTransform.apply(ctx.tokens); + + final CommandResult<K2, V2> res = innr.parseExpression(prcedence, newTokens, newState, isStatemnt); + switch (res.status) { + case SUCCESS: + break; + case FAIL: + return CommandResult.fail(); + case BACKTRACK: + return CommandResult.backtrack(); + default: + throw new IllegalStateException("Unhandled status " + res.status); + } + + Tree<Token<K2, V2>> expression = res.success(); + + ctx.state = stteTransform.from(newState); + + return CommandResult.success(xpressionTransform.apply(expression)); + } +}
\ No newline at end of file diff --git a/JPratt/src/main/java/com/ashardalon/pratt/blocks/ParseBlock.java b/JPratt/src/main/java/com/ashardalon/pratt/blocks/ParseBlock.java new file mode 100644 index 0000000..51aa7fa --- /dev/null +++ b/JPratt/src/main/java/com/ashardalon/pratt/blocks/ParseBlock.java @@ -0,0 +1,38 @@ +package com.ashardalon.pratt.blocks; + +import com.ashardalon.pratt.ParserContext; +import com.ashardalon.pratt.commands.CommandResult; + +import bjc.utils.parserutils.ParserException; + +/** + * Represents a embedded block in an expression. + * + * @author bjculkin + * + * @param <K> + * The key type of the token. + * + * @param <V> + * The value type of the token. + * + * @param <C> + * The state type of the parser. + */ +@FunctionalInterface +public interface ParseBlock<K, V, C> { + + /** + * 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. + */ + CommandResult<K, V> parse(ParserContext<K, V, C> ctx) throws ParserException; +}
\ No newline at end of file diff --git a/JPratt/src/main/java/com/ashardalon/pratt/blocks/ParseBlocks.java b/JPratt/src/main/java/com/ashardalon/pratt/blocks/ParseBlocks.java new file mode 100644 index 0000000..89989fd --- /dev/null +++ b/JPratt/src/main/java/com/ashardalon/pratt/blocks/ParseBlocks.java @@ -0,0 +1,98 @@ +package com.ashardalon.pratt.blocks; + +import java.util.function.Predicate; +import java.util.function.UnaryOperator; + +import com.ashardalon.pratt.tokens.Token; + +import bjc.data.Tree; + +/** + * Utility class for creating common implementations of {@link ParseBlock} + * + * @author bjculkin + * + */ +public class ParseBlocks { + /* + * Grammar parse blocks are complex enough to not get a builder method. + */ + + /** + * Create a new repeating parse block. + * + * @param <K> The key type for the tokens. + * @param <V> The value type for the tokens. + * @param <C> The context type for the tokens. + * + * @param inner + * The parse block to repeat. + * + * @param delim + * The token type that separates repetitions. + * + * @param term + * 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 repetition. + * + * @return A configured repeating parse block. + */ + public static <K, V, C> ParseBlock<K, V, C> repeating(final ParseBlock<K, V, C> inner, final K delim, + final K term, final Token<K, V> mark, final UnaryOperator<C> action) { + return new RepeatingParseBlock<>(inner, delim, term, mark, action); + } + + /** + * Create a new triggered parse block. + * + * @param <K> The key type for the tokens. + * @param <V> The value type for the tokens. + * @param <C> The context type for the tokens. + * + * @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 <K, V, C> ParseBlock<K, V, C> trigger(final ParseBlock<K, V, C> source, + final UnaryOperator<C> onEnter, final UnaryOperator<C> onExit) { + return new TriggeredParseBlock<>(onEnter, onExit, source); + } + + /** + * Create a new simple parse block. + * + * @param <K> The key type for the tokens. + * @param <V> The value type for the tokens. + * @param <C> The context type for the tokens. + * + * @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 <K, V, C> ParseBlock<K, V, C> simple(final int precedence, final K terminator, + final Predicate<Tree<Token<K, V>>> validator) { + return new SimpleParseBlock<>(precedence, validator, terminator); + } +}
\ No newline at end of file diff --git a/JPratt/src/main/java/com/ashardalon/pratt/blocks/RepeatingParseBlock.java b/JPratt/src/main/java/com/ashardalon/pratt/blocks/RepeatingParseBlock.java new file mode 100644 index 0000000..25827b4 --- /dev/null +++ b/JPratt/src/main/java/com/ashardalon/pratt/blocks/RepeatingParseBlock.java @@ -0,0 +1,101 @@ +package com.ashardalon.pratt.blocks; + +import java.util.function.UnaryOperator; + +import com.ashardalon.pratt.ParserContext; +import com.ashardalon.pratt.commands.CommandResult; +import com.ashardalon.pratt.commands.CommandResult.Status; +import com.ashardalon.pratt.tokens.Token; + +import bjc.data.Tree; +import bjc.data.SimpleTree; +import bjc.utils.parserutils.ParserException; + +/** + * A parse block that can parse a sequnce of zero or more occurances of another + * block. + * + * @author bjculkin + * + * @param <K> + * The key type of the tokens. + * + * @param <V> + * The value type of the tokens. + * + * @param <C> + * The state type of the parser. + */ +public class RepeatingParseBlock<K, V, C> implements ParseBlock<K, V, C> { + private final ParseBlock<K, V, C> innerBlock; + + private final K delim; + private final K term; + + private final UnaryOperator<C> onDelim; + + private final Token<K, V> 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(final ParseBlock<K, V, C> inner, final K delimiter, final K terminator, + final Token<K, V> marker, final UnaryOperator<C> action) { + super(); + + if(inner == null) + throw new NullPointerException("Inner block must not be null"); + else if(delimiter == null) + throw new NullPointerException("Delimiter must not be null"); + else if(terminator == null) throw new NullPointerException("Terminator must not be null"); + + innerBlock = inner; + + delim = delimiter; + term = terminator; + + mark = marker; + + onDelim = action; + } + + @Override + public CommandResult<K, V> parse(final ParserContext<K, V, C> ctx) throws ParserException { + final Tree<Token<K, V>> ret = new SimpleTree<>(mark); + + Token<K, V> tok = ctx.tokens.current(); + + while(!tok.getKey().equals(term)) { + final CommandResult<K,V> resKid = innerBlock.parse(ctx); + if (resKid.status != Status.SUCCESS) return resKid; + Tree<Token<K, V>> kid = resKid.success(); + ret.addChild(kid); + + tok = ctx.tokens.current(); + + ctx.tokens.expect(delim, term); + + if(onDelim != null) { + ctx.state = onDelim.apply(ctx.state); + } + } + + return CommandResult.success(ret); + } + +} diff --git a/JPratt/src/main/java/com/ashardalon/pratt/blocks/SimpleParseBlock.java b/JPratt/src/main/java/com/ashardalon/pratt/blocks/SimpleParseBlock.java new file mode 100644 index 0000000..8c4a046 --- /dev/null +++ b/JPratt/src/main/java/com/ashardalon/pratt/blocks/SimpleParseBlock.java @@ -0,0 +1,98 @@ +package com.ashardalon.pratt.blocks; + +import java.util.function.Predicate; + +import com.ashardalon.pratt.ParserContext; +import com.ashardalon.pratt.commands.CommandResult; +import com.ashardalon.pratt.commands.CommandResult.Status; +import com.ashardalon.pratt.tokens.Token; + +import bjc.data.Tree; +import bjc.utils.parserutils.ParserException; + +/** + * Simple implementation of {@link ParseBlock} + * + * @author bjculkin + * + * @param <K> + * The key type of the tokens. + * + * @param <V> + * The value type of the tokens. + * + * @param <C> + * The state type of the parser. + */ +public class SimpleParseBlock<K, V, C> implements ParseBlock<K, V, C> { + private final int pow; + + private final K term; + + private final Predicate<Tree<Token<K, V>>> validatr; + + /** + * Create a new block. + * + * @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. + */ + public SimpleParseBlock(final int precedence, final Predicate<Tree<Token<K, V>>> validator, + final K terminator) { + if(precedence < 0) throw new IllegalArgumentException("Precedence must be non-negative"); + + pow = precedence; + term = terminator; + validatr = validator; + } + + @Override + public CommandResult<K, V> parse(final ParserContext<K, V, C> ctx) throws ParserException { + final CommandResult<K,V> resBlock = ctx.parse.parseExpression(pow, ctx.tokens, ctx.state, false); + if (resBlock.status != Status.SUCCESS) return resBlock; + + Tree<Token<K, V>> res = resBlock.success(); + if(term != null) { + ctx.tokens.expect(term); + } + + if(validatr == null || validatr.test(res)) return CommandResult.success(res); + + // TODO: Figure out the right way to handle error context w/ CommandResult + throw new ParserException("Block failed validation"); + } + + @Override + public int hashCode() { + final int prime = 31; + + int result = 1; + + result = prime * result + pow; + result = prime * result + (term == null ? 0 : term.hashCode()); + + return result; + } + + @Override + public boolean equals(final Object obj) { + if(this == obj) return true; + if(obj == null) return false; + if(!(obj instanceof SimpleParseBlock)) return false; + + final SimpleParseBlock<?, ?, ?> other = (SimpleParseBlock<?, ?, ?>) obj; + + if(pow != other.pow) return false; + + if(term == null) { + if(other.term != null) return false; + } else if(!term.equals(other.term)) return false; + + return true; + } +}
\ No newline at end of file diff --git a/JPratt/src/main/java/com/ashardalon/pratt/blocks/TriggeredParseBlock.java b/JPratt/src/main/java/com/ashardalon/pratt/blocks/TriggeredParseBlock.java new file mode 100644 index 0000000..1aeabdb --- /dev/null +++ b/JPratt/src/main/java/com/ashardalon/pratt/blocks/TriggeredParseBlock.java @@ -0,0 +1,63 @@ +package com.ashardalon.pratt.blocks; + +import java.util.function.UnaryOperator; + +import com.ashardalon.pratt.ParserContext; +import com.ashardalon.pratt.commands.CommandResult; +import com.ashardalon.pratt.commands.CommandResult.Status; + +import bjc.utils.parserutils.ParserException; + +/** + * A parse block that can adjust the state before handling its context. + * + * @author bjculkin + * + * @param <K> + * The key type of the tokens. + * @param <V> + * The value type of the tokens. + * @param <C> + * The state type of the parser. + */ +public class TriggeredParseBlock<K, V, C> implements ParseBlock<K, V, C> { + private final UnaryOperator<C> onEntr; + private final UnaryOperator<C> onExt; + + private final ParseBlock<K, V, C> 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(final UnaryOperator<C> onEnter, final UnaryOperator<C> onExit, + final ParseBlock<K, V, C> source) { + onEntr = onEnter; + onExt = onExit; + sourc = source; + } + + @Override + public CommandResult<K, V> parse(final ParserContext<K, V, C> ctx) throws ParserException { + final C newState = onEntr.apply(ctx.state); + + final ParserContext<K, V, C> newCtx = new ParserContext<>(ctx.tokens, ctx.parse, newState); + + final CommandResult<K,V> res = sourc.parse(newCtx); + + if (res.status != Status.SUCCESS) return res; + + ctx.state = onExt.apply(newState); + + return res; + } + +} |
