Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/source/TextMap.java @ 13455:69d2e4baa215
Truffle: new infrastructure related to instrumentation, and in particular debugging: support for managing Source objects; framework for generalized "instrumentation proxy nodes" (to be inserted into ASTs with no runtime cost when inactive), and "probes" (which can be attached to proxy nodes to receive event notification); a rudimentary interface and abstract implementation for a "debug manager" (mostly a placeholder at this point); and the beginning of a language-agnostic ExecutionContext interface.
author | Michael Van De Vanter <michael.van.de.vanter@oracle.com> |
---|---|
date | Tue, 17 Dec 2013 20:22:45 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
13306:dfb780080923 | 13455:69d2e4baa215 |
---|---|
1 /* | |
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. Oracle designates this | |
8 * particular file as subject to the "Classpath" exception as provided | |
9 * by Oracle in the LICENSE file that accompanied this code. | |
10 * | |
11 * This code is distributed in the hope that it will be useful, but WITHOUT | |
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 * version 2 for more details (a copy is included in the LICENSE file that | |
15 * accompanied this code). | |
16 * | |
17 * You should have received a copy of the GNU General Public License version | |
18 * 2 along with this work; if not, write to the Free Software Foundation, | |
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
20 * | |
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
22 * or visit www.oracle.com if you need additional information or have any | |
23 * questions. | |
24 */ | |
25 package com.oracle.truffle.api.source; | |
26 | |
27 import java.util.*; | |
28 | |
29 /** | |
30 * A utility for converting between coordinate systems in a string of text interspersed with newline | |
31 * characters. The coordinate systems are: | |
32 * <ul> | |
33 * <li>0-based character offset from the beginning of the text, where newline characters count as a | |
34 * single character and the first character in the text occupies position 0.</li> | |
35 * <li>1-based position in the 2D space of lines and columns, in which the first position in the | |
36 * text is at (1,1).</li> | |
37 * </ul> | |
38 * <p> | |
39 * This utility is based on positions occupied by characters, not text stream positions as in a text | |
40 * editor. The distinction shows up in editors where you can put the cursor just past the last | |
41 * character in a buffer; this is necessary, among other reasons, so that you can put the edit | |
42 * cursor in a new (empty) buffer. For the purposes of this utility, however, there are no character | |
43 * positions in an empty text string and there are no lines in an empty text string. | |
44 * <p> | |
45 * A newline character designates the end of a line and occupies a column position. | |
46 * <p> | |
47 * If the text ends with a character other than a newline, then the characters following the final | |
48 * newline character count as a line, even though not newline-terminated. | |
49 * <p> | |
50 * <strong>Limitations:</strong> | |
51 * <ul> | |
52 * <li>Does not handle multiple character encodings correctly.</li> | |
53 * <li>Treats tabs as occupying 1 column.</li> | |
54 * <li>Does not handle multiple-character line termination sequences correctly.</li> | |
55 * </ul> | |
56 */ | |
57 public final class TextMap { | |
58 | |
59 // 0-based offsets of newline characters in the text, with sentinel | |
60 private final int[] nlOffsets; | |
61 | |
62 // The number of characters in the text, including newlines (which count as 1). | |
63 private final int textLength; | |
64 | |
65 // Is the final text character a newline? | |
66 final boolean finalNL; | |
67 | |
68 /** | |
69 * Constructs map permitting translation between 0-based character offsets and 1-based | |
70 * lines/columns. | |
71 */ | |
72 public TextMap(String text) { | |
73 this.textLength = text.length(); | |
74 final ArrayList<Integer> lines = new ArrayList<>(); | |
75 lines.add(0); | |
76 int offset = 0; | |
77 | |
78 while (offset < text.length()) { | |
79 final int nlIndex = text.indexOf('\n', offset); | |
80 if (nlIndex >= 0) { | |
81 offset = nlIndex + 1; | |
82 lines.add(offset); | |
83 } else { | |
84 break; | |
85 } | |
86 } | |
87 lines.add(Integer.MAX_VALUE); | |
88 | |
89 nlOffsets = new int[lines.size()]; | |
90 for (int line = 0; line < lines.size(); line++) { | |
91 nlOffsets[line] = lines.get(line); | |
92 } | |
93 | |
94 finalNL = textLength > 0 && (textLength == nlOffsets[nlOffsets.length - 2]); | |
95 } | |
96 | |
97 /** | |
98 * Converts 0-based character offset to 1-based number of the line containing the character. | |
99 * | |
100 * @throws IllegalArgumentException if the offset is outside the string. | |
101 */ | |
102 public int offsetToLine(int offset) throws IllegalArgumentException { | |
103 if (offset < 0 || offset >= textLength) { | |
104 throw new IllegalArgumentException("offset out of bounds"); | |
105 } | |
106 int line = 1; | |
107 while (offset >= nlOffsets[line]) { | |
108 line++; | |
109 } | |
110 return line; | |
111 } | |
112 | |
113 /** | |
114 * Converts 0-based character offset to 1-based number of the column occupied by the character. | |
115 * <p> | |
116 * Tabs are not expanded; they occupy 1 column. | |
117 * | |
118 * @throws IllegalArgumentException if the offset is outside the string. | |
119 */ | |
120 public int offsetToCol(int offset) throws IllegalArgumentException { | |
121 return 1 + offset - nlOffsets[offsetToLine(offset) - 1]; | |
122 } | |
123 | |
124 /** | |
125 * The number of lines in the text; if characters appear after the final newline, then they also | |
126 * count as a line, even though not newline-terminated. | |
127 */ | |
128 public int lineCount() { | |
129 if (textLength == 0) { | |
130 return 0; | |
131 } | |
132 return finalNL ? nlOffsets.length - 2 : nlOffsets.length - 1; | |
133 } | |
134 | |
135 /** | |
136 * Converts 1-based line number to the 0-based offset of the line's first character; this would | |
137 * be the offset of a newline if the line is empty. | |
138 * | |
139 * @throws IllegalArgumentException if there is no such line in the text. | |
140 */ | |
141 public int lineStartOffset(int line) throws IllegalArgumentException { | |
142 if (textLength == 0 || lineOutOfRange(line)) { | |
143 throw new IllegalArgumentException("line out of bounds"); | |
144 } | |
145 return nlOffsets[line - 1]; | |
146 } | |
147 | |
148 /** | |
149 * Gets the number of characters in a line, identified by 1-based line number; <em>does not</em> | |
150 * include the final newline, if any. | |
151 * | |
152 * @throws IllegalArgumentException if there is no such line in the text. | |
153 */ | |
154 public int lineLength(int line) throws IllegalArgumentException { | |
155 if (textLength == 0 || lineOutOfRange(line)) { | |
156 throw new IllegalArgumentException("line out of bounds"); | |
157 } | |
158 if (line == nlOffsets.length - 1 && !finalNL) { | |
159 return textLength - nlOffsets[line - 1]; | |
160 } | |
161 return (nlOffsets[line] - nlOffsets[line - 1]) - 1; | |
162 | |
163 } | |
164 | |
165 /** | |
166 * Is the line number out of range. | |
167 */ | |
168 private boolean lineOutOfRange(int line) { | |
169 return line <= 0 || line >= nlOffsets.length || (line == nlOffsets.length - 1 && finalNL); | |
170 } | |
171 | |
172 } |