1 /***********************************************************************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made
3 * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at
4 * http://www.eclipse.org/legal/cpl-v10.html
6 * Contributors: IBM Corporation - initial API and implementation
7 **********************************************************************************************************************************/
8 package net.sourceforge.phpdt.internal.compiler;
10 import net.sourceforge.phpdt.core.compiler.IProblem;
11 import net.sourceforge.phpdt.internal.compiler.ast.CompilationUnitDeclaration;
12 import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit;
13 import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
14 import net.sourceforge.phpdt.internal.compiler.parser.UnitParser;
15 import net.sourceforge.phpdt.internal.compiler.problem.AbortCompilation;
16 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
19 * A document element parser extracts structural information from a piece of
20 * source, providing detailed source positions info.
22 * also see @IDocumentElementRequestor
24 * The structural investigation includes: - the package statement - import
25 * statements - top-level types: package member, member types (member types of
26 * member types...) - fields - methods
28 * Any (parsing) problem encountered is also provided.
30 public class DocumentElementParser extends UnitParser {
31 IDocumentElementRequestor requestor;
33 private int localIntPtr;
35 private int lastFieldEndPosition;
37 private int lastFieldBodyEndPosition;
39 private int typeStartPosition;
41 private long selectorSourcePositions;
45 private int extendsDim;
47 private int declarationSourceStart;
49 /* int[] stack for storing javadoc positions */
50 int[][] intArrayStack;
54 // CompilerOptions options;
56 public DocumentElementParser(final IDocumentElementRequestor requestor,
57 IProblemFactory problemFactory, CompilerOptions options) {
58 super(new ProblemReporter(DefaultErrorHandlingPolicies
59 .exitAfterAllProblems(), options, problemFactory) {
60 public void record(IProblem problem, CompilationResult unitResult) {
61 requestor.acceptProblem(problem);
65 // options.sourceLevel >= CompilerOptions.JDK1_4);
66 this.requestor = requestor;
67 intArrayStack = new int[30][];
68 this.options = options;
75 // protected void adjustInterfaceModifiers() {
76 // intStack[intPtr - 2] |= AccInterface;
79 * Will clear the comment stack when looking for a potential JavaDoc which
80 * might contain @deprecated.
82 * Additionally, before investigating for @deprecated, retrieve the
83 * positions of the JavaDoc comments so as to notify requestor with them.
85 // public void checkAnnotation() {
87 // /* persisting javadoc positions */
88 // pushOnIntArrayStack(this.getJavaDocPositions());
89 // boolean deprecated = false;
90 // int lastAnnotationIndex = -1;
91 // int commentPtr = scanner.commentPtr;
93 // //since jdk1.2 look only in the last java doc comment...
94 // nextComment : for (lastAnnotationIndex = scanner.commentPtr;
95 // lastAnnotationIndex >= 0; lastAnnotationIndex--){
96 // //look for @deprecated into the first javadoc comment preceeding the
98 // int commentSourceStart = scanner.commentStarts[lastAnnotationIndex];
99 // // javadoc only (non javadoc comment have negative end positions.)
100 // if (modifiersSourceStart != -1 && modifiersSourceStart <
101 // commentSourceStart) {
102 // continue nextComment;
104 // if (scanner.commentStops[lastAnnotationIndex] < 0) {
105 // continue nextComment;
107 // int commentSourceEnd = scanner.commentStops[lastAnnotationIndex] - 1;
108 // //stop is one over
109 // char[] comment = scanner.source;
113 // commentSourceStart,
116 // break nextComment;
119 // checkAndSetModifiers(AccDeprecated);
121 // // modify the modifier source start to point at the first comment
122 // if (commentPtr >= 0) {
123 // declarationSourceStart = scanner.commentStarts[0];
130 // protected void consumeClassBodyDeclaration() {
131 // // ClassBodyDeclaration ::= Diet Block
132 // //push an Initializer
133 // //optimize the push/pop
135 // super.consumeClassBodyDeclaration();
136 // Initializer initializer = (Initializer) astStack[astPtr];
137 // requestor.acceptInitializer(
138 // initializer.declarationSourceStart,
139 // initializer.declarationSourceEnd,
140 // intArrayStack[intArrayPtr--],
142 // modifiersSourceStart,
143 // initializer.block.sourceStart,
144 // initializer.block.sourceEnd);
148 // * INTERNAL USE-ONLY
150 // protected void consumeClassDeclaration() {
151 // super.consumeClassDeclaration();
152 // // we know that we have a TypeDeclaration on the top of the astStack
153 // if (isLocalDeclaration()) {
154 // // we ignore the local variable declarations
157 // requestor.exitClass(endStatementPosition, // '}' is the end of the body
158 // ((TypeDeclaration) astStack[astPtr]).declarationSourceEnd);
162 // * INTERNAL USE-ONLY
164 // protected void consumeClassHeader() {
165 // //ClassHeader ::= $empty
166 // super.consumeClassHeader();
167 // if (isLocalDeclaration()) {
168 // // we ignore the local variable declarations
172 // TypeDeclaration typeDecl = (TypeDeclaration) astStack[astPtr];
173 // TypeReference[] superInterfaces = typeDecl.superInterfaces;
174 // char[][] interfaceNames = null;
175 // int[] interfaceNameStarts = null;
176 // int[] interfaceNameEnds = null;
177 // if (superInterfaces != null) {
178 // int superInterfacesLength = superInterfaces.length;
179 // interfaceNames = new char[superInterfacesLength][];
180 // interfaceNameStarts = new int[superInterfacesLength];
181 // interfaceNameEnds = new int[superInterfacesLength];
182 // for (int i = 0; i < superInterfacesLength; i++) {
183 // TypeReference superInterface = superInterfaces[i];
184 // interfaceNames[i] =
185 // CharOperation.concatWith(superInterface.getTypeName(), '.');
186 // interfaceNameStarts[i] = superInterface.sourceStart;
187 // interfaceNameEnds[i] = superInterface.sourceEnd;
190 // // flush the comments related to the class header
191 // scanner.commentPtr = -1;
192 // TypeReference superclass = typeDecl.superclass;
193 // if (superclass == null) {
194 // requestor.enterClass(
195 // typeDecl.declarationSourceStart,
196 // intArrayStack[intArrayPtr--],
197 // typeDecl.modifiers,
198 // typeDecl.modifiersSourceStart,
199 // typeStartPosition,
201 // typeDecl.sourceStart,
202 // typeDecl.sourceEnd,
207 // interfaceNameStarts,
208 // interfaceNameEnds,
209 // scanner.currentPosition - 1);
211 // requestor.enterClass(
212 // typeDecl.declarationSourceStart,
213 // intArrayStack[intArrayPtr--],
214 // typeDecl.modifiers,
215 // typeDecl.modifiersSourceStart,
216 // typeStartPosition,
218 // typeDecl.sourceStart,
219 // typeDecl.sourceEnd,
220 // CharOperation.concatWith(superclass.getTypeName(), '.'),
221 // superclass.sourceStart,
222 // superclass.sourceEnd,
224 // interfaceNameStarts,
225 // interfaceNameEnds,
226 // scanner.currentPosition - 1);
230 // protected void consumeClassHeaderName() {
231 // // ClassHeaderName ::= Modifiersopt 'class' 'Identifier'
232 // TypeDeclaration typeDecl;
233 // if (nestedMethod[nestedType] == 0) {
234 // if (nestedType != 0) {
236 // MemberTypeDeclaration(this.compilationUnit.compilationResult);
238 // typeDecl = new TypeDeclaration(this.compilationUnit.compilationResult);
241 // // Record that the block has a declaration for local types
243 // LocalTypeDeclaration(this.compilationUnit.compilationResult);
244 // markEnclosingMemberWithLocalType();
248 // //highlight the name of the type
249 // long pos = identifierPositionStack[identifierPtr];
250 // typeDecl.sourceEnd = (int) pos;
251 // typeDecl.sourceStart = (int) (pos >>> 32);
252 // typeDecl.name = identifierStack[identifierPtr--];
253 // identifierLengthPtr--;
255 // //compute the declaration source too
256 // // 'class' and 'interface' push an int position
257 // typeStartPosition = typeDecl.declarationSourceStart = intStack[intPtr--];
259 // int declarationSourceStart = intStack[intPtr--];
260 // typeDecl.modifiersSourceStart = intStack[intPtr--];
261 // typeDecl.modifiers = intStack[intPtr--];
262 // if (typeDecl.declarationSourceStart > declarationSourceStart) {
263 // typeDecl.declarationSourceStart = declarationSourceStart;
265 // typeDecl.bodyStart = typeDecl.sourceEnd + 1;
266 // pushOnAstStack(typeDecl);
270 // * INTERNAL USE-ONLY
272 // protected void consumeCompilationUnit() {
273 // // CompilationUnit ::= EnterCompilationUnit PackageDeclarationopt
274 // ImportDeclarationsopt
275 // requestor.exitCompilationUnit(scanner.source.length - 1);
281 // protected void consumeConstructorDeclaration() {
282 // // ConstructorDeclaration ::= ConstructorHeader ConstructorBody
283 // super.consumeConstructorDeclaration();
284 // if (isLocalDeclaration()) {
285 // // we ignore the local variable declarations
288 // ConstructorDeclaration cd = (ConstructorDeclaration) astStack[astPtr];
289 // requestor.exitConstructor(endStatementPosition, cd.declarationSourceEnd);
293 // * INTERNAL USE-ONLY
295 // protected void consumeConstructorHeader() {
296 // // ConstructorHeader ::= ConstructorHeaderName MethodHeaderParameters
297 // MethodHeaderThrowsClauseopt
298 // super.consumeConstructorHeader();
299 // if (isLocalDeclaration()) {
300 // // we ignore the local variable declarations
304 // ConstructorDeclaration cd = (ConstructorDeclaration) astStack[astPtr];
305 // Argument[] arguments = cd.arguments;
306 // char[][] argumentTypes = null;
307 // char[][] argumentNames = null;
308 // int[] argumentTypeStarts = null;
309 // int[] argumentTypeEnds = null;
310 // int[] argumentNameStarts = null;
311 // int[] argumentNameEnds = null;
312 // if (arguments != null) {
313 // int argumentLength = arguments.length;
314 // argumentTypes = new char[argumentLength][];
315 // argumentNames = new char[argumentLength][];
316 // argumentNameStarts = new int[argumentLength];
317 // argumentNameEnds = new int[argumentLength];
318 // argumentTypeStarts = new int[argumentLength];
319 // argumentTypeEnds = new int[argumentLength];
320 // for (int i = 0; i < argumentLength; i++) {
321 // Argument argument = arguments[i];
322 // TypeReference argumentType = argument.type;
323 // argumentTypes[i] = returnTypeName(argumentType);
324 // argumentNames[i] = argument.name;
325 // argumentNameStarts[i] = argument.sourceStart;
326 // argumentNameEnds[i] = argument.sourceEnd;
327 // argumentTypeStarts[i] = argumentType.sourceStart;
328 // argumentTypeEnds[i] = argumentType.sourceEnd;
331 // TypeReference[] thrownExceptions = cd.thrownExceptions;
332 // char[][] exceptionTypes = null;
333 // int[] exceptionTypeStarts = null;
334 // int[] exceptionTypeEnds = null;
335 // if (thrownExceptions != null) {
336 // int thrownExceptionLength = thrownExceptions.length;
337 // exceptionTypes = new char[thrownExceptionLength][];
338 // exceptionTypeStarts = new int[thrownExceptionLength];
339 // exceptionTypeEnds = new int[thrownExceptionLength];
340 // for (int i = 0; i < thrownExceptionLength; i++) {
341 // TypeReference exception = thrownExceptions[i];
342 // exceptionTypes[i] = CharOperation.concatWith(exception.getTypeName(),
344 // exceptionTypeStarts[i] = exception.sourceStart;
345 // exceptionTypeEnds[i] = exception.sourceEnd;
349 // .enterConstructor(
350 // cd.declarationSourceStart,
351 // intArrayStack[intArrayPtr--],
353 // cd.modifiersSourceStart,
356 // (int) (selectorSourcePositions & 0xFFFFFFFFL),
357 // // retrieve the source end of the name
359 // argumentTypeStarts,
362 // argumentNameStarts,
365 // // right parenthesis
367 // exceptionTypeStarts,
368 // exceptionTypeEnds,
369 // scanner.currentPosition - 1);
371 // protected void consumeConstructorHeaderName() {
372 // // ConstructorHeaderName ::= Modifiersopt 'Identifier' '('
373 // ConstructorDeclaration cd = new
374 // ConstructorDeclaration(this.compilationUnit.compilationResult);
376 // //name -- this is not really revelant but we do .....
377 // cd.selector = identifierStack[identifierPtr];
378 // selectorSourcePositions = identifierPositionStack[identifierPtr--];
379 // identifierLengthPtr--;
382 // cd.declarationSourceStart = intStack[intPtr--];
383 // cd.modifiersSourceStart = intStack[intPtr--];
384 // cd.modifiers = intStack[intPtr--];
386 // //highlight starts at the selector starts
387 // cd.sourceStart = (int) (selectorSourcePositions >>> 32);
388 // pushOnAstStack(cd);
390 // cd.sourceEnd = lParenPos;
391 // cd.bodyStart = lParenPos + 1;
393 // protected void consumeDefaultModifiers() {
394 // checkAnnotation(); // might update modifiers with AccDeprecated
395 // pushOnIntStack(modifiers); // modifiers
396 // pushOnIntStack(-1);
398 // declarationSourceStart >= 0 ? declarationSourceStart :
399 // scanner.startPosition);
402 // protected void consumeDiet() {
403 // // Diet ::= $empty
404 // super.consumeDiet();
405 // /* persisting javadoc positions
406 // * Will be consume in consumeClassBodyDeclaration
408 // pushOnIntArrayStack(this.getJavaDocPositions());
412 // * INTERNAL USE-ONLY
414 // protected void consumeEnterCompilationUnit() {
415 // // EnterCompilationUnit ::= $empty
416 // requestor.enterCompilationUnit();
420 // * INTERNAL USE-ONLY
422 // protected void consumeEnterVariable() {
423 // // EnterVariable ::= $empty
424 // boolean isLocalDeclaration = isLocalDeclaration();
425 // if (!isLocalDeclaration && (variablesCounter[nestedType] != 0)) {
426 // requestor.exitField(lastFieldBodyEndPosition, lastFieldEndPosition);
428 // char[] name = identifierStack[identifierPtr];
429 // long namePosition = identifierPositionStack[identifierPtr--];
430 // int extendedTypeDimension = intStack[intPtr--];
432 // AbstractVariableDeclaration declaration;
433 // if (nestedMethod[nestedType] != 0) {
434 // // create the local variable declarations
436 // new LocalDeclaration(null, name, (int) (namePosition >>> 32), (int)
439 // // create the field declaration
441 // new FieldDeclaration(null, name, (int) (namePosition >>> 32), (int)
444 // identifierLengthPtr--;
445 // TypeReference type;
446 // int variableIndex = variablesCounter[nestedType];
448 // if (variableIndex == 0) {
449 // // first variable of the declaration (FieldDeclaration or
451 // if (nestedMethod[nestedType] != 0) {
452 // // local declaration
453 // declaration.declarationSourceStart = intStack[intPtr--];
454 // declaration.modifiersSourceStart = intStack[intPtr--];
455 // declaration.modifiers = intStack[intPtr--];
456 // type = getTypeReference(typeDim = intStack[intPtr--]); // type dimension
457 // pushOnAstStack(type);
459 // // field declaration
460 // type = getTypeReference(typeDim = intStack[intPtr--]); // type dimension
461 // pushOnAstStack(type);
462 // declaration.declarationSourceStart = intStack[intPtr--];
463 // declaration.modifiersSourceStart = intStack[intPtr--];
464 // declaration.modifiers = intStack[intPtr--];
467 // type = (TypeReference) astStack[astPtr - variableIndex];
468 // typeDim = type.dimensions();
469 // AbstractVariableDeclaration previousVariable =
470 // (AbstractVariableDeclaration) astStack[astPtr];
471 // declaration.declarationSourceStart =
472 // previousVariable.declarationSourceStart;
473 // declaration.modifiers = previousVariable.modifiers;
474 // declaration.modifiersSourceStart = previousVariable.modifiersSourceStart;
477 // localIntPtr = intPtr;
479 // if (extendedTypeDimension == 0) {
480 // declaration.type = type;
482 // int dimension = typeDim + extendedTypeDimension;
483 // //on the identifierLengthStack there is the information about the
486 // if ((baseType = identifierLengthStack[identifierLengthPtr + 1]) < 0) {
487 // //it was a baseType
488 // declaration.type = TypeReference.baseTypeReference(-baseType, dimension);
489 // declaration.type.sourceStart = type.sourceStart;
490 // declaration.type.sourceEnd = type.sourceEnd;
492 // declaration.type = this.copyDims(type, dimension);
495 // variablesCounter[nestedType]++;
496 // nestedMethod[nestedType]++;
497 // pushOnAstStack(declaration);
499 // int[] javadocPositions = intArrayStack[intArrayPtr];
500 // if (!isLocalDeclaration) {
503 // declaration.declarationSourceStart,
505 // declaration.modifiers,
506 // declaration.modifiersSourceStart,
507 // returnTypeName(declaration.type),
512 // (int) (namePosition >>> 32),
513 // (int) namePosition,
514 // extendedTypeDimension,
515 // extendedTypeDimension == 0 ? -1 : endPosition);
520 // * INTERNAL USE-ONLY
522 // protected void consumeExitVariableWithInitialization() {
523 // // ExitVariableWithInitialization ::= $empty
524 // // the scanner is located after the comma or the semi-colon.
525 // // we want to include the comma or the semi-colon
526 // super.consumeExitVariableWithInitialization();
527 // nestedMethod[nestedType]--;
528 // lastFieldEndPosition = scanner.currentPosition - 1;
529 // lastFieldBodyEndPosition = ((AbstractVariableDeclaration)
530 // astStack[astPtr]).initialization.sourceEnd;
532 // protected void consumeExitVariableWithoutInitialization() {
533 // // ExitVariableWithoutInitialization ::= $empty
534 // // do nothing by default
535 // super.consumeExitVariableWithoutInitialization();
536 // nestedMethod[nestedType]--;
537 // lastFieldEndPosition = scanner.currentPosition - 1;
538 // lastFieldBodyEndPosition = scanner.startPosition - 1;
542 // * INTERNAL USE-ONLY
544 // protected void consumeFieldDeclaration() {
545 // // See consumeLocalVariableDeclarationDefaultModifier() in case of
546 // change: duplicated code
547 // // FieldDeclaration ::= Modifiersopt Type VariableDeclarators ';'
548 // // the super.consumeFieldDeclaration will reinitialize the
549 // variableCounter[nestedType]
550 // int variableIndex = variablesCounter[nestedType];
551 // super.consumeFieldDeclaration();
553 // if (isLocalDeclaration())
555 // if (variableIndex != 0) {
556 // requestor.exitField(lastFieldBodyEndPosition, lastFieldEndPosition);
559 // protected void consumeFormalParameter() {
560 // // FormalParameter ::= Type VariableDeclaratorId ==> false
561 // // FormalParameter ::= Modifiers Type VariableDeclaratorId ==> true
564 // identifierStack : type identifier
565 // intStack : dim dim
567 // astStack : Argument
572 // identifierLengthPtr--;
573 // char[] name = identifierStack[identifierPtr];
574 // long namePositions = identifierPositionStack[identifierPtr--];
575 // TypeReference type = getTypeReference(intStack[intPtr--] +
576 // intStack[intPtr--]);
583 // intStack[intPtr + 1]); // modifiers
584 // pushOnAstStack(arg);
589 // * INTERNAL USE-ONLY
591 // protected void consumeInterfaceDeclaration() {
592 // super.consumeInterfaceDeclaration();
593 // // we know that we have a TypeDeclaration on the top of the astStack
594 // if (isLocalDeclaration()) {
595 // // we ignore the local variable declarations
598 // requestor.exitInterface(endStatementPosition, // the '}' is the end of
600 // ((TypeDeclaration) astStack[astPtr]).declarationSourceEnd);
604 // * INTERNAL USE-ONLY
606 // protected void consumeInterfaceHeader() {
607 // //InterfaceHeader ::= $empty
608 // super.consumeInterfaceHeader();
609 // if (isLocalDeclaration()) {
610 // // we ignore the local variable declarations
614 // TypeDeclaration typeDecl = (TypeDeclaration) astStack[astPtr];
615 // TypeReference[] superInterfaces = typeDecl.superInterfaces;
616 // char[][] interfaceNames = null;
617 // int[] interfaceNameStarts = null;
618 // int[] interfacenameEnds = null;
619 // int superInterfacesLength = 0;
620 // if (superInterfaces != null) {
621 // superInterfacesLength = superInterfaces.length;
622 // interfaceNames = new char[superInterfacesLength][];
623 // interfaceNameStarts = new int[superInterfacesLength];
624 // interfacenameEnds = new int[superInterfacesLength];
626 // if (superInterfaces != null) {
627 // for (int i = 0; i < superInterfacesLength; i++) {
628 // TypeReference superInterface = superInterfaces[i];
629 // interfaceNames[i] =
630 // CharOperation.concatWith(superInterface.getTypeName(), '.');
631 // interfaceNameStarts[i] = superInterface.sourceStart;
632 // interfacenameEnds[i] = superInterface.sourceEnd;
635 // // flush the comments related to the interface header
636 // scanner.commentPtr = -1;
637 // requestor.enterInterface(
638 // typeDecl.declarationSourceStart,
639 // intArrayStack[intArrayPtr--],
640 // typeDecl.modifiers,
641 // typeDecl.modifiersSourceStart,
642 // typeStartPosition,
644 // typeDecl.sourceStart,
645 // typeDecl.sourceEnd,
647 // interfaceNameStarts,
648 // interfacenameEnds,
649 // scanner.currentPosition - 1);
651 // protected void consumeInterfaceHeaderName() {
652 // // InterfaceHeaderName ::= Modifiersopt 'interface' 'Identifier'
653 // TypeDeclaration typeDecl;
654 // if (nestedMethod[nestedType] == 0) {
655 // if (nestedType != 0) {
657 // MemberTypeDeclaration(this.compilationUnit.compilationResult);
659 // typeDecl = new TypeDeclaration(this.compilationUnit.compilationResult);
662 // // Record that the block has a declaration for local types
664 // LocalTypeDeclaration(this.compilationUnit.compilationResult);
665 // markEnclosingMemberWithLocalType();
669 // //highlight the name of the type
670 // long pos = identifierPositionStack[identifierPtr];
671 // typeDecl.sourceEnd = (int) pos;
672 // typeDecl.sourceStart = (int) (pos >>> 32);
673 // typeDecl.name = identifierStack[identifierPtr--];
674 // identifierLengthPtr--;
676 // //compute the declaration source too
677 // // 'class' and 'interface' push an int position
678 // typeStartPosition = typeDecl.declarationSourceStart = intStack[intPtr--];
680 // int declarationSourceStart = intStack[intPtr--];
681 // typeDecl.modifiersSourceStart = intStack[intPtr--];
682 // typeDecl.modifiers = intStack[intPtr--];
683 // if (typeDecl.declarationSourceStart > declarationSourceStart) {
684 // typeDecl.declarationSourceStart = declarationSourceStart;
686 // typeDecl.bodyStart = typeDecl.sourceEnd + 1;
687 // pushOnAstStack(typeDecl);
691 // * INTERNAL USE-ONLY
693 // protected void consumeLocalVariableDeclaration() {
694 // // See consumeLocalVariableDeclarationDefaultModifier() in case of
695 // change: duplicated code
696 // // FieldDeclaration ::= Modifiersopt Type VariableDeclarators ';'
698 // super.consumeLocalVariableDeclaration();
703 // * INTERNAL USE-ONLY
705 // protected void consumeMethodDeclaration(boolean isNotAbstract) {
706 // // MethodDeclaration ::= MethodHeader MethodBody
707 // // AbstractMethodDeclaration ::= MethodHeader ';'
708 // super.consumeMethodDeclaration(isNotAbstract);
709 // if (isLocalDeclaration()) {
710 // // we ignore the local variable declarations
713 // MethodDeclaration md = (MethodDeclaration) astStack[astPtr];
714 // requestor.exitMethod(endStatementPosition, md.declarationSourceEnd);
718 // * INTERNAL USE-ONLY
720 // protected void consumeMethodHeader() {
721 // // MethodHeader ::= MethodHeaderName MethodHeaderParameters
722 // MethodHeaderExtendedDims ThrowsClauseopt
723 // super.consumeMethodHeader();
724 // if (isLocalDeclaration()) {
725 // // we ignore the local variable declarations
729 // MethodDeclaration md = (MethodDeclaration) astStack[astPtr];
731 // TypeReference returnType = md.returnType;
732 // char[] returnTypeName = returnTypeName(returnType);
733 // Argument[] arguments = md.arguments;
734 // char[][] argumentTypes = null;
735 // char[][] argumentNames = null;
736 // int[] argumentTypeStarts = null;
737 // int[] argumentTypeEnds = null;
738 // int[] argumentNameStarts = null;
739 // int[] argumentNameEnds = null;
740 // if (arguments != null) {
741 // int argumentLength = arguments.length;
742 // argumentTypes = new char[argumentLength][];
743 // argumentNames = new char[argumentLength][];
744 // argumentNameStarts = new int[argumentLength];
745 // argumentNameEnds = new int[argumentLength];
746 // argumentTypeStarts = new int[argumentLength];
747 // argumentTypeEnds = new int[argumentLength];
748 // for (int i = 0; i < argumentLength; i++) {
749 // Argument argument = arguments[i];
750 // TypeReference argumentType = argument.type;
751 // argumentTypes[i] = returnTypeName(argumentType);
752 // argumentNames[i] = argument.name;
753 // argumentNameStarts[i] = argument.sourceStart;
754 // argumentNameEnds[i] = argument.sourceEnd;
755 // argumentTypeStarts[i] = argumentType.sourceStart;
756 // argumentTypeEnds[i] = argumentType.sourceEnd;
759 // TypeReference[] thrownExceptions = md.thrownExceptions;
760 // char[][] exceptionTypes = null;
761 // int[] exceptionTypeStarts = null;
762 // int[] exceptionTypeEnds = null;
763 // if (thrownExceptions != null) {
764 // int thrownExceptionLength = thrownExceptions.length;
765 // exceptionTypeStarts = new int[thrownExceptionLength];
766 // exceptionTypeEnds = new int[thrownExceptionLength];
767 // exceptionTypes = new char[thrownExceptionLength][];
768 // for (int i = 0; i < thrownExceptionLength; i++) {
769 // TypeReference exception = thrownExceptions[i];
770 // exceptionTypes[i] = CharOperation.concatWith(exception.getTypeName(),
772 // exceptionTypeStarts[i] = exception.sourceStart;
773 // exceptionTypeEnds[i] = exception.sourceEnd;
778 // md.declarationSourceStart,
779 // intArrayStack[intArrayPtr--],
781 // md.modifiersSourceStart,
783 // returnType.sourceStart,
784 // returnType.sourceEnd,
788 // (int) (selectorSourcePositions & 0xFFFFFFFFL),
790 // argumentTypeStarts,
793 // argumentNameStarts,
797 // extendsDim == 0 ? -1 : endPosition,
799 // exceptionTypeStarts,
800 // exceptionTypeEnds,
801 // scanner.currentPosition - 1);
803 // protected void consumeMethodHeaderExtendedDims() {
804 // // MethodHeaderExtendedDims ::= Dimsopt
805 // // now we update the returnType of the method
806 // MethodDeclaration md = (MethodDeclaration) astStack[astPtr];
807 // int extendedDims = intStack[intPtr--];
808 // extendsDim = extendedDims;
809 // if (extendedDims != 0) {
810 // TypeReference returnType = md.returnType;
811 // md.sourceEnd = endPosition;
812 // int dims = returnType.dimensions() + extendedDims;
814 // if ((baseType = identifierLengthStack[identifierLengthPtr + 1]) < 0) {
815 // //it was a baseType
816 // int sourceStart = returnType.sourceStart;
817 // int sourceEnd = returnType.sourceEnd;
818 // returnType = TypeReference.baseTypeReference(-baseType, dims);
819 // returnType.sourceStart = sourceStart;
820 // returnType.sourceEnd = sourceEnd;
821 // md.returnType = returnType;
823 // md.returnType = this.copyDims(md.returnType, dims);
825 // if (currentToken == TokenNameLBRACE) {
826 // md.bodyStart = endPosition + 1;
830 // protected void consumeMethodHeaderName() {
831 // // MethodHeaderName ::= Modifiersopt Type 'Identifier' '('
832 // MethodDeclaration md = new
833 // MethodDeclaration(this.compilationUnit.compilationResult);
836 // md.selector = identifierStack[identifierPtr];
837 // selectorSourcePositions = identifierPositionStack[identifierPtr--];
838 // identifierLengthPtr--;
840 // md.returnType = getTypeReference(typeDims = intStack[intPtr--]);
842 // md.declarationSourceStart = intStack[intPtr--];
843 // md.modifiersSourceStart = intStack[intPtr--];
844 // md.modifiers = intStack[intPtr--];
846 // //highlight starts at selector start
847 // md.sourceStart = (int) (selectorSourcePositions >>> 32);
848 // pushOnAstStack(md);
849 // md.bodyStart = scanner.currentPosition-1;
851 // protected void consumeModifiers() {
852 // checkAnnotation(); // might update modifiers with AccDeprecated
853 // pushOnIntStack(modifiers); // modifiers
854 // pushOnIntStack(modifiersSourceStart);
856 // declarationSourceStart >= 0 ? declarationSourceStart :
857 // modifiersSourceStart);
864 // protected void consumePackageDeclarationName() {
865 // /* persisting javadoc positions */
866 // pushOnIntArrayStack(this.getJavaDocPositions());
868 // super.consumePackageDeclarationName();
869 // ImportReference importReference = compilationUnit.currentPackage;
871 // requestor.acceptPackage(
872 // importReference.declarationSourceStart,
873 // importReference.declarationSourceEnd,
874 // intArrayStack[intArrayPtr--],
875 // CharOperation.concatWith(importReference.getImportName(), '.'),
876 // importReference.sourceStart);
878 // protected void consumePushModifiers() {
879 // checkAnnotation(); // might update modifiers with AccDeprecated
880 // pushOnIntStack(modifiers); // modifiers
881 // if (modifiersSourceStart < 0) {
882 // pushOnIntStack(-1);
884 // declarationSourceStart >= 0 ? declarationSourceStart :
885 // scanner.startPosition);
887 // pushOnIntStack(modifiersSourceStart);
889 // declarationSourceStart >= 0 ? declarationSourceStart :
890 // modifiersSourceStart);
896 // * INTERNAL USE-ONLY
898 // protected void consumeSingleTypeImportDeclarationName() {
899 // // SingleTypeImportDeclarationName ::= 'import' Name
901 // /* persisting javadoc positions */
902 // pushOnIntArrayStack(this.getJavaDocPositions());
904 // super.consumeSingleTypeImportDeclarationName();
905 // ImportReference importReference = (ImportReference) astStack[astPtr];
906 // requestor.acceptImport(
907 // importReference.declarationSourceStart,
908 // importReference.declarationSourceEnd,
909 // intArrayStack[intArrayPtr--],
910 // CharOperation.concatWith(importReference.getImportName(), '.'),
911 // importReference.sourceStart,
916 // * INTERNAL USE-ONLY
918 // protected void consumeStaticInitializer() {
919 // // StaticInitializer ::= StaticOnly Block
920 // //push an Initializer
921 // //optimize the push/pop
922 // super.consumeStaticInitializer();
923 // Initializer initializer = (Initializer) astStack[astPtr];
924 // requestor.acceptInitializer(
925 // initializer.declarationSourceStart,
926 // initializer.declarationSourceEnd,
927 // intArrayStack[intArrayPtr--],
929 // intStack[intPtr--],
930 // initializer.block.sourceStart,
931 // initializer.declarationSourceEnd);
933 // protected void consumeStaticOnly() {
934 // // StaticOnly ::= 'static'
935 // checkAnnotation(); // might update declaration source start
936 // pushOnIntStack(modifiersSourceStart);
938 // declarationSourceStart >= 0 ? declarationSourceStart :
939 // modifiersSourceStart);
940 // jumpOverMethodBody();
941 // nestedMethod[nestedType]++;
946 // * INTERNAL USE-ONLY
948 // protected void consumeTypeImportOnDemandDeclarationName() {
949 // // TypeImportOnDemandDeclarationName ::= 'import' Name '.' '*'
951 // /* persisting javadoc positions */
952 // pushOnIntArrayStack(this.getJavaDocPositions());
954 // super.consumeTypeImportOnDemandDeclarationName();
955 // ImportReference importReference = (ImportReference) astStack[astPtr];
956 // requestor.acceptImport(
957 // importReference.declarationSourceStart,
958 // importReference.declarationSourceEnd,
959 // intArrayStack[intArrayPtr--],
960 // CharOperation.concatWith(importReference.getImportName(), '.'),
961 // importReference.sourceStart,
964 public CompilationUnitDeclaration endParse(int act) {
965 if (scanner.recordLineSeparator) {
966 requestor.acceptLineSeparatorPositions(scanner.getLineEnds());
968 return super.endParse(act);
972 * Flush annotations defined prior to a given positions.
974 * Note: annotations are stacked in syntactical order
976 * Either answer given <position>, or the end position of a comment line
977 * immediately following the <position> (same line)
979 * e.g. void foo(){ } // end of method foo
982 // public int flushAnnotationsDefinedPriorTo(int position) {
984 // return lastFieldEndPosition =
985 // super.flushAnnotationsDefinedPriorTo(position);
987 // protected TypeReference getTypeReference(int dim) { /* build a Reference
988 // on a variable that may be qualified or not
989 // This variable is a type reference and dim will be its dimensions*/
992 // TypeReference ref;
993 // if ((length = identifierLengthStack[identifierLengthPtr--]) == 1) {
994 // // single variable reference
997 // new SingleTypeReference(
998 // identifierStack[identifierPtr],
999 // identifierPositionStack[identifierPtr--]);
1002 // new ArrayTypeReference(
1003 // identifierStack[identifierPtr],
1005 // identifierPositionStack[identifierPtr--]);
1006 // ref.sourceEnd = endPosition;
1009 // if (length < 0) { //flag for precompiled type reference on base types
1010 // ref = TypeReference.baseTypeReference(-length, dim);
1011 // ref.sourceStart = intStack[intPtr--];
1013 // ref.sourceEnd = intStack[intPtr--];
1016 // ref.sourceEnd = endPosition;
1018 // } else { //Qualified variable reference
1019 // char[][] tokens = new char[length][];
1020 // identifierPtr -= length;
1021 // long[] positions = new long[length];
1022 // System.arraycopy(identifierStack, identifierPtr + 1, tokens, 0, length);
1023 // System.arraycopy(
1024 // identifierPositionStack,
1025 // identifierPtr + 1,
1030 // ref = new QualifiedTypeReference(tokens, positions);
1032 // ref = new ArrayQualifiedTypeReference(tokens, dim, positions);
1033 // ref.sourceEnd = endPosition;
1039 public void initialize() {
1040 // positionning the parser for a new compilation unit
1041 // avoiding stack reallocation and all that....
1042 super.initialize(false);
1050 // private boolean isLocalDeclaration() {
1051 // int nestedDepth = nestedType;
1052 // while (nestedDepth >= 0) {
1053 // if (nestedMethod[nestedDepth] != 0) {
1061 * Investigate one entire unit.
1063 public void parseCompilationUnit(ICompilationUnit unit) {
1064 char[] regionSource = unit.getContents();
1067 goForCompilationUnit();
1068 referenceContext = compilationUnit = new CompilationUnitDeclaration(
1069 problemReporter(), new CompilationResult(unit, 0, 0, 10), // this.options.maxProblemsPerUnit),
1070 regionSource.length);
1071 scanner.resetTo(0, regionSource.length);
1072 scanner.setSource(regionSource);
1074 } catch (AbortCompilation ex) {
1079 * Investigate one constructor declaration.
1081 // public void parseConstructor(char[] regionSource) {
1084 // goForClassBodyDeclarations();
1085 // referenceContext =
1086 // compilationUnit =
1087 // compilationUnit =
1088 // new CompilationUnitDeclaration(
1089 // problemReporter(),
1090 // new CompilationResult(regionSource, 0, 0, 10),
1091 // //this.options.maxProblemsPerUnit),
1092 // regionSource.length);
1093 // scanner.resetTo(0, regionSource.length);
1094 // scanner.setSource(regionSource);
1096 // } catch (AbortCompilation ex) {
1100 * Investigate one field declaration statement (might have multiple
1101 * declarations in it).
1103 // public void parseField(char[] regionSource) {
1106 // goForFieldDeclaration();
1107 // referenceContext =
1108 // compilationUnit =
1109 // compilationUnit =
1110 // new CompilationUnitDeclaration(
1111 // problemReporter(),
1112 // new CompilationResult(regionSource, 0, 0,
1113 // this.options.maxProblemsPerUnit),
1114 // regionSource.length);
1115 // scanner.resetTo(0, regionSource.length);
1116 // scanner.setSource(regionSource);
1118 // } catch (AbortCompilation ex) {
1123 // * Investigate one import statement declaration.
1125 // public void parseImport(char[] regionSource) {
1128 // goForImportDeclaration();
1129 // referenceContext =
1130 // compilationUnit =
1131 // compilationUnit =
1132 // new CompilationUnitDeclaration(
1133 // problemReporter(),
1134 // new CompilationResult(regionSource, 0, 0,
1135 // this.options.maxProblemsPerUnit),
1136 // regionSource.length);
1137 // scanner.resetTo(0, regionSource.length);
1138 // scanner.setSource(regionSource);
1140 // } catch (AbortCompilation ex) {
1145 // * Investigate one initializer declaration.
1146 // * regionSource need to content exactly an initializer declaration.
1147 // * e.g: static { i = 4; }
1148 // * { name = "test"; }
1150 // public void parseInitializer(char[] regionSource) {
1153 // goForInitializer();
1154 // referenceContext =
1155 // compilationUnit =
1156 // compilationUnit =
1157 // new CompilationUnitDeclaration(
1158 // problemReporter(),
1159 // new CompilationResult(regionSource, 0, 0,
1160 // this.options.maxProblemsPerUnit),
1161 // regionSource.length);
1162 // scanner.resetTo(0, regionSource.length);
1163 // scanner.setSource(regionSource);
1165 // } catch (AbortCompilation ex) {
1170 // * Investigate one method declaration.
1172 // public void parseMethod(char[] regionSource) {
1175 // goForGenericMethodDeclaration();
1176 // referenceContext =
1177 // compilationUnit =
1178 // compilationUnit =
1179 // new CompilationUnitDeclaration(
1180 // problemReporter(),
1181 // new CompilationResult(regionSource, 0, 0,
1182 // this.options.maxProblemsPerUnit),
1183 // regionSource.length);
1184 // scanner.resetTo(0, regionSource.length);
1185 // scanner.setSource(regionSource);
1187 // } catch (AbortCompilation ex) {
1192 // * Investigate one package statement declaration.
1194 // public void parsePackage(char[] regionSource) {
1197 // goForPackageDeclaration();
1198 // referenceContext =
1199 // compilationUnit =
1200 // compilationUnit =
1201 // new CompilationUnitDeclaration(
1202 // problemReporter(),
1203 // new CompilationResult(regionSource, 0, 0,
1204 // this.options.maxProblemsPerUnit),
1205 // regionSource.length);
1206 // scanner.resetTo(0, regionSource.length);
1207 // scanner.setSource(regionSource);
1209 // } catch (AbortCompilation ex) {
1214 // * Investigate one type declaration, its fields, methods and member types.
1216 // public void parseType(char[] regionSource) {
1219 // goForTypeDeclaration();
1220 // referenceContext =
1221 // compilationUnit =
1222 // compilationUnit =
1223 // new CompilationUnitDeclaration(
1224 // problemReporter(),
1225 // new CompilationResult(regionSource, 0, 0,
1226 // this.options.maxProblemsPerUnit),
1227 // regionSource.length);
1228 // scanner.resetTo(0, regionSource.length);
1229 // scanner.setSource(regionSource);
1231 // } catch (AbortCompilation ex) {
1236 * Returns this parser's problem reporter initialized with its reference
1237 * context. Also it is assumed that a problem is going to be reported, so
1238 * initializes the compilation result's line positions.
1240 public ProblemReporter problemReporter() {
1241 problemReporter.referenceContext = referenceContext;
1242 return problemReporter;
1245 protected void pushOnIntArrayStack(int[] positions) {
1248 intArrayStack[++intArrayPtr] = positions;
1249 } catch (IndexOutOfBoundsException e) {
1250 // intPtr is correct
1251 int oldStackLength = intArrayStack.length;
1252 int oldStack[][] = intArrayStack;
1253 intArrayStack = new int[oldStackLength + StackIncrement][];
1254 System.arraycopy(oldStack, 0, intArrayStack, 0, oldStackLength);
1255 intArrayStack[intArrayPtr] = positions;
1259 // protected void resetModifiers() {
1260 // super.resetModifiers();
1261 // declarationSourceStart = -1;
1264 * Syntax error was detected. Will attempt to perform some recovery action
1265 * in order to resume to the regular parse loop.
1267 protected boolean resumeOnSyntaxError() {
1271 * Answer a char array representation of the type name formatted like: -
1272 * type name + dimensions Example: "A[][]".toCharArray()
1273 * "java.lang.String".toCharArray()
1275 // private char[] returnTypeName(TypeReference type) {
1276 // int dimension = type.dimensions();
1277 // if (dimension != 0) {
1278 // char[] dimensionsArray = new char[dimension * 2];
1279 // for (int i = 0; i < dimension; i++) {
1280 // dimensionsArray[i*2] = '[';
1281 // dimensionsArray[(i*2) + 1] = ']';
1283 // return CharOperation.concat(
1284 // CharOperation.concatWith(type.getTypeName(), '.'),
1285 // dimensionsArray);
1287 // return CharOperation.concatWith(type.getTypeName(), '.');
1289 // public String toString() {
1290 // StringBuffer buffer = new StringBuffer();
1291 // buffer.append("intArrayPtr = " + intArrayPtr + "\n"); //$NON-NLS-1$
1293 // buffer.append(super.toString());
1294 // return buffer.toString();
1297 // * INTERNAL USE ONLY
1299 // protected TypeReference typeReference(
1301 // int localIdentifierPtr,
1302 // int localIdentifierLengthPtr) {
1303 // /* build a Reference on a variable that may be qualified or not
1304 // * This variable is a type reference and dim will be its dimensions.
1305 // * We don't have any side effect on the stacks' pointers.
1309 // TypeReference ref;
1310 // if ((length = identifierLengthStack[localIdentifierLengthPtr]) == 1) {
1311 // // single variable reference
1314 // new SingleTypeReference(
1315 // identifierStack[localIdentifierPtr],
1316 // identifierPositionStack[localIdentifierPtr--]);
1319 // new ArrayTypeReference(
1320 // identifierStack[localIdentifierPtr],
1322 // identifierPositionStack[localIdentifierPtr--]);
1323 // ref.sourceEnd = endPosition;
1326 // if (length < 0) { //flag for precompiled type reference on base types
1327 // ref = TypeReference.baseTypeReference(-length, dim);
1328 // ref.sourceStart = intStack[localIntPtr--];
1330 // ref.sourceEnd = intStack[localIntPtr--];
1333 // ref.sourceEnd = endPosition;
1335 // } else { //Qualified variable reference
1336 // char[][] tokens = new char[length][];
1337 // localIdentifierPtr -= length;
1338 // long[] positions = new long[length];
1339 // System.arraycopy(identifierStack, localIdentifierPtr + 1, tokens, 0,
1341 // System.arraycopy(
1342 // identifierPositionStack,
1343 // localIdentifierPtr + 1,
1348 // ref = new QualifiedTypeReference(tokens, positions);
1350 // ref = new ArrayQualifiedTypeReference(tokens, dim, positions);