1 /*
   2  * Copyright (c) 1999, 2012, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.parser;
  27 
  28 import java.util.Locale;
  29 
  30 import com.sun.tools.javac.code.Source;
  31 import com.sun.tools.javac.code.Lint;
  32 import com.sun.tools.javac.tree.DocTreeMaker;
  33 import com.sun.tools.javac.tree.TreeMaker;
  34 import com.sun.tools.javac.util.Context;
  35 import com.sun.tools.javac.util.Log;
  36 import com.sun.tools.javac.util.Names;
  37 import com.sun.tools.javac.util.Options;
  38 
  39 /**
  40  * A factory for creating parsers.
  41  *
  42  * <p><b>This is NOT part of any supported API.
  43  * If you write code that depends on this, you do so at your own risk.
  44  * This code and its internal interfaces are subject to change or
  45  * deletion without notice.</b>
  46  */
  47 public class ParserFactory {
  48 
  49     /** The context key for the parser factory. */
  50     protected static final Context.Key<ParserFactory> parserFactoryKey = new Context.Key<>();
  51 
  52     public static ParserFactory instance(Context context) {
  53         ParserFactory instance = context.get(parserFactoryKey);
  54         if (instance == null) {
  55             instance = new ParserFactory(context);
  56         }
  57         return instance;
  58     }
  59 
  60     final TreeMaker F;
  61     final DocTreeMaker docTreeMaker;
  62     final Log log;
  63     final Tokens tokens;
  64     final Source source;
  65     final Names names;
  66     final Options options;
  67     final ScannerFactory scannerFactory;
  68     final Locale locale;
  69     final Lint lint;
  70 
  71     protected ParserFactory(Context context) {
  72         super();
  73         context.put(parserFactoryKey, this);
  74         this.F = TreeMaker.instance(context);
  75         this.docTreeMaker = DocTreeMaker.instance(context);
  76         this.log = Log.instance(context);
  77         this.names = Names.instance(context);
  78         this.tokens = Tokens.instance(context);
  79         this.source = Source.instance(context);
  80         this.options = Options.instance(context);
  81         this.scannerFactory = ScannerFactory.instance(context);
  82         this.locale = context.get(Locale.class);
  83         this.lint = Lint.instance(context);
  84     }
  85 
  86     public JavacParser newParser(CharSequence input, boolean keepDocComments, boolean keepEndPos, boolean keepLineMap) {
  87         return newParser(input, keepDocComments, keepEndPos, keepLineMap, false);
  88     }
  89 
  90     public JavacParser newParser(CharSequence input, boolean keepDocComments, boolean keepEndPos, boolean keepLineMap, boolean parseModuleInfo) {
  91         Lexer lexer = scannerFactory.newScanner(input, keepDocComments);
  92         return new JavacParser(this, lexer, keepDocComments, keepLineMap, keepEndPos, parseModuleInfo);
  93     }
  94 }