Bug 125270 - ASTParser fails to parse text to ArrayInitializer expression
Summary: ASTParser fails to parse text to ArrayInitializer expression
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows NT
: P3 normal (vote)
Target Milestone: 3.2 M5   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 125271 (view as bug list)
Depends on:
Blocks:
 
Reported: 2006-01-25 21:21 EST by Ajay Patil CLA
Modified: 2006-02-15 10:20 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 Ajay Patil CLA 2006-01-25 21:21:34 EST
I tried to use ASTParser to parse text into expressions. 
i.e after calling ASTParser.setKind(K_EXPRESSION);

It works successfully in almost all cases, except for text which is
an array initializer such as:
String text = "{\"red\", \"yellow\"}";
 
The createAST method returns a CompilationUnit node instead of an ArrayInitializer node.

The problem string in the CompilationUnit node is: 
'Syntax error on tokens, delete these tokens'

Note: If the entire Java file is parsed, instead of just the code snippet, 
then the ArrayInitializer node is correctly created. 
So, this problem seems to be only for the ASTParser.setKind(K_EXPRESSION) case.

The code for the test case is as follows:

******************TEST_CASE ****************************************

import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;

import junit.framework.TestCase;

public class ArrayInitializerTest extends TestCase {

  public void testASTParser() {
		
    String text = "{\"red\", \"yellow\"}";
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setKind(ASTParser.K_EXPRESSION);
    parser.setSource(text.toCharArray());
    ASTNode node  = parser.createAST(null);
		
    if (node instanceof CompilationUnit) {
      CompilationUnit compilationUnit = (CompilationUnit) node;
      IProblem[] problems = compilationUnit.getProblems();
      StringBuffer buffer = new StringBuffer();
      for (int i=0; i<problems.length; i++) {
        buffer.append("Problem = " + problems[i].getMessage());
      }
			
      fail(buffer.toString());
    }
  }		
}
Comment 1 Olivier Thomann CLA 2006-01-25 21:35:27 EST
*** Bug 125271 has been marked as a duplicate of this bug. ***
Comment 2 Olivier Thomann CLA 2006-01-26 14:47:36 EST
Array initializers are not considered as valid expression when parsing only an expression. The problem is in the grammar for the expression goal.
The fix is trivial.
Comment 3 Olivier Thomann CLA 2006-01-26 14:54:48 EST
Fixed and released in HEAD.
Regression test added in org.eclipse.jdt.core.tests.dom.ASTConverterTestAST3_2.test0628.
Comment 4 David Audel CLA 2006-02-15 10:20:39 EST
Verified for 3.2 M5 using build I20060215-0010