1 /*******************************************************************************
2 * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.html
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.parser;
14 * Internal field structure for parsing recovery
16 import net.sourceforge.phpdt.internal.compiler.ast.AnonymousLocalTypeDeclaration;
17 import net.sourceforge.phpdt.internal.compiler.ast.ArrayTypeReference;
18 import net.sourceforge.phpdt.internal.compiler.ast.AstNode;
19 import net.sourceforge.phpdt.internal.compiler.ast.Expression;
20 import net.sourceforge.phpdt.internal.compiler.ast.FieldDeclaration;
21 import net.sourceforge.phpdt.internal.compiler.ast.Statement;
22 import net.sourceforge.phpdt.internal.compiler.ast.TypeDeclaration;
24 public class RecoveredField extends RecoveredElement {
26 public FieldDeclaration fieldDeclaration;
27 boolean alreadyCompletedFieldInitialization;
29 public RecoveredType[] anonymousTypes;
30 public int anonymousTypeCount;
31 public RecoveredField(FieldDeclaration fieldDeclaration, RecoveredElement parent, int bracketBalance){
32 this(fieldDeclaration, parent, bracketBalance, null);
34 public RecoveredField(FieldDeclaration fieldDeclaration, RecoveredElement parent, int bracketBalance, Parser parser){
35 super(parent, bracketBalance, parser);
36 this.fieldDeclaration = fieldDeclaration;
37 this.alreadyCompletedFieldInitialization = fieldDeclaration.initialization != null;
40 * Record an expression statement if field is expecting an initialization expression,
41 * used for completion inside field initializers.
43 public RecoveredElement add(Statement statement, int bracketBalance) {
45 if (this.alreadyCompletedFieldInitialization || !(statement instanceof Expression)) {
46 return super.add(statement, bracketBalance);
48 this.alreadyCompletedFieldInitialization = true;
49 this.fieldDeclaration.initialization = (Expression)statement;
50 this.fieldDeclaration.declarationSourceEnd = statement.sourceEnd;
51 this.fieldDeclaration.declarationEnd = statement.sourceEnd;
56 * Record a type declaration if this field is expecting an initialization expression
57 * and the type is an anonymous type.
58 * Used for completion inside field initializers.
60 public RecoveredElement add(TypeDeclaration typeDeclaration, int bracketBalance) {
62 if (this.alreadyCompletedFieldInitialization
63 || !(typeDeclaration instanceof AnonymousLocalTypeDeclaration)
64 || (this.fieldDeclaration.declarationSourceEnd != 0 && typeDeclaration.sourceStart > this.fieldDeclaration.declarationSourceEnd)) {
65 return super.add(typeDeclaration, bracketBalance);
67 // Prepare anonymous type list
68 if (this.anonymousTypes == null) {
69 this.anonymousTypes = new RecoveredType[5];
70 this.anonymousTypeCount = 0;
72 if (this.anonymousTypeCount == this.anonymousTypes.length) {
76 (this.anonymousTypes = new RecoveredType[2 * this.anonymousTypeCount]),
78 this.anonymousTypeCount);
81 // Store type declaration as an anonymous type
82 RecoveredType element = new RecoveredType(typeDeclaration, this, bracketBalance);
83 this.anonymousTypes[this.anonymousTypeCount++] = element;
88 * Answer the associated parsed structure
90 public AstNode parseTree(){
91 return fieldDeclaration;
94 * Answer the very source end of the corresponding parse node
96 public int sourceEnd(){
97 return this.fieldDeclaration.declarationSourceEnd;
99 public String toString(int tab){
100 StringBuffer buffer = new StringBuffer(tabString(tab));
101 buffer.append("Recovered field:\n"); //$NON-NLS-1$
102 buffer.append(fieldDeclaration.toString(tab + 1));
103 if (this.anonymousTypes != null) {
104 for (int i = 0; i < this.anonymousTypeCount; i++){
105 buffer.append("\n"); //$NON-NLS-1$
106 buffer.append(anonymousTypes[i].toString(tab + 1));
109 return buffer.toString();
111 public FieldDeclaration updatedFieldDeclaration(){
113 if (this.anonymousTypes != null && fieldDeclaration.initialization == null) {
114 for (int i = 0; i < this.anonymousTypeCount; i++){
115 if (anonymousTypes[i].preserveContent){
116 fieldDeclaration.initialization =
117 ((AnonymousLocalTypeDeclaration)this.anonymousTypes[i].updatedTypeDeclaration()).allocation;
120 if (this.anonymousTypeCount > 0) fieldDeclaration.bits |= AstNode.HasLocalTypeMASK;
122 return fieldDeclaration;
125 * A closing brace got consumed, might have closed the current element,
126 * in which case both the currentElement is exited.
128 * Fields have no associated braces, thus if matches, then update parent.
130 public RecoveredElement updateOnClosingBrace(int braceStart, int braceEnd){
131 if (bracketBalance > 0){ // was an array initializer
133 if (bracketBalance == 0) alreadyCompletedFieldInitialization = true;
137 return parent.updateOnClosingBrace(braceStart, braceEnd);
142 * An opening brace got consumed, might be the expected opening one of the current element,
143 * in which case the bodyStart is updated.
145 public RecoveredElement updateOnOpeningBrace(int currentPosition){
146 if (fieldDeclaration.declarationSourceEnd == 0
147 && fieldDeclaration.type instanceof ArrayTypeReference
148 && !alreadyCompletedFieldInitialization){
150 return null; // no update is necessary (array initializer)
152 // might be an array initializer
153 this.updateSourceEndIfNecessary(currentPosition - 1);
154 return this.parent.updateOnOpeningBrace(currentPosition);
156 public void updateParseTree(){
157 this.updatedFieldDeclaration();
160 * Update the declarationSourceEnd of the corresponding parse node
162 public void updateSourceEndIfNecessary(int sourceEnd){
163 if (this.fieldDeclaration.declarationSourceEnd == 0) {
164 this.fieldDeclaration.declarationSourceEnd = sourceEnd;
165 this.fieldDeclaration.declarationEnd = sourceEnd;