提交 95b4f357 编写于 作者: J jjg

6724118: change JavaCompiler to not use Scanner directly

6736119: refactor Parser and Parser.Factory
Reviewed-by: mcimadamore
上级 f94871f0
......@@ -27,6 +27,7 @@ package com.sun.tools.javac.api;
import java.io.File;
import java.io.IOException;
import java.nio.CharBuffer;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
......@@ -45,7 +46,7 @@ import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.main.*;
import com.sun.tools.javac.model.*;
import com.sun.tools.javac.parser.Parser;
import com.sun.tools.javac.parser.Scanner;
import com.sun.tools.javac.parser.ParserFactory;
import com.sun.tools.javac.tree.*;
import com.sun.tools.javac.tree.JCTree.*;
import com.sun.tools.javac.util.*;
......@@ -93,6 +94,9 @@ public class JavacTaskImpl extends JavacTask {
args.getClass();
context.getClass();
fileObjects.getClass();
// force the use of the scanner that captures Javadoc comments
com.sun.tools.javac.parser.DocCommentScanner.Factory.preRegister(context);
}
JavacTaskImpl(JavacTool tool,
......@@ -166,8 +170,6 @@ public class JavacTaskImpl extends JavacTask {
if (!filenames.isEmpty())
throw new IllegalArgumentException("Malformed arguments " + filenames.toString(" "));
compiler = JavaCompiler.instance(context);
// force the use of the scanner that captures Javadoc comments
com.sun.tools.javac.parser.DocCommentScanner.Factory.preRegister(context);
compiler.keepComments = true;
compiler.genEndPos = true;
// NOTE: this value will be updated after annotation processing
......@@ -519,14 +521,12 @@ public class JavacTaskImpl extends JavacTask {
throw new IllegalArgumentException();
compiler = JavaCompiler.instance(context);
JavaFileObject prev = compiler.log.useSource(null);
Scanner.Factory scannerFactory = Scanner.Factory.instance(context);
Parser.Factory parserFactory = Parser.Factory.instance(context);
ParserFactory parserFactory = ParserFactory.instance(context);
Attr attr = Attr.instance(context);
try {
Scanner scanner = scannerFactory.newScanner((expr+"\u0000").toCharArray(),
expr.length());
Parser parser = parserFactory.newParser(scanner, false, false);
JCTree tree = parser.type();
CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length());
Parser parser = parserFactory.newParser(buf, false, false, false);
JCTree tree = parser.parseType();
return attr.attribType(tree, (Symbol.TypeSymbol)scope);
} finally {
compiler.log.useSource(prev);
......
......@@ -276,7 +276,7 @@ public class JavaCompiler implements ClassReader.SourceCompleter {
/** Factory for parsers.
*/
protected Parser.Factory parserFactory;
protected ParserFactory parserFactory;
/** Optional listener for progress events
*/
......@@ -320,7 +320,7 @@ public class JavaCompiler implements ClassReader.SourceCompleter {
todo = Todo.instance(context);
fileManager = context.get(JavaFileManager.class);
parserFactory = Parser.Factory.instance(context);
parserFactory = ParserFactory.instance(context);
try {
// catch completion problems with predefineds
......@@ -510,10 +510,6 @@ public class JavaCompiler implements ClassReader.SourceCompleter {
return parseErrors;
}
protected Scanner.Factory getScannerFactory() {
return Scanner.Factory.instance(context);
}
/** Try to open input stream with given name.
* Report an error if this fails.
* @param filename The file name of the input stream to be opened.
......@@ -545,13 +541,9 @@ public class JavaCompiler implements ClassReader.SourceCompleter {
taskListener.started(e);
}
int initialErrorCount = log.nerrors;
Scanner scanner = getScannerFactory().newScanner(content);
Parser parser = parserFactory.newParser(scanner, keepComments(), genEndPos);
tree = parser.compilationUnit();
Parser parser = parserFactory.newParser(content, keepComments(), genEndPos, lineDebugInfo);
tree = parser.parseCompilationUnit();
parseErrors |= (log.nerrors > initialErrorCount);
if (lineDebugInfo) {
tree.lineMap = scanner.getLineMap();
}
if (verbose) {
printVerbose("parsing.done", Long.toString(elapsed(msec)));
}
......
......@@ -41,10 +41,10 @@ import static com.sun.tools.javac.tree.JCTree.*;
* This code and its internal interfaces are subject to change or
* deletion without notice.</b></p>
*/
public class EndPosParser extends Parser {
public class EndPosParser extends JavacParser {
public EndPosParser(Factory fac, Lexer S, boolean keepDocComments) {
super(fac, S, keepDocComments);
public EndPosParser(ParserFactory fac, Lexer S, boolean keepDocComments, boolean keepLineMap) {
super(fac, S, keepDocComments, keepLineMap);
this.S = S;
endPositions = new HashMap<JCTree,Integer>();
}
......@@ -79,8 +79,8 @@ public class EndPosParser extends Parser {
}
@Override
public JCCompilationUnit compilationUnit() {
JCCompilationUnit t = super.compilationUnit();
public JCCompilationUnit parseCompilationUnit() {
JCCompilationUnit t = super.parseCompilationUnit();
t.endPositions = endPositions;
return t;
}
......
/*
* Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
package com.sun.tools.javac.parser;
import com.sun.tools.javac.code.Source;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Log;
import com.sun.tools.javac.util.Name;
import com.sun.tools.javac.util.Options;
/**
* A factory for creating parsers.
*/
public class ParserFactory {
/** The context key for the parser factory. */
protected static final Context.Key<ParserFactory> parserFactoryKey = new Context.Key<ParserFactory>();
public static ParserFactory instance(Context context) {
ParserFactory instance = context.get(parserFactoryKey);
if (instance == null) {
instance = new ParserFactory(context);
}
return instance;
}
final TreeMaker F;
final Log log;
final Keywords keywords;
final Source source;
final Name.Table names;
final Options options;
final Scanner.Factory scannerFactory;
protected ParserFactory(Context context) {
super();
context.put(parserFactoryKey, this);
this.F = TreeMaker.instance(context);
this.log = Log.instance(context);
this.names = Name.Table.instance(context);
this.keywords = Keywords.instance(context);
this.source = Source.instance(context);
this.options = Options.instance(context);
this.scannerFactory = Scanner.Factory.instance(context);
}
public Parser newParser(CharSequence input, boolean keepDocComments, boolean keepEndPos, boolean keepLineMap) {
Lexer lexer = scannerFactory.newScanner(input);
if (keepEndPos) {
return new EndPosParser(this, lexer, keepDocComments, keepLineMap);
} else {
return new JavacParser(this, lexer, keepDocComments, keepLineMap);
}
}
}
......@@ -34,6 +34,7 @@ import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.parser.Parser;
import com.sun.tools.javac.parser.ParserFactory;
import com.sun.tools.javac.parser.Scanner;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeScanner;
......@@ -60,7 +61,7 @@ public class TestLog
JavacFileManager.preRegister(context);
Scanner.Factory sfac = Scanner.Factory.instance(context);
Parser.Factory pfac = Parser.Factory.instance(context);
ParserFactory pfac = ParserFactory.instance(context);
final String text =
"public class Foo {\n"
......@@ -74,9 +75,9 @@ public class TestLog
JavaFileObject fo = new StringJavaFileObject("Foo", text);
log.useSource(fo);
Scanner s = sfac.newScanner(fo.getCharContent(true));
Parser parser = pfac.newParser(s, false, genEndPos);
JCTree.JCCompilationUnit tree = parser.compilationUnit();
CharSequence cs = fo.getCharContent(true);
Parser parser = pfac.newParser(cs, false, genEndPos, false);
JCTree.JCCompilationUnit tree = parser.parseCompilationUnit();
log.setEndPosTable(fo, tree.endPositions);
TreeScanner ts = new LogTester(log, tree.endPositions);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册