com.sun.tools.javac.parser.ScannerFactory Java Examples

The following examples show how to use com.sun.tools.javac.parser.ScannerFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JavaLexerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
void run() throws Exception {
    Context ctx = new Context();
    Log log = Log.instance(ctx);
    String input = "0bL 0b20L 0xL ";
    log.useSource(new SimpleJavaFileObject(new URI("mem://Test.java"), JavaFileObject.Kind.SOURCE) {
        @Override
        public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
            return input;
        }
    });
    char[] inputArr = input.toCharArray();
    JavaTokenizer tokenizer = new JavaTokenizer(ScannerFactory.instance(ctx), inputArr, inputArr.length) {
    };

    assertKind(input, tokenizer, TokenKind.LONGLITERAL, "0bL");
    assertKind(input, tokenizer, TokenKind.LONGLITERAL, "0b20L");
    assertKind(input, tokenizer, TokenKind.LONGLITERAL, "0xL");
}
 
Example #2
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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 = newParser(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);
    }
}
 
Example #3
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static JCStatement parseStatement(Context context, CharSequence stmt, SourcePositions[] pos, final List<Diagnostic<? extends JavaFileObject>> errors) {
    if (stmt == 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((stmt+"\u0000").toCharArray(), 0, stmt.length());
        ParserFactory factory = ParserFactory.instance(context);
        ScannerFactory scannerFactory = ScannerFactory.instance(context);
        Names names = Names.instance(context);
        Parser parser = newParser(context, (NBParserFactory) factory, scannerFactory.newScanner(buf, false), false, false, CancelService.instance(context), names);
        if (parser instanceof JavacParser) {
            if (pos != null)
                pos[0] = new ParserSourcePositions((JavacParser)parser);
            return parser.parseStatement();
        }
        return null;
    } finally {
        compiler.log.useSource(prev);
        compiler.log.popDiagnosticHandler(discardHandler);
    }
}
 
Example #4
Source File: CompletenessAnalyzer.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
CompletenessAnalyzer(JShell proc) {
    this.proc = proc;
    Context context = new Context();
    Log log = CaLog.createLog(context);
    context.put(Log.class, log);
    context.put(Source.class, Source.JDK1_9);
    scannerFactory = ScannerFactory.instance(context);
}
 
Example #5
Source File: ManParserFactory.java    From manifold with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
public JavacParser newParser( CharSequence input, boolean keepDocComments, boolean keepEndPos, boolean keepLineMap, boolean parseModuleInfo )
{
  input = _preprocessor.process( _taskEvent.getSourceFile(), input );
  mapInput( _taskEvent.getSourceFile(), input );
  Lexer lexer = ((ScannerFactory)ReflectUtil.field( this, "scannerFactory" ).get()).newScanner(input, keepDocComments);
  return (JavacParser)ReflectUtil.constructor( "com.sun.tools.javac.parser.ManJavacParser",
    ParserFactory.class, Lexer.class, boolean.class, boolean.class, boolean.class, boolean.class )
    .newInstance( this, lexer, keepDocComments, keepLineMap, keepEndPos, parseModuleInfo );
}
 
Example #6
Source File: ManParserFactory.java    From manifold with Apache License 2.0 5 votes vote down vote up
public static ScannerFactory instance( Context ctx, ManParserFactory parserFactory )
{
  ScannerFactory scannerFactory = ctx.get( scannerFactoryKey );
  if( !(scannerFactory instanceof ManScannerFactory) )
  {
    ctx.put( scannerFactoryKey, (ScannerFactory)null );
    scannerFactory = new ManScannerFactory( ctx, parserFactory );
  }

  return scannerFactory;
}
 
Example #7
Source File: CommentCollectingParserFactory.java    From EasyMPermission with MIT License 5 votes vote down vote up
public Parser newParser(CharSequence input, boolean keepDocComments, boolean keepEndPos, boolean keepLineMap) {
	ScannerFactory scannerFactory = ScannerFactory.instance(context);
	Lexer lexer = scannerFactory.newScanner(input, true);
	Object x = new CommentCollectingParser(this, lexer, true, keepLineMap);
	return (Parser) x;
	// CCP is based on a stub which extends nothing, but at runtime the stub is replaced with either
	//javac6's EndPosParser which extends Parser, or javac7's EndPosParser which implements Parser.
	//Either way this will work out.
}
 
Example #8
Source File: CommentCollectingScannerFactory.java    From EasyMPermission with MIT License 5 votes vote down vote up
@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.
			public Object make(Context c) {
				return new CommentCollectingScannerFactory(c);
			}
		}
		@SuppressWarnings("unchecked") Context.Factory<ScannerFactory> factory = new MyFactory();
		context.put(scannerFactoryKey, factory);
	}
}
 
Example #9
Source File: CommentCollectingParserFactory.java    From EasyMPermission with MIT License 5 votes vote down vote up
public JavacParser newParser(CharSequence input, boolean keepDocComments, boolean keepEndPos, boolean keepLineMap) {
	ScannerFactory scannerFactory = ScannerFactory.instance(context);
	Lexer lexer = scannerFactory.newScanner(input, true);
	Object x = new CommentCollectingParser(this, lexer, true, keepLineMap, keepEndPos);
	return (JavacParser) x;
	// CCP is based on a stub which extends nothing, but at runtime the stub is replaced with either
	//javac6's EndPosParser which extends Parser, or javac8's JavacParser which implements Parser.
	//Either way this will work out.
}
 
