1 11 package org.eclipse.jdt.internal.core.util; 12 13 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; 14 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; 15 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; 16 import org.eclipse.jdt.internal.compiler.parser.Parser; 17 import org.eclipse.jdt.internal.compiler.problem.ProblemReporter; 18 import org.eclipse.jdt.internal.compiler.problem.ProblemSeverities; 19 20 25 public class CommentRecorderParser extends Parser { 26 27 int[] commentStops = new int[10]; 29 int[] commentStarts = new int[10]; 30 int commentPtr = -1; protected final static int CommentIncrement = 100; 32 33 37 public CommentRecorderParser(ProblemReporter problemReporter, boolean optimizeStringLiterals) { 38 super(problemReporter, optimizeStringLiterals); 39 } 40 41 public void checkComment() { 44 45 if (!(this.diet && this.dietInt==0) && this.scanner.commentPtr >= 0) { 47 flushCommentsDefinedPriorTo(this.endStatementPosition); 48 } 49 boolean deprecated = false; 50 boolean checkDeprecated = false; 51 int lastCommentIndex = -1; 52 53 nextComment : for (lastCommentIndex = this.scanner.commentPtr; lastCommentIndex >= 0; lastCommentIndex--){ 55 int commentSourceStart = this.scanner.commentStarts[lastCommentIndex]; 57 if ((commentSourceStart < 0) || 59 (this.modifiersSourceStart != -1 && this.modifiersSourceStart < commentSourceStart) || 60 (this.scanner.commentStops[lastCommentIndex] < 0)) 61 { 62 continue nextComment; 63 } 64 checkDeprecated = true; 65 int commentSourceEnd = this.scanner.commentStops[lastCommentIndex] - 1; 67 this.javadocParser.reportProblems = this.currentElement == null || commentSourceEnd > this.lastJavadocEnd; 69 deprecated = this.javadocParser.checkDeprecation(lastCommentIndex); 70 this.javadoc = this.javadocParser.docComment; 71 break nextComment; 72 } 73 if (deprecated) { 74 checkAndSetModifiers(ClassFileConstants.AccDeprecated); 75 } 76 if (lastCommentIndex >= 0 && checkDeprecated) { 78 this.modifiersSourceStart = this.scanner.commentStarts[lastCommentIndex]; 79 if (this.modifiersSourceStart < 0) { 80 this.modifiersSourceStart = -this.modifiersSourceStart; 81 } 82 } 83 } 84 85 88 protected void consumeClassHeader() { 89 pushOnCommentsStack(0, this.scanner.commentPtr); 90 super.consumeClassHeader(); 91 } 92 95 protected void consumeEmptyTypeDeclaration() { 96 pushOnCommentsStack(0, this.scanner.commentPtr); 97 super.consumeEmptyTypeDeclaration(); 98 } 99 102 protected void consumeInterfaceHeader() { 103 pushOnCommentsStack(0, this.scanner.commentPtr); 104 super.consumeInterfaceHeader(); 105 } 106 107 111 public boolean containsComment(int sourceStart, int sourceEnd) { 112 int iComment = this.scanner.commentPtr; 113 for (; iComment >= 0; iComment--) { 114 int commentStart = this.scanner.commentStarts[iComment]; 115 if (commentStart < 0) { 116 commentStart = -commentStart; 117 } 118 if (commentStart < sourceStart) continue; 120 if (commentStart > sourceEnd) continue; 122 return true; 123 } 124 return false; 125 } 126 127 130 protected CompilationUnitDeclaration endParse(int act) { 131 CompilationUnitDeclaration unit = super.endParse(act); 132 if (unit.comments == null) { 133 pushOnCommentsStack(0, this.scanner.commentPtr); 134 unit.comments = getCommentsPositions(); 135 } 136 return unit; 137 } 138 139 143 public int flushCommentsDefinedPriorTo(int position) { 144 145 int lastCommentIndex = this.scanner.commentPtr; 146 if (lastCommentIndex < 0) return position; 148 int index = lastCommentIndex; 150 int validCount = 0; 151 while (index >= 0){ 152 int commentEnd = this.scanner.commentStops[index]; 153 if (commentEnd < 0) commentEnd = -commentEnd; if (commentEnd <= position){ 155 break; 156 } 157 index--; 158 validCount++; 159 } 160 if (validCount > 0){ 163 int immediateCommentEnd = 0; 164 while (index<lastCommentIndex && (immediateCommentEnd = -this.scanner.commentStops[index+1]) > 0){ immediateCommentEnd--; if (org.eclipse.jdt.internal.compiler.util.Util.getLineNumber(position, this.scanner.lineEnds, 0, this.scanner.linePtr) 168 != org.eclipse.jdt.internal.compiler.util.Util.getLineNumber(immediateCommentEnd, this.scanner.lineEnds, 0, this.scanner.linePtr)) break; 169 position = immediateCommentEnd; 170 validCount--; index++; 172 } 173 } 174 175 if (index < 0) return position; pushOnCommentsStack(0, index); 178 switch (validCount) { 179 case 0: 180 break; 182 case 2: 184 this.scanner.commentStarts[0] = this.scanner.commentStarts[index+1]; 185 this.scanner.commentStops[0] = this.scanner.commentStops[index+1]; 186 this.scanner.commentTagStarts[0] = this.scanner.commentTagStarts[index+1]; 187 this.scanner.commentStarts[1] = this.scanner.commentStarts[index+2]; 188 this.scanner.commentStops[1] = this.scanner.commentStops[index+2]; 189 this.scanner.commentTagStarts[1] = this.scanner.commentTagStarts[index+2]; 190 break; 191 case 1: 192 this.scanner.commentStarts[0] = this.scanner.commentStarts[index+1]; 193 this.scanner.commentStops[0] = this.scanner.commentStops[index+1]; 194 this.scanner.commentTagStarts[0] = this.scanner.commentTagStarts[index+1]; 195 break; 196 default: 197 System.arraycopy(this.scanner.commentStarts, index + 1, this.scanner.commentStarts, 0, validCount); 198 System.arraycopy(this.scanner.commentStops, index + 1, this.scanner.commentStops, 0, validCount); 199 System.arraycopy(this.scanner.commentTagStarts, index + 1, this.scanner.commentTagStarts, 0, validCount); 200 } 201 this.scanner.commentPtr = validCount - 1; 202 return position; 203 } 204 205 209 public int[][] getCommentsPositions() { 210 int[][] positions = new int[this.commentPtr+1][2]; 211 for (int i = 0, max = this.commentPtr; i <= max; i++){ 212 positions[i][0] = this.commentStarts[i]; 213 positions[i][1] = this.commentStops[i]; 214 } 215 return positions; 216 } 217 218 221 public void initialize(boolean initializeNLS) { 222 super.initialize(initializeNLS); 223 this.commentPtr = -1; 224 } 225 228 public void initialize() { 229 super.initialize(); 230 this.commentPtr = -1; 231 } 232 233 237 public void initializeScanner() { 238 this.scanner = new CommentRecorderScanner( 239 false , 240 false , 241 this.options.getSeverity(CompilerOptions.NonExternalizedString) != ProblemSeverities.Ignore , 242 this.options.sourceLevel , 243 this.options.taskTags, 244 this.options.taskPriorites, 245 this.options.isTaskCaseSensitive); 246 } 247 248 251 private void pushOnCommentsStack(int start, int end) { 252 253 for (int i=start; i<=end; i++) { 254 int scannerStart = this.scanner.commentStarts[i]<0 ? -this.scanner.commentStarts[i] : this.scanner.commentStarts[i]; 256 int commentStart = this.commentPtr == -1 ? -1 : (this.commentStarts[this.commentPtr]<0 ? -this.commentStarts[this.commentPtr] : this.commentStarts[this.commentPtr]); 257 if (commentStart == -1 || scannerStart > commentStart) { 258 int stackLength = this.commentStarts.length; 259 if (++this.commentPtr >= stackLength) { 260 System.arraycopy( 261 this.commentStarts, 0, 262 this.commentStarts = new int[stackLength + CommentIncrement], 0, 263 stackLength); 264 System.arraycopy( 265 this.commentStops, 0, 266 this.commentStops = new int[stackLength + CommentIncrement], 0, 267 stackLength); 268 } 269 this.commentStarts[this.commentPtr] = this.scanner.commentStarts[i]; 270 this.commentStops[this.commentPtr] = this.scanner.commentStops[i]; 271 } 272 } 273 } 274 278 protected void resetModifiers() { 279 pushOnCommentsStack(0, this.scanner.commentPtr); 280 super.resetModifiers(); 281 } 282 } 283 | Popular Tags |