author | rfield |
Wed, 08 Jun 2016 00:32:31 -0700 | |
changeset 38908 | f0c186d76c8a |
parent 38835 | 37280d52d723 |
child 41940 | 048d559e9da7 |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
2 |
* Copyright (c) 2015, 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 |
*/ |
|
34752
9c262a013456
8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents:
33362
diff
changeset
|
25 |
|
33362 | 26 |
package jdk.jshell; |
27 |
||
28 |
import java.util.Set; |
|
29 |
import java.util.stream.Collectors; |
|
30 |
import java.util.stream.Stream; |
|
31 |
||
32 |
/** |
|
33 |
* Within a String, mask code comments and ignored modifiers (within context). |
|
34 |
* |
|
35 |
* @author Robert Field |
|
36 |
*/ |
|
37 |
class MaskCommentsAndModifiers { |
|
38 |
||
39 |
private final static Set<String> IGNORED_MODIFERS = |
|
40 |
Stream.of( "public", "protected", "private", "static", "final" ) |
|
41 |
.collect( Collectors.toSet() ); |
|
42 |
||
38835
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
43 |
// Builder to accumulate non-masked characters |
33362 | 44 |
private final StringBuilder sbCleared = new StringBuilder(); |
38835
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
45 |
|
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
46 |
// Builder to accumulate masked characters |
33362 | 47 |
private final StringBuilder sbMask = new StringBuilder(); |
38835
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
48 |
|
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
49 |
// The input string |
33362 | 50 |
private final String str; |
38835
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
51 |
|
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
52 |
// Entire input string length |
33362 | 53 |
private final int length; |
38835
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
54 |
|
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
55 |
// Should leading modifiers be masked away |
33362 | 56 |
private final boolean maskModifiers; |
38835
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
57 |
|
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
58 |
// The next character |
33362 | 59 |
private int next = 0; |
38835
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
60 |
|
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
61 |
// We have past any point where a top-level modifier could be |
33362 | 62 |
private boolean inside = false; |
63 |
||
38835
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
64 |
// Does the string end with an unclosed '/*' style comment? |
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
65 |
private boolean openComment = false; |
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
66 |
|
33362 | 67 |
@SuppressWarnings("empty-statement") |
38835
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
68 |
MaskCommentsAndModifiers(String s, boolean maskModifiers) { |
33362 | 69 |
this.str = s; |
70 |
this.length = s.length(); |
|
71 |
this.maskModifiers = maskModifiers; |
|
72 |
do { } while (next()); |
|
73 |
} |
|
74 |
||
38835
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
75 |
String cleared() { |
33362 | 76 |
return sbCleared.toString(); |
77 |
} |
|
78 |
||
38835
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
79 |
String mask() { |
33362 | 80 |
return sbMask.toString(); |
81 |
} |
|
82 |
||
38835
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
83 |
boolean endsWithOpenComment() { |
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
84 |
return openComment; |
33362 | 85 |
} |
86 |
||
38835
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
87 |
/****** private implementation methods ******/ |
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
88 |
|
33362 | 89 |
/** |
90 |
* Read the next character |
|
91 |
*/ |
|
92 |
private int read() { |
|
93 |
if (next >= length) { |
|
94 |
return -1; |
|
95 |
} |
|
96 |
return str.charAt(next++); |
|
97 |
} |
|
98 |
||
99 |
private void write(StringBuilder sb, int ch) { |
|
100 |
sb.append((char)ch); |
|
101 |
} |
|
102 |
||
103 |
private void write(int ch) { |
|
104 |
write(sbCleared, ch); |
|
105 |
write(sbMask, Character.isWhitespace(ch) ? ch : ' '); |
|
106 |
} |
|
107 |
||
108 |
private void writeMask(int ch) { |
|
109 |
write(sbMask, ch); |
|
110 |
write(sbCleared, Character.isWhitespace(ch) ? ch : ' '); |
|
111 |
} |
|
112 |
||
113 |
private void write(CharSequence s) { |
|
114 |
for (int cp : s.chars().toArray()) { |
|
115 |
write(cp); |
|
116 |
} |
|
117 |
} |
|
118 |
||
119 |
private void writeMask(CharSequence s) { |
|
120 |
for (int cp : s.chars().toArray()) { |
|
121 |
writeMask(cp); |
|
122 |
} |
|
123 |
} |
|
124 |
||
125 |
private boolean next() { |
|
126 |
return next(read()); |
|
127 |
} |
|
128 |
||
129 |
private boolean next(int c) { |
|
130 |
if (c < 0) { |
|
131 |
return false; |
|
132 |
} |
|
133 |
||
134 |
if (c == '\'' || c == '"') { |
|
135 |
inside = true; |
|
136 |
write(c); |
|
137 |
int match = c; |
|
138 |
c = read(); |
|
139 |
while (c != match) { |
|
140 |
if (c < 0) { |
|
141 |
return false; |
|
142 |
} |
|
143 |
if (c == '\n' || c == '\r') { |
|
144 |
write(c); |
|
145 |
return true; |
|
146 |
} |
|
147 |
if (c == '\\') { |
|
148 |
write(c); |
|
149 |
c = read(); |
|
150 |
} |
|
151 |
write(c); |
|
152 |
c = read(); |
|
153 |
} |
|
154 |
write(c); |
|
155 |
return true; |
|
156 |
} |
|
157 |
||
158 |
if (c == '/') { |
|
159 |
c = read(); |
|
160 |
if (c == '*') { |
|
161 |
writeMask('/'); |
|
162 |
writeMask(c); |
|
163 |
int prevc = 0; |
|
164 |
while ((c = read()) != '/' || prevc != '*') { |
|
165 |
if (c < 0) { |
|
38835
37280d52d723
8131024: JShell: multi-line comment not detected as incomplete
rfield
parents:
34752
diff
changeset
|
166 |
openComment = true; |
33362 | 167 |
return false; |
168 |
} |
|
169 |
writeMask(c); |
|
170 |
prevc = c; |
|
171 |
} |
|
172 |
writeMask(c); |
|
173 |
return true; |
|
174 |
} else if (c == '/') { |
|
175 |
writeMask('/'); |
|
176 |
writeMask(c); |
|
177 |
while ((c = read()) != '\n' && c != '\r') { |
|
178 |
if (c < 0) { |
|
179 |
return false; |
|
180 |
} |
|
181 |
writeMask(c); |
|
182 |
} |
|
183 |
writeMask(c); |
|
184 |
return true; |
|
185 |
} else { |
|
186 |
inside = true; |
|
187 |
write('/'); |
|
188 |
// read character falls through |
|
189 |
} |
|
190 |
} |
|
191 |
||
192 |
if (Character.isJavaIdentifierStart(c)) { |
|
193 |
if (maskModifiers && !inside) { |
|
194 |
StringBuilder sb = new StringBuilder(); |
|
195 |
do { |
|
196 |
write(sb, c); |
|
197 |
c = read(); |
|
198 |
} while (Character.isJavaIdentifierPart(c)); |
|
199 |
String id = sb.toString(); |
|
200 |
if (IGNORED_MODIFERS.contains(id)) { |
|
201 |
writeMask(sb); |
|
202 |
} else { |
|
203 |
write(sb); |
|
204 |
if (id.equals("import")) { |
|
205 |
inside = true; |
|
206 |
} |
|
207 |
} |
|
208 |
return next(c); // recurse to handle left-over character |
|
209 |
} |
|
210 |
} else if (!Character.isWhitespace(c)) { |
|
211 |
inside = true; |
|
212 |
} |
|
213 |
||
214 |
if (c < 0) { |
|
215 |
return false; |
|
216 |
} |
|
217 |
write(c); |
|
218 |
return true; |
|
219 |
} |
|
220 |
} |