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.AbstractMethodDeclaration;
17 import net.sourceforge.phpdt.internal.compiler.ast.AnonymousLocalTypeDeclaration;
18 import net.sourceforge.phpdt.internal.compiler.ast.AstNode;
19 import net.sourceforge.phpdt.internal.compiler.ast.CompilationUnitDeclaration;
20 import net.sourceforge.phpdt.internal.compiler.ast.FieldDeclaration;
21 import net.sourceforge.phpdt.internal.compiler.ast.ImportReference;
22 import net.sourceforge.phpdt.internal.compiler.ast.LocalTypeDeclaration;
23 import net.sourceforge.phpdt.internal.compiler.ast.TypeDeclaration;
25 public class RecoveredUnit extends RecoveredElement {
27 public CompilationUnitDeclaration unitDeclaration;
29 public RecoveredImport[] imports;
30 public int importCount;
31 public RecoveredType[] types;
33 public RecoveredUnit(CompilationUnitDeclaration unitDeclaration, int bracketBalance, Parser parser){
34 super(null, bracketBalance, parser);
35 this.unitDeclaration = unitDeclaration;
38 * Record a method declaration: should be attached to last type
40 public RecoveredElement add(AbstractMethodDeclaration methodDeclaration, int bracketBalance) {
42 /* attach it to last type - if any */
44 RecoveredType type = this.types[typeCount -1];
45 type.bodyEnd = 0; // reset position
46 type.typeDeclaration.declarationSourceEnd = 0; // reset position
47 type.typeDeclaration.bodyEnd = 0;
48 return type.add(methodDeclaration, bracketBalance);
50 return this; // ignore
53 * Record a field declaration: should be attached to last type
55 public RecoveredElement add(FieldDeclaration fieldDeclaration, int bracketBalance) {
57 /* attach it to last type - if any */
59 RecoveredType type = this.types[typeCount -1];
60 type.bodyEnd = 0; // reset position
61 type.typeDeclaration.declarationSourceEnd = 0; // reset position
62 type.typeDeclaration.bodyEnd = 0;
63 return type.add(fieldDeclaration, bracketBalance);
65 return this; // ignore
67 public RecoveredElement add(ImportReference importReference, int bracketBalance) {
68 if (imports == null) {
69 imports = new RecoveredImport[5];
72 if (importCount == imports.length) {
76 (imports = new RecoveredImport[2 * importCount]),
81 RecoveredImport element = new RecoveredImport(importReference, this, bracketBalance);
82 imports[importCount++] = element;
84 /* if import not finished, then import becomes current */
85 if (importReference.declarationSourceEnd == 0) return element;
88 public RecoveredElement add(TypeDeclaration typeDeclaration, int bracketBalance) {
90 if (typeDeclaration instanceof AnonymousLocalTypeDeclaration){
91 if (this.typeCount > 0) {
92 // add it to the last type
93 RecoveredType lastType = this.types[this.typeCount-1];
94 lastType.bodyEnd = 0; // reopen type
95 lastType.typeDeclaration.bodyEnd = 0; // reopen type
96 lastType.typeDeclaration.declarationSourceEnd = 0; // reopen type
97 lastType.bracketBalance++; // expect one closing brace
98 return lastType.add(typeDeclaration, bracketBalance);
102 types = new RecoveredType[5];
105 if (typeCount == types.length) {
109 (types = new RecoveredType[2 * typeCount]),
114 RecoveredType element = new RecoveredType(typeDeclaration, this, bracketBalance);
115 types[typeCount++] = element;
117 /* if type not finished, then type becomes current */
118 if (typeDeclaration.declarationSourceEnd == 0) return element;
122 * Answer the associated parsed structure
124 public AstNode parseTree(){
125 return unitDeclaration;
128 * Answer the very source end of the corresponding parse node
130 public int sourceEnd(){
131 return this.unitDeclaration.sourceEnd;
133 public String toString(int tab) {
134 StringBuffer result = new StringBuffer(tabString(tab));
135 result.append("Recovered unit: [\n"); //$NON-NLS-1$
136 result.append(unitDeclaration.toString(tab + 1));
137 result.append(tabString(tab + 1));
138 result.append("]"); //$NON-NLS-1$
139 if (this.imports != null) {
140 for (int i = 0; i < this.importCount; i++) {
141 result.append("\n"); //$NON-NLS-1$
142 result.append(this.imports[i].toString(tab + 1));
145 if (this.types != null) {
146 for (int i = 0; i < this.typeCount; i++) {
147 result.append("\n"); //$NON-NLS-1$
148 result.append(this.types[i].toString(tab + 1));
151 return result.toString();
153 public CompilationUnitDeclaration updatedCompilationUnitDeclaration(){
156 if (importCount > 0){
157 ImportReference[] importRefences = new ImportReference[importCount];
158 for (int i = 0; i < importCount; i++){
159 importRefences[i] = imports[i].updatedImportReference();
161 unitDeclaration.imports = importRefences;
165 int existingCount = unitDeclaration.types == null ? 0 : unitDeclaration.types.length;
166 TypeDeclaration[] typeDeclarations = new TypeDeclaration[existingCount + typeCount];
167 if (existingCount > 0){
168 System.arraycopy(unitDeclaration.types, 0, typeDeclarations, 0, existingCount);
170 // may need to update the declarationSourceEnd of the last type
171 if (types[typeCount - 1].typeDeclaration.declarationSourceEnd == 0){
172 types[typeCount - 1].typeDeclaration.declarationSourceEnd = unitDeclaration.sourceEnd;
173 types[typeCount - 1].typeDeclaration.bodyEnd = unitDeclaration.sourceEnd;
175 int actualCount = existingCount;
176 for (int i = 0; i < typeCount; i++){
177 TypeDeclaration typeDecl = types[i].updatedTypeDeclaration();
178 // filter out local types (12454)
179 if (!(typeDecl instanceof LocalTypeDeclaration)){
180 typeDeclarations[actualCount++] = typeDecl;
183 if (actualCount != typeCount){
187 typeDeclarations = new TypeDeclaration[existingCount+actualCount],
189 existingCount+actualCount);
191 unitDeclaration.types = typeDeclarations;
193 return unitDeclaration;
195 public void updateParseTree(){
196 this.updatedCompilationUnitDeclaration();
199 * Update the sourceEnd of the corresponding parse node
201 public void updateSourceEndIfNecessary(int sourceEnd){
202 if (this.unitDeclaration.sourceEnd == 0)
203 this.unitDeclaration.sourceEnd = sourceEnd;