first scanner /parser copied from the jdt java version
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / parser / RecoveredField.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.parser;
12
13 /**
14  * Internal field structure for parsing recovery 
15  */
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;
23
24 public class RecoveredField extends RecoveredElement {
25
26         public FieldDeclaration fieldDeclaration;
27         boolean alreadyCompletedFieldInitialization;
28         
29         public RecoveredType[] anonymousTypes;
30         public int anonymousTypeCount;
31 public RecoveredField(FieldDeclaration fieldDeclaration, RecoveredElement parent, int bracketBalance){
32         this(fieldDeclaration, parent, bracketBalance, null);
33 }
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;
38 }
39 /*
40  * Record an expression statement if field is expecting an initialization expression,
41  * used for completion inside field initializers.
42  */
43 public RecoveredElement add(Statement statement, int bracketBalance) {
44
45         if (this.alreadyCompletedFieldInitialization || !(statement instanceof Expression)) {
46                 return super.add(statement, bracketBalance);
47         } else {
48                 this.alreadyCompletedFieldInitialization = true;
49                 this.fieldDeclaration.initialization = (Expression)statement;
50                 this.fieldDeclaration.declarationSourceEnd = statement.sourceEnd;
51                 this.fieldDeclaration.declarationEnd = statement.sourceEnd;
52                 return this;
53         }
54 }
55 /*
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.
59  */
60 public RecoveredElement add(TypeDeclaration typeDeclaration, int bracketBalance) {
61
62         if (this.alreadyCompletedFieldInitialization 
63                         || !(typeDeclaration instanceof AnonymousLocalTypeDeclaration)
64                         || (this.fieldDeclaration.declarationSourceEnd != 0 && typeDeclaration.sourceStart > this.fieldDeclaration.declarationSourceEnd)) {
65                 return super.add(typeDeclaration, bracketBalance);
66         } else { 
67                 // Prepare anonymous type list
68                 if (this.anonymousTypes == null) {
69                         this.anonymousTypes = new RecoveredType[5];
70                         this.anonymousTypeCount = 0;
71                 } else {
72                         if (this.anonymousTypeCount == this.anonymousTypes.length) {
73                                 System.arraycopy(
74                                         this.anonymousTypes, 
75                                         0, 
76                                         (this.anonymousTypes = new RecoveredType[2 * this.anonymousTypeCount]), 
77                                         0, 
78                                         this.anonymousTypeCount); 
79                         }
80                 }
81                 // Store type declaration as an anonymous type
82                 RecoveredType element = new RecoveredType(typeDeclaration, this, bracketBalance);
83                 this.anonymousTypes[this.anonymousTypeCount++] = element;
84                 return element;
85         }
86 }
87 /* 
88  * Answer the associated parsed structure
89  */
90 public AstNode parseTree(){
91         return fieldDeclaration;
92 }
93 /*
94  * Answer the very source end of the corresponding parse node
95  */
96 public int sourceEnd(){
97         return this.fieldDeclaration.declarationSourceEnd;
98 }
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));
107                 }
108         }
109         return buffer.toString();
110 }
111 public FieldDeclaration updatedFieldDeclaration(){
112
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;
118                         }
119                 }
120                 if (this.anonymousTypeCount > 0) fieldDeclaration.bits |= AstNode.HasLocalTypeMASK;
121         }
122         return fieldDeclaration;
123 }
124 /*
125  * A closing brace got consumed, might have closed the current element,
126  * in which case both the currentElement is exited.
127  *
128  * Fields have no associated braces, thus if matches, then update parent.
129  */
130 public RecoveredElement updateOnClosingBrace(int braceStart, int braceEnd){
131         if (bracketBalance > 0){ // was an array initializer
132                 bracketBalance--;
133                 if (bracketBalance == 0) alreadyCompletedFieldInitialization = true;
134                 return this;
135         }
136         if (parent != null){
137                 return parent.updateOnClosingBrace(braceStart, braceEnd);
138         }
139         return this;
140 }
141 /*
142  * An opening brace got consumed, might be the expected opening one of the current element,
143  * in which case the bodyStart is updated.
144  */
145 public RecoveredElement updateOnOpeningBrace(int currentPosition){
146         if (fieldDeclaration.declarationSourceEnd == 0 
147                 && fieldDeclaration.type instanceof ArrayTypeReference
148                 && !alreadyCompletedFieldInitialization){
149                 bracketBalance++;
150                 return null; // no update is necessary  (array initializer)
151         }
152         // might be an array initializer
153         this.updateSourceEndIfNecessary(currentPosition - 1);   
154         return this.parent.updateOnOpeningBrace(currentPosition);       
155 }
156 public void updateParseTree(){
157         this.updatedFieldDeclaration();
158 }
159 /*
160  * Update the declarationSourceEnd of the corresponding parse node
161  */
162 public void updateSourceEndIfNecessary(int sourceEnd){
163         if (this.fieldDeclaration.declarationSourceEnd == 0) {
164                 this.fieldDeclaration.declarationSourceEnd = sourceEnd;
165                 this.fieldDeclaration.declarationEnd = sourceEnd;
166         }
167 }
168 }