Bug 39512 - API needed to parse expressions and statements
Summary: API needed to parse expressions and statements
Status: RESOLVED DUPLICATE of bug 48489
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.1   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: 3.0 M7   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-07-01 16:17 EDT by David J. Orme CLA
Modified: 2004-01-16 13:42 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David J. Orme CLA 2003-07-01 16:17:15 EDT
Sometimes it's nice to be able to parse a String representing just an Expression
or just a Statement.  Here's some code originally written by Konstantin for
EclipseColorer that I think should be considered for inclusion into Eclipse JDT
Core:

package org.eclipse.jdt.core.dom;
import java.util.ArrayList;

import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.compiler.AbstractSyntaxTreeVisitorAdapter;
import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
/**
 * @author scheglov_ke
 * @author daveo - Updated to use StringBuffer; provided 2.0.2 hack
 */
public class JavaCoreUtils {
	public static Expression parseExpression(AST ast, final int start, String src,
IJavaProject project)
		throws JavaModelException {
		StringBuffer source = new StringBuffer("class Fake { public void foo()
{System.out.println(");
		final int baseLength = source.length();
		source.append(src);
		source.append(");}}");
		//
		final ArrayList resultList = new ArrayList();
		CompilationUnit cu = parseCompilationUnit(ast, source.toString(), project);
		cu.accept(new ASTVisitor() {
			public void preVisit(ASTNode node) {
				if ((node instanceof Expression) && (node.getStartPosition() == baseLength)) {
					resultList.add(node);
				}
				int pos = node.getStartPosition();
				int length = node.getLength();
				if (pos >= baseLength)
					node.setSourceRange(start + pos - baseLength, length);
			}
		});
		//
		Expression expression = (Expression) resultList.get(0);
		expression.setParent(null);
		//((Block) expression.getParent()).statements().remove(expression);
		return expression;
	}
	public static Statement parseStatement(AST ast, final int start, String src,
IJavaProject project)
		throws JavaModelException {
		StringBuffer source = new StringBuffer("class Fake { public void foo() {");
		final int baseLength = source.length();
		source.append(src);
		source.append(";}}");
		//
		final ArrayList resultList = new ArrayList();
		CompilationUnit cu = parseCompilationUnit(ast, source.toString(), project);
		cu.accept(new ASTVisitor() {
			public void preVisit(ASTNode node) {
				if ((node instanceof Statement) && (node.getStartPosition() == baseLength)) {
					resultList.add(node);
				}
				int pos = node.getStartPosition();
				int length = node.getLength();
				if (pos >= baseLength)
					node.setSourceRange(start + pos - baseLength, length);
			}
		});
		//
		Statement statement = (Statement) resultList.get(0);
		((Block) statement.getParent()).statements().remove(statement);
		return statement;
	}
	public static CompilationUnit parseCompilationUnit(AST ast, String source,
IJavaProject project)
		throws JavaModelException {
		if (source == null)
			throw new IllegalArgumentException();
		if (project == null)
			throw new IllegalArgumentException();
		CompilationUnitDeclaration compilationUnitDeclaration =
			CompilationUnitResolver.resolve(
				source.toCharArray(),
				"fakeUnit",
				project,
				new AbstractSyntaxTreeVisitorAdapter());

////////////////////////////////////////////
// 2.0.2 version here
//        ASTConverter converter = new ASTConverter(true);

////////////////////////////////////////////
// 2.1 version here
        ASTConverter converter = new ASTConverter(project.getOptions(true), true);

		//AST ast = new AST();
		BindingResolver resolver = new
DefaultBindingResolver(compilationUnitDeclaration.scope);
		ast.setBindingResolver(resolver);
		converter.setAST(ast);
		CompilationUnit cu = converter.convert(compilationUnitDeclaration,
source.toCharArray());
	
cu.setLineEndTable(compilationUnitDeclaration.compilationResult.lineSeparatorPositions);
		resolver.storeModificationCount(ast.modificationCount());
		return cu;
	}
}
Comment 1 Philipe Mulet CLA 2003-07-02 12:41:47 EDT
Parsing is likely easy, but binding resolution will require proper context.
Comment 2 David J. Orme CLA 2003-07-02 12:48:24 EDT
That is correct.  The submitted code does not do binding resolution.
Comment 3 Olivier Thomann CLA 2004-01-16 13:07:49 EST
Let me know if you think that the support for bug 48489 is enough.
Comment 4 David J. Orme CLA 2004-01-16 13:37:57 EST
Ths is a dupe of Bug 48489.  I needed the API for exactly the same reason.

*** This bug has been marked as a duplicate of 48489 ***
Comment 5 Olivier Thomann CLA 2004-01-16 13:42:42 EST
Could you please try it when next integration build is out and let me know  by 
annotating this PR or bug 48489 that it works as expected?
Thanks.