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.env.ICompilationUnit;
12 import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
13 import net.sourceforge.phpdt.internal.compiler.parser.UnitParser;
14 import net.sourceforge.phpdt.internal.compiler.problem.AbortCompilation;
15 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
16 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
19 * A document element parser extracts structural information from a piece of source, providing detailed source positions info.
21 * also see @IDocumentElementRequestor
23 * The structural investigation includes: - the package statement - import statements - top-level types: package member, member
24 * types (member types of member types...) - fields - methods
26 * Any (parsing) problem encountered is also provided.
28 public class DocumentElementParser extends UnitParser {
29 IDocumentElementRequestor requestor;
31 private int localIntPtr;
33 private int lastFieldEndPosition;
35 private int lastFieldBodyEndPosition;
37 private int typeStartPosition;
39 private long selectorSourcePositions;
43 private int extendsDim;
45 private int declarationSourceStart;
47 /* int[] stack for storing javadoc positions */
48 int[][] intArrayStack;
52 // CompilerOptions options;
54 public DocumentElementParser(final IDocumentElementRequestor requestor, IProblemFactory problemFactory,
55 CompilerOptions options) {
56 super(new ProblemReporter(DefaultErrorHandlingPolicies.exitAfterAllProblems(), options, problemFactory) {
57 public void record(IProblem problem, CompilationResult unitResult) {
58 requestor.acceptProblem(problem);
62 // options.sourceLevel >= CompilerOptions.JDK1_4);
63 this.requestor = requestor;
64 intArrayStack = new int[30][];
65 this.options = options;
72 //protected void adjustInterfaceModifiers() {
73 // intStack[intPtr - 2] |= AccInterface;
76 * Will clear the comment stack when looking for a potential JavaDoc which might contain @deprecated.
78 * Additionally, before investigating for @deprecated, retrieve the positions of the JavaDoc comments so as to notify requestor
81 //public void checkAnnotation() {
83 // /* persisting javadoc positions */
84 // pushOnIntArrayStack(this.getJavaDocPositions());
85 // boolean deprecated = false;
86 // int lastAnnotationIndex = -1;
87 // int commentPtr = scanner.commentPtr;
89 // //since jdk1.2 look only in the last java doc comment...
90 // nextComment : for (lastAnnotationIndex = scanner.commentPtr; lastAnnotationIndex >= 0; lastAnnotationIndex--){
91 // //look for @deprecated into the first javadoc comment preceeding the declaration
92 // int commentSourceStart = scanner.commentStarts[lastAnnotationIndex];
93 // // javadoc only (non javadoc comment have negative end positions.)
94 // if (modifiersSourceStart != -1 && modifiersSourceStart < commentSourceStart) {
95 // continue nextComment;
97 // if (scanner.commentStops[lastAnnotationIndex] < 0) {
98 // continue nextComment;
100 // int commentSourceEnd = scanner.commentStops[lastAnnotationIndex] - 1; //stop is one over
101 // char[] comment = scanner.source;
105 // commentSourceStart,
108 // break nextComment;
111 // checkAndSetModifiers(AccDeprecated);
113 // // modify the modifier source start to point at the first comment
114 // if (commentPtr >= 0) {
115 // declarationSourceStart = scanner.commentStarts[0];
122 //protected void consumeClassBodyDeclaration() {
123 // // ClassBodyDeclaration ::= Diet Block
124 // //push an Initializer
125 // //optimize the push/pop
127 // super.consumeClassBodyDeclaration();
128 // Initializer initializer = (Initializer) astStack[astPtr];
129 // requestor.acceptInitializer(
130 // initializer.declarationSourceStart,
131 // initializer.declarationSourceEnd,
132 // intArrayStack[intArrayPtr--],
134 // modifiersSourceStart,
135 // initializer.block.sourceStart,
136 // initializer.block.sourceEnd);
140 // * INTERNAL USE-ONLY
142 //protected void consumeClassDeclaration() {
143 // super.consumeClassDeclaration();
144 // // we know that we have a TypeDeclaration on the top of the astStack
145 // if (isLocalDeclaration()) {
146 // // we ignore the local variable declarations
149 // requestor.exitClass(endStatementPosition, // '}' is the end of the body
150 // ((TypeDeclaration) astStack[astPtr]).declarationSourceEnd);
154 // * INTERNAL USE-ONLY
156 //protected void consumeClassHeader() {
157 // //ClassHeader ::= $empty
158 // super.consumeClassHeader();
159 // if (isLocalDeclaration()) {
160 // // we ignore the local variable declarations
164 // TypeDeclaration typeDecl = (TypeDeclaration) astStack[astPtr];
165 // TypeReference[] superInterfaces = typeDecl.superInterfaces;
166 // char[][] interfaceNames = null;
167 // int[] interfaceNameStarts = null;
168 // int[] interfaceNameEnds = null;
169 // if (superInterfaces != null) {
170 // int superInterfacesLength = superInterfaces.length;
171 // interfaceNames = new char[superInterfacesLength][];
172 // interfaceNameStarts = new int[superInterfacesLength];
173 // interfaceNameEnds = new int[superInterfacesLength];
174 // for (int i = 0; i < superInterfacesLength; i++) {
175 // TypeReference superInterface = superInterfaces[i];
176 // interfaceNames[i] = CharOperation.concatWith(superInterface.getTypeName(), '.');
177 // interfaceNameStarts[i] = superInterface.sourceStart;
178 // interfaceNameEnds[i] = superInterface.sourceEnd;
181 // // flush the comments related to the class header
182 // scanner.commentPtr = -1;
183 // TypeReference superclass = typeDecl.superclass;
184 // if (superclass == null) {
185 // requestor.enterClass(
186 // typeDecl.declarationSourceStart,
187 // intArrayStack[intArrayPtr--],
188 // typeDecl.modifiers,
189 // typeDecl.modifiersSourceStart,
190 // typeStartPosition,
192 // typeDecl.sourceStart,
193 // typeDecl.sourceEnd,
198 // interfaceNameStarts,
199 // interfaceNameEnds,
200 // scanner.currentPosition - 1);
202 // requestor.enterClass(
203 // typeDecl.declarationSourceStart,
204 // intArrayStack[intArrayPtr--],
205 // typeDecl.modifiers,
206 // typeDecl.modifiersSourceStart,
207 // typeStartPosition,
209 // typeDecl.sourceStart,
210 // typeDecl.sourceEnd,
211 // CharOperation.concatWith(superclass.getTypeName(), '.'),
212 // superclass.sourceStart,
213 // superclass.sourceEnd,
215 // interfaceNameStarts,
216 // interfaceNameEnds,
217 // scanner.currentPosition - 1);
221 //protected void consumeClassHeaderName() {
222 // // ClassHeaderName ::= Modifiersopt 'class' 'Identifier'
223 // TypeDeclaration typeDecl;
224 // if (nestedMethod[nestedType] == 0) {
225 // if (nestedType != 0) {
226 // typeDecl = new MemberTypeDeclaration(this.compilationUnit.compilationResult);
228 // typeDecl = new TypeDeclaration(this.compilationUnit.compilationResult);
231 // // Record that the block has a declaration for local types
232 // typeDecl = new LocalTypeDeclaration(this.compilationUnit.compilationResult);
233 // markEnclosingMemberWithLocalType();
237 // //highlight the name of the type
238 // long pos = identifierPositionStack[identifierPtr];
239 // typeDecl.sourceEnd = (int) pos;
240 // typeDecl.sourceStart = (int) (pos >>> 32);
241 // typeDecl.name = identifierStack[identifierPtr--];
242 // identifierLengthPtr--;
244 // //compute the declaration source too
245 // // 'class' and 'interface' push an int position
246 // typeStartPosition = typeDecl.declarationSourceStart = intStack[intPtr--];
248 // int declarationSourceStart = intStack[intPtr--];
249 // typeDecl.modifiersSourceStart = intStack[intPtr--];
250 // typeDecl.modifiers = intStack[intPtr--];
251 // if (typeDecl.declarationSourceStart > declarationSourceStart) {
252 // typeDecl.declarationSourceStart = declarationSourceStart;
254 // typeDecl.bodyStart = typeDecl.sourceEnd + 1;
255 // pushOnAstStack(typeDecl);
259 // * INTERNAL USE-ONLY
261 //protected void consumeCompilationUnit() {
262 // // CompilationUnit ::= EnterCompilationUnit PackageDeclarationopt ImportDeclarationsopt
263 // requestor.exitCompilationUnit(scanner.source.length - 1);
269 //protected void consumeConstructorDeclaration() {
270 // // ConstructorDeclaration ::= ConstructorHeader ConstructorBody
271 // super.consumeConstructorDeclaration();
272 // if (isLocalDeclaration()) {
273 // // we ignore the local variable declarations
276 // ConstructorDeclaration cd = (ConstructorDeclaration) astStack[astPtr];
277 // requestor.exitConstructor(endStatementPosition, cd.declarationSourceEnd);
281 // * INTERNAL USE-ONLY
283 //protected void consumeConstructorHeader() {
284 // // ConstructorHeader ::= ConstructorHeaderName MethodHeaderParameters MethodHeaderThrowsClauseopt
285 // super.consumeConstructorHeader();
286 // if (isLocalDeclaration()) {
287 // // we ignore the local variable declarations
291 // ConstructorDeclaration cd = (ConstructorDeclaration) astStack[astPtr];
292 // Argument[] arguments = cd.arguments;
293 // char[][] argumentTypes = null;
294 // char[][] argumentNames = null;
295 // int[] argumentTypeStarts = null;
296 // int[] argumentTypeEnds = null;
297 // int[] argumentNameStarts = null;
298 // int[] argumentNameEnds = null;
299 // if (arguments != null) {
300 // int argumentLength = arguments.length;
301 // argumentTypes = new char[argumentLength][];
302 // argumentNames = new char[argumentLength][];
303 // argumentNameStarts = new int[argumentLength];
304 // argumentNameEnds = new int[argumentLength];
305 // argumentTypeStarts = new int[argumentLength];
306 // argumentTypeEnds = new int[argumentLength];
307 // for (int i = 0; i < argumentLength; i++) {
308 // Argument argument = arguments[i];
309 // TypeReference argumentType = argument.type;
310 // argumentTypes[i] = returnTypeName(argumentType);
311 // argumentNames[i] = argument.name;
312 // argumentNameStarts[i] = argument.sourceStart;
313 // argumentNameEnds[i] = argument.sourceEnd;
314 // argumentTypeStarts[i] = argumentType.sourceStart;
315 // argumentTypeEnds[i] = argumentType.sourceEnd;
318 // TypeReference[] thrownExceptions = cd.thrownExceptions;
319 // char[][] exceptionTypes = null;
320 // int[] exceptionTypeStarts = null;
321 // int[] exceptionTypeEnds = null;
322 // if (thrownExceptions != null) {
323 // int thrownExceptionLength = thrownExceptions.length;
324 // exceptionTypes = new char[thrownExceptionLength][];
325 // exceptionTypeStarts = new int[thrownExceptionLength];
326 // exceptionTypeEnds = new int[thrownExceptionLength];
327 // for (int i = 0; i < thrownExceptionLength; i++) {
328 // TypeReference exception = thrownExceptions[i];
329 // exceptionTypes[i] = CharOperation.concatWith(exception.getTypeName(), '.');
330 // exceptionTypeStarts[i] = exception.sourceStart;
331 // exceptionTypeEnds[i] = exception.sourceEnd;
335 // .enterConstructor(
336 // cd.declarationSourceStart,
337 // intArrayStack[intArrayPtr--],
339 // cd.modifiersSourceStart,
342 // (int) (selectorSourcePositions & 0xFFFFFFFFL),
343 // // retrieve the source end of the name
345 // argumentTypeStarts,
348 // argumentNameStarts,
351 // // right parenthesis
353 // exceptionTypeStarts,
354 // exceptionTypeEnds,
355 // scanner.currentPosition - 1);
357 //protected void consumeConstructorHeaderName() {
358 // // ConstructorHeaderName ::= Modifiersopt 'Identifier' '('
359 // ConstructorDeclaration cd = new ConstructorDeclaration(this.compilationUnit.compilationResult);
361 // //name -- this is not really revelant but we do .....
362 // cd.selector = identifierStack[identifierPtr];
363 // selectorSourcePositions = identifierPositionStack[identifierPtr--];
364 // identifierLengthPtr--;
367 // cd.declarationSourceStart = intStack[intPtr--];
368 // cd.modifiersSourceStart = intStack[intPtr--];
369 // cd.modifiers = intStack[intPtr--];
371 // //highlight starts at the selector starts
372 // cd.sourceStart = (int) (selectorSourcePositions >>> 32);
373 // pushOnAstStack(cd);
375 // cd.sourceEnd = lParenPos;
376 // cd.bodyStart = lParenPos + 1;
378 //protected void consumeDefaultModifiers() {
379 // checkAnnotation(); // might update modifiers with AccDeprecated
380 // pushOnIntStack(modifiers); // modifiers
381 // pushOnIntStack(-1);
383 // declarationSourceStart >= 0 ? declarationSourceStart : scanner.startPosition);
386 //protected void consumeDiet() {
387 // // Diet ::= $empty
388 // super.consumeDiet();
389 // /* persisting javadoc positions
390 // * Will be consume in consumeClassBodyDeclaration
392 // pushOnIntArrayStack(this.getJavaDocPositions());
396 // * INTERNAL USE-ONLY
398 //protected void consumeEnterCompilationUnit() {
399 // // EnterCompilationUnit ::= $empty
400 // requestor.enterCompilationUnit();
404 // * INTERNAL USE-ONLY
406 //protected void consumeEnterVariable() {
407 // // EnterVariable ::= $empty
408 // boolean isLocalDeclaration = isLocalDeclaration();
409 // if (!isLocalDeclaration && (variablesCounter[nestedType] != 0)) {
410 // requestor.exitField(lastFieldBodyEndPosition, lastFieldEndPosition);
412 // char[] name = identifierStack[identifierPtr];
413 // long namePosition = identifierPositionStack[identifierPtr--];
414 // int extendedTypeDimension = intStack[intPtr--];
416 // AbstractVariableDeclaration declaration;
417 // if (nestedMethod[nestedType] != 0) {
418 // // create the local variable declarations
420 // new LocalDeclaration(null, name, (int) (namePosition >>> 32), (int) namePosition);
422 // // create the field declaration
424 // new FieldDeclaration(null, name, (int) (namePosition >>> 32), (int) namePosition);
426 // identifierLengthPtr--;
427 // TypeReference type;
428 // int variableIndex = variablesCounter[nestedType];
430 // if (variableIndex == 0) {
431 // // first variable of the declaration (FieldDeclaration or LocalDeclaration)
432 // if (nestedMethod[nestedType] != 0) {
433 // // local declaration
434 // declaration.declarationSourceStart = intStack[intPtr--];
435 // declaration.modifiersSourceStart = intStack[intPtr--];
436 // declaration.modifiers = intStack[intPtr--];
437 // type = getTypeReference(typeDim = intStack[intPtr--]); // type dimension
438 // pushOnAstStack(type);
440 // // field declaration
441 // type = getTypeReference(typeDim = intStack[intPtr--]); // type dimension
442 // pushOnAstStack(type);
443 // declaration.declarationSourceStart = intStack[intPtr--];
444 // declaration.modifiersSourceStart = intStack[intPtr--];
445 // declaration.modifiers = intStack[intPtr--];
448 // type = (TypeReference) astStack[astPtr - variableIndex];
449 // typeDim = type.dimensions();
450 // AbstractVariableDeclaration previousVariable =
451 // (AbstractVariableDeclaration) astStack[astPtr];
452 // declaration.declarationSourceStart = previousVariable.declarationSourceStart;
453 // declaration.modifiers = previousVariable.modifiers;
454 // declaration.modifiersSourceStart = previousVariable.modifiersSourceStart;
457 // localIntPtr = intPtr;
459 // if (extendedTypeDimension == 0) {
460 // declaration.type = type;
462 // int dimension = typeDim + extendedTypeDimension;
463 // //on the identifierLengthStack there is the information about the type....
465 // if ((baseType = identifierLengthStack[identifierLengthPtr + 1]) < 0) {
466 // //it was a baseType
467 // declaration.type = TypeReference.baseTypeReference(-baseType, dimension);
468 // declaration.type.sourceStart = type.sourceStart;
469 // declaration.type.sourceEnd = type.sourceEnd;
471 // declaration.type = this.copyDims(type, dimension);
474 // variablesCounter[nestedType]++;
475 // nestedMethod[nestedType]++;
476 // pushOnAstStack(declaration);
478 // int[] javadocPositions = intArrayStack[intArrayPtr];
479 // if (!isLocalDeclaration) {
482 // declaration.declarationSourceStart,
484 // declaration.modifiers,
485 // declaration.modifiersSourceStart,
486 // returnTypeName(declaration.type),
491 // (int) (namePosition >>> 32),
492 // (int) namePosition,
493 // extendedTypeDimension,
494 // extendedTypeDimension == 0 ? -1 : endPosition);
499 // * INTERNAL USE-ONLY
501 //protected void consumeExitVariableWithInitialization() {
502 // // ExitVariableWithInitialization ::= $empty
503 // // the scanner is located after the comma or the semi-colon.
504 // // we want to include the comma or the semi-colon
505 // super.consumeExitVariableWithInitialization();
506 // nestedMethod[nestedType]--;
507 // lastFieldEndPosition = scanner.currentPosition - 1;
508 // lastFieldBodyEndPosition = ((AbstractVariableDeclaration) astStack[astPtr]).initialization.sourceEnd;
510 //protected void consumeExitVariableWithoutInitialization() {
511 // // ExitVariableWithoutInitialization ::= $empty
512 // // do nothing by default
513 // super.consumeExitVariableWithoutInitialization();
514 // nestedMethod[nestedType]--;
515 // lastFieldEndPosition = scanner.currentPosition - 1;
516 // lastFieldBodyEndPosition = scanner.startPosition - 1;
520 // * INTERNAL USE-ONLY
522 //protected void consumeFieldDeclaration() {
523 // // See consumeLocalVariableDeclarationDefaultModifier() in case of change: duplicated code
524 // // FieldDeclaration ::= Modifiersopt Type VariableDeclarators ';'
525 // // the super.consumeFieldDeclaration will reinitialize the variableCounter[nestedType]
526 // int variableIndex = variablesCounter[nestedType];
527 // super.consumeFieldDeclaration();
529 // if (isLocalDeclaration())
531 // if (variableIndex != 0) {
532 // requestor.exitField(lastFieldBodyEndPosition, lastFieldEndPosition);
535 //protected void consumeFormalParameter() {
536 // // FormalParameter ::= Type VariableDeclaratorId ==> false
537 // // FormalParameter ::= Modifiers Type VariableDeclaratorId ==> true
540 // identifierStack : type identifier
541 // intStack : dim dim
543 // astStack : Argument
548 // identifierLengthPtr--;
549 // char[] name = identifierStack[identifierPtr];
550 // long namePositions = identifierPositionStack[identifierPtr--];
551 // TypeReference type = getTypeReference(intStack[intPtr--] + intStack[intPtr--]);
558 // intStack[intPtr + 1]); // modifiers
559 // pushOnAstStack(arg);
564 // * INTERNAL USE-ONLY
566 //protected void consumeInterfaceDeclaration() {
567 // super.consumeInterfaceDeclaration();
568 // // we know that we have a TypeDeclaration on the top of the astStack
569 // if (isLocalDeclaration()) {
570 // // we ignore the local variable declarations
573 // requestor.exitInterface(endStatementPosition, // the '}' is the end of the body
574 // ((TypeDeclaration) astStack[astPtr]).declarationSourceEnd);
578 // * INTERNAL USE-ONLY
580 //protected void consumeInterfaceHeader() {
581 // //InterfaceHeader ::= $empty
582 // super.consumeInterfaceHeader();
583 // if (isLocalDeclaration()) {
584 // // we ignore the local variable declarations
588 // TypeDeclaration typeDecl = (TypeDeclaration) astStack[astPtr];
589 // TypeReference[] superInterfaces = typeDecl.superInterfaces;
590 // char[][] interfaceNames = null;
591 // int[] interfaceNameStarts = null;
592 // int[] interfacenameEnds = null;
593 // int superInterfacesLength = 0;
594 // if (superInterfaces != null) {
595 // superInterfacesLength = superInterfaces.length;
596 // interfaceNames = new char[superInterfacesLength][];
597 // interfaceNameStarts = new int[superInterfacesLength];
598 // interfacenameEnds = new int[superInterfacesLength];
600 // if (superInterfaces != null) {
601 // for (int i = 0; i < superInterfacesLength; i++) {
602 // TypeReference superInterface = superInterfaces[i];
603 // interfaceNames[i] = CharOperation.concatWith(superInterface.getTypeName(), '.');
604 // interfaceNameStarts[i] = superInterface.sourceStart;
605 // interfacenameEnds[i] = superInterface.sourceEnd;
608 // // flush the comments related to the interface header
609 // scanner.commentPtr = -1;
610 // requestor.enterInterface(
611 // typeDecl.declarationSourceStart,
612 // intArrayStack[intArrayPtr--],
613 // typeDecl.modifiers,
614 // typeDecl.modifiersSourceStart,
615 // typeStartPosition,
617 // typeDecl.sourceStart,
618 // typeDecl.sourceEnd,
620 // interfaceNameStarts,
621 // interfacenameEnds,
622 // scanner.currentPosition - 1);
624 //protected void consumeInterfaceHeaderName() {
625 // // InterfaceHeaderName ::= Modifiersopt 'interface' 'Identifier'
626 // TypeDeclaration typeDecl;
627 // if (nestedMethod[nestedType] == 0) {
628 // if (nestedType != 0) {
629 // typeDecl = new MemberTypeDeclaration(this.compilationUnit.compilationResult);
631 // typeDecl = new TypeDeclaration(this.compilationUnit.compilationResult);
634 // // Record that the block has a declaration for local types
635 // typeDecl = new LocalTypeDeclaration(this.compilationUnit.compilationResult);
636 // markEnclosingMemberWithLocalType();
640 // //highlight the name of the type
641 // long pos = identifierPositionStack[identifierPtr];
642 // typeDecl.sourceEnd = (int) pos;
643 // typeDecl.sourceStart = (int) (pos >>> 32);
644 // typeDecl.name = identifierStack[identifierPtr--];
645 // identifierLengthPtr--;
647 // //compute the declaration source too
648 // // 'class' and 'interface' push an int position
649 // typeStartPosition = typeDecl.declarationSourceStart = intStack[intPtr--];
651 // int declarationSourceStart = intStack[intPtr--];
652 // typeDecl.modifiersSourceStart = intStack[intPtr--];
653 // typeDecl.modifiers = intStack[intPtr--];
654 // if (typeDecl.declarationSourceStart > declarationSourceStart) {
655 // typeDecl.declarationSourceStart = declarationSourceStart;
657 // typeDecl.bodyStart = typeDecl.sourceEnd + 1;
658 // pushOnAstStack(typeDecl);
662 // * INTERNAL USE-ONLY
664 //protected void consumeLocalVariableDeclaration() {
665 // // See consumeLocalVariableDeclarationDefaultModifier() in case of change: duplicated code
666 // // FieldDeclaration ::= Modifiersopt Type VariableDeclarators ';'
668 // super.consumeLocalVariableDeclaration();
673 // * INTERNAL USE-ONLY
675 //protected void consumeMethodDeclaration(boolean isNotAbstract) {
676 // // MethodDeclaration ::= MethodHeader MethodBody
677 // // AbstractMethodDeclaration ::= MethodHeader ';'
678 // super.consumeMethodDeclaration(isNotAbstract);
679 // if (isLocalDeclaration()) {
680 // // we ignore the local variable declarations
683 // MethodDeclaration md = (MethodDeclaration) astStack[astPtr];
684 // requestor.exitMethod(endStatementPosition, md.declarationSourceEnd);
688 // * INTERNAL USE-ONLY
690 //protected void consumeMethodHeader() {
691 // // MethodHeader ::= MethodHeaderName MethodHeaderParameters MethodHeaderExtendedDims ThrowsClauseopt
692 // super.consumeMethodHeader();
693 // if (isLocalDeclaration()) {
694 // // we ignore the local variable declarations
698 // MethodDeclaration md = (MethodDeclaration) astStack[astPtr];
700 // TypeReference returnType = md.returnType;
701 // char[] returnTypeName = returnTypeName(returnType);
702 // Argument[] arguments = md.arguments;
703 // char[][] argumentTypes = null;
704 // char[][] argumentNames = null;
705 // int[] argumentTypeStarts = null;
706 // int[] argumentTypeEnds = null;
707 // int[] argumentNameStarts = null;
708 // int[] argumentNameEnds = null;
709 // if (arguments != null) {
710 // int argumentLength = arguments.length;
711 // argumentTypes = new char[argumentLength][];
712 // argumentNames = new char[argumentLength][];
713 // argumentNameStarts = new int[argumentLength];
714 // argumentNameEnds = new int[argumentLength];
715 // argumentTypeStarts = new int[argumentLength];
716 // argumentTypeEnds = new int[argumentLength];
717 // for (int i = 0; i < argumentLength; i++) {
718 // Argument argument = arguments[i];
719 // TypeReference argumentType = argument.type;
720 // argumentTypes[i] = returnTypeName(argumentType);
721 // argumentNames[i] = argument.name;
722 // argumentNameStarts[i] = argument.sourceStart;
723 // argumentNameEnds[i] = argument.sourceEnd;
724 // argumentTypeStarts[i] = argumentType.sourceStart;
725 // argumentTypeEnds[i] = argumentType.sourceEnd;
728 // TypeReference[] thrownExceptions = md.thrownExceptions;
729 // char[][] exceptionTypes = null;
730 // int[] exceptionTypeStarts = null;
731 // int[] exceptionTypeEnds = null;
732 // if (thrownExceptions != null) {
733 // int thrownExceptionLength = thrownExceptions.length;
734 // exceptionTypeStarts = new int[thrownExceptionLength];
735 // exceptionTypeEnds = new int[thrownExceptionLength];
736 // exceptionTypes = new char[thrownExceptionLength][];
737 // for (int i = 0; i < thrownExceptionLength; i++) {
738 // TypeReference exception = thrownExceptions[i];
739 // exceptionTypes[i] = CharOperation.concatWith(exception.getTypeName(), '.');
740 // exceptionTypeStarts[i] = exception.sourceStart;
741 // exceptionTypeEnds[i] = exception.sourceEnd;
746 // md.declarationSourceStart,
747 // intArrayStack[intArrayPtr--],
749 // md.modifiersSourceStart,
751 // returnType.sourceStart,
752 // returnType.sourceEnd,
756 // (int) (selectorSourcePositions & 0xFFFFFFFFL),
758 // argumentTypeStarts,
761 // argumentNameStarts,
765 // extendsDim == 0 ? -1 : endPosition,
767 // exceptionTypeStarts,
768 // exceptionTypeEnds,
769 // scanner.currentPosition - 1);
771 //protected void consumeMethodHeaderExtendedDims() {
772 // // MethodHeaderExtendedDims ::= Dimsopt
773 // // now we update the returnType of the method
774 // MethodDeclaration md = (MethodDeclaration) astStack[astPtr];
775 // int extendedDims = intStack[intPtr--];
776 // extendsDim = extendedDims;
777 // if (extendedDims != 0) {
778 // TypeReference returnType = md.returnType;
779 // md.sourceEnd = endPosition;
780 // int dims = returnType.dimensions() + extendedDims;
782 // if ((baseType = identifierLengthStack[identifierLengthPtr + 1]) < 0) {
783 // //it was a baseType
784 // int sourceStart = returnType.sourceStart;
785 // int sourceEnd = returnType.sourceEnd;
786 // returnType = TypeReference.baseTypeReference(-baseType, dims);
787 // returnType.sourceStart = sourceStart;
788 // returnType.sourceEnd = sourceEnd;
789 // md.returnType = returnType;
791 // md.returnType = this.copyDims(md.returnType, dims);
793 // if (currentToken == TokenNameLBRACE) {
794 // md.bodyStart = endPosition + 1;
798 //protected void consumeMethodHeaderName() {
799 // // MethodHeaderName ::= Modifiersopt Type 'Identifier' '('
800 // MethodDeclaration md = new MethodDeclaration(this.compilationUnit.compilationResult);
803 // md.selector = identifierStack[identifierPtr];
804 // selectorSourcePositions = identifierPositionStack[identifierPtr--];
805 // identifierLengthPtr--;
807 // md.returnType = getTypeReference(typeDims = intStack[intPtr--]);
809 // md.declarationSourceStart = intStack[intPtr--];
810 // md.modifiersSourceStart = intStack[intPtr--];
811 // md.modifiers = intStack[intPtr--];
813 // //highlight starts at selector start
814 // md.sourceStart = (int) (selectorSourcePositions >>> 32);
815 // pushOnAstStack(md);
816 // md.bodyStart = scanner.currentPosition-1;
818 //protected void consumeModifiers() {
819 // checkAnnotation(); // might update modifiers with AccDeprecated
820 // pushOnIntStack(modifiers); // modifiers
821 // pushOnIntStack(modifiersSourceStart);
823 // declarationSourceStart >= 0 ? declarationSourceStart : modifiersSourceStart);
830 //protected void consumePackageDeclarationName() {
831 // /* persisting javadoc positions */
832 // pushOnIntArrayStack(this.getJavaDocPositions());
834 // super.consumePackageDeclarationName();
835 // ImportReference importReference = compilationUnit.currentPackage;
837 // requestor.acceptPackage(
838 // importReference.declarationSourceStart,
839 // importReference.declarationSourceEnd,
840 // intArrayStack[intArrayPtr--],
841 // CharOperation.concatWith(importReference.getImportName(), '.'),
842 // importReference.sourceStart);
844 //protected void consumePushModifiers() {
845 // checkAnnotation(); // might update modifiers with AccDeprecated
846 // pushOnIntStack(modifiers); // modifiers
847 // if (modifiersSourceStart < 0) {
848 // pushOnIntStack(-1);
850 // declarationSourceStart >= 0 ? declarationSourceStart : scanner.startPosition);
852 // pushOnIntStack(modifiersSourceStart);
854 // declarationSourceStart >= 0 ? declarationSourceStart : modifiersSourceStart);
860 // * INTERNAL USE-ONLY
862 //protected void consumeSingleTypeImportDeclarationName() {
863 // // SingleTypeImportDeclarationName ::= 'import' Name
865 // /* persisting javadoc positions */
866 // pushOnIntArrayStack(this.getJavaDocPositions());
868 // super.consumeSingleTypeImportDeclarationName();
869 // ImportReference importReference = (ImportReference) astStack[astPtr];
870 // requestor.acceptImport(
871 // importReference.declarationSourceStart,
872 // importReference.declarationSourceEnd,
873 // intArrayStack[intArrayPtr--],
874 // CharOperation.concatWith(importReference.getImportName(), '.'),
875 // importReference.sourceStart,
880 // * INTERNAL USE-ONLY
882 //protected void consumeStaticInitializer() {
883 // // StaticInitializer ::= StaticOnly Block
884 // //push an Initializer
885 // //optimize the push/pop
886 // super.consumeStaticInitializer();
887 // Initializer initializer = (Initializer) astStack[astPtr];
888 // requestor.acceptInitializer(
889 // initializer.declarationSourceStart,
890 // initializer.declarationSourceEnd,
891 // intArrayStack[intArrayPtr--],
893 // intStack[intPtr--],
894 // initializer.block.sourceStart,
895 // initializer.declarationSourceEnd);
897 //protected void consumeStaticOnly() {
898 // // StaticOnly ::= 'static'
899 // checkAnnotation(); // might update declaration source start
900 // pushOnIntStack(modifiersSourceStart);
902 // declarationSourceStart >= 0 ? declarationSourceStart : modifiersSourceStart);
903 // jumpOverMethodBody();
904 // nestedMethod[nestedType]++;
909 // * INTERNAL USE-ONLY
911 //protected void consumeTypeImportOnDemandDeclarationName() {
912 // // TypeImportOnDemandDeclarationName ::= 'import' Name '.' '*'
914 // /* persisting javadoc positions */
915 // pushOnIntArrayStack(this.getJavaDocPositions());
917 // super.consumeTypeImportOnDemandDeclarationName();
918 // ImportReference importReference = (ImportReference) astStack[astPtr];
919 // requestor.acceptImport(
920 // importReference.declarationSourceStart,
921 // importReference.declarationSourceEnd,
922 // intArrayStack[intArrayPtr--],
923 // CharOperation.concatWith(importReference.getImportName(), '.'),
924 // importReference.sourceStart,
927 public CompilationUnitDeclaration endParse(int act) {
928 if (scanner.recordLineSeparator) {
929 requestor.acceptLineSeparatorPositions(scanner.getLineEnds());
931 return super.endParse(act);
935 * Flush annotations defined prior to a given positions.
937 * Note: annotations are stacked in syntactical order
939 * Either answer given <position>, or the end position of a comment line immediately following the <position> (same line)
941 * e.g. void foo(){ } // end of method foo
944 //public int flushAnnotationsDefinedPriorTo(int position) {
946 // return lastFieldEndPosition = super.flushAnnotationsDefinedPriorTo(position);
948 //protected TypeReference getTypeReference(int dim) { /* build a Reference on a variable that may be qualified or not
949 //This variable is a type reference and dim will be its dimensions*/
952 // TypeReference ref;
953 // if ((length = identifierLengthStack[identifierLengthPtr--]) == 1) {
954 // // single variable reference
957 // new SingleTypeReference(
958 // identifierStack[identifierPtr],
959 // identifierPositionStack[identifierPtr--]);
962 // new ArrayTypeReference(
963 // identifierStack[identifierPtr],
965 // identifierPositionStack[identifierPtr--]);
966 // ref.sourceEnd = endPosition;
969 // if (length < 0) { //flag for precompiled type reference on base types
970 // ref = TypeReference.baseTypeReference(-length, dim);
971 // ref.sourceStart = intStack[intPtr--];
973 // ref.sourceEnd = intStack[intPtr--];
976 // ref.sourceEnd = endPosition;
978 // } else { //Qualified variable reference
979 // char[][] tokens = new char[length][];
980 // identifierPtr -= length;
981 // long[] positions = new long[length];
982 // System.arraycopy(identifierStack, identifierPtr + 1, tokens, 0, length);
984 // identifierPositionStack,
985 // identifierPtr + 1,
990 // ref = new QualifiedTypeReference(tokens, positions);
992 // ref = new ArrayQualifiedTypeReference(tokens, dim, positions);
993 // ref.sourceEnd = endPosition;
999 public void initialize() {
1000 //positionning the parser for a new compilation unit
1001 //avoiding stack reallocation and all that....
1002 super.initialize(false);
1010 //private boolean isLocalDeclaration() {
1011 // int nestedDepth = nestedType;
1012 // while (nestedDepth >= 0) {
1013 // if (nestedMethod[nestedDepth] != 0) {
1021 * Investigate one entire unit.
1023 public void parseCompilationUnit(ICompilationUnit unit) {
1024 char[] regionSource = unit.getContents();
1027 goForCompilationUnit();
1028 referenceContext = compilationUnit = new CompilationUnitDeclaration(problemReporter(),
1029 new CompilationResult(unit, 0, 0, 10), //this.options.maxProblemsPerUnit),
1030 regionSource.length);
1031 scanner.resetTo(0, regionSource.length);
1032 scanner.setSource(regionSource);
1034 } catch (AbortCompilation ex) {
1039 * Investigate one constructor declaration.
1041 //public void parseConstructor(char[] regionSource) {
1044 // goForClassBodyDeclarations();
1045 // referenceContext =
1046 // compilationUnit =
1047 // compilationUnit =
1048 // new CompilationUnitDeclaration(
1049 // problemReporter(),
1050 // new CompilationResult(regionSource, 0, 0, 10), //this.options.maxProblemsPerUnit),
1051 // regionSource.length);
1052 // scanner.resetTo(0, regionSource.length);
1053 // scanner.setSource(regionSource);
1055 // } catch (AbortCompilation ex) {
1059 * Investigate one field declaration statement (might have multiple declarations in it).
1061 //public void parseField(char[] regionSource) {
1064 // goForFieldDeclaration();
1065 // referenceContext =
1066 // compilationUnit =
1067 // compilationUnit =
1068 // new CompilationUnitDeclaration(
1069 // problemReporter(),
1070 // new CompilationResult(regionSource, 0, 0, this.options.maxProblemsPerUnit),
1071 // regionSource.length);
1072 // scanner.resetTo(0, regionSource.length);
1073 // scanner.setSource(regionSource);
1075 // } catch (AbortCompilation ex) {
1080 // * Investigate one import statement declaration.
1082 //public void parseImport(char[] regionSource) {
1085 // goForImportDeclaration();
1086 // referenceContext =
1087 // compilationUnit =
1088 // compilationUnit =
1089 // new CompilationUnitDeclaration(
1090 // problemReporter(),
1091 // new CompilationResult(regionSource, 0, 0, this.options.maxProblemsPerUnit),
1092 // regionSource.length);
1093 // scanner.resetTo(0, regionSource.length);
1094 // scanner.setSource(regionSource);
1096 // } catch (AbortCompilation ex) {
1101 // * Investigate one initializer declaration.
1102 // * regionSource need to content exactly an initializer declaration.
1103 // * e.g: static { i = 4; }
1104 // * { name = "test"; }
1106 //public void parseInitializer(char[] regionSource) {
1109 // goForInitializer();
1110 // referenceContext =
1111 // compilationUnit =
1112 // compilationUnit =
1113 // new CompilationUnitDeclaration(
1114 // problemReporter(),
1115 // new CompilationResult(regionSource, 0, 0, this.options.maxProblemsPerUnit),
1116 // regionSource.length);
1117 // scanner.resetTo(0, regionSource.length);
1118 // scanner.setSource(regionSource);
1120 // } catch (AbortCompilation ex) {
1125 // * Investigate one method declaration.
1127 //public void parseMethod(char[] regionSource) {
1130 // goForGenericMethodDeclaration();
1131 // referenceContext =
1132 // compilationUnit =
1133 // compilationUnit =
1134 // new CompilationUnitDeclaration(
1135 // problemReporter(),
1136 // new CompilationResult(regionSource, 0, 0, this.options.maxProblemsPerUnit),
1137 // regionSource.length);
1138 // scanner.resetTo(0, regionSource.length);
1139 // scanner.setSource(regionSource);
1141 // } catch (AbortCompilation ex) {
1146 // * Investigate one package statement declaration.
1148 //public void parsePackage(char[] regionSource) {
1151 // goForPackageDeclaration();
1152 // referenceContext =
1153 // compilationUnit =
1154 // compilationUnit =
1155 // new CompilationUnitDeclaration(
1156 // problemReporter(),
1157 // new CompilationResult(regionSource, 0, 0, this.options.maxProblemsPerUnit),
1158 // regionSource.length);
1159 // scanner.resetTo(0, regionSource.length);
1160 // scanner.setSource(regionSource);
1162 // } catch (AbortCompilation ex) {
1167 // * Investigate one type declaration, its fields, methods and member types.
1169 //public void parseType(char[] regionSource) {
1172 // goForTypeDeclaration();
1173 // referenceContext =
1174 // compilationUnit =
1175 // compilationUnit =
1176 // new CompilationUnitDeclaration(
1177 // problemReporter(),
1178 // new CompilationResult(regionSource, 0, 0, this.options.maxProblemsPerUnit),
1179 // regionSource.length);
1180 // scanner.resetTo(0, regionSource.length);
1181 // scanner.setSource(regionSource);
1183 // } catch (AbortCompilation ex) {
1188 * Returns this parser's problem reporter initialized with its reference context. Also it is assumed that a problem is going to be
1189 * reported, so initializes the compilation result's line positions.
1191 public ProblemReporter problemReporter() {
1192 problemReporter.referenceContext = referenceContext;
1193 return problemReporter;
1196 protected void pushOnIntArrayStack(int[] positions) {
1199 intArrayStack[++intArrayPtr] = positions;
1200 } catch (IndexOutOfBoundsException e) {
1202 int oldStackLength = intArrayStack.length;
1203 int oldStack[][] = intArrayStack;
1204 intArrayStack = new int[oldStackLength + StackIncrement][];
1205 System.arraycopy(oldStack, 0, intArrayStack, 0, oldStackLength);
1206 intArrayStack[intArrayPtr] = positions;
1210 //protected void resetModifiers() {
1211 // super.resetModifiers();
1212 // declarationSourceStart = -1;
1215 * Syntax error was detected. Will attempt to perform some recovery action in order to resume to the regular parse loop.
1217 protected boolean resumeOnSyntaxError() {
1221 * Answer a char array representation of the type name formatted like: - type name + dimensions Example: "A[][]".toCharArray()
1222 * "java.lang.String".toCharArray()
1224 //private char[] returnTypeName(TypeReference type) {
1225 // int dimension = type.dimensions();
1226 // if (dimension != 0) {
1227 // char[] dimensionsArray = new char[dimension * 2];
1228 // for (int i = 0; i < dimension; i++) {
1229 // dimensionsArray[i*2] = '[';
1230 // dimensionsArray[(i*2) + 1] = ']';
1232 // return CharOperation.concat(
1233 // CharOperation.concatWith(type.getTypeName(), '.'),
1234 // dimensionsArray);
1236 // return CharOperation.concatWith(type.getTypeName(), '.');
1238 //public String toString() {
1239 // StringBuffer buffer = new StringBuffer();
1240 // buffer.append("intArrayPtr = " + intArrayPtr + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
1241 // buffer.append(super.toString());
1242 // return buffer.toString();
1245 // * INTERNAL USE ONLY
1247 //protected TypeReference typeReference(
1249 // int localIdentifierPtr,
1250 // int localIdentifierLengthPtr) {
1251 // /* build a Reference on a variable that may be qualified or not
1252 // * This variable is a type reference and dim will be its dimensions.
1253 // * We don't have any side effect on the stacks' pointers.
1257 // TypeReference ref;
1258 // if ((length = identifierLengthStack[localIdentifierLengthPtr]) == 1) {
1259 // // single variable reference
1262 // new SingleTypeReference(
1263 // identifierStack[localIdentifierPtr],
1264 // identifierPositionStack[localIdentifierPtr--]);
1267 // new ArrayTypeReference(
1268 // identifierStack[localIdentifierPtr],
1270 // identifierPositionStack[localIdentifierPtr--]);
1271 // ref.sourceEnd = endPosition;
1274 // if (length < 0) { //flag for precompiled type reference on base types
1275 // ref = TypeReference.baseTypeReference(-length, dim);
1276 // ref.sourceStart = intStack[localIntPtr--];
1278 // ref.sourceEnd = intStack[localIntPtr--];
1281 // ref.sourceEnd = endPosition;
1283 // } else { //Qualified variable reference
1284 // char[][] tokens = new char[length][];
1285 // localIdentifierPtr -= length;
1286 // long[] positions = new long[length];
1287 // System.arraycopy(identifierStack, localIdentifierPtr + 1, tokens, 0, length);
1288 // System.arraycopy(
1289 // identifierPositionStack,
1290 // localIdentifierPtr + 1,
1295 // ref = new QualifiedTypeReference(tokens, positions);
1297 // ref = new ArrayQualifiedTypeReference(tokens, dim, positions);