langtools/src/share/classes/com/sun/tools/javac/parser/ParserFactory.java
changeset 1258 1cf37d8837d1
child 1260 a772ba9ba43d
equal deleted inserted replaced
1257:873b053bf757 1258:1cf37d8837d1
       
     1 /*
       
     2  * Copyright 1999-2008 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package com.sun.tools.javac.parser;
       
    27 
       
    28 import com.sun.tools.javac.code.Source;
       
    29 import com.sun.tools.javac.tree.TreeMaker;
       
    30 import com.sun.tools.javac.util.Context;
       
    31 import com.sun.tools.javac.util.Log;
       
    32 import com.sun.tools.javac.util.Name;
       
    33 import com.sun.tools.javac.util.Options;
       
    34 
       
    35 /**
       
    36  * A factory for creating parsers.
       
    37  */
       
    38 public class ParserFactory {
       
    39 
       
    40     /** The context key for the parser factory. */
       
    41     protected static final Context.Key<ParserFactory> parserFactoryKey = new Context.Key<ParserFactory>();
       
    42 
       
    43     public static ParserFactory instance(Context context) {
       
    44         ParserFactory instance = context.get(parserFactoryKey);
       
    45         if (instance == null) {
       
    46             instance = new ParserFactory(context);
       
    47         }
       
    48         return instance;
       
    49     }
       
    50 
       
    51     final TreeMaker F;
       
    52     final Log log;
       
    53     final Keywords keywords;
       
    54     final Source source;
       
    55     final Name.Table names;
       
    56     final Options options;
       
    57     final Scanner.Factory scannerFactory;
       
    58 
       
    59     protected ParserFactory(Context context) {
       
    60         super();
       
    61         context.put(parserFactoryKey, this);
       
    62         this.F = TreeMaker.instance(context);
       
    63         this.log = Log.instance(context);
       
    64         this.names = Name.Table.instance(context);
       
    65         this.keywords = Keywords.instance(context);
       
    66         this.source = Source.instance(context);
       
    67         this.options = Options.instance(context);
       
    68         this.scannerFactory = Scanner.Factory.instance(context);
       
    69     }
       
    70 
       
    71     public Parser newParser(CharSequence input, boolean keepDocComments, boolean keepEndPos, boolean keepLineMap) {
       
    72         Lexer lexer = scannerFactory.newScanner(input);
       
    73         if (keepEndPos) {
       
    74             return new EndPosParser(this, lexer, keepDocComments, keepLineMap);
       
    75         } else {
       
    76             return new JavacParser(this, lexer, keepDocComments, keepLineMap);
       
    77         }
       
    78     }
       
    79 }