From 15a2b29e48f134bc93cfd0a3d8512001e9242f3d Mon Sep 17 00:00:00 2001 From: Benjamin Culkin Date: Mon, 3 Jun 2024 17:33:53 -0400 Subject: Rename package to new domain Rename the package to the new domain --- .../ashardalon/pratt/blocks/ChainParseBlock.java | 86 ++++++++++++++++++ .../ashardalon/pratt/blocks/GrammarParseBlock.java | 92 +++++++++++++++++++ .../com/ashardalon/pratt/blocks/ParseBlock.java | 38 ++++++++ .../com/ashardalon/pratt/blocks/ParseBlocks.java | 98 ++++++++++++++++++++ .../pratt/blocks/RepeatingParseBlock.java | 101 +++++++++++++++++++++ .../ashardalon/pratt/blocks/SimpleParseBlock.java | 98 ++++++++++++++++++++ .../pratt/blocks/TriggeredParseBlock.java | 63 +++++++++++++ 7 files changed, 576 insertions(+) create mode 100644 JPratt/src/main/java/com/ashardalon/pratt/blocks/ChainParseBlock.java create mode 100644 JPratt/src/main/java/com/ashardalon/pratt/blocks/GrammarParseBlock.java create mode 100644 JPratt/src/main/java/com/ashardalon/pratt/blocks/ParseBlock.java create mode 100644 JPratt/src/main/java/com/ashardalon/pratt/blocks/ParseBlocks.java create mode 100644 JPratt/src/main/java/com/ashardalon/pratt/blocks/RepeatingParseBlock.java create mode 100644 JPratt/src/main/java/com/ashardalon/pratt/blocks/SimpleParseBlock.java create mode 100644 JPratt/src/main/java/com/ashardalon/pratt/blocks/TriggeredParseBlock.java (limited to 'JPratt/src/main/java/com/ashardalon/pratt/blocks') 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 + * 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 CommandResult parse(ParserContext ctx) throws ParserException { + CommandResult resOuter = iner.parse(ctx); + if (resOuter.status != Status.SUCCESS) return resOuter; + + Tree> expression = resOuter.success(); + Token currentToken = ctx.tokens.current(); + if(indicators.contains(currentToken.getKey())) { + Tree> res = new SimpleTree<>(trm); + res.addChild(expression); + + while(indicators.contains(currentToken.getKey())) { + res.addChild(new SimpleTree<>(currentToken)); + ctx.tokens.next(); + + CommandResult resInner = iner.parse(ctx); + if (resInner.status != Status.SUCCESS) return resInner; + + Tree> 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 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 final PrattParser innr; + + private final int prcedence; + private final boolean isStatemnt; + + private final Function, TokenStream> tkenTransform; + private final Isomorphism stteTransform; + private final Function>, Tree>> 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 inner, final int precedence, final boolean isStatement, + final Function, TokenStream> tokenTransform, + final Isomorphism stateTransform, + final Function>, Tree>> expressionTransform) { + innr = inner; + prcedence = precedence; + isStatemnt = isStatement; + tkenTransform = tokenTransform; + stteTransform = stateTransform; + xpressionTransform = expressionTransform; + } + + @Override + public CommandResult parse(final ParserContext ctx) throws ParserException { + final C2 newState = stteTransform.to(ctx.state); + + final TokenStream newTokens = tkenTransform.apply(ctx.tokens); + + final CommandResult 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> 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 + * 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. + */ + CommandResult parse(ParserContext 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 The key type for the tokens. + * @param The value type for the tokens. + * @param 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 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 The key type for the tokens. + * @param The value type for the tokens. + * @param 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 ParseBlock trigger(final ParseBlock source, + final UnaryOperator onEnter, final UnaryOperator onExit) { + return new TriggeredParseBlock<>(onEnter, onExit, source); + } + + /** + * Create a new simple parse block. + * + * @param The key type for the tokens. + * @param The value type for the tokens. + * @param 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 ParseBlock simple(final int precedence, final K terminator, + final Predicate>> 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 + * 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 final ParseBlock innerBlock; + + private final K delim; + private final K term; + + private final UnaryOperator onDelim; + + 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(final ParseBlock inner, final K delimiter, final K terminator, + final Token marker, final UnaryOperator 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 parse(final ParserContext ctx) throws ParserException { + final Tree> ret = new SimpleTree<>(mark); + + Token tok = ctx.tokens.current(); + + while(!tok.getKey().equals(term)) { + final CommandResult resKid = innerBlock.parse(ctx); + if (resKid.status != Status.SUCCESS) return resKid; + Tree> 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 + * 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 final int pow; + + private final K term; + + private final Predicate>> 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>> validator, + final K terminator) { + if(precedence < 0) throw new IllegalArgumentException("Precedence must be non-negative"); + + pow = precedence; + term = terminator; + validatr = validator; + } + + @Override + public CommandResult parse(final ParserContext ctx) throws ParserException { + final CommandResult resBlock = ctx.parse.parseExpression(pow, ctx.tokens, ctx.state, false); + if (resBlock.status != Status.SUCCESS) return resBlock; + + Tree> 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 + * The key type of the tokens. + * @param + * The value type of the tokens. + * @param + * The state type of the parser. + */ +public class TriggeredParseBlock implements ParseBlock { + private final UnaryOperator onEntr; + private final UnaryOperator onExt; + + 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(final UnaryOperator onEnter, final UnaryOperator onExit, + final ParseBlock source) { + onEntr = onEnter; + onExt = onExit; + sourc = source; + } + + @Override + public CommandResult parse(final ParserContext ctx) throws ParserException { + final C newState = onEntr.apply(ctx.state); + + final ParserContext newCtx = new ParserContext<>(ctx.tokens, ctx.parse, newState); + + final CommandResult res = sourc.parse(newCtx); + + if (res.status != Status.SUCCESS) return res; + + ctx.state = onExt.apply(newState); + + return res; + } + +} -- cgit v1.2.3