Skip to main content
summaryrefslogtreecommitdiffstats
blob: 79ee70ca0e27e376ba9cf25f2a49a119f7ee7a6f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package org.eclipse.jdt.internal.compiler.parser;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */
/**
 * Internal block structure for parsing recovery 
 */
import org.eclipse.jdt.internal.compiler.ast.*;
import org.eclipse.jdt.internal.compiler.lookup.*;

public class RecoveredBlock extends RecoveredStatement implements CompilerModifiers, TerminalSymbols {

	public Block blockDeclaration;

	public RecoveredStatement[] statements;
	public int statementCount;

	public boolean preserveContent = false;
	public RecoveredLocalVariable pendingArgument;
public RecoveredBlock(Block block, RecoveredElement parent, int bracketBalance){
	super(block, parent, bracketBalance);
	this.blockDeclaration = block;
	this.foundOpeningBrace = true;
}
/*
 * Record a nested block declaration 
 */
public RecoveredElement add(Block nestedBlockDeclaration, int bracketBalance) {

	/* do not consider a nested block starting passed the block end (if set)
		it must be belonging to an enclosing block */
	if (blockDeclaration.sourceEnd != 0 
		&& nestedBlockDeclaration.sourceStart > blockDeclaration.sourceEnd){
		return this.parent.add(nestedBlockDeclaration, bracketBalance);
	}
			
	RecoveredBlock element = new RecoveredBlock(nestedBlockDeclaration, this, bracketBalance);

	// if we have a pending Argument, promote it into the new block
	if (pendingArgument != null){
		element.attach(pendingArgument);
		pendingArgument = null;
	}
	this.attach(element);
	if (nestedBlockDeclaration.sourceEnd == 0) return element;
	return this;	
}
/*
 * Record a local declaration 
 */
public RecoveredElement add(LocalDeclaration localDeclaration, int bracketBalance) {

	/* do not consider a local variable starting passed the block end (if set)
		it must be belonging to an enclosing block */
	if (blockDeclaration.sourceEnd != 0 
		&& localDeclaration.declarationSourceStart > blockDeclaration.sourceEnd){
		return this.parent.add(localDeclaration, bracketBalance);
	}

	RecoveredLocalVariable element = new RecoveredLocalVariable(localDeclaration, this, bracketBalance);

	if (localDeclaration instanceof Argument){
		pendingArgument = element;
		return this;
	}
	
	this.attach(element);
	if (localDeclaration.declarationSourceEnd == 0) return element;
	return this;	
}
/*
 * Record a statement declaration 
 */
public RecoveredElement add(Statement statement, int bracketBalance) {

	/* do not consider a nested block starting passed the block end (if set)
		it must be belonging to an enclosing block */
	if (blockDeclaration.sourceEnd != 0 
		&& statement.sourceStart > blockDeclaration.sourceEnd){
		return this.parent.add(statement, bracketBalance);
	}
			
	RecoveredStatement element = new RecoveredStatement(statement, this, bracketBalance);
	this.attach(element);
	if (statement.sourceEnd == 0) return element;
	return this;	
}
/*
 * Addition of a type to an initializer (act like inside method body)
 */
public RecoveredElement add(TypeDeclaration typeDeclaration, int bracketBalance) {

	/* do not consider a type starting passed the block end (if set)
		it must be belonging to an enclosing block */
	if (blockDeclaration.sourceEnd != 0 
		&& typeDeclaration.declarationSourceStart > blockDeclaration.sourceEnd){
		return this.parent.add(typeDeclaration, bracketBalance);
	}
			
	RecoveredStatement element = new RecoveredType(typeDeclaration, this, bracketBalance);
	this.attach(element);
	if (typeDeclaration.declarationSourceEnd == 0) return element;
	return this;
}
/*
 * Attach a recovered statement
 */
void attach(RecoveredStatement recoveredStatement) {

	if (statements == null) {
		statements = new RecoveredStatement[5];
		statementCount = 0;
	} else {
		if (statementCount == statements.length) {
			System.arraycopy(
				statements, 
				0, 
				(statements = new RecoveredStatement[2 * statementCount]), 
				0, 
				statementCount); 
		}
	}
	statements[statementCount++] = recoveredStatement;
}
/* 
 * Answer the associated parsed structure
 */
public AstNode parseTree(){
	return blockDeclaration;
}
public String toString(int tab) {
	StringBuffer result = new StringBuffer(tabString(tab));
	result.append("Recovered block:\n");
	result.append(blockDeclaration.toString(tab + 1));
	if (this.statements != null) {
		for (int i = 0; i < this.statementCount; i++) {
			result.append("\n");
			result.append(this.statements[i].toString(tab + 1));
		}
	}
	return result.toString();
}
/*
 * Rebuild a block from the nested structure which is in scope
 */
public Block updatedBlock(){

	// if block was not marked to be preserved or empty, then ignore it
	if (!preserveContent || statementCount == 0) return null;

	Statement[] updatedStatements = new Statement[statementCount];
	int updatedCount = 0;
	
	// only collect the non-null updated statements
	for (int i = 0; i < statementCount; i++){
		Statement updatedStatement = statements[i].updatedStatement();
		if (updatedStatement != null){
			updatedStatements[updatedCount++] = updatedStatement;
		}
	}
	if (updatedCount == 0) return null; // not interesting block

	// resize statement collection if necessary
	if (updatedCount != statementCount){
		blockDeclaration.statements = new Statement[updatedCount];
		System.arraycopy(updatedStatements, 0, blockDeclaration.statements, 0, updatedCount);
	} else {
		blockDeclaration.statements = updatedStatements;
	}

	return blockDeclaration;
}
/*
 * Rebuild a statement from the nested structure which is in scope
 */
public Statement updatedStatement(){

	return this.updatedBlock();
}
/*
 * A closing brace got consumed, might have closed the current element,
 * in which case both the currentElement is exited
 */
public RecoveredElement updateOnClosingBrace(int braceStart, int braceEnd){
	if ((--bracketBalance <= 0) && (parent != null)){
		this.updateSourceEndIfNecessary(braceEnd);

		/* if the block is the method body, then it closes the method too */
		RecoveredMethod method = enclosingMethod();
		if (method != null && method.methodBody == this){
			return parent.updateOnClosingBrace(braceStart, braceEnd);
		}
		return parent;
	}
	return this;
}
/*
 * An opening brace got consumed, might be the expected opening one of the current element,
 * in which case the bodyStart is updated.
 */
public RecoveredElement updateOnOpeningBrace(int currentPosition){

	// create a nested block
	Block block = new Block(0);
	block.sourceStart = parser().scanner.startPosition;
	return this.add(block, 1);
}
/*
 * Final update the corresponding parse node
 */
public void updateParseTree(){

	this.updatedBlock();
}
/*
 * Rebuild a flattened block from the nested structure which is in scope
 */
public Statement updateStatement(){

	// if block was closed or empty, then ignore it
	if (this.blockDeclaration.sourceEnd != 0 || statementCount == 0) return null;

	Statement[] updatedStatements = new Statement[statementCount];
	int updatedCount = 0;
	
	// only collect the non-null updated statements
	for (int i = 0; i < statementCount; i++){
		Statement updatedStatement = statements[i].updatedStatement();
		if (updatedStatement != null){
			updatedStatements[updatedCount++] = updatedStatement;
		}
	}
	if (updatedCount == 0) return null; // not interesting block

	// resize statement collection if necessary
	if (updatedCount != statementCount){
		blockDeclaration.statements = new Statement[updatedCount];
		System.arraycopy(updatedStatements, 0, blockDeclaration.statements, 0, updatedCount);
	} else {
		blockDeclaration.statements = updatedStatements;
	}

	return blockDeclaration;
}
}

Back to the top