src/share/classes/com/sun/tools/javac/parser/DocCommentScanner.java

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 695
3c9b64e55c5d
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

duke@1 1 /*
jjg@695 2 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.parser;
duke@1 27
duke@1 28 import java.nio.*;
duke@1 29
duke@1 30 import com.sun.tools.javac.util.*;
duke@1 31 import static com.sun.tools.javac.util.LayoutCharacters.*;
duke@1 32
duke@1 33 /** An extension to the base lexical analyzer that captures
duke@1 34 * and processes the contents of doc comments. It does so by
duke@1 35 * translating Unicode escape sequences and by stripping the
duke@1 36 * leading whitespace and starts from each line of the comment.
duke@1 37 *
jjg@581 38 * <p><b>This is NOT part of any supported API.
jjg@581 39 * If you write code that depends on this, you do so at your own risk.
duke@1 40 * This code and its internal interfaces are subject to change or
duke@1 41 * deletion without notice.</b>
duke@1 42 */
duke@1 43 public class DocCommentScanner extends Scanner {
duke@1 44
duke@1 45 /** Create a scanner from the input buffer. buffer must implement
duke@1 46 * array() and compact(), and remaining() must be less than limit().
duke@1 47 */
jjg@695 48 protected DocCommentScanner(ScannerFactory fac, CharBuffer buffer) {
duke@1 49 super(fac, buffer);
duke@1 50 }
duke@1 51
duke@1 52 /** Create a scanner from the input array. The array must have at
duke@1 53 * least a single character of extra space.
duke@1 54 */
jjg@695 55 protected DocCommentScanner(ScannerFactory fac, char[] input, int inputLength) {
duke@1 56 super(fac, input, inputLength);
duke@1 57 }
duke@1 58
duke@1 59 /** Starting position of the comment in original source
duke@1 60 */
duke@1 61 private int pos;
duke@1 62
duke@1 63 /** The comment input buffer, index of next chacter to be read,
duke@1 64 * index of one past last character in buffer.
duke@1 65 */
duke@1 66 private char[] buf;
duke@1 67 private int bp;
duke@1 68 private int buflen;
duke@1 69
duke@1 70 /** The current character.
duke@1 71 */
duke@1 72 private char ch;
duke@1 73
duke@1 74 /** The column number position of the current character.
duke@1 75 */
duke@1 76 private int col;
duke@1 77
duke@1 78 /** The buffer index of the last converted Unicode character
duke@1 79 */
duke@1 80 private int unicodeConversionBp = 0;
duke@1 81
duke@1 82 /**
duke@1 83 * Buffer for doc comment.
duke@1 84 */
duke@1 85 private char[] docCommentBuffer = new char[1024];
duke@1 86
duke@1 87 /**
duke@1 88 * Number of characters in doc comment buffer.
duke@1 89 */
duke@1 90 private int docCommentCount;
duke@1 91
duke@1 92 /**
duke@1 93 * Translated and stripped contents of doc comment
duke@1 94 */
duke@1 95 private String docComment = null;
duke@1 96
duke@1 97
duke@1 98 /** Unconditionally expand the comment buffer.
duke@1 99 */
duke@1 100 private void expandCommentBuffer() {
duke@1 101 char[] newBuffer = new char[docCommentBuffer.length * 2];
duke@1 102 System.arraycopy(docCommentBuffer, 0, newBuffer,
duke@1 103 0, docCommentBuffer.length);
duke@1 104 docCommentBuffer = newBuffer;
duke@1 105 }
duke@1 106
duke@1 107 /** Convert an ASCII digit from its base (8, 10, or 16)
duke@1 108 * to its value.
duke@1 109 */
duke@1 110 private int digit(int base) {
duke@1 111 char c = ch;
duke@1 112 int result = Character.digit(c, base);
duke@1 113 if (result >= 0 && c > 0x7f) {
duke@1 114 ch = "0123456789abcdef".charAt(result);
duke@1 115 }
duke@1 116 return result;
duke@1 117 }
duke@1 118
duke@1 119 /** Convert Unicode escape; bp points to initial '\' character
duke@1 120 * (Spec 3.3).
duke@1 121 */
duke@1 122 private void convertUnicode() {
duke@1 123 if (ch == '\\' && unicodeConversionBp != bp) {
duke@1 124 bp++; ch = buf[bp]; col++;
duke@1 125 if (ch == 'u') {
duke@1 126 do {
duke@1 127 bp++; ch = buf[bp]; col++;
duke@1 128 } while (ch == 'u');
duke@1 129 int limit = bp + 3;
duke@1 130 if (limit < buflen) {
duke@1 131 int d = digit(16);
duke@1 132 int code = d;
duke@1 133 while (bp < limit && d >= 0) {
duke@1 134 bp++; ch = buf[bp]; col++;
duke@1 135 d = digit(16);
duke@1 136 code = (code << 4) + d;
duke@1 137 }
duke@1 138 if (d >= 0) {
duke@1 139 ch = (char)code;
duke@1 140 unicodeConversionBp = bp;
duke@1 141 return;
duke@1 142 }
duke@1 143 }
duke@1 144 // "illegal.Unicode.esc", reported by base scanner
duke@1 145 } else {
duke@1 146 bp--;
duke@1 147 ch = '\\';
duke@1 148 col--;
duke@1 149 }
duke@1 150 }
duke@1 151 }
duke@1 152
duke@1 153
duke@1 154 /** Read next character.
duke@1 155 */
duke@1 156 private void scanChar() {
duke@1 157 bp++;
duke@1 158 ch = buf[bp];
duke@1 159 switch (ch) {
duke@1 160 case '\r': // return
duke@1 161 col = 0;
duke@1 162 break;
duke@1 163 case '\n': // newline
duke@1 164 if (bp == 0 || buf[bp-1] != '\r') {
duke@1 165 col = 0;
duke@1 166 }
duke@1 167 break;
duke@1 168 case '\t': // tab
duke@1 169 col = (col / TabInc * TabInc) + TabInc;
duke@1 170 break;
duke@1 171 case '\\': // possible Unicode
duke@1 172 col++;
duke@1 173 convertUnicode();
duke@1 174 break;
duke@1 175 default:
duke@1 176 col++;
duke@1 177 break;
duke@1 178 }
duke@1 179 }
duke@1 180
duke@1 181 /**
duke@1 182 * Read next character in doc comment, skipping over double '\' characters.
duke@1 183 * If a double '\' is skipped, put in the buffer and update buffer count.
duke@1 184 */
duke@1 185 private void scanDocCommentChar() {
duke@1 186 scanChar();
duke@1 187 if (ch == '\\') {
duke@1 188 if (buf[bp+1] == '\\' && unicodeConversionBp != bp) {
duke@1 189 if (docCommentCount == docCommentBuffer.length)
duke@1 190 expandCommentBuffer();
duke@1 191 docCommentBuffer[docCommentCount++] = ch;
duke@1 192 bp++; col++;
duke@1 193 } else {
duke@1 194 convertUnicode();
duke@1 195 }
duke@1 196 }
duke@1 197 }
duke@1 198
duke@1 199 /* Reset doc comment before reading each new token
duke@1 200 */
duke@1 201 public void nextToken() {
duke@1 202 docComment = null;
duke@1 203 super.nextToken();
duke@1 204 }
duke@1 205
duke@1 206 /**
duke@1 207 * Returns the documentation string of the current token.
duke@1 208 */
duke@1 209 public String docComment() {
duke@1 210 return docComment;
duke@1 211 }
duke@1 212
duke@1 213 /**
duke@1 214 * Process a doc comment and make the string content available.
duke@1 215 * Strips leading whitespace and stars.
duke@1 216 */
duke@1 217 @SuppressWarnings("fallthrough")
duke@1 218 protected void processComment(CommentStyle style) {
duke@1 219 if (style != CommentStyle.JAVADOC) {
duke@1 220 return;
duke@1 221 }
duke@1 222
duke@1 223 pos = pos();
duke@1 224 buf = getRawCharacters(pos, endPos());
duke@1 225 buflen = buf.length;
duke@1 226 bp = 0;
duke@1 227 col = 0;
duke@1 228
duke@1 229 docCommentCount = 0;
duke@1 230
duke@1 231 boolean firstLine = true;
duke@1 232
duke@1 233 // Skip over first slash
duke@1 234 scanDocCommentChar();
duke@1 235 // Skip over first star
duke@1 236 scanDocCommentChar();
duke@1 237
duke@1 238 // consume any number of stars
duke@1 239 while (bp < buflen && ch == '*') {
duke@1 240 scanDocCommentChar();
duke@1 241 }
duke@1 242 // is the comment in the form /**/, /***/, /****/, etc. ?
duke@1 243 if (bp < buflen && ch == '/') {
duke@1 244 docComment = "";
duke@1 245 return;
duke@1 246 }
duke@1 247
duke@1 248 // skip a newline on the first line of the comment.
duke@1 249 if (bp < buflen) {
duke@1 250 if (ch == LF) {
duke@1 251 scanDocCommentChar();
duke@1 252 firstLine = false;
duke@1 253 } else if (ch == CR) {
duke@1 254 scanDocCommentChar();
duke@1 255 if (ch == LF) {
duke@1 256 scanDocCommentChar();
duke@1 257 firstLine = false;
duke@1 258 }
duke@1 259 }
duke@1 260 }
duke@1 261
duke@1 262 outerLoop:
duke@1 263
duke@1 264 // The outerLoop processes the doc comment, looping once
duke@1 265 // for each line. For each line, it first strips off
duke@1 266 // whitespace, then it consumes any stars, then it
duke@1 267 // puts the rest of the line into our buffer.
duke@1 268 while (bp < buflen) {
duke@1 269
duke@1 270 // The wsLoop consumes whitespace from the beginning
duke@1 271 // of each line.
duke@1 272 wsLoop:
duke@1 273
duke@1 274 while (bp < buflen) {
duke@1 275 switch(ch) {
duke@1 276 case ' ':
duke@1 277 scanDocCommentChar();
duke@1 278 break;
duke@1 279 case '\t':
duke@1 280 col = ((col - 1) / TabInc * TabInc) + TabInc;
duke@1 281 scanDocCommentChar();
duke@1 282 break;
duke@1 283 case FF:
duke@1 284 col = 0;
duke@1 285 scanDocCommentChar();
duke@1 286 break;
duke@1 287 // Treat newline at beginning of line (blank line, no star)
duke@1 288 // as comment text. Old Javadoc compatibility requires this.
duke@1 289 /*---------------------------------*
duke@1 290 case CR: // (Spec 3.4)
duke@1 291 scanDocCommentChar();
duke@1 292 if (ch == LF) {
duke@1 293 col = 0;
duke@1 294 scanDocCommentChar();
duke@1 295 }
duke@1 296 break;
duke@1 297 case LF: // (Spec 3.4)
duke@1 298 scanDocCommentChar();
duke@1 299 break;
duke@1 300 *---------------------------------*/
duke@1 301 default:
duke@1 302 // we've seen something that isn't whitespace;
duke@1 303 // jump out.
duke@1 304 break wsLoop;
duke@1 305 }
duke@1 306 }
duke@1 307
duke@1 308 // Are there stars here? If so, consume them all
duke@1 309 // and check for the end of comment.
duke@1 310 if (ch == '*') {
duke@1 311 // skip all of the stars
duke@1 312 do {
duke@1 313 scanDocCommentChar();
duke@1 314 } while (ch == '*');
duke@1 315
duke@1 316 // check for the closing slash.
duke@1 317 if (ch == '/') {
duke@1 318 // We're done with the doc comment
duke@1 319 // scanChar() and breakout.
duke@1 320 break outerLoop;
duke@1 321 }
duke@1 322 } else if (! firstLine) {
duke@1 323 //The current line does not begin with a '*' so we will indent it.
duke@1 324 for (int i = 1; i < col; i++) {
duke@1 325 if (docCommentCount == docCommentBuffer.length)
duke@1 326 expandCommentBuffer();
duke@1 327 docCommentBuffer[docCommentCount++] = ' ';
duke@1 328 }
duke@1 329 }
duke@1 330
duke@1 331 // The textLoop processes the rest of the characters
duke@1 332 // on the line, adding them to our buffer.
duke@1 333 textLoop:
duke@1 334 while (bp < buflen) {
duke@1 335 switch (ch) {
duke@1 336 case '*':
duke@1 337 // Is this just a star? Or is this the
duke@1 338 // end of a comment?
duke@1 339 scanDocCommentChar();
duke@1 340 if (ch == '/') {
duke@1 341 // This is the end of the comment,
duke@1 342 // set ch and return our buffer.
duke@1 343 break outerLoop;
duke@1 344 }
duke@1 345 // This is just an ordinary star. Add it to
duke@1 346 // the buffer.
duke@1 347 if (docCommentCount == docCommentBuffer.length)
duke@1 348 expandCommentBuffer();
duke@1 349 docCommentBuffer[docCommentCount++] = '*';
duke@1 350 break;
duke@1 351 case ' ':
duke@1 352 case '\t':
duke@1 353 if (docCommentCount == docCommentBuffer.length)
duke@1 354 expandCommentBuffer();
duke@1 355 docCommentBuffer[docCommentCount++] = ch;
duke@1 356 scanDocCommentChar();
duke@1 357 break;
duke@1 358 case FF:
duke@1 359 scanDocCommentChar();
duke@1 360 break textLoop; // treat as end of line
duke@1 361 case CR: // (Spec 3.4)
duke@1 362 scanDocCommentChar();
duke@1 363 if (ch != LF) {
duke@1 364 // Canonicalize CR-only line terminator to LF
duke@1 365 if (docCommentCount == docCommentBuffer.length)
duke@1 366 expandCommentBuffer();
duke@1 367 docCommentBuffer[docCommentCount++] = (char)LF;
duke@1 368 break textLoop;
duke@1 369 }
duke@1 370 /* fall through to LF case */
duke@1 371 case LF: // (Spec 3.4)
duke@1 372 // We've seen a newline. Add it to our
duke@1 373 // buffer and break out of this loop,
duke@1 374 // starting fresh on a new line.
duke@1 375 if (docCommentCount == docCommentBuffer.length)
duke@1 376 expandCommentBuffer();
duke@1 377 docCommentBuffer[docCommentCount++] = ch;
duke@1 378 scanDocCommentChar();
duke@1 379 break textLoop;
duke@1 380 default:
duke@1 381 // Add the character to our buffer.
duke@1 382 if (docCommentCount == docCommentBuffer.length)
duke@1 383 expandCommentBuffer();
duke@1 384 docCommentBuffer[docCommentCount++] = ch;
duke@1 385 scanDocCommentChar();
duke@1 386 }
duke@1 387 } // end textLoop
duke@1 388 firstLine = false;
duke@1 389 } // end outerLoop
duke@1 390
duke@1 391 if (docCommentCount > 0) {
duke@1 392 int i = docCommentCount - 1;
duke@1 393 trailLoop:
duke@1 394 while (i > -1) {
duke@1 395 switch (docCommentBuffer[i]) {
duke@1 396 case '*':
duke@1 397 i--;
duke@1 398 break;
duke@1 399 default:
duke@1 400 break trailLoop;
duke@1 401 }
duke@1 402 }
duke@1 403 docCommentCount = i + 1;
duke@1 404
duke@1 405 // Store the text of the doc comment
duke@1 406 docComment = new String(docCommentBuffer, 0 , docCommentCount);
duke@1 407 } else {
duke@1 408 docComment = "";
duke@1 409 }
duke@1 410 }
duke@1 411
duke@1 412 /** Build a map for translating between line numbers and
duke@1 413 * positions in the input.
duke@1 414 *
duke@1 415 * @return a LineMap */
duke@1 416 public Position.LineMap getLineMap() {
duke@1 417 char[] buf = getRawCharacters();
duke@1 418 return Position.makeLineMap(buf, buf.length, true);
duke@1 419 }
duke@1 420 }

mercurial