Example #10
Source File: CommentCollectingScannerFactory.java    From EasyMPermission with MIT License 5 votes vote down vote up
@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.
			public Object make(Context c) {
				return new CommentCollectingScannerFactory(c);
			}
		}
		@SuppressWarnings("unchecked") Context.Factory<ScannerFactory> factory = new MyFactory();
		context.put(scannerFactoryKey, factory);
	}
}
 
Example #11
Source File: JavacTokens.java    From google-java-format with Apache License 2.0 4 votes vote down vote up
protected AccessibleReader(ScannerFactory fac, char[] buffer, int length) {
  super(fac, buffer, length);
}
 
Example #12
Source File: JavacTokens.java    From google-java-format with Apache License 2.0 4 votes vote down vote up
protected AccessibleScanner(ScannerFactory fac, JavaTokenizer tokenizer) {
  super(fac, tokenizer);
}
 
Example #13
Source File: JavacTokens.java    From google-java-format with Apache License 2.0 4 votes vote down vote up
CommentSavingTokenizer(ScannerFactory fac, char[] buffer, int length) {
  super(fac, buffer, length);
}
 
Example #14
Source File: JavacTokens.java    From google-java-format with Apache License 2.0 4 votes vote down vote up
/** Lex the input and return a list of {@link RawTok}s. */
public static ImmutableList<RawTok> getTokens(
    String source, Context context, Set<TokenKind> stopTokens) {
  if (source == null) {
    return ImmutableList.of();
  }
  ScannerFactory fac = ScannerFactory.instance(context);
  char[] buffer = (source + EOF_COMMENT).toCharArray();
  Scanner scanner =
      new AccessibleScanner(fac, new CommentSavingTokenizer(fac, buffer, buffer.length));
  ImmutableList.Builder<RawTok> tokens = ImmutableList.builder();
  int end = source.length();
  int last = 0;
  do {
    scanner.nextToken();
    Token t = scanner.token();
    if (t.comments != null) {
      for (Comment c : Lists.reverse(t.comments)) {
        if (last < c.getSourcePos(0)) {
          tokens.add(new RawTok(null, null, last, c.getSourcePos(0)));
        }
        tokens.add(
            new RawTok(null, null, c.getSourcePos(0), c.getSourcePos(0) + c.getText().length()));
        last = c.getSourcePos(0) + c.getText().length();
      }
    }
    if (stopTokens.contains(t.kind)) {
      if (t.kind != TokenKind.EOF) {
        end = t.pos;
      }
      break;
    }
    if (last < t.pos) {
      tokens.add(new RawTok(null, null, last, t.pos));
    }
    tokens.add(
        new RawTok(
            t.kind == TokenKind.STRINGLITERAL ? "\"" + t.stringVal() + "\"" : null,
            t.kind,
            t.pos,
            t.endPos));
    last = t.endPos;
  } while (scanner.token().kind != TokenKind.EOF);
  if (last < end) {
    tokens.add(new RawTok(null, null, last, end));
  }
  return tokens.build();
}
 
Example #15
Source File: CommentCollectingScanner.java    From EasyMPermission with MIT License 4 votes vote down vote up
public CommentCollectingScanner(ScannerFactory fac, CommentCollectingTokenizer tokenizer) {
	super(fac, tokenizer);
	this.tokenizer = tokenizer;
}
 
Example #16
Source File: CommentCollectingTokenizer.java    From EasyMPermission with MIT License 4 votes vote down vote up
public PositionUnicodeReader(ScannerFactory sf, CharBuffer buffer) {
	super(sf, buffer);
}
 
Example #17
Source File: CommentCollectingTokenizer.java    From EasyMPermission with MIT License 4 votes vote down vote up
protected PositionUnicodeReader(ScannerFactory sf, char[] input, int inputLength) {
	super(sf, input, inputLength);
}
 
Example #18
Source File: CommentCollectingTokenizer.java    From EasyMPermission with MIT License 4 votes vote down vote up
CommentCollectingTokenizer(ScannerFactory fac, CharBuffer buf) {
	super(fac, new PositionUnicodeReader(fac, buf));
}
 
Example #19
Source File: CommentCollectingTokenizer.java    From EasyMPermission with MIT License 4 votes vote down vote up
CommentCollectingTokenizer(ScannerFactory fac, char[] buf, int inputLength) {
	super(fac, new PositionUnicodeReader(fac, buf, inputLength));
}
 
Example #20
Source File: TrialParserFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
protected TrialParserFactory(Context context) {
    super(context);
    this.scannerFactory = ScannerFactory.instance(context);
}
 
Example #21
Source File: Documentifier.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private Documentifier(Context context) {
    docGen = new DocCommentGenerator();
    scanners = ScannerFactory.instance(context);
}
 
Example #22
Source File: ReplParserFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
protected ReplParserFactory(Context context, boolean forceExpression) {
    super(context);
    this.forceExpression = forceExpression;
    this.scannerFactory = ScannerFactory.instance(context);
}
 
Example #23
Source File: NBParserFactory.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected NBParserFactory(Context context) {
    super(context);
    this.scannerFactory = ScannerFactory.instance(context);
    this.names = Names.instance(context);
    this.cancelService = CancelService.instance(context);
}