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