ParseException.java

1
/*
2
 * Copyright 2006 - 2013
3
 *     Stefan Balev     <stefan.balev@graphstream-project.org>
4
 *     Julien Baudry    <julien.baudry@graphstream-project.org>
5
 *     Antoine Dutot    <antoine.dutot@graphstream-project.org>
6
 *     Yoann Pign��      <yoann.pigne@graphstream-project.org>
7
 *     Guilhelm Savin   <guilhelm.savin@graphstream-project.org>
8
 * 
9
 * This file is part of GraphStream <http://graphstream-project.org>.
10
 * 
11
 * GraphStream is a library whose purpose is to handle static or dynamic
12
 * graph, create them from scratch, file or any source and display them.
13
 * 
14
 * This program is free software distributed under the terms of two licenses, the
15
 * CeCILL-C license that fits European law, and the GNU Lesser General Public
16
 * License. You can  use, modify and/ or redistribute the software under the terms
17
 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
18
 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
19
 * the Free Software Foundation, either version 3 of the License, or (at your
20
 * option) any later version.
21
 * 
22
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
23
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
24
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
25
 * 
26
 * You should have received a copy of the GNU Lesser General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 * 
29
 * The fact that you are presently reading this means that you have had
30
 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
31
 */
32
package org.graphstream.util.parser;
33
34
/**
35
 * This exception is thrown when parse errors are encountered. You can
36
 * explicitly create objects of this exception type by calling the method
37
 * generateParseException in the generated parser.
38
 * 
39
 * You can modify this class to customize your error reporting mechanisms so
40
 * long as you retain the public fields.
41
 */
42
public class ParseException extends Exception {
43
44
	/**
45
	 * The version identifier for this Serializable class. Increment only if the
46
	 * <i>serialized</i> form of the class changes.
47
	 */
48
	private static final long serialVersionUID = 1L;
49
50
	/**
51
	 * This constructor is used by the method "generateParseException" in the
52
	 * generated parser. Calling this constructor generates a new object of this
53
	 * type with the fields "currentToken", "expectedTokenSequences", and
54
	 * "tokenImage" set.
55
	 */
56
	public ParseException(Token currentTokenVal,
57
			int[][] expectedTokenSequencesVal, String[] tokenImageVal) {
58
		super(initialise(currentTokenVal, expectedTokenSequencesVal,
59
				tokenImageVal));
60
		currentToken = currentTokenVal;
61
		expectedTokenSequences = expectedTokenSequencesVal;
62
		tokenImage = tokenImageVal;
63
	}
64
65
	/**
66
	 * The following constructors are for use by you for whatever purpose you
67
	 * can think of. Constructing the exception in this manner makes the
68
	 * exception behave in the normal way - i.e., as documented in the class
69
	 * "Throwable". The fields "errorToken", "expectedTokenSequences", and
70
	 * "tokenImage" do not contain relevant information. The JavaCC generated
71
	 * code does not use these constructors.
72
	 */
73
74
	public ParseException() {
75
		super();
76
	}
77
78
	/** Constructor with message. */
79
	public ParseException(String message) {
80
		super(message);
81
	}
82
83
	/**
84
	 * This is the last token that has been consumed successfully. If this
85
	 * object has been created due to a parse error, the token followng this
86
	 * token will (therefore) be the first error token.
87
	 */
88
	public Token currentToken;
89
90
	/**
91
	 * Each entry in this array is an array of integers. Each array of integers
92
	 * represents a sequence of tokens (by their ordinal values) that is
93
	 * expected at this point of the parse.
94
	 */
95
	public int[][] expectedTokenSequences;
96
97
	/**
98
	 * This is a reference to the "tokenImage" array of the generated parser
99
	 * within which the parse error occurred. This array is defined in the
100
	 * generated ...Constants interface.
101
	 */
102
	public String[] tokenImage;
103
104
	/**
105
	 * It uses "currentToken" and "expectedTokenSequences" to generate a parse
106
	 * error message and returns it. If this object has been created due to a
107
	 * parse error, and you do not catch it (it gets thrown from the parser) the
108
	 * correct error message gets displayed.
109
	 */
110
	private static String initialise(Token currentToken,
111
			int[][] expectedTokenSequences, String[] tokenImage) {
112
		String eol = System.getProperty("line.separator", "\n");
113
		StringBuffer expected = new StringBuffer();
114
		int maxSize = 0;
115 3 1. initialise : changed conditional boundary → NO_COVERAGE
2. initialise : Changed increment from 1 to -1 → NO_COVERAGE
3. initialise : negated conditional → NO_COVERAGE
		for (int i = 0; i < expectedTokenSequences.length; i++) {
116 2 1. initialise : changed conditional boundary → NO_COVERAGE
2. initialise : negated conditional → NO_COVERAGE
			if (maxSize < expectedTokenSequences[i].length) {
117
				maxSize = expectedTokenSequences[i].length;
118
			}
119 3 1. initialise : changed conditional boundary → NO_COVERAGE
2. initialise : Changed increment from 1 to -1 → NO_COVERAGE
3. initialise : negated conditional → NO_COVERAGE
			for (int j = 0; j < expectedTokenSequences[i].length; j++) {
120
				expected.append(tokenImage[expectedTokenSequences[i][j]])
121
						.append(' ');
122
			}
123 2 1. initialise : Replaced integer subtraction with addition → NO_COVERAGE
2. initialise : negated conditional → NO_COVERAGE
			if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
124
				expected.append("...");
125
			}
126
			expected.append(eol).append("    ");
127
		}
128
		String retval = "Encountered \"";
129
		Token tok = currentToken.next;
130 3 1. initialise : changed conditional boundary → NO_COVERAGE
2. initialise : Changed increment from 1 to -1 → NO_COVERAGE
3. initialise : negated conditional → NO_COVERAGE
		for (int i = 0; i < maxSize; i++) {
131 1 1. initialise : negated conditional → NO_COVERAGE
			if (i != 0)
132
				retval += " ";
133 1 1. initialise : negated conditional → NO_COVERAGE
			if (tok.kind == 0) {
134
				retval += tokenImage[0];
135
				break;
136
			}
137
			retval += " " + tokenImage[tok.kind];
138
			retval += " \"";
139
			retval += add_escapes(tok.image);
140
			retval += " \"";
141
			tok = tok.next;
142
		}
143
		retval += "\" at line " + currentToken.next.beginLine + ", column "
144
				+ currentToken.next.beginColumn;
145
		retval += "." + eol;
146 1 1. initialise : negated conditional → NO_COVERAGE
		if (expectedTokenSequences.length == 1) {
147
			retval += "Was expecting:" + eol + "    ";
148
		} else {
149
			retval += "Was expecting one of:" + eol + "    ";
150
		}
151
		retval += expected.toString();
152 1 1. initialise : mutated return of Object value for org/graphstream/util/parser/ParseException::initialise to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
		return retval;
153
	}
