summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/dicelang/scl/Errors.java
blob: ceb79e32a616a3b3c1a7873e6297a8f4acec5389 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package bjc.dicelang.scl;

import java.util.Arrays;

/**
 * Repository for error messages.
 *
 *
 * @author EVE
 */
/*
 * @TODO 10/08/17 Ben Culkin :ErrorRefactor
 * 
 * This way of handling error messages is not easy to deal with. Something else
 * needs to be done, but I'm not sure what at the moment.
 *
 * ADDENDA: 5/28/18 Ben Culkin
 * 	The error messages were moved into ErrorKey, as well as checking how
 * 	many arguments they expect. This is better than it was, but still mildly
 * 	annoying.
 *
 */
public class Errors {
	/**
	 * The types of error/warning message.
	 *
	 * Error messages are marked by starting with EK, warnings start with WK
	 *
	 * @author EVE
	 *
	 */
	public static enum ErrorKey {
		/* Stream Errors */
		/**
		 * Attempted to switch to a non-existant stream
		 */
		EK_STRM_NONEX("\tERROR: Fell off the stream list, attempting to move %s", 1),
		/**
		 * Can't delete the last stream
		 */
		EK_STRM_LAST("\tERROR: Cannot delete last remaining stream", 0),
		/**
		 * Unknown stream command
		 */
		EK_STRM_INVCOM("\tERROR: Unknown stream control command %s\n", 1),

		/* SCL Warnings */
		/**
		 * Word execution failed.
		 */
		WK_SCL_WRDFAIL("\tWARNING: Execution of word %s failed\n", 1),

		/* SCL Errors */
		/**
		 * Unknown SCL token
		 */
		EK_SCL_INVTOKEN("\tERROR: Unknown SCL token %s\n", 1),
		/**
		 * Mismatched quote in SCL command
		 */
		EK_SCL_MMQUOTE("\tERROR: Mismatched delimiter %s in SCL command\n", 1),
		/**
		 * Stack underflow in SCL command
		 */
		EK_SCL_SUNDERFLOW("\tERROR: Not enough items in stack for word %s (need at least %d)\n", 2),
		/**
		 * Unknown word in SCL command
		 */
		EK_SCL_UNWORD("\tERROR: Unknown word %s\n", 1),
		/**
		 * Invalid argument to SCL command
		 */
		EK_SCL_INVARG("\tERROR: Invalid argument to SCL command\n", 0);

		/**
		 * The message of the error.
		 */
		public final String msg;
		/**
		 * The number of arguments to the error.
		 */
		public final int argc;

		private ErrorKey(String message, int argcount) {
			msg = message;

			argc = argcount;
		}
	}
	/**
	 * The mode for the type of error messages to print out.
	 *
	 * @author EVE
	 *
	 */
	public static enum ErrorMode {
		/**
		 * Output error messages for wizards.
		 */
		WIZARD,
		/**
		 * Output error messages for developers.
		 */
		DEV
	}

	private ErrorMode mode;

	/**
	 * Print an error.
	 *
	 * @param key
	 *        The key of the error.
	 *
	 * @param args
	 *        The arguments for the error.
	 */
	public void printError(final ErrorKey key, final Object... args) {
		switch(mode) {
		case WIZARD:
			System.out.printf("\t? %d %s\n", key.ordinal(), Arrays.deepToString(args));
			break;
		case DEV:
			devError(key, args);
			break;
		default:
			System.out.printf("\tERROR ERROR: Unknown error mode %s\n", mode);
		}
	}

	private static void devError(final ErrorKey key, final Object[] args) {
		if(args.length != key.argc) {
			System.out.printf("\tERROR ERROR: Incorrect # of format arguments (got %d, expected %d)\n", args.length, key.argc);

			return;
		}

		System.out.printf(key.msg, args);
	}

	/**
	 * The instance of the errors.
	 */
	public final static Errors inst;

	static {
		inst = new Errors();

		inst.mode = ErrorMode.DEV;
	}
}