summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/everge/ReplError.java
blob: f9a30a955086d347ece6778089786513e7097408 (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
package bjc.everge;

import bjc.data.IntHolder;

/**
 * Represents an error encountered parsing ReplPairs
 *
 * @author Ben Culkin
 */
public class ReplError {
	/**
	 * The line the error occurred on.
	 */
	public int line;
	/**
	 * The number of pairs we have processed so far.
	 */
	public int numPairs;

	/**
	 * The text of the line we errored on.
	 */
	public String txt;
	/**
	 * The message of the error.
	 */
	public String msg;

	/**
	 * Create a new ReplPair parse error.
	 *
	 * @param lne
	 *               The line the error occured on.
	 * @param nPairs
	 *               The number of pairs processed up to this point.
	 * @param msg
	 *               The message detailing the error.
	 * @param txt
	 *               The text that caused the error.
	 */
	public ReplError(IntHolder lne, IntHolder nPairs, String msg, String txt) {
		this(lne.get(), nPairs.get(), msg, txt);
	}

	/**
	 * Create a new ReplPair parse error.
	 *
	 * @param lne
	 *               The line the error occured on.
	 * @param nPairs
	 *               The number of pairs processed up to this point.
	 * @param msg
	 *               The message detailing the error.
	 * @param txt
	 *               The text that caused the error.
	 */
	public ReplError(int lne, int nPairs, String msg, String txt) {
		line = lne;
		numPairs = nPairs;

		this.txt = txt;
		this.msg = msg;
	}

	@Override
	public String toString() {
		String errString;
		if (txt == null)
			errString = "No associated line";
		else if (txt.equals(""))
			errString = "Text of line was empty";
		else
			errString = "Text of line was: " + txt;

		return String.format("line %d, pair %d:%s\n\t%s", line, numPairs, msg, errString);
	}

	/**
	 * Convert the error to a printable string.
	 *
	 * @return The error as a printable string.
	 */
	public String toPrintString() {
		return toPrintString("");
	}

	/**
	 * Convert the error to a printable string, with a custom header.
	 *
	 * @param hdr
	 *            The text to include in the header.
	 *
	 * @return The error as a printable string.
	 */
	public String toPrintString(String hdr) {
		String errString;
		if (txt == null)
			errString = "No associated line";
		else if (txt.equals(""))
			errString = "Text of line was empty";
		else
			errString = "Text of line was: " + txt;

		return String.format("[ERROR] line %d, pair %d: %s\n%s\tContext: %s", line,
				numPairs, msg, hdr, errString);
	}
}