本文整理汇总了Java中com.sun.tools.javac.parser.Scanner类的典型用法代码示例。如果您正苦于以下问题:Java Scanner类的具体用法?Java Scanner怎么用?Java Scanner使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Scanner类属于com.sun.tools.javac.parser包,在下文中一共展示了Scanner类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parseType
import com.sun.tools.javac.parser.Scanner; //导入依赖的package包/类
/**
* For internal use by Sun Microsystems only. This method will be
* removed without warning.
*/
public Type parseType(String expr, TypeElement scope) {
if (expr == null || expr.equals(""))
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);
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();
return attr.attribType(tree, (Symbol.TypeSymbol)scope);
} finally {
compiler.log.useSource(prev);
}
}
示例2: parseType
import com.sun.tools.javac.parser.Scanner; //导入依赖的package包/类
/**
* For internal use only. This method will be
* removed without warning.
*/
public Type parseType(String expr, TypeElement scope) {
if (expr == null || expr.equals(""))
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);
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();
return attr.attribType(tree, (Symbol.TypeSymbol)scope);
} finally {
compiler.log.useSource(prev);
}
}
示例3: parseExpression
import com.sun.tools.javac.parser.Scanner; //导入依赖的package包/类
private static JCExpression parseExpression(Context context, CharSequence expr, boolean onlyFullInput, SourcePositions[] pos, final List<Diagnostic<? extends JavaFileObject>> errors) {
if (expr == null || (pos != null && pos.length != 1))
throw new IllegalArgumentException();
JavaCompiler compiler = JavaCompiler.instance(context);
JavaFileObject prev = compiler.log.useSource(new DummyJFO());
Log.DiagnosticHandler discardHandler = new Log.DiscardDiagnosticHandler(compiler.log) {
@Override
public void report(JCDiagnostic diag) {
errors.add(diag);
}
};
try {
CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length());
ParserFactory factory = ParserFactory.instance(context);
ScannerFactory scannerFactory = ScannerFactory.instance(context);
Names names = Names.instance(context);
Scanner scanner = scannerFactory.newScanner(buf, false);
Parser parser = new JackpotJavacParser(context, (NBParserFactory) factory, scanner, false, false, CancelService.instance(context), names);
if (parser instanceof JavacParser) {
if (pos != null)
pos[0] = new ParserSourcePositions((JavacParser)parser);
JCExpression result = parser.parseExpression();
if (!onlyFullInput || scanner.token().kind == TokenKind.EOF) {
return result;
}
}
return null;
} finally {
compiler.log.useSource(prev);
compiler.log.popDiagnosticHandler(discardHandler);
}
}
示例4: comment
import com.sun.tools.javac.parser.Scanner; //导入依赖的package包/类
private Comment comment(String docString) {
StringBuilder docComment = new StringBuilder()
.append("/**")
.append(docString)
.append("*/");
Scanner scanner = scanners.newScanner(docComment, true);
scanner.nextToken();
Token token = scanner.token();
return token.comment(CommentStyle.JAVADOC);
}
示例5: verifyTreeSpans
import com.sun.tools.javac.parser.Scanner; //导入依赖的package包/类
public void verifyTreeSpans(File file, List<int[]> spans,
String updatedContent, int semicolon) throws IOException {
JCCompilationUnit updated = read(file.toURI(), updatedContent);
Iterator<int[]> nextSpan = spans.iterator();
new TreePathScanner<Void, Void>() {
@Override
public Void scan(Tree tree, Void p) {
if (tree != null) {
int start = ((JCTree) tree).getStartPosition();
int end = ((JCTree) tree).getEndPosition(updated.endPositions);
if (tree.getKind() != Kind.ERRONEOUS) {
int[] expected = nextSpan.next();
int expectedEnd = expected[1];
if (expectedEnd == semicolon + 1) {
Scanner scanner = scannerFactory.newScanner(updatedContent, true);
scanner.nextToken();
while (scanner.token().pos < expectedEnd)
scanner.nextToken();
expectedEnd = scanner.token().pos;
}
if (expected[0] != start || expectedEnd != end) {
error(updatedContent + "; semicolon: " + semicolon + "; expected: " +
expected[0] + "-" + expectedEnd + "; found=" + start + "-" + end +
";" + tree);
}
}
}
return super.scan(tree, p);
}
}.scan(updated, null);
}
示例6: scan
import com.sun.tools.javac.parser.Scanner; //导入依赖的package包/类
CaInfo scan(String s) {
try {
Scanner scanner = scannerFactory.newScanner(s, false);
Matched in = new Matched(scanner);
Parser parser = new Parser(in, proc, s);
Completeness stat = parser.parseUnit();
int endPos = stat == Completeness.UNKNOWN
? s.length()
: in.prevCT.endPos;
return new CaInfo(stat, endPos);
} catch (SyntaxException ex) {
return new CaInfo(error(), s.length());
}
}
示例7: newScanner
import com.sun.tools.javac.parser.Scanner; //导入依赖的package包/类
@Override
public Scanner newScanner(CharSequence input, boolean keepDocComments) {
if (input instanceof CharBuffer) {
CharBuffer buf = (CharBuffer) input;
return new CommentCollectingScanner(this, new CommentCollectingTokenizer(this, buf));
}
char[] array = input.toString().toCharArray();
return newScanner(array, array.length, keepDocComments);
}
示例8: preRegister
import com.sun.tools.javac.parser.Scanner; //导入依赖的package包/类
@SuppressWarnings("all")
public static void preRegister(final Context context) {
if (context.get(scannerFactoryKey) == null) {
// Careful! There is voodoo magic here!
//
// Context.Factory is parameterized. make() is for javac6 and below; make(Context) is for javac7 and up.
// this anonymous inner class definition is intentionally 'raw' - the return type of both 'make' methods is 'T',
// which means the compiler will only generate the correct "real" override method (with returntype Object, which is
// the lower bound for T, as a synthetic accessor for the make with returntype ScannerFactory) for that make method which
// is actually on the classpath (either make() for javac6-, or make(Context) for javac7+).
//
// We normally solve this issue via src/stubs, with BOTH make methods listed, but for some reason the presence of a stubbed out
// Context (or even a complete copy, it doesn't matter) results in a really strange eclipse bug, where any mention of any kind
// of com.sun.tools.javac.tree.TreeMaker in a source file disables ALL usage of 'go to declaration' and auto-complete in the entire
// source file.
//
// Thus, in short:
// * Do NOT parameterize the anonymous inner class literal.
// * Leave the return types as 'j.l.Object'.
// * Leave both make methods intact; deleting one has no effect on javac6- / javac7+, but breaks the other. Hard to test for.
// * Do not stub com.sun.tools.javac.util.Context or any of its inner types, like Factory.
@SuppressWarnings("all")
class MyFactory implements Context.Factory {
// This overrides the javac6- version of make.
public Object make() {
return new CommentCollectingScannerFactory(context);
}
// This overrides the javac7+ version of make.
public Object make(Context c) {
return new CommentCollectingScannerFactory(c);
}
}
@SuppressWarnings("unchecked") Context.Factory<Scanner.Factory> factory = new MyFactory();
context.put(scannerFactoryKey, factory);
}
}
示例9: newScanner
import com.sun.tools.javac.parser.Scanner; //导入依赖的package包/类
@Override
public Scanner newScanner(CharSequence input) {
if (input instanceof CharBuffer) {
return new CommentCollectingScanner(this, (CharBuffer)input);
}
char[] array = input.toString().toCharArray();
return newScanner(array, array.length);
}
示例10: newScanner
import com.sun.tools.javac.parser.Scanner; //导入依赖的package包/类
@Override
public Scanner newScanner(CharSequence input, boolean keepDocComments) {
if (input instanceof CharBuffer) {
return new CommentCollectingScanner(this, (CharBuffer)input);
}
char[] array = input.toString().toCharArray();
return newScanner(array, array.length, keepDocComments);
}
示例11: test
import com.sun.tools.javac.parser.Scanner; //导入依赖的package包/类
static void test(boolean genEndPos) throws IOException {
Context context = new Context();
Options options = Options.instance(context);
options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");
Log log = Log.instance(context);
log.multipleErrors = true;
JavacFileManager.preRegister(context);
Scanner.Factory sfac = Scanner.Factory.instance(context);
Parser.Factory pfac = Parser.Factory.instance(context);
final String text =
"public class Foo {\n"
+ " public static void main(String[] args) {\n"
+ " if (args.length == 0)\n"
+ " System.out.println(\"no args\");\n"
+ " else\n"
+ " System.out.println(args.length + \" args\");\n"
+ " }\n"
+ "}\n";
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();
log.setEndPosTable(fo, tree.endPositions);
TreeScanner ts = new LogTester(log, tree.endPositions);
ts.scan(tree);
check(log.nerrors, 4, "errors");
check(log.nwarnings, 4, "warnings");
}
示例12: getTokens
import com.sun.tools.javac.parser.Scanner; //导入依赖的package包/类
public ImmutableList<ErrorProneToken> getTokens() {
Scanner scanner = new AccessibleScanner(scannerFactory, commentSavingTokenizer);
ImmutableList.Builder<ErrorProneToken> tokens = ImmutableList.builder();
do {
scanner.nextToken();
tokens.add(new ErrorProneToken(scanner.token()));
} while (scanner.token().kind != TokenKind.EOF);
return tokens.build();
}
示例13: readTokens
import com.sun.tools.javac.parser.Scanner; //导入依赖的package包/类
private static int readTokens(Scanner scanner, Token limitingToken, String unclosedError) {
scanner.nextToken();
while (scanner.token() != limitingToken) {
if (scanner.token() == Token.SEMI) {
throw new RuntimeException(unclosedError);
}
scanner.nextToken();
}
return scanner.pos();
}
示例14: getTokens
import com.sun.tools.javac.parser.Scanner; //导入依赖的package包/类
public List<? extends Pair<String, Integer>> getTokens(int start, int end) {
if (!hasSource()) {
throw new IllegalStateException ();
}
if (start < end) {
try {
ArrayList<Pair<String, Integer>> ret = new ArrayList<>();
ScannerFactory sf = ScannerFactory.instance(getJavacTask().getContext());
Scanner s = sf.newScanner(this.jfo.getCharContent(false).subSequence(start, end), false);
while(true) {
s.nextToken();
Tokens.Token t = s.token();
if (t == null || t.kind == Tokens.TokenKind.EOF)
break;
switch (t.kind) {
case DOT: case COMMA: case SEMI: case LPAREN: case RPAREN:
case LBRACKET: case RBRACKET: case LBRACE: case RBRACE:
ret.add(new Pair(t.kind.name, start + t.pos));
break;
default:
ret.add(new Pair(t.kind.toString(), start + t.pos));
}
}
return ret;
} catch (IOException ioe) {}
}
return null;
}
示例15: preRegister
import com.sun.tools.javac.parser.Scanner; //导入依赖的package包/类
public static void preRegister(final Context context) {
if (context.get(scannerFactoryKey) == null) {
context.put(scannerFactoryKey, new Context.Factory<Scanner.Factory>() {
public CommentCollectingScanner.Factory make() {
return new CommentCollectingScannerFactory(context);
}
public CommentCollectingScanner.Factory make(Context c) {
return new CommentCollectingScannerFactory(c);
}
});
}
}