Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Web Tools Project (WTP) » [jsdt] possible bug in ASTParser parsing FunctionExpression
[jsdt] possible bug in ASTParser parsing FunctionExpression [message #647086] Tue, 04 January 2011 09:21
Harald Finster is currently offline Harald Finster
Messages: 37
Registered: July 2009
Member
Hello,

could one of the JSDT-ASTParser-experts please have a look at the following?

If I try to parse the ObjectLiteral in the JavaScript compilation unit below

var x = {
lit : {} ,
func : function() { var x=0; } ,
str : "Hello"
};

the length of the FunctionExpression, i.e. the initializer of the 'func'
ObjectLiteralField seems to be incorrect (the closing '}' seems to be missing).

If I calculate the start and length of the initializers and write the
associated sections of the document, I get the following output:

lit : {} (correct)
func: function() { var x=0; (with trailing space) (incorrect, i.e. the } is missing
str : "Hello" (correct)

If I write the initializers using "toString()" everything seems to be fine,
i.e. I get

lit : {}

func : function (){
var x=0;
}

str : "Hello"



Please find a quick-and-dirty test program below to reproduce the error:

--- snip ---

package de.topsystem.tsdd.model.test.bugreport;

import java.util.List;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.wst.jsdt.core.dom.AST;
import org.eclipse.wst.jsdt.core.dom.ASTNode;
import org.eclipse.wst.jsdt.core.dom.ASTParser;
import org.eclipse.wst.jsdt.core.dom.JavaScriptUnit;
import org.eclipse.wst.jsdt.core.dom.ObjectLiteral;
import org.eclipse.wst.jsdt.core.dom.ObjectLiteralField;
import org.eclipse.wst.jsdt.core.dom.VariableDeclarationFragment;
import org.eclipse.wst.jsdt.core.dom.VariableDeclarationStatement;

public class TestASTFunction {

public static void main(String[] args) throws BadLocationException {

Document document = new Document(
"var x = { lit : {} , func : function() { var x=0; } , str : \"Hello\" };");

ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(document.get().toCharArray());

parser.setKind(ASTParser.K_COMPILATION_UNIT);

JavaScriptUnit unit = (JavaScriptUnit) parser.createAST(null);

System.out.println("unit = " + unit);

ObjectLiteral objectLiteral = getObjectLiteral(unit);

System.out.println("ObjectLiteral = " + objectLiteral);

for (ObjectLiteralField field : (List<ObjectLiteralField>) objectLiteral
.fields()) {
ASTNode initializer = field.getInitializer();
System.out.println("initializer class = " + initializer.getClass());
System.out.println("initializer = `" + initializer + '`');

int start = initializer.getStartPosition();
int length = initializer.getLength();

String initializerText = document.get(start, length);

System.out.println("initializer text =`" + initializerText + "`");

}

}

//
// just gets the ObjectLiteral from the CompilationUnit
//
static ObjectLiteral getObjectLiteral(JavaScriptUnit unit) {

VariableDeclarationStatement statement = (VariableDeclarationStatement) unit
.statements().get(0);

VariableDeclarationFragment fragment = (VariableDeclarationFragment) statement
.fragments().get(0);

return (ObjectLiteral) fragment.getInitializer();

}

}


--- snip ---


Version info (from MANIFEST.MF)

Bundle-SymbolicName: org.eclipse.wst.jsdt.core; singleton:=true
Bundle-Version: 1.0.201.qualifier


Thanks for any feedback!

Greetings Harald
Previous Topic:HTML/JSP Formatting
Next Topic:Web Page editor only shows me some part of a file
Goto Forum:
  


Current Time: Thu Apr 17 14:41:40 EDT 2014

Powered by FUDForum. Page generated in 0.01672 seconds