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 --- .../pratt/commands/impls/NonInitialCommands.java | 167 +++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 JPratt/src/main/java/com/ashardalon/pratt/commands/impls/NonInitialCommands.java (limited to 'JPratt/src/main/java/com/ashardalon/pratt/commands/impls/NonInitialCommands.java') diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/NonInitialCommands.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/NonInitialCommands.java new file mode 100644 index 0000000..41f0bab --- /dev/null +++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/NonInitialCommands.java @@ -0,0 +1,167 @@ +package com.ashardalon.pratt.commands.impls; + +import java.util.Set; + +import com.ashardalon.pratt.blocks.ParseBlock; +import com.ashardalon.pratt.blocks.SimpleParseBlock; +import com.ashardalon.pratt.commands.NonInitialCommand; +import com.ashardalon.pratt.tokens.Token; + +/** + * Contains factory methods for producing common implementations of + * {@link NonInitialCommand} + * + * @author EVE + * + */ +public class NonInitialCommands { + /** + * Create a left-associative infix operator. + * + * @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 operator. + * + * @return A command implementing that operator. + */ + public static NonInitialCommand infixLeft(final int precedence) { + return new LeftBinaryCommand<>(precedence); + } + + /** + * Create a right-associative infix operator. + * + * @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 operator. + * + * @return A command implementing that operator. + */ + public static NonInitialCommand infixRight(final int precedence) { + return new RightBinaryCommand<>(precedence); + } + + /** + * Create a non-associative infix operator. + * + * @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 operator. + * + * @return A command implementing that operator. + */ + public static NonInitialCommand infixNon(final int precedence) { + return new NonBinaryCommand<>(precedence); + } + + /** + * Create a chained operator. + * + * @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 operator. + * + * @param chainSet + * The operators it forms a chain with. + * + * @param marker + * The token to use as the AST node for the chained operators. + * + * @return A command implementing that operator. + */ + public static NonInitialCommand chain(final int precedence, final Set chainSet, + final Token marker) { + return new ChainCommand<>(precedence, chainSet, marker); + } + + /** + * Create a postfix operator. + * + * @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 operator. + * + * @return A command implementing that operator. + */ + public static NonInitialCommand postfix(final int precedence) { + return new PostfixCommand<>(precedence); + } + + /** + * Create a post-circumfix operator. + * + * This is an operator in form similar to array indexing. + * + * @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 this operator + * + * @param insidePrecedence + * The precedence of the expression inside the operator + * + * @param closer + * The token that closes the circumfix. + * + * @param marker + * The token to use as the AST node for the operator. + * + * @return A command implementing that operator. + */ + public static NonInitialCommand postCircumfix(final int precedence, + final int insidePrecedence, final K closer, final Token marker) { + final ParseBlock innerBlock = new SimpleParseBlock<>(insidePrecedence, null, closer); + + return new PostCircumfixCommand<>(precedence, innerBlock, marker); + } + + /** + * Create a ternary operator. + * + * This is like C's ?: operator. + * + * @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 operator. + * + * @param insidePrecedence + * The precedence of the inner section of the operator. + * + * @param closer + * The token that marks the end of the inner section. + * + * @param marker + * The token to use as the AST node for the operator. + * + * @param nonassoc + * True if the command is non-associative, false otherwise. + * + * @return A command implementing this operator. + */ + public static NonInitialCommand ternary(final int precedence, final int insidePrecedence, + final K closer, final Token marker, final boolean nonassoc) { + final ParseBlock innerBlock = new SimpleParseBlock<>(insidePrecedence, null, closer); + + return new TernaryCommand<>(precedence, innerBlock, marker, nonassoc); + } +} -- cgit v1.2.3