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/commands/CommandResult.java | 113 +++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 JPratt/src/main/java/com/ashardalon/pratt/commands/CommandResult.java (limited to 'JPratt/src/main/java/com/ashardalon/pratt/commands/CommandResult.java') diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/CommandResult.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/CommandResult.java new file mode 100644 index 0000000..45c1788 --- /dev/null +++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/CommandResult.java @@ -0,0 +1,113 @@ +package com.ashardalon.pratt.commands; + +import com.ashardalon.pratt.tokens.Token; + +import bjc.data.Tree; + +/** + * Represents the result of executing a command. + * + * @author bjcul + * + * @param The key type of the tokens + * @param The value type of the tokens + */ +public class CommandResult { + /** + * Represents the status of a command execution + * + * @author bjcul + * + */ + public static enum Status { + /** + * The command successfully parsed. + */ + SUCCESS, + /** + * The command failed, in a non-recoverable way + */ + FAIL, + /** + * The command failed. Attempt recovery via backtracking + */ + BACKTRACK + } + + /** + * The status of this command. + */ + public final Status status; + + private Tree> success; + + private CommandResult(Status status) { + this.status = status; + } + + /** + * Get the success value of this command, or null if it failed. + * + * @return The success value of the command + */ + public Tree> success() { + return success; + } + + /** + * Create a success result + * + * @param The key type of the token + * @param The value type of the token + * + * @param succ The tree produced by the command + * + * @return A command result representing a success + */ + public static CommandResult success(Tree> succ) { + CommandResult result = new CommandResult<>(Status.SUCCESS); + result.success = succ; + return result; + } + + /** + * Create a non-backtracking failure result. + * + * @param The key type of the token + * @param The value type of the token + * + * @return A command result representing a non-backtracking fail + */ + public static CommandResult fail() { + CommandResult result = new CommandResult<>(Status.FAIL); + return result; + } + + /** + * Create a backtracking failure result. + * + * @param The key type of the token + * @param The value type of the token + * + * @return A command result representing a backtracking fail + */ + public static CommandResult backtrack() { + CommandResult result = new CommandResult<>(Status.BACKTRACK); + return result; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("CommandResult [status="); + builder.append(status); + if (status == Status.SUCCESS) { + builder.append(", success="); + builder.append(success); + } + builder.append("]"); + return builder.toString(); + } + + +} -- cgit v1.2.3