langtools/src/share/classes/com/sun/tools/javac/parser/ScannerFactory.java
author briangoetz
Wed, 18 Dec 2013 16:05:18 -0500
changeset 22163 3651128c74eb
parent 10815 a719aa5f1631
permissions -rw-r--r--
8030244: Update langtools to use Diamond Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
6716
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
     2
 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
10
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
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4072
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4072
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
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
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4072
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4072
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4072
diff changeset
    23
 * questions.
10
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
6716
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    28
import java.nio.CharBuffer;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 10
diff changeset
    30
import com.sun.tools.javac.code.Source;
6716
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    31
import com.sun.tools.javac.util.Context;
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    32
import com.sun.tools.javac.util.Log;
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    33
import com.sun.tools.javac.util.Names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
6716
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    36
/**
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    37
 * A factory for creating scanners.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    39
 *  <p><b>This is NOT part of any supported API.
6716
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    40
 *  If you write code that depends on this, you do so at your own
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    41
 *  risk.  This code and its internal interfaces are subject to change
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    42
 *  or deletion without notice.</b>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 */
6716
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    44
public class ScannerFactory {
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    45
    /** The context key for the scanner factory. */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 10815
diff changeset
    46
    public static final Context.Key<ScannerFactory> scannerFactoryKey = new Context.Key<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
6716
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    48
    /** Get the Factory instance for this context. */
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    49
    public static ScannerFactory instance(Context context) {
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    50
        ScannerFactory instance = context.get(scannerFactoryKey);
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    51
        if (instance == null)
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    52
            instance = new ScannerFactory(context);
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    53
        return instance;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
6716
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    56
    final Log log;
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    57
    final Names names;
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    58
    final Source source;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 6716
diff changeset
    59
    final Tokens tokens;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
6716
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    61
    /** Create a new scanner factory. */
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    62
    protected ScannerFactory(Context context) {
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    63
        context.put(scannerFactoryKey, this);
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    64
        this.log = Log.instance(context);
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    65
        this.names = Names.instance(context);
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    66
        this.source = Source.instance(context);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 6716
diff changeset
    67
        this.tokens = Tokens.instance(context);
3895
3b3c2a1e5e8a 6860965: Project Coin: binary literals
jjg
parents: 2723
diff changeset
    68
    }
3b3c2a1e5e8a 6860965: Project Coin: binary literals
jjg
parents: 2723
diff changeset
    69
6716
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    70
    public Scanner newScanner(CharSequence input, boolean keepDocComments) {
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    71
        if (input instanceof CharBuffer) {
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    72
            CharBuffer buf = (CharBuffer) input;
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    73
            if (keepDocComments)
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 6716
diff changeset
    74
                return new Scanner(this, new JavadocTokenizer(this, buf));
6716
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    75
            else
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    76
                return new Scanner(this, buf);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        } else {
6716
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    78
            char[] array = input.toString().toCharArray();
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    79
            return newScanner(array, array.length, keepDocComments);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
6716
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    83
    public Scanner newScanner(char[] input, int inputLength, boolean keepDocComments) {
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    84
        if (keepDocComments)
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 6716
diff changeset
    85
            return new Scanner(this, new JavadocTokenizer(this, input, inputLength));
6716
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    86
        else
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6592
diff changeset
    87
            return new Scanner(this, input, inputLength);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
}