SourcePositions.txt 12 KB
Newer Older
dgelessus's avatar
dgelessus committed
1
/*
hansen's avatar
hansen committed
2
 * (c) 2009 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
Jens Bendisposto's avatar
Jens Bendisposto committed
3
 * Heinrich Heine Universitaet Duesseldorf
hansen's avatar
hansen committed
4
 * This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
Jens Bendisposto's avatar
Jens Bendisposto committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 * */

package de.hhu.stups.sablecc.patch;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class SourcePositions {
	private final List<IToken> tokenList;
	private final Map<PositionedNode, SourcecodeRange> positions;

	public SourcePositions(final List<IToken> tokenList,
			final Map<PositionedNode, SourcecodeRange> positions) {
		this.tokenList = tokenList;
		this.positions = positions;
	}

	/**
	 * Returns the {@link SourcecodeRange} of this {@link PositionedNode} or
dgelessus's avatar
dgelessus committed
25
	 * {@code null} if no {@link SourcecodeRange} is available.
hansen's avatar
hansen committed
26
27
	 *
	 * @param node the node with source code information
dgelessus's avatar
dgelessus committed
28
	 * @return the source code range of {@code node}
Jens Bendisposto's avatar
Jens Bendisposto committed
29
30
31
32
33
34
35
	 */
	public SourcecodeRange getSourcecodeRange(final PositionedNode node) {
		return positions.get(node);
	}

	/**
	 * Returns the line in which this {@link PositionedNode} begins. The value
dgelessus's avatar
dgelessus committed
36
	 * {@code 0} is returned if no sourcecode range is available for this
Jens Bendisposto's avatar
Jens Bendisposto committed
37
	 * {@link PositionedNode}.
hansen's avatar
hansen committed
38
39
	 *
	 * @param node the node with source code information
dgelessus's avatar
dgelessus committed
40
	 * @return the line where the {@code node} starts
Jens Bendisposto's avatar
Jens Bendisposto committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
	 */
	public int getBeginLine(final PositionedNode node) {
		if (node instanceof IToken) {
			return ((IToken) node).getLine();
		}

		return getBeginLine(getSourcecodeRange(node));
	}

	public int getBeginLine(final SourcecodeRange range) {
		if (range != null) {
			return tokenList.get(range.getBeginIndex()).getLine();
		} else {
			return 0;
		}
	}

	/**
	 * Returns the column of the first character of this {@link PositionedNode},
dgelessus's avatar
dgelessus committed
60
	 * i.e. the begin column. The value {@code 0} is returned if no
Jens Bendisposto's avatar
Jens Bendisposto committed
61
	 * sourcecode range is available for this {@link PositionedNode}.
hansen's avatar
hansen committed
62
63
	 *
	 * @param node	the node with source code information
dgelessus's avatar
dgelessus committed
64
	 * @return	the begin column of {@code node}
Jens Bendisposto's avatar
Jens Bendisposto committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
	 */
	public int getBeginColumn(final PositionedNode node) {
		if (node instanceof IToken) {
			return ((IToken) node).getPos();
		}

		return getBeginColumn(getSourcecodeRange(node));
	}

	public int getBeginColumn(final SourcecodeRange range) {
		if (range != null) {
			return tokenList.get(range.getBeginIndex()).getPos();
		} else {
			return 0;
		}
	}

	/**
	 * Returns the line in which the {@link PositionedNode} ends. The value
dgelessus's avatar
dgelessus committed
84
	 * {@code 0} is returned if no sourcecode range is available for this
Jens Bendisposto's avatar
Jens Bendisposto committed
85
	 * {@link PositionedNode}.
hansen's avatar
hansen committed
86
87
88
	 *
	 * @param node	the node with source code information
	 * @return	the end line of <code>node</code>
Jens Bendisposto's avatar
Jens Bendisposto committed
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
	 */
	public int getEndLine(final PositionedNode node) {
		// TODO handle multi line comments
		/*
		 * if (node instanceof TComment) { final TComment comment = (TComment)
		 * node; return comment.getLine() + countLineBreaks(comment); }
		 */

		if (node instanceof IToken) {
			return ((IToken) node).getLine();
		}

		return getEndLine(getSourcecodeRange(node));
	}

	private int getEndLine(final SourcecodeRange range) {
		if (range != null) {
			return tokenList.get(range.getEndIndex()).getLine();
		} else {
			return 0;
		}
	}

	// private int countLineBreaks(final PositionedNode node) {
	// final char[] text = getNodeString(node).toCharArray();
	// int count = 0;
	//
	// for (int i = 0; i < text.length; i++) {
	// if (text[i] == '\n' || text[i] == '\r') {
	// count++;
	// }
	// }
	//
	// return count;
	// }

	/**
	 * Returns the last column of this {@link PositionedNode}, i.e. the column
	 * of the last character of the {@link PositionedNode}. The value
dgelessus's avatar
dgelessus committed
128
	 * {@code 0} is returned if no sourcecode range is available for this
Jens Bendisposto's avatar
Jens Bendisposto committed
129
	 * {@link PositionedNode}.
hansen's avatar
hansen committed
130
131
132
	 *
	 * @param node	the node with source code information
	 * @return	the end column of <code>node</code>
Jens Bendisposto's avatar
Jens Bendisposto committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
	 */
	public int getEndColumn(final PositionedNode node) {
		// TODO handle multi line comments
		/*
		 * if (node instanceof TComment) { return getEndColumn((TComment) node);
		 * }
		 */

		if (node instanceof IToken) {
			final IToken token = (IToken) node;
			return token.getPos() + token.getText().length() - 1;
		}

		return getEndColumn(getSourcecodeRange(node));
	}

	private int getEndColumn(final SourcecodeRange range) {
		if (range != null) {
			final IToken token = tokenList.get(range.getEndIndex());
			return token.getPos() + token.getText().length() - 1;
		} else {
			return 0;
		}
	}

	/*
	 * private int getEndColumn(final TComment commentToken) { final String
	 * asString = commentToken.getText(); final StringTokenizer tokenizer = new
	 * StringTokenizer(asString, "\n\r");
hansen's avatar
hansen committed
162
	 *
Jens Bendisposto's avatar
Jens Bendisposto committed
163
164
	 * // multi line comment if (tokenizer.countTokens() > 1) { String line =
	 * null; while (tokenizer.hasMoreTokens()) { line = tokenizer.nextToken(); }
hansen's avatar
hansen committed
165
	 *
Jens Bendisposto's avatar
Jens Bendisposto committed
166
	 * if (line == null) { return 0; }
hansen's avatar
hansen committed
167
	 *
Jens Bendisposto's avatar
Jens Bendisposto committed
168
169
170
171
172
173
174
175
	 * return line.length(); } // single line comment else { return
	 * commentToken.getPos() + asString.length(); } }
	 */

	/**
	 * Returns the array of {@link IToken}s belonging to this
	 * {@link PositionedNode}. The array may be empty, if no sourcecode range
	 * can be determined for this {@link PositionedNode}.
hansen's avatar
hansen committed
176
177
	 *
	 * @param node	the node with source code information
dgelessus's avatar
dgelessus committed
178
	 * @return	all tokens of {@code node}
Jens Bendisposto's avatar
Jens Bendisposto committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
	 */
	public IToken[] getTokens(final PositionedNode node) {
		if (node instanceof IToken) {
			return new IToken[] { (IToken) node };
		}

		final SourcecodeRange range = getSourcecodeRange(node);

		if (range != null) {
			final int beginIndex = range.getBeginIndex();
			final int endIndex = range.getEndIndex();
			final IToken[] result = new IToken[endIndex - beginIndex + 1];

			for (int i = 0; i + beginIndex <= endIndex; i++) {
				result[i] = tokenList.get(i + beginIndex);
			}

			return result;
		} else {
			return new IToken[0];
		}
	}

	public String getNodeString(final PositionedNode node) {
		// TODO handle comments
		/*
		 * if (node instanceof TComment) { return ((TComment) node).getText(); }
		 */

		return getRangeString(getSourcecodeRange(node));
	}

	public String getRangeString(final SourcecodeRange range) {
		final StringBuilder buffer = new StringBuilder();

		if (range != null) {
			final int beginIndex = range.getBeginIndex();
			final int endIndex = range.getEndIndex();

			for (int i = beginIndex; i <= endIndex; i++) {
				buffer.append(tokenList.get(i).getText());
			}
		}

		return buffer.toString();
	}

	// TODO handle comments
	/*
hansen's avatar
hansen committed
228
	 *
Jens Bendisposto's avatar
Jens Bendisposto committed
229
230
231
232
233
	 * public IToken getCommentBefore(final PositionedNode node) { final
	 * SourcecodeRange range = getSourcecodeRange(node); final int beginIndex =
	 * range.getBeginIndex(); if (beginIndex > 0) { final IToken token =
	 * tokenList.get(beginIndex - 1); if (token instanceof TComment) { return
	 * token; } else { return null; } } else { return null; } }
hansen's avatar
hansen committed
234
	 *
Jens Bendisposto's avatar
Jens Bendisposto committed
235
236
237
238
239
240
241
	 * public IToken getCommentAfter(final PositionedNode node) { final
	 * SourcecodeRange range = getSourcecodeRange(node); final int endIndex =
	 * range.getEndIndex(); if (endIndex < tokenList.size() - 1) { final IToken
	 * token = tokenList.get(endIndex + 1); if (token instanceof TComment) {
	 * return token; } else { return null; } } else { return null; } } public
	 * IToken[] getIncludedComments(final PositionedNode node) { final
	 * SourcecodeRange range = getSourcecodeRange(node);
hansen's avatar
hansen committed
242
	 *
Jens Bendisposto's avatar
Jens Bendisposto committed
243
244
245
	 * if (range != null) { final int beginIndex = range.getBeginIndex(); final
	 * int endIndex = range.getEndIndex(); final List<IToken> comments = new
	 * ArrayList<IToken>();
hansen's avatar
hansen committed
246
	 *
Jens Bendisposto's avatar
Jens Bendisposto committed
247
248
249
	 * for (int i = 0; i + beginIndex <= endIndex; i++) { final IToken token =
	 * tokenList.get(i + beginIndex); if (token instanceof TComment) {
	 * comments.add(token); } }
hansen's avatar
hansen committed
250
	 *
Jens Bendisposto's avatar
Jens Bendisposto committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
	 * return comments.toArray(new IToken[comments.size()]); } else { return new
	 * IToken[0]; } }
	 */

	public List<IToken> getTokenList() {
		return tokenList;
	}

	public void replaceMapping(final PositionedNode origNode,
			final PositionedNode newNode) {
		final SourcecodeRange sourcecodeRange = positions.remove(origNode);

		if (sourcecodeRange != null) {
			positions.put(newNode, sourcecodeRange);
		}
	}

	public PositionedNode getSurroundingNode(final int index) {
		if (index < 0 || index >= tokenList.size()) {
			return null;
		}

		PositionedNode bestNode = null;
		int bestBeginIndex = 0;
		int bestEndIndex = tokenList.size() - 1;

		// TODO find better solution than searching all?
		for (final Iterator<PositionedNode> iterator = positions.keySet()
				.iterator(); iterator.hasNext();) {
			final PositionedNode node = iterator.next();
			final SourcecodeRange range = positions.get(node);

			final int beginIndex = range.getBeginIndex();
			final int endIndex = range.getEndIndex();

			if (beginIndex <= index && endIndex >= index
					&& beginIndex >= bestBeginIndex && endIndex <= bestEndIndex) {
				bestNode = node;
				bestBeginIndex = beginIndex;
				bestEndIndex = endIndex;
			}
		}

		return bestNode;
	}

	/**
	 * <p>
	 * Finds the index of the token that belongs to the position.
	 * </p>
	 * <p>
dgelessus's avatar
dgelessus committed
302
303
304
	 * If no token list is available {@code -1} is returned. For
	 * {@code line < 0} the index {@code 0} is returned. If
	 * {@code line >= 1 && column < 0} the line number is returned.
Jens Bendisposto's avatar
Jens Bendisposto committed
305
306
307
308
309
310
311
312
313
314
	 * </p>
	 * <p>
	 * If the line matches but the requested column is beyond the max. length of
	 * the line, the last token of this line is returned. The last token of all
	 * tokens (EOF) is chosen if the requested positions is beyond the
	 * absolutely last token.
	 * </p>
	 * <p>
	 * <b>Attention</b>: Line and column counting starts at 1!
	 * </p>
hansen's avatar
hansen committed
315
	 *
Jens Bendisposto's avatar
Jens Bendisposto committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
	 * @param line
	 *            line of the position
	 * @param column
	 *            column of the position
	 * @return Index in {@link #tokenList}
	 */
	public int getTokenforPosition(final int line, final int column) {
		// Sort out nonsense input
		if (tokenList.size() == 0) {
			return -1;
		}
		if (line < 1) {
			return 0;
		}
		if (column < 1) {
			return line;
		}

		/*
		 * Shortcut for special case: Position beyond last token
		 */
		final IToken lastToken = tokenList.get(tokenList.size() - 1);
		if (line > lastToken.getLine()
				|| (line == lastToken.getLine() && column > lastToken.getPos()
						+ lastToken.getText().length())) {
			return tokenList.size() - 1;
		}

		int result = -1;
		int left = 0;
		int right = tokenList.size() - 1;

		while (left <= right && result < 0) {
			if (left != right) {
				final int currentIndex = left + (right - left) / 2;

				final int lineDiff = line
						- tokenList.get(currentIndex).getLine();

				if (lineDiff > 0) {
					// continue in right half
					left = Math.min(currentIndex + 1, right);
				} else if (lineDiff < 0) {
					// continue in left half
					right = Math.max(currentIndex - 1, left);
				} else {
					// we are in the correct line now, switch to linear search
					IToken token = tokenList.get(currentIndex);
					result = currentIndex;

					final int compare = compareTokenColumn(token, column);

					// move left
					if (compare < 0) {
						while (compareTokenColumn(token, column) < 0) {
							result--;

							if (result < 0) {
								break;
							}

							token = tokenList.get(result);
						}
					}
					// move right
					else if (compare > 0) {
						while (compareTokenColumn(token, column) > 0) {
							result++;

							if (result > tokenList.size() - 1) {
								result = tokenList.size() - 1;
								break;
							}

							token = tokenList.get(result);

							/*
							 * Only move as long as line end is not reached.
							 * This happens when queried for column beyond max
							 * column of this line. Then we return the index of
							 * the last token in this line.
							 */
							if (token.getLine() > line) {
								result--;
								break;
							}
						}
					}
				}
			} else {
				result = left;
			}
		}

		return result;
	}

	/**
	 * Compares the token position (within line only) with the column.
hansen's avatar
hansen committed
415
	 *
dgelessus's avatar
dgelessus committed
416
417
418
	 * @return {@code -1} if {@code column < beginColumn},
	 *         {@code 1} if {@code endColumn < column} or
	 *         {@code 0} if the column is within the range of the token.
Jens Bendisposto's avatar
Jens Bendisposto committed
419
420
421
422
423
424
425
426
427
428
429
430
431
432
	 */
	private int compareTokenColumn(final IToken token, final int column) {
		final int beginColumn = token.getPos();
		final int endColumn = beginColumn + token.getText().length() - 1;

		if (column < beginColumn) {
			return -1;
		} else if (endColumn < column) {
			return 1;
		} else {
			return 0;
		}
	}
}