List of usage examples for org.eclipse.jdt.internal.compiler.parser Parser Parser
public Parser(ProblemReporter problemReporter, boolean optimizeStringLiterals)
From source file:com.android.tools.idea.lint.LombokPsiConverterTest.java
License:Apache License
@Nullable private static Node parse(String code) { CompilerOptions options = new CompilerOptions(); options.complianceLevel = options.sourceLevel = options.targetJDK = ClassFileConstants.JDK1_7; options.parseLiteralExpressionsAsConstants = true; ProblemReporter problemReporter = new ProblemReporter(DefaultErrorHandlingPolicies.exitOnFirstError(), options, new DefaultProblemFactory()); Parser parser = new Parser(problemReporter, options.parseLiteralExpressionsAsConstants); parser.javadocParser.checkDocComment = false; EcjTreeConverter converter = new EcjTreeConverter(); org.eclipse.jdt.internal.compiler.batch.CompilationUnit sourceUnit = new org.eclipse.jdt.internal.compiler.batch.CompilationUnit( code.toCharArray(), "unitTest", "UTF-8"); CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0); CompilationUnitDeclaration unit = parser.parse(sourceUnit, compilationResult); if (unit == null) { return null; }// w w w. ja va 2s .co m converter.visit(code, unit); List<? extends Node> nodes = converter.getAll(); for (lombok.ast.Node node : nodes) { if (node instanceof lombok.ast.CompilationUnit) { return node; } } return null; }
From source file:com.android.tools.lint.EcjParser.java
License:Apache License
private Parser getParser() { if (mParser == null) { CompilerOptions options = createCompilerOptions(); ProblemReporter problemReporter = new ProblemReporter(DefaultErrorHandlingPolicies.exitOnFirstError(), options, new DefaultProblemFactory()); mParser = new Parser(problemReporter, options.parseLiteralExpressionsAsConstants); mParser.javadocParser.checkDocComment = false; }//from w ww . j a v a 2s .c o m return mParser; }
From source file:com.codenvy.ide.ext.java.server.internal.compiler.parser.SourceTypeConverter.java
License:Open Source License
private CompilationUnitDeclaration convert(ISourceType[] sourceTypes, CompilationResult compilationResult) throws JavaModelException { this.unit = new CompilationUnitDeclaration(this.problemReporter, compilationResult, 0); // not filled at this point if (sourceTypes.length == 0) return this.unit; SourceTypeElementInfo topLevelTypeInfo = (SourceTypeElementInfo) sourceTypes[0]; org.eclipse.jdt.core.ICompilationUnit cuHandle = topLevelTypeInfo.getHandle().getCompilationUnit(); this.cu = (ICompilationUnit) cuHandle; final CompilationUnitElementInfo compilationUnitElementInfo = (CompilationUnitElementInfo) ((JavaElement) this.cu) .getElementInfo();//ww w . ja v a 2 s. c o m if (this.has1_5Compliance && (compilationUnitElementInfo.annotationNumber >= CompilationUnitElementInfo.ANNOTATION_THRESHOLD_FOR_DIET_PARSE || (compilationUnitElementInfo.hasFunctionalTypes && (this.flags & LOCAL_TYPE) != 0))) { // If more than 10 annotations, diet parse as this is faster, but not if // the client wants local and anonymous types to be converted (https://bugs.eclipse.org/bugs/show_bug.cgi?id=254738) // Also see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=405843 if ((this.flags & LOCAL_TYPE) == 0) { return new Parser(this.problemReporter, true).dietParse(this.cu, compilationResult); } else { return new Parser(this.problemReporter, true).parse(this.cu, compilationResult); } } /* only positions available */ int start = topLevelTypeInfo.getNameSourceStart(); int end = topLevelTypeInfo.getNameSourceEnd(); /* convert package and imports */ String[] packageName = ((PackageFragment) cuHandle.getParent()).names; if (packageName.length > 0) // if its null then it is defined in the default package this.unit.currentPackage = createImportReference(packageName, start, end, false, ClassFileConstants.AccDefault); IImportDeclaration[] importDeclarations = topLevelTypeInfo.getHandle().getCompilationUnit().getImports(); int importCount = importDeclarations.length; this.unit.imports = new ImportReference[importCount]; for (int i = 0; i < importCount; i++) { ImportDeclaration importDeclaration = (ImportDeclaration) importDeclarations[i]; ISourceImport sourceImport = (ISourceImport) importDeclaration.getElementInfo(); String nameWithoutStar = importDeclaration.getNameWithoutStar(); this.unit.imports[i] = createImportReference( Util.splitOn('.', nameWithoutStar, 0, nameWithoutStar.length()), sourceImport.getDeclarationSourceStart(), sourceImport.getDeclarationSourceEnd(), importDeclaration.isOnDemand(), sourceImport.getModifiers()); } /* convert type(s) */ try { int typeCount = sourceTypes.length; final TypeDeclaration[] types = new TypeDeclaration[typeCount]; /* * We used a temporary types collection to prevent this.unit.types from being null during a call to * convert(...) when the source is syntactically incorrect and the parser is flushing the unit's types. * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=97466 */ for (int i = 0; i < typeCount; i++) { SourceTypeElementInfo typeInfo = (SourceTypeElementInfo) sourceTypes[i]; types[i] = convert((SourceType) typeInfo.getHandle(), compilationResult); } this.unit.types = types; return this.unit; } catch (AnonymousMemberFound e) { return new Parser(this.problemReporter, true).parse(this.cu, compilationResult); } }
From source file:com.codenvy.ide.ext.java.server.internal.compiler.parser.SourceTypeConverter.java
License:Open Source License
private FieldDeclaration convert(SourceField fieldHandle, TypeDeclaration type, CompilationResult compilationResult) throws JavaModelException { SourceFieldElementInfo fieldInfo = (SourceFieldElementInfo) fieldHandle.getElementInfo(); FieldDeclaration field = new FieldDeclaration(); int start = fieldInfo.getNameSourceStart(); int end = fieldInfo.getNameSourceEnd(); field.name = fieldHandle.getElementName().toCharArray(); field.sourceStart = start;/* ww w . j a v a2s .c om*/ field.sourceEnd = end; field.declarationSourceStart = fieldInfo.getDeclarationSourceStart(); field.declarationSourceEnd = fieldInfo.getDeclarationSourceEnd(); int modifiers = fieldInfo.getModifiers(); boolean isEnumConstant = (modifiers & ClassFileConstants.AccEnum) != 0; if (isEnumConstant) { field.modifiers = modifiers & ~ClassFileConstants.AccEnum; // clear AccEnum bit onto AST (binding will add it) } else { field.modifiers = modifiers; field.type = createTypeReference(fieldInfo.getTypeName(), start, end); } // convert 1.5 specific constructs only if compliance is 1.5 or above if (this.has1_5Compliance) { /* convert annotations */ field.annotations = convertAnnotations(fieldHandle); } /* conversion of field constant */ if ((this.flags & FIELD_INITIALIZATION) != 0) { char[] initializationSource = fieldInfo.getInitializationSource(); if (initializationSource != null) { if (this.parser == null) { this.parser = new Parser(this.problemReporter, true); } this.parser.parse(field, type, this.unit, initializationSource); } } /* conversion of local and anonymous types */ if ((this.flags & LOCAL_TYPE) != 0) { IJavaElement[] children = fieldInfo.getChildren(); int childrenLength = children.length; if (childrenLength == 1) { field.initialization = convert(children[0], isEnumConstant ? field : null, compilationResult); } else if (childrenLength > 1) { ArrayInitializer initializer = new ArrayInitializer(); field.initialization = initializer; Expression[] expressions = new Expression[childrenLength]; initializer.expressions = expressions; for (int i = 0; i < childrenLength; i++) { expressions[i] = convert(children[i], isEnumConstant ? field : null, compilationResult); } } } return field; }
From source file:com.codenvy.ide.ext.java.server.internal.compiler.parser.SourceTypeConverter.java
License:Open Source License
private Expression parseMemberValue(char[] memberValue) { // memberValue must not be null if (this.parser == null) { this.parser = new Parser(this.problemReporter, true); }/*from ww w .j ava2 s.com*/ return this.parser.parseMemberValue(memberValue, 0, memberValue.length, this.unit); }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.BasicSearchEngine.java
License:Open Source License
private Parser getParser() { if (this.parser == null) { this.compilerOptions = new CompilerOptions(JavaCore.getOptions()); ProblemReporter problemReporter = new ProblemReporter( DefaultErrorHandlingPolicies.proceedWithAllProblems(), this.compilerOptions, new DefaultProblemFactory()); this.parser = new Parser(problemReporter, true); }/*from ww w .j a va 2 s.c om*/ return this.parser; }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java
License:Open Source License
protected Parser basicParser() { if (this.basicParser == null) { ProblemReporter problemReporter = new ProblemReporter( DefaultErrorHandlingPolicies.proceedWithAllProblems(), this.options, new DefaultProblemFactory()); this.basicParser = new Parser(problemReporter, false); this.basicParser.reportOnlyOneSyntaxError = true; }/*w w w .ja v a 2s .com*/ return this.basicParser; }
From source file:com.redhat.ceylon.eclipse.core.model.loader.JDTModelLoader.java
License:Open Source License
public void createLookupEnvironment() { try {// w w w . j a v a2 s. co m lookupEnvironment = new LookupEnvironment(new ITypeRequestor() { private Parser basicParser; @Override public void accept(ISourceType[] sourceTypes, PackageBinding packageBinding, AccessRestriction accessRestriction) { // case of SearchableEnvironment of an IJavaProject is used ISourceType sourceType = sourceTypes[0]; while (sourceType.getEnclosingType() != null) sourceType = sourceType.getEnclosingType(); if (sourceType instanceof SourceTypeElementInfo) { // get source SourceTypeElementInfo elementInfo = (SourceTypeElementInfo) sourceType; IType type = elementInfo.getHandle(); ICompilationUnit sourceUnit = (ICompilationUnit) type.getCompilationUnit(); accept(sourceUnit, accessRestriction); } else { CompilationResult result = new CompilationResult(sourceType.getFileName(), 1, 1, 0); CompilationUnitDeclaration unit = SourceTypeConverter.buildCompilationUnit(sourceTypes, SourceTypeConverter.FIELD_AND_METHOD // need field and methods | SourceTypeConverter.MEMBER_TYPE, // need member types // no need for field initialization lookupEnvironment.problemReporter, result); lookupEnvironment.buildTypeBindings(unit, accessRestriction); lookupEnvironment.completeTypeBindings(unit, true); } } @Override public void accept(IBinaryType binaryType, PackageBinding packageBinding, AccessRestriction accessRestriction) { lookupEnvironment.createBinaryTypeFrom(binaryType, packageBinding, accessRestriction); } @Override public void accept(ICompilationUnit sourceUnit, AccessRestriction accessRestriction) { // Switch the current policy and compilation result for this unit to the requested one. CompilationResult unitResult = new CompilationResult(sourceUnit, 1, 1, compilerOptions.maxProblemsPerUnit); try { CompilationUnitDeclaration parsedUnit = basicParser().dietParse(sourceUnit, unitResult); lookupEnvironment.buildTypeBindings(parsedUnit, accessRestriction); lookupEnvironment.completeTypeBindings(parsedUnit, true); } catch (AbortCompilationUnit e) { // at this point, currentCompilationUnitResult may not be sourceUnit, but some other // one requested further along to resolve sourceUnit. if (unitResult.compilationUnit == sourceUnit) { // only report once //requestor.acceptResult(unitResult.tagAsAccepted()); } else { throw e; // want to abort enclosing request to compile } } // Display unit error in debug mode if (BasicSearchEngine.VERBOSE) { if (unitResult.problemCount > 0) { System.out.println(unitResult); } } } private Parser basicParser() { if (this.basicParser == null) { ProblemReporter problemReporter = new ProblemReporter( DefaultErrorHandlingPolicies.proceedWithAllProblems(), compilerOptions, new DefaultProblemFactory()); this.basicParser = new Parser(problemReporter, false); this.basicParser.reportOnlyOneSyntaxError = true; } return this.basicParser; } }, compilerOptions, problemReporter, ((JavaProject) javaProject).newSearchableNameEnvironment((WorkingCopyOwner) null)); } catch (JavaModelException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:lombok.ast.grammar.EcjTreeBuilderTest.java
License:Open Source License
@Override protected ASTNode parseWithTargetCompiler(Source source) { CompilerOptions compilerOptions = ecjCompilerOptions(); Parser parser = new Parser(new ProblemReporter(DefaultErrorHandlingPolicies.proceedWithAllProblems(), compilerOptions, new DefaultProblemFactory()), compilerOptions.parseLiteralExpressionsAsConstants); parser.javadocParser.checkDocComment = true; CompilationUnit sourceUnit = new CompilationUnit(source.getRawInput().toCharArray(), source.getName(), "UTF-8"); CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0); CompilationUnitDeclaration cud = parser.parse(sourceUnit, compilationResult); if (cud.hasErrors()) return null; return cud;//from ww w . j av a 2 s .com }
From source file:lombok.ast.grammar.EcjTreeConverterType1Test.java
License:Open Source License
protected Node parseWithTargetCompiler(Source source) { CompilerOptions compilerOptions = ecjCompilerOptions(); Parser parser = new Parser(new ProblemReporter(DefaultErrorHandlingPolicies.proceedWithAllProblems(), compilerOptions, new DefaultProblemFactory()), compilerOptions.parseLiteralExpressionsAsConstants); parser.javadocParser.checkDocComment = true; CompilationUnit sourceUnit = new CompilationUnit(source.getRawInput().toCharArray(), source.getName(), "UTF-8"); CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0); CompilationUnitDeclaration cud = parser.parse(sourceUnit, compilationResult); if (cud.hasErrors()) return null; EcjTreeConverter converter = new EcjTreeConverter(); converter.visit(source.getRawInput(), cud); return converter.get(); }