1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler;
13 import net.sourceforge.phpdt.core.compiler.IProblem;
14 import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit;
15 import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
16 import net.sourceforge.phpdt.internal.compiler.parser.UnitParser;
17 import net.sourceforge.phpdt.internal.compiler.problem.AbortCompilation;
18 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
19 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
23 * A document element parser extracts structural information
24 * from a piece of source, providing detailed source positions info.
26 * also see @IDocumentElementRequestor
28 * The structural investigation includes:
29 * - the package statement
31 * - top-level types: package member, member types (member types of member types...)
35 * Any (parsing) problem encountered is also provided.
37 public class DocumentElementParser extends UnitParser {
38 IDocumentElementRequestor requestor;
39 private int localIntPtr;
40 private int lastFieldEndPosition;
41 private int lastFieldBodyEndPosition;
42 private int typeStartPosition;
43 private long selectorSourcePositions;
45 private int extendsDim;
46 private int declarationSourceStart;
48 /* int[] stack for storing javadoc positions */
49 int[][] intArrayStack;
52 // CompilerOptions options;
54 public DocumentElementParser(
55 final IDocumentElementRequestor requestor,
56 IProblemFactory problemFactory,
57 CompilerOptions options) {
58 super(new ProblemReporter(
59 DefaultErrorHandlingPolicies.exitAfterAllProblems(),
62 public void record(IProblem problem, CompilationResult unitResult) {
63 requestor.acceptProblem(problem);
67 // options.sourceLevel >= CompilerOptions.JDK1_4);
68 this.requestor = requestor;
69 intArrayStack = new int[30][];
70 this.options = options;
77 //protected void adjustInterfaceModifiers() {
78 // intStack[intPtr - 2] |= AccInterface;
81 * Will clear the comment stack when looking
82 * for a potential JavaDoc which might contain @deprecated.
84 * Additionally, before investigating for @deprecated, retrieve the positions
85 * of the JavaDoc comments so as to notify requestor with them.
87 //public void checkAnnotation() {
89 // /* persisting javadoc positions */
90 // pushOnIntArrayStack(this.getJavaDocPositions());
91 // boolean deprecated = false;
92 // int lastAnnotationIndex = -1;
93 // int commentPtr = scanner.commentPtr;
95 // //since jdk1.2 look only in the last java doc comment...
96 // nextComment : for (lastAnnotationIndex = scanner.commentPtr; lastAnnotationIndex >= 0; lastAnnotationIndex--){
97 // //look for @deprecated into the first javadoc comment preceeding the declaration
98 // int commentSourceStart = scanner.commentStarts[lastAnnotationIndex];
99 // // javadoc only (non javadoc comment have negative end positions.)
100 // if (modifiersSourceStart != -1 && modifiersSourceStart < commentSourceStart) {
101 // continue nextComment;
103 // if (scanner.commentStops[lastAnnotationIndex] < 0) {
104 // continue nextComment;
106 // int commentSourceEnd = scanner.commentStops[lastAnnotationIndex] - 1; //stop is one over
107 // char[] comment = scanner.source;
111 // commentSourceStart,
114 // break nextComment;
117 // checkAndSetModifiers(AccDeprecated);
119 // // modify the modifier source start to point at the first comment
120 // if (commentPtr >= 0) {
121 // declarationSourceStart = scanner.commentStarts[0];
128 //protected void consumeClassBodyDeclaration() {
129 // // ClassBodyDeclaration ::= Diet Block
130 // //push an Initializer
131 // //optimize the push/pop
133 // super.consumeClassBodyDeclaration();
134 // Initializer initializer = (Initializer) astStack[astPtr];
135 // requestor.acceptInitializer(
136 // initializer.declarationSourceStart,
137 // initializer.declarationSourceEnd,
138 // intArrayStack[intArrayPtr--],
140 // modifiersSourceStart,
141 // initializer.block.sourceStart,
142 // initializer.block.sourceEnd);
146 // * INTERNAL USE-ONLY
148 //protected void consumeClassDeclaration() {
149 // super.consumeClassDeclaration();
150 // // we know that we have a TypeDeclaration on the top of the astStack
151 // if (isLocalDeclaration()) {
152 // // we ignore the local variable declarations
155 // requestor.exitClass(endStatementPosition, // '}' is the end of the body
156 // ((TypeDeclaration) astStack[astPtr]).declarationSourceEnd);
160 // * INTERNAL USE-ONLY
162 //protected void consumeClassHeader() {
163 // //ClassHeader ::= $empty
164 // super.consumeClassHeader();
165 // if (isLocalDeclaration()) {
166 // // we ignore the local variable declarations
170 // TypeDeclaration typeDecl = (TypeDeclaration) astStack[astPtr];
171 // TypeReference[] superInterfaces = typeDecl.superInterfaces;
172 // char[][] interfaceNames = null;
173 // int[] interfaceNameStarts = null;
174 // int[] interfaceNameEnds = null;
175 // if (superInterfaces != null) {
176 // int superInterfacesLength = superInterfaces.length;
177 // interfaceNames = new char[superInterfacesLength][];
178 // interfaceNameStarts = new int[superInterfacesLength];
179 // interfaceNameEnds = new int[superInterfacesLength];
180 // for (int i = 0; i < superInterfacesLength; i++) {
181 // TypeReference superInterface = superInterfaces[i];
182 // interfaceNames[i] = CharOperation.concatWith(superInterface.getTypeName(), '.');
183 // interfaceNameStarts[i] = superInterface.sourceStart;
184 // interfaceNameEnds[i] = superInterface.sourceEnd;
187 // // flush the comments related to the class header
188 // scanner.commentPtr = -1;
189 // TypeReference superclass = typeDecl.superclass;
190 // if (superclass == null) {
191 // requestor.enterClass(
192 // typeDecl.declarationSourceStart,
193 // intArrayStack[intArrayPtr--],
194 // typeDecl.modifiers,
195 // typeDecl.modifiersSourceStart,
196 // typeStartPosition,
198 // typeDecl.sourceStart,
199 // typeDecl.sourceEnd,
204 // interfaceNameStarts,
205 // interfaceNameEnds,
206 // scanner.currentPosition - 1);
208 // requestor.enterClass(
209 // typeDecl.declarationSourceStart,
210 // intArrayStack[intArrayPtr--],
211 // typeDecl.modifiers,
212 // typeDecl.modifiersSourceStart,
213 // typeStartPosition,
215 // typeDecl.sourceStart,
216 // typeDecl.sourceEnd,
217 // CharOperation.concatWith(superclass.getTypeName(), '.'),
218 // superclass.sourceStart,
219 // superclass.sourceEnd,
221 // interfaceNameStarts,
222 // interfaceNameEnds,
223 // scanner.currentPosition - 1);
227 //protected void consumeClassHeaderName() {
228 // // ClassHeaderName ::= Modifiersopt 'class' 'Identifier'
229 // TypeDeclaration typeDecl;
230 // if (nestedMethod[nestedType] == 0) {
231 // if (nestedType != 0) {
232 // typeDecl = new MemberTypeDeclaration(this.compilationUnit.compilationResult);
234 // typeDecl = new TypeDeclaration(this.compilationUnit.compilationResult);
237 // // Record that the block has a declaration for local types
238 // typeDecl = new LocalTypeDeclaration(this.compilationUnit.compilationResult);
239 // markEnclosingMemberWithLocalType();
243 // //highlight the name of the type
244 // long pos = identifierPositionStack[identifierPtr];
245 // typeDecl.sourceEnd = (int) pos;
246 // typeDecl.sourceStart = (int) (pos >>> 32);
247 // typeDecl.name = identifierStack[identifierPtr--];
248 // identifierLengthPtr--;
250 // //compute the declaration source too
251 // // 'class' and 'interface' push an int position
252 // typeStartPosition = typeDecl.declarationSourceStart = intStack[intPtr--];
254 // int declarationSourceStart = intStack[intPtr--];
255 // typeDecl.modifiersSourceStart = intStack[intPtr--];
256 // typeDecl.modifiers = intStack[intPtr--];
257 // if (typeDecl.declarationSourceStart > declarationSourceStart) {
258 // typeDecl.declarationSourceStart = declarationSourceStart;
260 // typeDecl.bodyStart = typeDecl.sourceEnd + 1;
261 // pushOnAstStack(typeDecl);
265 // * INTERNAL USE-ONLY
267 //protected void consumeCompilationUnit() {
268 // // CompilationUnit ::= EnterCompilationUnit PackageDeclarationopt ImportDeclarationsopt
269 // requestor.exitCompilationUnit(scanner.source.length - 1);
275 //protected void consumeConstructorDeclaration() {
276 // // ConstructorDeclaration ::= ConstructorHeader ConstructorBody
277 // super.consumeConstructorDeclaration();
278 // if (isLocalDeclaration()) {
279 // // we ignore the local variable declarations
282 // ConstructorDeclaration cd = (ConstructorDeclaration) astStack[astPtr];
283 // requestor.exitConstructor(endStatementPosition, cd.declarationSourceEnd);
287 // * INTERNAL USE-ONLY
289 //protected void consumeConstructorHeader() {
290 // // ConstructorHeader ::= ConstructorHeaderName MethodHeaderParameters MethodHeaderThrowsClauseopt
291 // super.consumeConstructorHeader();
292 // if (isLocalDeclaration()) {
293 // // we ignore the local variable declarations
297 // ConstructorDeclaration cd = (ConstructorDeclaration) astStack[astPtr];
298 // Argument[] arguments = cd.arguments;
299 // char[][] argumentTypes = null;
300 // char[][] argumentNames = null;
301 // int[] argumentTypeStarts = null;
302 // int[] argumentTypeEnds = null;
303 // int[] argumentNameStarts = null;
304 // int[] argumentNameEnds = null;
305 // if (arguments != null) {
306 // int argumentLength = arguments.length;
307 // argumentTypes = new char[argumentLength][];
308 // argumentNames = new char[argumentLength][];
309 // argumentNameStarts = new int[argumentLength];
310 // argumentNameEnds = new int[argumentLength];
311 // argumentTypeStarts = new int[argumentLength];
312 // argumentTypeEnds = new int[argumentLength];
313 // for (int i = 0; i < argumentLength; i++) {
314 // Argument argument = arguments[i];
315 // TypeReference argumentType = argument.type;
316 // argumentTypes[i] = returnTypeName(argumentType);
317 // argumentNames[i] = argument.name;
318 // argumentNameStarts[i] = argument.sourceStart;
319 // argumentNameEnds[i] = argument.sourceEnd;
320 // argumentTypeStarts[i] = argumentType.sourceStart;
321 // argumentTypeEnds[i] = argumentType.sourceEnd;
324 // TypeReference[] thrownExceptions = cd.thrownExceptions;
325 // char[][] exceptionTypes = null;
326 // int[] exceptionTypeStarts = null;
327 // int[] exceptionTypeEnds = null;
328 // if (thrownExceptions != null) {
329 // int thrownExceptionLength = thrownExceptions.length;
330 // exceptionTypes = new char[thrownExceptionLength][];
331 // exceptionTypeStarts = new int[thrownExceptionLength];
332 // exceptionTypeEnds = new int[thrownExceptionLength];
333 // for (int i = 0; i < thrownExceptionLength; i++) {
334 // TypeReference exception = thrownExceptions[i];
335 // exceptionTypes[i] = CharOperation.concatWith(exception.getTypeName(), '.');
336 // exceptionTypeStarts[i] = exception.sourceStart;
337 // exceptionTypeEnds[i] = exception.sourceEnd;
341 // .enterConstructor(
342 // cd.declarationSourceStart,
343 // intArrayStack[intArrayPtr--],
345 // cd.modifiersSourceStart,
348 // (int) (selectorSourcePositions & 0xFFFFFFFFL),
349 // // retrieve the source end of the name
351 // argumentTypeStarts,
354 // argumentNameStarts,
357 // // right parenthesis
359 // exceptionTypeStarts,
360 // exceptionTypeEnds,
361 // scanner.currentPosition - 1);
363 //protected void consumeConstructorHeaderName() {
364 // // ConstructorHeaderName ::= Modifiersopt 'Identifier' '('
365 // ConstructorDeclaration cd = new ConstructorDeclaration(this.compilationUnit.compilationResult);
367 // //name -- this is not really revelant but we do .....
368 // cd.selector = identifierStack[identifierPtr];
369 // selectorSourcePositions = identifierPositionStack[identifierPtr--];
370 // identifierLengthPtr--;
373 // cd.declarationSourceStart = intStack[intPtr--];
374 // cd.modifiersSourceStart = intStack[intPtr--];
375 // cd.modifiers = intStack[intPtr--];
377 // //highlight starts at the selector starts
378 // cd.sourceStart = (int) (selectorSourcePositions >>> 32);
379 // pushOnAstStack(cd);
381 // cd.sourceEnd = lParenPos;
382 // cd.bodyStart = lParenPos + 1;
384 //protected void consumeDefaultModifiers() {
385 // checkAnnotation(); // might update modifiers with AccDeprecated
386 // pushOnIntStack(modifiers); // modifiers
387 // pushOnIntStack(-1);
389 // declarationSourceStart >= 0 ? declarationSourceStart : scanner.startPosition);
392 //protected void consumeDiet() {
393 // // Diet ::= $empty
394 // super.consumeDiet();
395 // /* persisting javadoc positions
396 // * Will be consume in consumeClassBodyDeclaration
398 // pushOnIntArrayStack(this.getJavaDocPositions());
402 // * INTERNAL USE-ONLY
404 //protected void consumeEnterCompilationUnit() {
405 // // EnterCompilationUnit ::= $empty
406 // requestor.enterCompilationUnit();
410 // * INTERNAL USE-ONLY
412 //protected void consumeEnterVariable() {
413 // // EnterVariable ::= $empty
414 // boolean isLocalDeclaration = isLocalDeclaration();
415 // if (!isLocalDeclaration && (variablesCounter[nestedType] != 0)) {
416 // requestor.exitField(lastFieldBodyEndPosition, lastFieldEndPosition);
418 // char[] name = identifierStack[identifierPtr];
419 // long namePosition = identifierPositionStack[identifierPtr--];
420 // int extendedTypeDimension = intStack[intPtr--];
422 // AbstractVariableDeclaration declaration;
423 // if (nestedMethod[nestedType] != 0) {
424 // // create the local variable declarations
426 // new LocalDeclaration(null, name, (int) (namePosition >>> 32), (int) namePosition);
428 // // create the field declaration
430 // new FieldDeclaration(null, name, (int) (namePosition >>> 32), (int) namePosition);
432 // identifierLengthPtr--;
433 // TypeReference type;
434 // int variableIndex = variablesCounter[nestedType];
436 // if (variableIndex == 0) {
437 // // first variable of the declaration (FieldDeclaration or LocalDeclaration)
438 // if (nestedMethod[nestedType] != 0) {
439 // // local declaration
440 // declaration.declarationSourceStart = intStack[intPtr--];
441 // declaration.modifiersSourceStart = intStack[intPtr--];
442 // declaration.modifiers = intStack[intPtr--];
443 // type = getTypeReference(typeDim = intStack[intPtr--]); // type dimension
444 // pushOnAstStack(type);
446 // // field declaration
447 // type = getTypeReference(typeDim = intStack[intPtr--]); // type dimension
448 // pushOnAstStack(type);
449 // declaration.declarationSourceStart = intStack[intPtr--];
450 // declaration.modifiersSourceStart = intStack[intPtr--];
451 // declaration.modifiers = intStack[intPtr--];
454 // type = (TypeReference) astStack[astPtr - variableIndex];
455 // typeDim = type.dimensions();
456 // AbstractVariableDeclaration previousVariable =
457 // (AbstractVariableDeclaration) astStack[astPtr];
458 // declaration.declarationSourceStart = previousVariable.declarationSourceStart;
459 // declaration.modifiers = previousVariable.modifiers;
460 // declaration.modifiersSourceStart = previousVariable.modifiersSourceStart;
463 // localIntPtr = intPtr;
465 // if (extendedTypeDimension == 0) {
466 // declaration.type = type;
468 // int dimension = typeDim + extendedTypeDimension;
469 // //on the identifierLengthStack there is the information about the type....
471 // if ((baseType = identifierLengthStack[identifierLengthPtr + 1]) < 0) {
472 // //it was a baseType
473 // declaration.type = TypeReference.baseTypeReference(-baseType, dimension);
474 // declaration.type.sourceStart = type.sourceStart;
475 // declaration.type.sourceEnd = type.sourceEnd;
477 // declaration.type = this.copyDims(type, dimension);
480 // variablesCounter[nestedType]++;
481 // nestedMethod[nestedType]++;
482 // pushOnAstStack(declaration);
484 // int[] javadocPositions = intArrayStack[intArrayPtr];
485 // if (!isLocalDeclaration) {
488 // declaration.declarationSourceStart,
490 // declaration.modifiers,
491 // declaration.modifiersSourceStart,
492 // returnTypeName(declaration.type),
497 // (int) (namePosition >>> 32),
498 // (int) namePosition,
499 // extendedTypeDimension,
500 // extendedTypeDimension == 0 ? -1 : endPosition);
505 // * INTERNAL USE-ONLY
507 //protected void consumeExitVariableWithInitialization() {
508 // // ExitVariableWithInitialization ::= $empty
509 // // the scanner is located after the comma or the semi-colon.
510 // // we want to include the comma or the semi-colon
511 // super.consumeExitVariableWithInitialization();
512 // nestedMethod[nestedType]--;
513 // lastFieldEndPosition = scanner.currentPosition - 1;
514 // lastFieldBodyEndPosition = ((AbstractVariableDeclaration) astStack[astPtr]).initialization.sourceEnd;
516 //protected void consumeExitVariableWithoutInitialization() {
517 // // ExitVariableWithoutInitialization ::= $empty
518 // // do nothing by default
519 // super.consumeExitVariableWithoutInitialization();
520 // nestedMethod[nestedType]--;
521 // lastFieldEndPosition = scanner.currentPosition - 1;
522 // lastFieldBodyEndPosition = scanner.startPosition - 1;
526 // * INTERNAL USE-ONLY
528 //protected void consumeFieldDeclaration() {
529 // // See consumeLocalVariableDeclarationDefaultModifier() in case of change: duplicated code
530 // // FieldDeclaration ::= Modifiersopt Type VariableDeclarators ';'
531 // // the super.consumeFieldDeclaration will reinitialize the variableCounter[nestedType]
532 // int variableIndex = variablesCounter[nestedType];
533 // super.consumeFieldDeclaration();
535 // if (isLocalDeclaration())
537 // if (variableIndex != 0) {
538 // requestor.exitField(lastFieldBodyEndPosition, lastFieldEndPosition);
541 //protected void consumeFormalParameter() {
542 // // FormalParameter ::= Type VariableDeclaratorId ==> false
543 // // FormalParameter ::= Modifiers Type VariableDeclaratorId ==> true
546 // identifierStack : type identifier
547 // intStack : dim dim
549 // astStack : Argument
554 // identifierLengthPtr--;
555 // char[] name = identifierStack[identifierPtr];
556 // long namePositions = identifierPositionStack[identifierPtr--];
557 // TypeReference type = getTypeReference(intStack[intPtr--] + intStack[intPtr--]);
564 // intStack[intPtr + 1]); // modifiers
565 // pushOnAstStack(arg);
570 // * INTERNAL USE-ONLY
572 //protected void consumeInterfaceDeclaration() {
573 // super.consumeInterfaceDeclaration();
574 // // we know that we have a TypeDeclaration on the top of the astStack
575 // if (isLocalDeclaration()) {
576 // // we ignore the local variable declarations
579 // requestor.exitInterface(endStatementPosition, // the '}' is the end of the body
580 // ((TypeDeclaration) astStack[astPtr]).declarationSourceEnd);
584 // * INTERNAL USE-ONLY
586 //protected void consumeInterfaceHeader() {
587 // //InterfaceHeader ::= $empty
588 // super.consumeInterfaceHeader();
589 // if (isLocalDeclaration()) {
590 // // we ignore the local variable declarations
594 // TypeDeclaration typeDecl = (TypeDeclaration) astStack[astPtr];
595 // TypeReference[] superInterfaces = typeDecl.superInterfaces;
596 // char[][] interfaceNames = null;
597 // int[] interfaceNameStarts = null;
598 // int[] interfacenameEnds = null;
599 // int superInterfacesLength = 0;
600 // if (superInterfaces != null) {
601 // superInterfacesLength = superInterfaces.length;
602 // interfaceNames = new char[superInterfacesLength][];
603 // interfaceNameStarts = new int[superInterfacesLength];
604 // interfacenameEnds = new int[superInterfacesLength];
606 // if (superInterfaces != null) {
607 // for (int i = 0; i < superInterfacesLength; i++) {
608 // TypeReference superInterface = superInterfaces[i];
609 // interfaceNames[i] = CharOperation.concatWith(superInterface.getTypeName(), '.');
610 // interfaceNameStarts[i] = superInterface.sourceStart;
611 // interfacenameEnds[i] = superInterface.sourceEnd;
614 // // flush the comments related to the interface header
615 // scanner.commentPtr = -1;
616 // requestor.enterInterface(
617 // typeDecl.declarationSourceStart,
618 // intArrayStack[intArrayPtr--],
619 // typeDecl.modifiers,
620 // typeDecl.modifiersSourceStart,
621 // typeStartPosition,
623 // typeDecl.sourceStart,
624 // typeDecl.sourceEnd,
626 // interfaceNameStarts,
627 // interfacenameEnds,
628 // scanner.currentPosition - 1);
630 //protected void consumeInterfaceHeaderName() {
631 // // InterfaceHeaderName ::= Modifiersopt 'interface' 'Identifier'
632 // TypeDeclaration typeDecl;
633 // if (nestedMethod[nestedType] == 0) {
634 // if (nestedType != 0) {
635 // typeDecl = new MemberTypeDeclaration(this.compilationUnit.compilationResult);
637 // typeDecl = new TypeDeclaration(this.compilationUnit.compilationResult);
640 // // Record that the block has a declaration for local types
641 // typeDecl = new LocalTypeDeclaration(this.compilationUnit.compilationResult);
642 // markEnclosingMemberWithLocalType();
646 // //highlight the name of the type
647 // long pos = identifierPositionStack[identifierPtr];
648 // typeDecl.sourceEnd = (int) pos;
649 // typeDecl.sourceStart = (int) (pos >>> 32);
650 // typeDecl.name = identifierStack[identifierPtr--];
651 // identifierLengthPtr--;
653 // //compute the declaration source too
654 // // 'class' and 'interface' push an int position
655 // typeStartPosition = typeDecl.declarationSourceStart = intStack[intPtr--];
657 // int declarationSourceStart = intStack[intPtr--];
658 // typeDecl.modifiersSourceStart = intStack[intPtr--];
659 // typeDecl.modifiers = intStack[intPtr--];
660 // if (typeDecl.declarationSourceStart > declarationSourceStart) {
661 // typeDecl.declarationSourceStart = declarationSourceStart;
663 // typeDecl.bodyStart = typeDecl.sourceEnd + 1;
664 // pushOnAstStack(typeDecl);
668 // * INTERNAL USE-ONLY
670 //protected void consumeLocalVariableDeclaration() {
671 // // See consumeLocalVariableDeclarationDefaultModifier() in case of change: duplicated code
672 // // FieldDeclaration ::= Modifiersopt Type VariableDeclarators ';'
674 // super.consumeLocalVariableDeclaration();
679 // * INTERNAL USE-ONLY
681 //protected void consumeMethodDeclaration(boolean isNotAbstract) {
682 // // MethodDeclaration ::= MethodHeader MethodBody
683 // // AbstractMethodDeclaration ::= MethodHeader ';'
684 // super.consumeMethodDeclaration(isNotAbstract);
685 // if (isLocalDeclaration()) {
686 // // we ignore the local variable declarations
689 // MethodDeclaration md = (MethodDeclaration) astStack[astPtr];
690 // requestor.exitMethod(endStatementPosition, md.declarationSourceEnd);
694 // * INTERNAL USE-ONLY
696 //protected void consumeMethodHeader() {
697 // // MethodHeader ::= MethodHeaderName MethodHeaderParameters MethodHeaderExtendedDims ThrowsClauseopt
698 // super.consumeMethodHeader();
699 // if (isLocalDeclaration()) {
700 // // we ignore the local variable declarations
704 // MethodDeclaration md = (MethodDeclaration) astStack[astPtr];
706 // TypeReference returnType = md.returnType;
707 // char[] returnTypeName = returnTypeName(returnType);
708 // Argument[] arguments = md.arguments;
709 // char[][] argumentTypes = null;
710 // char[][] argumentNames = null;
711 // int[] argumentTypeStarts = null;
712 // int[] argumentTypeEnds = null;
713 // int[] argumentNameStarts = null;
714 // int[] argumentNameEnds = null;
715 // if (arguments != null) {
716 // int argumentLength = arguments.length;
717 // argumentTypes = new char[argumentLength][];
718 // argumentNames = new char[argumentLength][];
719 // argumentNameStarts = new int[argumentLength];
720 // argumentNameEnds = new int[argumentLength];
721 // argumentTypeStarts = new int[argumentLength];
722 // argumentTypeEnds = new int[argumentLength];
723 // for (int i = 0; i < argumentLength; i++) {
724 // Argument argument = arguments[i];
725 // TypeReference argumentType = argument.type;
726 // argumentTypes[i] = returnTypeName(argumentType);
727 // argumentNames[i] = argument.name;
728 // argumentNameStarts[i] = argument.sourceStart;
729 // argumentNameEnds[i] = argument.sourceEnd;
730 // argumentTypeStarts[i] = argumentType.sourceStart;
731 // argumentTypeEnds[i] = argumentType.sourceEnd;
734 // TypeReference[] thrownExceptions = md.thrownExceptions;
735 // char[][] exceptionTypes = null;
736 // int[] exceptionTypeStarts = null;
737 // int[] exceptionTypeEnds = null;
738 // if (thrownExceptions != null) {
739 // int thrownExceptionLength = thrownExceptions.length;
740 // exceptionTypeStarts = new int[thrownExceptionLength];
741 // exceptionTypeEnds = new int[thrownExceptionLength];
742 // exceptionTypes = new char[thrownExceptionLength][];
743 // for (int i = 0; i < thrownExceptionLength; i++) {
744 // TypeReference exception = thrownExceptions[i];
745 // exceptionTypes[i] = CharOperation.concatWith(exception.getTypeName(), '.');
746 // exceptionTypeStarts[i] = exception.sourceStart;
747 // exceptionTypeEnds[i] = exception.sourceEnd;
752 // md.declarationSourceStart,
753 // intArrayStack[intArrayPtr--],
755 // md.modifiersSourceStart,
757 // returnType.sourceStart,
758 // returnType.sourceEnd,
762 // (int) (selectorSourcePositions & 0xFFFFFFFFL),
764 // argumentTypeStarts,
767 // argumentNameStarts,
771 // extendsDim == 0 ? -1 : endPosition,
773 // exceptionTypeStarts,
774 // exceptionTypeEnds,
775 // scanner.currentPosition - 1);
777 //protected void consumeMethodHeaderExtendedDims() {
778 // // MethodHeaderExtendedDims ::= Dimsopt
779 // // now we update the returnType of the method
780 // MethodDeclaration md = (MethodDeclaration) astStack[astPtr];
781 // int extendedDims = intStack[intPtr--];
782 // extendsDim = extendedDims;
783 // if (extendedDims != 0) {
784 // TypeReference returnType = md.returnType;
785 // md.sourceEnd = endPosition;
786 // int dims = returnType.dimensions() + extendedDims;
788 // if ((baseType = identifierLengthStack[identifierLengthPtr + 1]) < 0) {
789 // //it was a baseType
790 // int sourceStart = returnType.sourceStart;
791 // int sourceEnd = returnType.sourceEnd;
792 // returnType = TypeReference.baseTypeReference(-baseType, dims);
793 // returnType.sourceStart = sourceStart;
794 // returnType.sourceEnd = sourceEnd;
795 // md.returnType = returnType;
797 // md.returnType = this.copyDims(md.returnType, dims);
799 // if (currentToken == TokenNameLBRACE) {
800 // md.bodyStart = endPosition + 1;
804 //protected void consumeMethodHeaderName() {
805 // // MethodHeaderName ::= Modifiersopt Type 'Identifier' '('
806 // MethodDeclaration md = new MethodDeclaration(this.compilationUnit.compilationResult);
809 // md.selector = identifierStack[identifierPtr];
810 // selectorSourcePositions = identifierPositionStack[identifierPtr--];
811 // identifierLengthPtr--;
813 // md.returnType = getTypeReference(typeDims = intStack[intPtr--]);
815 // md.declarationSourceStart = intStack[intPtr--];
816 // md.modifiersSourceStart = intStack[intPtr--];
817 // md.modifiers = intStack[intPtr--];
819 // //highlight starts at selector start
820 // md.sourceStart = (int) (selectorSourcePositions >>> 32);
821 // pushOnAstStack(md);
822 // md.bodyStart = scanner.currentPosition-1;
824 //protected void consumeModifiers() {
825 // checkAnnotation(); // might update modifiers with AccDeprecated
826 // pushOnIntStack(modifiers); // modifiers
827 // pushOnIntStack(modifiersSourceStart);
829 // declarationSourceStart >= 0 ? declarationSourceStart : modifiersSourceStart);
836 //protected void consumePackageDeclarationName() {
837 // /* persisting javadoc positions */
838 // pushOnIntArrayStack(this.getJavaDocPositions());
840 // super.consumePackageDeclarationName();
841 // ImportReference importReference = compilationUnit.currentPackage;
843 // requestor.acceptPackage(
844 // importReference.declarationSourceStart,
845 // importReference.declarationSourceEnd,
846 // intArrayStack[intArrayPtr--],
847 // CharOperation.concatWith(importReference.getImportName(), '.'),
848 // importReference.sourceStart);
850 //protected void consumePushModifiers() {
851 // checkAnnotation(); // might update modifiers with AccDeprecated
852 // pushOnIntStack(modifiers); // modifiers
853 // if (modifiersSourceStart < 0) {
854 // pushOnIntStack(-1);
856 // declarationSourceStart >= 0 ? declarationSourceStart : scanner.startPosition);
858 // pushOnIntStack(modifiersSourceStart);
860 // declarationSourceStart >= 0 ? declarationSourceStart : modifiersSourceStart);
866 // * INTERNAL USE-ONLY
868 //protected void consumeSingleTypeImportDeclarationName() {
869 // // SingleTypeImportDeclarationName ::= 'import' Name
871 // /* persisting javadoc positions */
872 // pushOnIntArrayStack(this.getJavaDocPositions());
874 // super.consumeSingleTypeImportDeclarationName();
875 // ImportReference importReference = (ImportReference) astStack[astPtr];
876 // requestor.acceptImport(
877 // importReference.declarationSourceStart,
878 // importReference.declarationSourceEnd,
879 // intArrayStack[intArrayPtr--],
880 // CharOperation.concatWith(importReference.getImportName(), '.'),
881 // importReference.sourceStart,
886 // * INTERNAL USE-ONLY
888 //protected void consumeStaticInitializer() {
889 // // StaticInitializer ::= StaticOnly Block
890 // //push an Initializer
891 // //optimize the push/pop
892 // super.consumeStaticInitializer();
893 // Initializer initializer = (Initializer) astStack[astPtr];
894 // requestor.acceptInitializer(
895 // initializer.declarationSourceStart,
896 // initializer.declarationSourceEnd,
897 // intArrayStack[intArrayPtr--],
899 // intStack[intPtr--],
900 // initializer.block.sourceStart,
901 // initializer.declarationSourceEnd);
903 //protected void consumeStaticOnly() {
904 // // StaticOnly ::= 'static'
905 // checkAnnotation(); // might update declaration source start
906 // pushOnIntStack(modifiersSourceStart);
908 // declarationSourceStart >= 0 ? declarationSourceStart : modifiersSourceStart);
909 // jumpOverMethodBody();
910 // nestedMethod[nestedType]++;
915 // * INTERNAL USE-ONLY
917 //protected void consumeTypeImportOnDemandDeclarationName() {
918 // // TypeImportOnDemandDeclarationName ::= 'import' Name '.' '*'
920 // /* persisting javadoc positions */
921 // pushOnIntArrayStack(this.getJavaDocPositions());
923 // super.consumeTypeImportOnDemandDeclarationName();
924 // ImportReference importReference = (ImportReference) astStack[astPtr];
925 // requestor.acceptImport(
926 // importReference.declarationSourceStart,
927 // importReference.declarationSourceEnd,
928 // intArrayStack[intArrayPtr--],
929 // CharOperation.concatWith(importReference.getImportName(), '.'),
930 // importReference.sourceStart,
933 public CompilationUnitDeclaration endParse(int act) {
934 if (scanner.recordLineSeparator) {
935 requestor.acceptLineSeparatorPositions(scanner.getLineEnds());
937 return super.endParse(act);
940 * Flush annotations defined prior to a given positions.
942 * Note: annotations are stacked in syntactical order
944 * Either answer given <position>, or the end position of a comment line
945 * immediately following the <position> (same line)
949 * } // end of method foo
952 //public int flushAnnotationsDefinedPriorTo(int position) {
954 // return lastFieldEndPosition = super.flushAnnotationsDefinedPriorTo(position);
956 //protected TypeReference getTypeReference(int dim) { /* build a Reference on a variable that may be qualified or not
957 //This variable is a type reference and dim will be its dimensions*/
960 // TypeReference ref;
961 // if ((length = identifierLengthStack[identifierLengthPtr--]) == 1) {
962 // // single variable reference
965 // new SingleTypeReference(
966 // identifierStack[identifierPtr],
967 // identifierPositionStack[identifierPtr--]);
970 // new ArrayTypeReference(
971 // identifierStack[identifierPtr],
973 // identifierPositionStack[identifierPtr--]);
974 // ref.sourceEnd = endPosition;
977 // if (length < 0) { //flag for precompiled type reference on base types
978 // ref = TypeReference.baseTypeReference(-length, dim);
979 // ref.sourceStart = intStack[intPtr--];
981 // ref.sourceEnd = intStack[intPtr--];
984 // ref.sourceEnd = endPosition;
986 // } else { //Qualified variable reference
987 // char[][] tokens = new char[length][];
988 // identifierPtr -= length;
989 // long[] positions = new long[length];
990 // System.arraycopy(identifierStack, identifierPtr + 1, tokens, 0, length);
992 // identifierPositionStack,
993 // identifierPtr + 1,
998 // ref = new QualifiedTypeReference(tokens, positions);
1000 // ref = new ArrayQualifiedTypeReference(tokens, dim, positions);
1001 // ref.sourceEnd = endPosition;
1007 public void initialize() {
1008 //positionning the parser for a new compilation unit
1009 //avoiding stack reallocation and all that....
1010 super.initialize(false);
1017 //private boolean isLocalDeclaration() {
1018 // int nestedDepth = nestedType;
1019 // while (nestedDepth >= 0) {
1020 // if (nestedMethod[nestedDepth] != 0) {
1028 * Investigate one entire unit.
1030 public void parseCompilationUnit(ICompilationUnit unit) {
1031 char[] regionSource = unit.getContents();
1034 goForCompilationUnit();
1038 new CompilationUnitDeclaration(
1040 new CompilationResult(unit, 0, 0, 10), //this.options.maxProblemsPerUnit),
1041 regionSource.length);
1042 scanner.resetTo(0, regionSource.length);
1043 scanner.setSource(regionSource);
1045 } catch (AbortCompilation ex) {
1049 * Investigate one constructor declaration.
1051 //public void parseConstructor(char[] regionSource) {
1054 // goForClassBodyDeclarations();
1055 // referenceContext =
1056 // compilationUnit =
1057 // compilationUnit =
1058 // new CompilationUnitDeclaration(
1059 // problemReporter(),
1060 // new CompilationResult(regionSource, 0, 0, 10), //this.options.maxProblemsPerUnit),
1061 // regionSource.length);
1062 // scanner.resetTo(0, regionSource.length);
1063 // scanner.setSource(regionSource);
1065 // } catch (AbortCompilation ex) {
1069 * Investigate one field declaration statement (might have multiple declarations in it).
1071 //public void parseField(char[] regionSource) {
1074 // goForFieldDeclaration();
1075 // referenceContext =
1076 // compilationUnit =
1077 // compilationUnit =
1078 // new CompilationUnitDeclaration(
1079 // problemReporter(),
1080 // new CompilationResult(regionSource, 0, 0, this.options.maxProblemsPerUnit),
1081 // regionSource.length);
1082 // scanner.resetTo(0, regionSource.length);
1083 // scanner.setSource(regionSource);
1085 // } catch (AbortCompilation ex) {
1090 // * Investigate one import statement declaration.
1092 //public void parseImport(char[] regionSource) {
1095 // goForImportDeclaration();
1096 // referenceContext =
1097 // compilationUnit =
1098 // compilationUnit =
1099 // new CompilationUnitDeclaration(
1100 // problemReporter(),
1101 // new CompilationResult(regionSource, 0, 0, this.options.maxProblemsPerUnit),
1102 // regionSource.length);
1103 // scanner.resetTo(0, regionSource.length);
1104 // scanner.setSource(regionSource);
1106 // } catch (AbortCompilation ex) {
1111 // * Investigate one initializer declaration.
1112 // * regionSource need to content exactly an initializer declaration.
1113 // * e.g: static { i = 4; }
1114 // * { name = "test"; }
1116 //public void parseInitializer(char[] regionSource) {
1119 // goForInitializer();
1120 // referenceContext =
1121 // compilationUnit =
1122 // compilationUnit =
1123 // new CompilationUnitDeclaration(
1124 // problemReporter(),
1125 // new CompilationResult(regionSource, 0, 0, this.options.maxProblemsPerUnit),
1126 // regionSource.length);
1127 // scanner.resetTo(0, regionSource.length);
1128 // scanner.setSource(regionSource);
1130 // } catch (AbortCompilation ex) {
1135 // * Investigate one method declaration.
1137 //public void parseMethod(char[] regionSource) {
1140 // goForGenericMethodDeclaration();
1141 // referenceContext =
1142 // compilationUnit =
1143 // compilationUnit =
1144 // new CompilationUnitDeclaration(
1145 // problemReporter(),
1146 // new CompilationResult(regionSource, 0, 0, this.options.maxProblemsPerUnit),
1147 // regionSource.length);
1148 // scanner.resetTo(0, regionSource.length);
1149 // scanner.setSource(regionSource);
1151 // } catch (AbortCompilation ex) {
1156 // * Investigate one package statement declaration.
1158 //public void parsePackage(char[] regionSource) {
1161 // goForPackageDeclaration();
1162 // referenceContext =
1163 // compilationUnit =
1164 // compilationUnit =
1165 // new CompilationUnitDeclaration(
1166 // problemReporter(),
1167 // new CompilationResult(regionSource, 0, 0, this.options.maxProblemsPerUnit),
1168 // regionSource.length);
1169 // scanner.resetTo(0, regionSource.length);
1170 // scanner.setSource(regionSource);
1172 // } catch (AbortCompilation ex) {
1177 // * Investigate one type declaration, its fields, methods and member types.
1179 //public void parseType(char[] regionSource) {
1182 // goForTypeDeclaration();
1183 // referenceContext =
1184 // compilationUnit =
1185 // compilationUnit =
1186 // new CompilationUnitDeclaration(
1187 // problemReporter(),
1188 // new CompilationResult(regionSource, 0, 0, this.options.maxProblemsPerUnit),
1189 // regionSource.length);
1190 // scanner.resetTo(0, regionSource.length);
1191 // scanner.setSource(regionSource);
1193 // } catch (AbortCompilation ex) {
1198 * Returns this parser's problem reporter initialized with its reference context.
1199 * Also it is assumed that a problem is going to be reported, so initializes
1200 * the compilation result's line positions.
1202 public ProblemReporter problemReporter() {
1203 problemReporter.referenceContext = referenceContext;
1204 return problemReporter;
1206 protected void pushOnIntArrayStack(int[] positions) {
1209 intArrayStack[++intArrayPtr] = positions;
1210 } catch (IndexOutOfBoundsException e) {
1212 int oldStackLength = intArrayStack.length;
1213 int oldStack[][] = intArrayStack;
1214 intArrayStack = new int[oldStackLength + StackIncrement][];
1215 System.arraycopy(oldStack, 0, intArrayStack, 0, oldStackLength);
1216 intArrayStack[intArrayPtr] = positions;
1219 //protected void resetModifiers() {
1220 // super.resetModifiers();
1221 // declarationSourceStart = -1;
1224 * Syntax error was detected. Will attempt to perform some recovery action in order
1225 * to resume to the regular parse loop.
1227 protected boolean resumeOnSyntaxError() {
1231 * Answer a char array representation of the type name formatted like:
1232 * - type name + dimensions
1234 * "A[][]".toCharArray()
1235 * "java.lang.String".toCharArray()
1237 //private char[] returnTypeName(TypeReference type) {
1238 // int dimension = type.dimensions();
1239 // if (dimension != 0) {
1240 // char[] dimensionsArray = new char[dimension * 2];
1241 // for (int i = 0; i < dimension; i++) {
1242 // dimensionsArray[i*2] = '[';
1243 // dimensionsArray[(i*2) + 1] = ']';
1245 // return CharOperation.concat(
1246 // CharOperation.concatWith(type.getTypeName(), '.'),
1247 // dimensionsArray);
1249 // return CharOperation.concatWith(type.getTypeName(), '.');
1251 //public String toString() {
1252 // StringBuffer buffer = new StringBuffer();
1253 // buffer.append("intArrayPtr = " + intArrayPtr + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
1254 // buffer.append(super.toString());
1255 // return buffer.toString();
1258 // * INTERNAL USE ONLY
1260 //protected TypeReference typeReference(
1262 // int localIdentifierPtr,
1263 // int localIdentifierLengthPtr) {
1264 // /* build a Reference on a variable that may be qualified or not
1265 // * This variable is a type reference and dim will be its dimensions.
1266 // * We don't have any side effect on the stacks' pointers.
1270 // TypeReference ref;
1271 // if ((length = identifierLengthStack[localIdentifierLengthPtr]) == 1) {
1272 // // single variable reference
1275 // new SingleTypeReference(
1276 // identifierStack[localIdentifierPtr],
1277 // identifierPositionStack[localIdentifierPtr--]);
1280 // new ArrayTypeReference(
1281 // identifierStack[localIdentifierPtr],
1283 // identifierPositionStack[localIdentifierPtr--]);
1284 // ref.sourceEnd = endPosition;
1287 // if (length < 0) { //flag for precompiled type reference on base types
1288 // ref = TypeReference.baseTypeReference(-length, dim);
1289 // ref.sourceStart = intStack[localIntPtr--];
1291 // ref.sourceEnd = intStack[localIntPtr--];
1294 // ref.sourceEnd = endPosition;
1296 // } else { //Qualified variable reference
1297 // char[][] tokens = new char[length][];
1298 // localIdentifierPtr -= length;
1299 // long[] positions = new long[length];
1300 // System.arraycopy(identifierStack, localIdentifierPtr + 1, tokens, 0, length);
1301 // System.arraycopy(
1302 // identifierPositionStack,
1303 // localIdentifierPtr + 1,
1308 // ref = new QualifiedTypeReference(tokens, positions);
1310 // ref = new ArrayQualifiedTypeReference(tokens, dim, positions);