154
155
	/**
156
	 * The end of line string for this machine.
157
	 */
158
	protected String eol = System.getProperty("line.separator", "\n");
159
160
	/**
161
	 * Used to convert raw characters to their escaped version when these raw
162
	 * version cannot be used as part of an ASCII string literal.
163
	 */
164
	static String add_escapes(String str) {
165
		StringBuffer retval = new StringBuffer();
166
		char ch;
167 3 1. add_escapes : changed conditional boundary → NO_COVERAGE
2. add_escapes : Changed increment from 1 to -1 → NO_COVERAGE
3. add_escapes : negated conditional → NO_COVERAGE
		for (int i = 0; i < str.length(); i++) {
168
			switch (str.charAt(i)) {
169
			case 0:
170
				continue;
171
			case '\b':
172
				retval.append("\\b");
173
				continue;
174
			case '\t':
175
				retval.append("\\t");
176
				continue;
177
			case '\n':
178
				retval.append("\\n");
179
				continue;
180
			case '\f':
181
				retval.append("\\f");
182
				continue;
183
			case '\r':
184
				retval.append("\\r");
185
				continue;
186
			case '\"':
187
				retval.append("\\\"");
188
				continue;
189
			case '\'':
190
				retval.append("\\\'");
191
				continue;
192
			case '\\':
193
				retval.append("\\\\");
194
				continue;
195
			default:
196 4 1. add_escapes : changed conditional boundary → NO_COVERAGE
2. add_escapes : changed conditional boundary → NO_COVERAGE
3. add_escapes : negated conditional → NO_COVERAGE
4. add_escapes : negated conditional → NO_COVERAGE
				if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
197
					String s = "0000" + Integer.toString(ch, 16);
198
					retval.append("\\u"
199 1 1. add_escapes : Replaced integer subtraction with addition → NO_COVERAGE
							+ s.substring(s.length() - 4, s.length()));
200
				} else {
201
					retval.append(ch);
202
				}
203
				continue;
204
			}
205
		}
206 1 1. add_escapes : mutated return of Object value for org/graphstream/util/parser/ParseException::add_escapes to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
		return retval.toString();
207
	}
208
209
}
210
/*
211
 * JavaCC - OriginalChecksum=9046b556c7577c2f7d673183ae801879 (do not edit this
212
 * line)
213
 */

Mutations

115

1.1
Location : initialise
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : initialise
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

3.3
Location : initialise
Killed by : none
negated conditional → NO_COVERAGE

116

1.1
Location : initialise
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : initialise
Killed by : none
negated conditional → NO_COVERAGE

119

1.1
Location : initialise
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : initialise
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

3.3
Location : initialise
Killed by : none
negated conditional → NO_COVERAGE

123

1.1
Location : initialise
Killed by : none
Replaced integer subtraction with addition → NO_COVERAGE

2.2
Location : initialise
Killed by : none
negated conditional → NO_COVERAGE

130

1.1
Location : initialise
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : initialise
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

3.3
Location : initialise
Killed by : none
negated conditional → NO_COVERAGE

131

1.1
Location : initialise
Killed by : none
negated conditional → NO_COVERAGE

133

1.1
Location : initialise
Killed by : none
negated conditional → NO_COVERAGE

146

1.1
Location : initialise
Killed by : none
negated conditional → NO_COVERAGE

152

1.1
Location : initialise
Killed by : none
mutated return of Object value for org/graphstream/util/parser/ParseException::initialise to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

167

1.1
Location : add_escapes
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : add_escapes
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

3.3
Location : add_escapes
Killed by : none
negated conditional → NO_COVERAGE

196

1.1
Location : add_escapes
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : add_escapes
Killed by : none
changed conditional boundary → NO_COVERAGE

3.3
Location : add_escapes
Killed by : none
negated conditional → NO_COVERAGE

4.4
Location : add_escapes
Killed by : none
negated conditional → NO_COVERAGE

199

1.1
Location : add_escapes
Killed by : none
Replaced integer subtraction with addition → NO_COVERAGE

206

1.1
Location : add_escapes
Killed by : none
mutated return of Object value for org/graphstream/util/parser/ParseException::add_escapes to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 0.33