Community
Participate
Working Groups
I20030703++ Example: public void foo() { if (true) int i= 10; } procduces a compile error since a declaration must occur inside a block. Should this be documented in the AST world ?
I think the DOM AST tells somewhere that it accepts a superset of the grammar, but I agree with you that this scenario should be addressed. Some more validation check should occur when constructing such an IF statement (same applies to other controls).
DOM/AST must reflect the source. It doesn't mean that there is no compile errors. The DOM/AST tree simply reflects the source code. If the source code is incorrect, then the generated code can lead to compile errors. The construction of a tree simply tries to garantee that it remains a tree. It is easily possible to build a tree using methods on the org.eclipse.jdt.dom.AST class that won't compile. It is up to the user to know what he/she is doing.
Per construction, the DOM ast still prevents obvious errors to occur. For instance, you cannot attach a type declaration as a statement of an IF, though based on your previous comment, it could have been legite. Either it should be documented, or checked more strictly... either one is ok. The fact we got a PR means that there is some misunderstanding.
I am only asking for some documentation although one can argue that the language spec already documents it. More strikt checking would lead to "incomplete" AST since we wouldn't get an AST for the provided example. And this isn't something I would expect to happen.
DOM/AST ensures that you add the right "type" on nodes in each collection. For example, you cannot add an expression as a child of a block. It is specified to be a statement. For the same reason, you cannot add a type declaration inside a block. So I don't see what we could add. Each type of children is documented. My previous comment was based on the fact that you try to put a statement in the then clause of an if statement. We don't check anything else than the fact that it is a statement. A local variable declaration statement is a statement. Therefore it is legite inside an if statement even if the code won't compile.
Jim, could you please clarify the doc in this case? We don't need to add more checks in the code. Simply add a note to clarify the fact that the DOM/AST can handle a superset of the Java grammar.
Add CC
Added boilerplate to Ifstatement.setThenStatement * Special note: The Java language does not allow a local variable declaration * to appear as the "then" part of an if statement (they may only appear within a * block). However, the AST will allow a <code>VariableDeclarationStatement</code> * as the thenStatement of a <code>IfStatement</code>. To get something that will * compile, be sure to embed the <code>VariableDeclarationStatement</code> * inside a <code>Block</code>. Also in the other places with a similar gotcha: Ifstatement.setElseStatement Whilestatement.setBody DoStatement.setBody ForStatement.setBody LabeledStatement.setBody Fixed in builds > N20031114. Updated release notes.
Verified.