author | rfield |
Mon, 25 Apr 2016 08:50:16 -0700 | |
changeset 37644 | 33cf53901cac |
parent 34752 | 9c262a013456 |
child 38835 | 37280d52d723 |
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 |
||
43 |
private final StringBuilder sbCleared = new StringBuilder(); |
|
44 |
private final StringBuilder sbMask = new StringBuilder(); |
|
45 |
private final String str; |
|
46 |
private final int length; |
|
47 |
private final boolean maskModifiers; |
|
48 |
private int next = 0; |
|
49 |
private boolean wasMasked = false; |
|
50 |
private boolean inside = false; |
|
51 |
||
52 |
@SuppressWarnings("empty-statement") |
|
53 |
public MaskCommentsAndModifiers(String s, boolean maskModifiers) { |
|
54 |
this.str = s; |
|
55 |
this.length = s.length(); |
|
56 |
this.maskModifiers = maskModifiers; |
|
57 |
do { } while (next()); |
|
58 |
} |
|
59 |
||
60 |
public String cleared() { |
|
61 |
return sbCleared.toString(); |
|
62 |
} |
|
63 |
||
64 |
public String mask() { |
|
65 |
return sbMask.toString(); |
|
66 |
} |
|
67 |
||
68 |
public boolean wasMasked() { |
|
69 |
return wasMasked; |
|
70 |
} |
|
71 |
||
72 |
/** |
|
73 |
* Read the next character |
|
74 |
*/ |
|
75 |
private int read() { |
|
76 |
if (next >= length) { |
|
77 |
return -1; |
|
78 |
} |
|
79 |
return str.charAt(next++); |
|
80 |
} |
|
81 |
||
82 |
private void write(StringBuilder sb, int ch) { |
|
83 |
sb.append((char)ch); |
|
84 |
} |
|
85 |
||
86 |
private void write(int ch) { |
|
87 |
write(sbCleared, ch); |
|
88 |
write(sbMask, Character.isWhitespace(ch) ? ch : ' '); |
|
89 |
} |
|
90 |
||
91 |
private void writeMask(int ch) { |
|
92 |
wasMasked = true; |
|
93 |
write(sbMask, ch); |
|
94 |
write(sbCleared, Character.isWhitespace(ch) ? ch : ' '); |
|
95 |
} |
|
96 |
||
97 |
private void write(CharSequence s) { |
|
98 |
for (int cp : s.chars().toArray()) { |
|
99 |
write(cp); |
|
100 |
} |
|
101 |
} |
|
102 |
||
103 |
private void writeMask(CharSequence s) { |
|
104 |
for (int cp : s.chars().toArray()) { |
|
105 |
writeMask(cp); |
|
106 |
} |
|
107 |
} |
|
108 |
||
109 |
private boolean next() { |
|
110 |
return next(read()); |
|
111 |
} |
|
112 |
||
113 |
private boolean next(int c) { |
|
114 |
if (c < 0) { |
|
115 |
return false; |
|
116 |
} |
|
117 |
||
118 |
if (c == '\'' || c == '"') { |
|
119 |
inside = true; |
|
120 |
write(c); |
|
121 |
int match = c; |
|
122 |
c = read(); |
|
123 |
while (c != match) { |
|
124 |
if (c < 0) { |
|
125 |
return false; |
|
126 |
} |
|
127 |
if (c == '\n' || c == '\r') { |
|
128 |
write(c); |
|
129 |
return true; |
|
130 |
} |
|
131 |
if (c == '\\') { |
|
132 |
write(c); |
|
133 |
c = read(); |
|
134 |
} |
|
135 |
write(c); |
|
136 |
c = read(); |
|
137 |
} |
|
138 |
write(c); |
|
139 |
return true; |
|
140 |
} |
|
141 |
||
142 |
if (c == '/') { |
|
143 |
c = read(); |
|
144 |
if (c == '*') { |
|
145 |
writeMask('/'); |
|
146 |
writeMask(c); |
|
147 |
int prevc = 0; |
|
148 |
while ((c = read()) != '/' || prevc != '*') { |
|
149 |
if (c < 0) { |
|
150 |
return false; |
|
151 |
} |
|
152 |
writeMask(c); |
|
153 |
prevc = c; |
|
154 |
} |
|
155 |
writeMask(c); |
|
156 |
return true; |
|
157 |
} else if (c == '/') { |
|
158 |
writeMask('/'); |
|
159 |
writeMask(c); |
|
160 |
while ((c = read()) != '\n' && c != '\r') { |
|
161 |
if (c < 0) { |
|
162 |
return false; |
|
163 |
} |
|
164 |
writeMask(c); |
|
165 |
} |
|
166 |
writeMask(c); |
|
167 |
return true; |
|
168 |
} else { |
|
169 |
inside = true; |
|
170 |
write('/'); |
|
171 |
// read character falls through |
|
172 |
} |
|
173 |
} |
|
174 |
||
175 |
if (Character.isJavaIdentifierStart(c)) { |
|
176 |
if (maskModifiers && !inside) { |
|
177 |
StringBuilder sb = new StringBuilder(); |
|
178 |
do { |
|
179 |
write(sb, c); |
|
180 |
c = read(); |
|
181 |
} while (Character.isJavaIdentifierPart(c)); |
|
182 |
String id = sb.toString(); |
|
183 |
if (IGNORED_MODIFERS.contains(id)) { |
|
184 |
writeMask(sb); |
|
185 |
} else { |
|
186 |
write(sb); |
|
187 |
if (id.equals("import")) { |
|
188 |
inside = true; |
|
189 |
} |
|
190 |
} |
|
191 |
return next(c); // recurse to handle left-over character |
|
192 |
} |
|
193 |
} else if (!Character.isWhitespace(c)) { |
|
194 |
inside = true; |
|
195 |
} |
|
196 |
||
197 |
if (c < 0) { |
|
198 |
return false; |
|
199 |
} |
|
200 |
write(c); |
|
201 |
return true; |
|
202 |
} |
|
203 |
} |