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.parser.UnitParser;
16 import net.sourceforge.phpdt.internal.compiler.problem.AbortCompilation;
17 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
18 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
22 * A document element parser extracts structural information
23 * from a piece of source, providing detailed source positions info.
25 * also see @IDocumentElementRequestor
27 * The structural investigation includes:
28 * - the package statement
30 * - top-level types: package member, member types (member types of member types...)
34 * Any (parsing) problem encountered is also provided.
36 public class DocumentElementParser extends UnitParser {
37 IDocumentElementRequestor requestor;
38 private int localIntPtr;
39 private int lastFieldEndPosition;
40 private int lastFieldBodyEndPosition;
41 private int typeStartPosition;
42 private long selectorSourcePositions;
44 private int extendsDim;
45 private int declarationSourceStart;
47 /* int[] stack for storing javadoc positions */
48 int[][] intArrayStack;
51 // CompilerOptions options;
53 public DocumentElementParser(
54 final IDocumentElementRequestor requestor,
55 IProblemFactory problemFactory) {
56 // CompilerOptions options) {
57 super(new ProblemReporter(
58 DefaultErrorHandlingPolicies.exitAfterAllProblems(),
61 public void record(IProblem problem, CompilationResult unitResult) {
62 requestor.acceptProblem(problem);
66 // options.sourceLevel >= CompilerOptions.JDK1_4);
67 this.requestor = requestor;
68 intArrayStack = new int[30][];
69 // this.options = options;
76 //protected void adjustInterfaceModifiers() {
77 // intStack[intPtr - 2] |= AccInterface;
80 * Will clear the comment stack when looking
81 * for a potential JavaDoc which might contain @deprecated.
83 * Additionally, before investigating for @deprecated, retrieve the positions
84 * of the JavaDoc comments so as to notify requestor with them.
86 //public void checkAnnotation() {
88 // /* persisting javadoc positions */
89 // pushOnIntArrayStack(this.getJavaDocPositions());
90 // boolean deprecated = false;
91 // int lastAnnotationIndex = -1;
92 // int commentPtr = scanner.commentPtr;
94 // //since jdk1.2 look only in the last java doc comment...
95 // nextComment : for (lastAnnotationIndex = scanner.commentPtr; lastAnnotationIndex >= 0; lastAnnotationIndex--){
96 // //look for @deprecated into the first javadoc comment preceeding the declaration
97 // int commentSourceStart = scanner.commentStarts[lastAnnotationIndex];
98 // // javadoc only (non javadoc comment have negative end positions.)
99 // if (modifiersSourceStart != -1 && modifiersSourceStart < commentSourceStart) {
100 // continue nextComment;
102 // if (scanner.commentStops[lastAnnotationIndex] < 0) {
103 // continue nextComment;
105 // int commentSourceEnd = scanner.commentStops[lastAnnotationIndex] - 1; //stop is one over
106 // char[] comment = scanner.source;
110 // commentSourceStart,
113 // break nextComment;
116 // checkAndSetModifiers(AccDeprecated);
118 // // modify the modifier source start to point at the first comment
119 // if (commentPtr >= 0) {
120 // declarationSourceStart = scanner.commentStarts[0];
127 //protected void consumeClassBodyDeclaration() {
128 // // ClassBodyDeclaration ::= Diet Block
129 // //push an Initializer
130 // //optimize the push/pop
132 // super.consumeClassBodyDeclaration();
133 // Initializer initializer = (Initializer) astStack[astPtr];
134 // requestor.acceptInitializer(
135 // initializer.declarationSourceStart,
136 // initializer.declarationSourceEnd,
137 // intArrayStack[intArrayPtr--],
139 // modifiersSourceStart,
140 // initializer.block.sourceStart,
141 // initializer.block.sourceEnd);
145 // * INTERNAL USE-ONLY
147 //protected void consumeClassDeclaration() {
148 // super.consumeClassDeclaration();
149 // // we know that we have a TypeDeclaration on the top of the astStack
150 // if (isLocalDeclaration()) {
151 // // we ignore the local variable declarations
154 // requestor.exitClass(endStatementPosition, // '}' is the end of the body
155 // ((TypeDeclaration) astStack[astPtr]).declarationSourceEnd);
159 // * INTERNAL USE-ONLY
161 //protected void consumeClassHeader() {
162 // //ClassHeader ::= $empty
163 // super.consumeClassHeader();
164 // if (isLocalDeclaration()) {
165 // // we ignore the local variable declarations
169 // TypeDeclaration typeDecl = (TypeDeclaration) astStack[astPtr];
170 // TypeReference[] superInterfaces = typeDecl.superInterfaces;
171 // char[][] interfaceNames = null;
172 // int[] interfaceNameStarts = null;
173 // int[] interfaceNameEnds = null;
174 // if (superInterfaces != null) {
175 // int superInterfacesLength = superInterfaces.length;
176 // interfaceNames = new char[superInterfacesLength][];
177 // interfaceNameStarts = new int[superInterfacesLength];
178 // interfaceNameEnds = new int[superInterfacesLength];
179 // for (int i = 0; i < superInterfacesLength; i++) {
180 // TypeReference superInterface = superInterfaces[i];
181 // interfaceNames[i] = CharOperation.concatWith(superInterface.getTypeName(), '.');
182 // interfaceNameStarts[i] = superInterface.sourceStart;
183 // interfaceNameEnds[i] = superInterface.sourceEnd;
186 // // flush the comments related to the class header
187 // scanner.commentPtr = -1;
188 // TypeReference superclass = typeDecl.superclass;
189 // if (superclass == null) {
190 // requestor.enterClass(
191 // typeDecl.declarationSourceStart,
192 // intArrayStack[intArrayPtr--],
193 // typeDecl.modifiers,
194 // typeDecl.modifiersSourceStart,
195 // typeStartPosition,
197 // typeDecl.sourceStart,
198 // typeDecl.sourceEnd,
203 // interfaceNameStarts,
204 // interfaceNameEnds,
205 // scanner.currentPosition - 1);
207 // requestor.enterClass(
208 // typeDecl.declarationSourceStart,
209 // intArrayStack[intArrayPtr--],
210 // typeDecl.modifiers,
211 // typeDecl.modifiersSourceStart,
212 // typeStartPosition,
214 // typeDecl.sourceStart,
215 // typeDecl.sourceEnd,
216 // CharOperation.concatWith(superclass.getTypeName(), '.'),
217 // superclass.sourceStart,
218 // superclass.sourceEnd,
220 // interfaceNameStarts,
221 // interfaceNameEnds,
222 // scanner.currentPosition - 1);
226 //protected void consumeClassHeaderName() {
227 // // ClassHeaderName ::= Modifiersopt 'class' 'Identifier'
228 // TypeDeclaration typeDecl;
229 // if (nestedMethod[nestedType] == 0) {
230 // if (nestedType != 0) {
231 // typeDecl = new MemberTypeDeclaration(this.compilationUnit.compilationResult);
233 // typeDecl = new TypeDeclaration(this.compilationUnit.compilationResult);
236 // // Record that the block has a declaration for local types
237 // typeDecl = new LocalTypeDeclaration(this.compilationUnit.compilationResult);
238 // markEnclosingMemberWithLocalType();
242 // //highlight the name of the type
243 // long pos = identifierPositionStack[identifierPtr];
244 // typeDecl.sourceEnd = (int) pos;
245 // typeDecl.sourceStart = (int) (pos >>> 32);
246 // typeDecl.name = identifierStack[identifierPtr--];
247 // identifierLengthPtr--;
249 // //compute the declaration source too
250 // // 'class' and 'interface' push an int position
251 // typeStartPosition = typeDecl.declarationSourceStart = intStack[intPtr--];
253 // int declarationSourceStart = intStack[intPtr--];
254 // typeDecl.modifiersSourceStart = intStack[intPtr--];
255 // typeDecl.modifiers = intStack[intPtr--];
256 // if (typeDecl.declarationSourceStart > declarationSourceStart) {
257 // typeDecl.declarationSourceStart = declarationSourceStart;
259 // typeDecl.bodyStart = typeDecl.sourceEnd + 1;
260 // pushOnAstStack(typeDecl);
264 // * INTERNAL USE-ONLY
266 //protected void consumeCompilationUnit() {
267 // // CompilationUnit ::= EnterCompilationUnit PackageDeclarationopt ImportDeclarationsopt
268 // requestor.exitCompilationUnit(scanner.source.length - 1);
274 //protected void consumeConstructorDeclaration() {
275 // // ConstructorDeclaration ::= ConstructorHeader ConstructorBody
276 // super.consumeConstructorDeclaration();
277 // if (isLocalDeclaration()) {
278 // // we ignore the local variable declarations
281 // ConstructorDeclaration cd = (ConstructorDeclaration) astStack[astPtr];
282 // requestor.exitConstructor(endStatementPosition, cd.declarationSourceEnd);
286 // * INTERNAL USE-ONLY
288 //protected void consumeConstructorHeader() {
289 // // ConstructorHeader ::= ConstructorHeaderName MethodHeaderParameters MethodHeaderThrowsClauseopt
290 // super.consumeConstructorHeader();
291 // if (isLocalDeclaration()) {
292 // // we ignore the local variable declarations
296 // ConstructorDeclaration cd = (ConstructorDeclaration) astStack[astPtr];
297 // Argument[] arguments = cd.arguments;
298 // char[][] argumentTypes = null;
299 // char[][] argumentNames = null;
300 // int[] argumentTypeStarts = null;
301 // int[] argumentTypeEnds = null;
302 // int[] argumentNameStarts = null;
303 // int[] argumentNameEnds = null;
304 // if (arguments != null) {
305 // int argumentLength = arguments.length;
306 // argumentTypes = new char[argumentLength][];
307 // argumentNames = new char[argumentLength][];
308 // argumentNameStarts = new int[argumentLength];
309 // argumentNameEnds = new int[argumentLength];
310 // argumentTypeStarts = new int[argumentLength];
311 // argumentTypeEnds = new int[argumentLength];
312 // for (int i = 0; i < argumentLength; i++) {
313 // Argument argument = arguments[i];
314 // TypeReference argumentType = argument.type;
315 // argumentTypes[i] = returnTypeName(argumentType);
316 // argumentNames[i] = argument.name;
317 // argumentNameStarts[i] = argument.sourceStart;
318 // argumentNameEnds[i] = argument.sourceEnd;
319 // argumentTypeStarts[i] = argumentType.sourceStart;
320 // argumentTypeEnds[i] = argumentType.sourceEnd;
323 // TypeReference[] thrownExceptions = cd.thrownExceptions;
324 // char[][] exceptionTypes = null;
325 // int[] exceptionTypeStarts = null;
326 // int[] exceptionTypeEnds = null;
327 // if (thrownExceptions != null) {
328 // int thrownExceptionLength = thrownExceptions.length;
329 // exceptionTypes = new char[thrownExceptionLength][];
330 // exceptionTypeStarts = new int[thrownExceptionLength];
331 // exceptionTypeEnds = new int[thrownExceptionLength];
332 // for (int i = 0; i < thrownExceptionLength; i++) {
333 // TypeReference exception = thrownExceptions[i];
334 // exceptionTypes[i] = CharOperation.concatWith(exception.getTypeName(), '.');
335 // exceptionTypeStarts[i] = exception.sourceStart;
336 // exceptionTypeEnds[i] = exception.sourceEnd;
340 // .enterConstructor(
341 // cd.declarationSourceStart,
342 // intArrayStack[intArrayPtr--],
344 // cd.modifiersSourceStart,
347 // (int) (selectorSourcePositions & 0xFFFFFFFFL),
348 // // retrieve the source end of the name
350 // argumentTypeStarts,
353 // argumentNameStarts,
356 // // right parenthesis
358 // exceptionTypeStarts,
359 // exceptionTypeEnds,
360 // scanner.currentPosition - 1);
362 //protected void consumeConstructorHeaderName() {
363 // // ConstructorHeaderName ::= Modifiersopt 'Identifier' '('
364 // ConstructorDeclaration cd = new ConstructorDeclaration(this.compilationUnit.compilationResult);
366 // //name -- this is not really revelant but we do .....
367 // cd.selector = identifierStack[identifierPtr];
368 // selectorSourcePositions = identifierPositionStack[identifierPtr--];
369 // identifierLengthPtr--;
372 // cd.declarationSourceStart = intStack[intPtr--];
373 // cd.modifiersSourceStart = intStack[intPtr--];
374 // cd.modifiers = intStack[intPtr--];
376 // //highlight starts at the selector starts
377 // cd.sourceStart = (int) (selectorSourcePositions >>> 32);
378 // pushOnAstStack(cd);
380 // cd.sourceEnd = lParenPos;
381 // cd.bodyStart = lParenPos + 1;
383 //protected void consumeDefaultModifiers() {
384 // checkAnnotation(); // might update modifiers with AccDeprecated
385 // pushOnIntStack(modifiers); // modifiers
386 // pushOnIntStack(-1);
388 // declarationSourceStart >= 0 ? declarationSourceStart : scanner.startPosition);
391 //protected void consumeDiet() {
392 // // Diet ::= $empty
393 // super.consumeDiet();
394 // /* persisting javadoc positions
395 // * Will be consume in consumeClassBodyDeclaration
397 // pushOnIntArrayStack(this.getJavaDocPositions());
401 // * INTERNAL USE-ONLY
403 //protected void consumeEnterCompilationUnit() {
404 // // EnterCompilationUnit ::= $empty
405 // requestor.enterCompilationUnit();
409 // * INTERNAL USE-ONLY
411 //protected void consumeEnterVariable() {
412 // // EnterVariable ::= $empty
413 // boolean isLocalDeclaration = isLocalDeclaration();
414 // if (!isLocalDeclaration && (variablesCounter[nestedType] != 0)) {
415 // requestor.exitField(lastFieldBodyEndPosition, lastFieldEndPosition);
417 // char[] name = identifierStack[identifierPtr];
418 // long namePosition = identifierPositionStack[identifierPtr--];
419 // int extendedTypeDimension = intStack[intPtr--];
421 // AbstractVariableDeclaration declaration;
422 // if (nestedMethod[nestedType] != 0) {
423 // // create the local variable declarations
425 // new LocalDeclaration(null, name, (int) (namePosition >>> 32), (int) namePosition);
427 // // create the field declaration
429 // new FieldDeclaration(null, name, (int) (namePosition >>> 32), (int) namePosition);
431 // identifierLengthPtr--;
432 // TypeReference type;
433 // int variableIndex = variablesCounter[nestedType];
435 // if (variableIndex == 0) {
436 // // first variable of the declaration (FieldDeclaration or LocalDeclaration)
437 // if (nestedMethod[nestedType] != 0) {
438 // // local declaration
439 // declaration.declarationSourceStart = intStack[intPtr--];
440 // declaration.modifiersSourceStart = intStack[intPtr--];
441 // declaration.modifiers = intStack[intPtr--];
442 // type = getTypeReference(typeDim = intStack[intPtr--]); // type dimension
443 // pushOnAstStack(type);
445 // // field declaration
446 // type = getTypeReference(typeDim = intStack[intPtr--]); // type dimension
447 // pushOnAstStack(type);
448 // declaration.declarationSourceStart = intStack[intPtr--];
449 // declaration.modifiersSourceStart = intStack[intPtr--];
450 // declaration.modifiers = intStack[intPtr--];
453 // type = (TypeReference) astStack[astPtr - variableIndex];
454 // typeDim = type.dimensions();
455 // AbstractVariableDeclaration previousVariable =
456 // (AbstractVariableDeclaration) astStack[astPtr];
457 // declaration.declarationSourceStart = previousVariable.declarationSourceStart;
458 // declaration.modifiers = previousVariable.modifiers;
459 // declaration.modifiersSourceStart = previousVariable.modifiersSourceStart;
462 // localIntPtr = intPtr;
464 // if (extendedTypeDimension == 0) {
465 // declaration.type = type;
467 // int dimension = typeDim + extendedTypeDimension;
468 // //on the identifierLengthStack there is the information about the type....
470 // if ((baseType = identifierLengthStack[identifierLengthPtr + 1]) < 0) {
471 // //it was a baseType
472 // declaration.type = TypeReference.baseTypeReference(-baseType, dimension);
473 // declaration.type.sourceStart = type.sourceStart;
474 // declaration.type.sourceEnd = type.sourceEnd;
476 // declaration.type = this.copyDims(type, dimension);
479 // variablesCounter[nestedType]++;
480 // nestedMethod[nestedType]++;
481 // pushOnAstStack(declaration);
483 // int[] javadocPositions = intArrayStack[intArrayPtr];
484 // if (!isLocalDeclaration) {
487 // declaration.declarationSourceStart,
489 // declaration.modifiers,
490 // declaration.modifiersSourceStart,
491 // returnTypeName(declaration.type),
496 // (int) (namePosition >>> 32),
497 // (int) namePosition,
498 // extendedTypeDimension,
499 // extendedTypeDimension == 0 ? -1 : endPosition);
504 // * INTERNAL USE-ONLY
506 //protected void consumeExitVariableWithInitialization() {
507 // // ExitVariableWithInitialization ::= $empty
508 // // the scanner is located after the comma or the semi-colon.
509 // // we want to include the comma or the semi-colon
510 // super.consumeExitVariableWithInitialization();
511 // nestedMethod[nestedType]--;
512 // lastFieldEndPosition = scanner.currentPosition - 1;
513 // lastFieldBodyEndPosition = ((AbstractVariableDeclaration) astStack[astPtr]).initialization.sourceEnd;
515 //protected void consumeExitVariableWithoutInitialization() {
516 // // ExitVariableWithoutInitialization ::= $empty
517 // // do nothing by default
518 // super.consumeExitVariableWithoutInitialization();
519 // nestedMethod[nestedType]--;
520 // lastFieldEndPosition = scanner.currentPosition - 1;
521 // lastFieldBodyEndPosition = scanner.startPosition - 1;
525 // * INTERNAL USE-ONLY
527 //protected void consumeFieldDeclaration() {
528 // // See consumeLocalVariableDeclarationDefaultModifier() in case of change: duplicated code
529 // // FieldDeclaration ::= Modifiersopt Type VariableDeclarators ';'
530 // // the super.consumeFieldDeclaration will reinitialize the variableCounter[nestedType]
531 // int variableIndex = variablesCounter[nestedType];
532 // super.consumeFieldDeclaration();
534 // if (isLocalDeclaration())
536 // if (variableIndex != 0) {
537 // requestor.exitField(lastFieldBodyEndPosition, lastFieldEndPosition);
540 //protected void consumeFormalParameter() {
541 // // FormalParameter ::= Type VariableDeclaratorId ==> false
542 // // FormalParameter ::= Modifiers Type VariableDeclaratorId ==> true
545 // identifierStack : type identifier
546 // intStack : dim dim
548 // astStack : Argument
553 // identifierLengthPtr--;
554 // char[] name = identifierStack[identifierPtr];
555 // long namePositions = identifierPositionStack[identifierPtr--];
556 // TypeReference type = getTypeReference(intStack[intPtr--] + intStack[intPtr--]);
563 // intStack[intPtr + 1]); // modifiers
564 // pushOnAstStack(arg);
569 // * INTERNAL USE-ONLY
571 //protected void consumeInterfaceDeclaration() {
572 // super.consumeInterfaceDeclaration();
573 // // we know that we have a TypeDeclaration on the top of the astStack
574 // if (isLocalDeclaration()) {
575 // // we ignore the local variable declarations
578 // requestor.exitInterface(endStatementPosition, // the '}' is the end of the body
579 // ((TypeDeclaration) astStack[astPtr]).declarationSourceEnd);
583 // * INTERNAL USE-ONLY
585 //protected void consumeInterfaceHeader() {
586 // //InterfaceHeader ::= $empty
587 // super.consumeInterfaceHeader();
588 // if (isLocalDeclaration()) {
589 // // we ignore the local variable declarations
593 // TypeDeclaration typeDecl = (TypeDeclaration) astStack[astPtr];
594 // TypeReference[] superInterfaces = typeDecl.superInterfaces;
595 // char[][] interfaceNames = null;
596 // int[] interfaceNameStarts = null;
597 // int[] interfacenameEnds = null;
598 // int superInterfacesLength = 0;
599 // if (superInterfaces != null) {
600 // superInterfacesLength = superInterfaces.length;
601 // interfaceNames = new char[superInterfacesLength][];
602 // interfaceNameStarts = new int[superInterfacesLength];
603 // interfacenameEnds = new int[superInterfacesLength];
605 // if (superInterfaces != null) {
606 // for (int i = 0; i < superInterfacesLength; i++) {
607 // TypeReference superInterface = superInterfaces[i];
608 // interfaceNames[i] = CharOperation.concatWith(superInterface.getTypeName(), '.');
609 // interfaceNameStarts[i] = superInterface.sourceStart;
610 // interfacenameEnds[i] = superInterface.sourceEnd;
613 // // flush the comments related to the interface header
614 // scanner.commentPtr = -1;
615 // requestor.enterInterface(
616 // typeDecl.declarationSourceStart,
617 // intArrayStack[intArrayPtr--],
618 // typeDecl.modifiers,
619 // typeDecl.modifiersSourceStart,
620 // typeStartPosition,
622 // typeDecl.sourceStart,
623 // typeDecl.sourceEnd,
625 // interfaceNameStarts,
626 // interfacenameEnds,
627 // scanner.currentPosition - 1);
629 //protected void consumeInterfaceHeaderName() {
630 // // InterfaceHeaderName ::= Modifiersopt 'interface' 'Identifier'
631 // TypeDeclaration typeDecl;
632 // if (nestedMethod[nestedType] == 0) {
633 // if (nestedType != 0) {
634 // typeDecl = new MemberTypeDeclaration(this.compilationUnit.compilationResult);
636 // typeDecl = new TypeDeclaration(this.compilationUnit.compilationResult);
639 // // Record that the block has a declaration for local types
640 // typeDecl = new LocalTypeDeclaration(this.compilationUnit.compilationResult);
641 // markEnclosingMemberWithLocalType();
645 // //highlight the name of the type
646 // long pos = identifierPositionStack[identifierPtr];
647 // typeDecl.sourceEnd = (int) pos;
648 // typeDecl.sourceStart = (int) (pos >>> 32);
649 // typeDecl.name = identifierStack[identifierPtr--];
650 // identifierLengthPtr--;
652 // //compute the declaration source too
653 // // 'class' and 'interface' push an int position
654 // typeStartPosition = typeDecl.declarationSourceStart = intStack[intPtr--];
656 // int declarationSourceStart = intStack[intPtr--];
657 // typeDecl.modifiersSourceStart = intStack[intPtr--];
658 // typeDecl.modifiers = intStack[intPtr--];
659 // if (typeDecl.declarationSourceStart > declarationSourceStart) {
660 // typeDecl.declarationSourceStart = declarationSourceStart;
662 // typeDecl.bodyStart = typeDecl.sourceEnd + 1;
663 // pushOnAstStack(typeDecl);
667 // * INTERNAL USE-ONLY
669 //protected void consumeLocalVariableDeclaration() {
670 // // See consumeLocalVariableDeclarationDefaultModifier() in case of change: duplicated code
671 // // FieldDeclaration ::= Modifiersopt Type VariableDeclarators ';'
673 // super.consumeLocalVariableDeclaration();
678 // * INTERNAL USE-ONLY
680 //protected void consumeMethodDeclaration(boolean isNotAbstract) {
681 // // MethodDeclaration ::= MethodHeader MethodBody
682 // // AbstractMethodDeclaration ::= MethodHeader ';'
683 // super.consumeMethodDeclaration(isNotAbstract);
684 // if (isLocalDeclaration()) {
685 // // we ignore the local variable declarations
688 // MethodDeclaration md = (MethodDeclaration) astStack[astPtr];
689 // requestor.exitMethod(endStatementPosition, md.declarationSourceEnd);
693 // * INTERNAL USE-ONLY
695 //protected void consumeMethodHeader() {
696 // // MethodHeader ::= MethodHeaderName MethodHeaderParameters MethodHeaderExtendedDims ThrowsClauseopt
697 // super.consumeMethodHeader();
698 // if (isLocalDeclaration()) {
699 // // we ignore the local variable declarations
703 // MethodDeclaration md = (MethodDeclaration) astStack[astPtr];
705 // TypeReference returnType = md.returnType;
706 // char[] returnTypeName = returnTypeName(returnType);
707 // Argument[] arguments = md.arguments;
708 // char[][] argumentTypes = null;
709 // char[][] argumentNames = null;
710 // int[] argumentTypeStarts = null;
711 // int[] argumentTypeEnds = null;
712 // int[] argumentNameStarts = null;
713 // int[] argumentNameEnds = null;
714 // if (arguments != null) {
715 // int argumentLength = arguments.length;
716 // argumentTypes = new char[argumentLength][];
717 // argumentNames = new char[argumentLength][];
718 // argumentNameStarts = new int[argumentLength];
719 // argumentNameEnds = new int[argumentLength];
720 // argumentTypeStarts = new int[argumentLength];
721 // argumentTypeEnds = new int[argumentLength];
722 // for (int i = 0; i < argumentLength; i++) {
723 // Argument argument = arguments[i];
724 // TypeReference argumentType = argument.type;
725 // argumentTypes[i] = returnTypeName(argumentType);
726 // argumentNames[i] = argument.name;
727 // argumentNameStarts[i] = argument.sourceStart;
728 // argumentNameEnds[i] = argument.sourceEnd;
729 // argumentTypeStarts[i] = argumentType.sourceStart;
730 // argumentTypeEnds[i] = argumentType.sourceEnd;
733 // TypeReference[] thrownExceptions = md.thrownExceptions;
734 // char[][] exceptionTypes = null;
735 // int[] exceptionTypeStarts = null;
736 // int[] exceptionTypeEnds = null;
737 // if (thrownExceptions != null) {
738 // int thrownExceptionLength = thrownExceptions.length;
739 // exceptionTypeStarts = new int[thrownExceptionLength];
740 // exceptionTypeEnds = new int[thrownExceptionLength];
741 // exceptionTypes = new char[thrownExceptionLength][];
742 // for (int i = 0; i < thrownExceptionLength; i++) {
743 // TypeReference exception = thrownExceptions[i];
744 // exceptionTypes[i] = CharOperation.concatWith(exception.getTypeName(), '.');
745 // exceptionTypeStarts[i] = exception.sourceStart;
746 // exceptionTypeEnds[i] = exception.sourceEnd;
751 // md.declarationSourceStart,
752 // intArrayStack[intArrayPtr--],
754 // md.modifiersSourceStart,
756 // returnType.sourceStart,
757 // returnType.sourceEnd,
761 // (int) (selectorSourcePositions & 0xFFFFFFFFL),
763 // argumentTypeStarts,
766 // argumentNameStarts,
770 // extendsDim == 0 ? -1 : endPosition,
772 // exceptionTypeStarts,
773 // exceptionTypeEnds,
774 // scanner.currentPosition - 1);
776 //protected void consumeMethodHeaderExtendedDims() {
777 // // MethodHeaderExtendedDims ::= Dimsopt
778 // // now we update the returnType of the method
779 // MethodDeclaration md = (MethodDeclaration) astStack[astPtr];
780 // int extendedDims = intStack[intPtr--];
781 // extendsDim = extendedDims;
782 // if (extendedDims != 0) {
783 // TypeReference returnType = md.returnType;
784 // md.sourceEnd = endPosition;
785 // int dims = returnType.dimensions() + extendedDims;
787 // if ((baseType = identifierLengthStack[identifierLengthPtr + 1]) < 0) {
788 // //it was a baseType
789 // int sourceStart = returnType.sourceStart;
790 // int sourceEnd = returnType.sourceEnd;
791 // returnType = TypeReference.baseTypeReference(-baseType, dims);
792 // returnType.sourceStart = sourceStart;
793 // returnType.sourceEnd = sourceEnd;
794 // md.returnType = returnType;
796 // md.returnType = this.copyDims(md.returnType, dims);
798 // if (currentToken == TokenNameLBRACE) {
799 // md.bodyStart = endPosition + 1;
803 //protected void consumeMethodHeaderName() {
804 // // MethodHeaderName ::= Modifiersopt Type 'Identifier' '('
805 // MethodDeclaration md = new MethodDeclaration(this.compilationUnit.compilationResult);
808 // md.selector = identifierStack[identifierPtr];
809 // selectorSourcePositions = identifierPositionStack[identifierPtr--];
810 // identifierLengthPtr--;
812 // md.returnType = getTypeReference(typeDims = intStack[intPtr--]);
814 // md.declarationSourceStart = intStack[intPtr--];
815 // md.modifiersSourceStart = intStack[intPtr--];
816 // md.modifiers = intStack[intPtr--];
818 // //highlight starts at selector start
819 // md.sourceStart = (int) (selectorSourcePositions >>> 32);
820 // pushOnAstStack(md);
821 // md.bodyStart = scanner.currentPosition-1;
823 //protected void consumeModifiers() {
824 // checkAnnotation(); // might update modifiers with AccDeprecated
825 // pushOnIntStack(modifiers); // modifiers
826 // pushOnIntStack(modifiersSourceStart);
828 // declarationSourceStart >= 0 ? declarationSourceStart : modifiersSourceStart);
835 //protected void consumePackageDeclarationName() {
836 // /* persisting javadoc positions */
837 // pushOnIntArrayStack(this.getJavaDocPositions());
839 // super.consumePackageDeclarationName();
840 // ImportReference importReference = compilationUnit.currentPackage;
842 // requestor.acceptPackage(
843 // importReference.declarationSourceStart,
844 // importReference.declarationSourceEnd,
845 // intArrayStack[intArrayPtr--],
846 // CharOperation.concatWith(importReference.getImportName(), '.'),
847 // importReference.sourceStart);
849 //protected void consumePushModifiers() {
850 // checkAnnotation(); // might update modifiers with AccDeprecated
851 // pushOnIntStack(modifiers); // modifiers
852 // if (modifiersSourceStart < 0) {
853 // pushOnIntStack(-1);
855 // declarationSourceStart >= 0 ? declarationSourceStart : scanner.startPosition);
857 // pushOnIntStack(modifiersSourceStart);
859 // declarationSourceStart >= 0 ? declarationSourceStart : modifiersSourceStart);
865 // * INTERNAL USE-ONLY
867 //protected void consumeSingleTypeImportDeclarationName() {
868 // // SingleTypeImportDeclarationName ::= 'import' Name
870 // /* persisting javadoc positions */
871 // pushOnIntArrayStack(this.getJavaDocPositions());
873 // super.consumeSingleTypeImportDeclarationName();
874 // ImportReference importReference = (ImportReference) astStack[astPtr];
875 // requestor.acceptImport(
876 // importReference.declarationSourceStart,
877 // importReference.declarationSourceEnd,
878 // intArrayStack[intArrayPtr--],
879 // CharOperation.concatWith(importReference.getImportName(), '.'),
880 // importReference.sourceStart,
885 // * INTERNAL USE-ONLY
887 //protected void consumeStaticInitializer() {
888 // // StaticInitializer ::= StaticOnly Block
889 // //push an Initializer
890 // //optimize the push/pop
891 // super.consumeStaticInitializer();
892 // Initializer initializer = (Initializer) astStack[astPtr];
893 // requestor.acceptInitializer(
894 // initializer.declarationSourceStart,
895 // initializer.declarationSourceEnd,
896 // intArrayStack[intArrayPtr--],
898 // intStack[intPtr--],
899 // initializer.block.sourceStart,
900 // initializer.declarationSourceEnd);
902 //protected void consumeStaticOnly() {
903 // // StaticOnly ::= 'static'
904 // checkAnnotation(); // might update declaration source start
905 // pushOnIntStack(modifiersSourceStart);
907 // declarationSourceStart >= 0 ? declarationSourceStart : modifiersSourceStart);
908 // jumpOverMethodBody();
909 // nestedMethod[nestedType]++;
914 // * INTERNAL USE-ONLY
916 //protected void consumeTypeImportOnDemandDeclarationName() {
917 // // TypeImportOnDemandDeclarationName ::= 'import' Name '.' '*'
919 // /* persisting javadoc positions */
920 // pushOnIntArrayStack(this.getJavaDocPositions());
922 // super.consumeTypeImportOnDemandDeclarationName();
923 // ImportReference importReference = (ImportReference) astStack[astPtr];
924 // requestor.acceptImport(
925 // importReference.declarationSourceStart,
926 // importReference.declarationSourceEnd,
927 // intArrayStack[intArrayPtr--],
928 // CharOperation.concatWith(importReference.getImportName(), '.'),
929 // importReference.sourceStart,
932 public CompilationUnitDeclaration endParse(int act) {
933 if (scanner.recordLineSeparator) {
934 requestor.acceptLineSeparatorPositions(scanner.getLineEnds());
936 return super.endParse(act);
939 * Flush annotations defined prior to a given positions.
941 * Note: annotations are stacked in syntactical order
943 * Either answer given <position>, or the end position of a comment line
944 * immediately following the <position> (same line)
948 * } // end of method foo
951 //public int flushAnnotationsDefinedPriorTo(int position) {
953 // return lastFieldEndPosition = super.flushAnnotationsDefinedPriorTo(position);
955 //protected TypeReference getTypeReference(int dim) { /* build a Reference on a variable that may be qualified or not
956 //This variable is a type reference and dim will be its dimensions*/
959 // TypeReference ref;
960 // if ((length = identifierLengthStack[identifierLengthPtr--]) == 1) {
961 // // single variable reference
964 // new SingleTypeReference(
965 // identifierStack[identifierPtr],
966 // identifierPositionStack[identifierPtr--]);
969 // new ArrayTypeReference(
970 // identifierStack[identifierPtr],
972 // identifierPositionStack[identifierPtr--]);
973 // ref.sourceEnd = endPosition;
976 // if (length < 0) { //flag for precompiled type reference on base types
977 // ref = TypeReference.baseTypeReference(-length, dim);
978 // ref.sourceStart = intStack[intPtr--];
980 // ref.sourceEnd = intStack[intPtr--];
983 // ref.sourceEnd = endPosition;
985 // } else { //Qualified variable reference
986 // char[][] tokens = new char[length][];
987 // identifierPtr -= length;
988 // long[] positions = new long[length];
989 // System.arraycopy(identifierStack, identifierPtr + 1, tokens, 0, length);
991 // identifierPositionStack,
992 // identifierPtr + 1,
997 // ref = new QualifiedTypeReference(tokens, positions);
999 // ref = new ArrayQualifiedTypeReference(tokens, dim, positions);
1000 // ref.sourceEnd = endPosition;
1006 public void initialize() {
1007 //positionning the parser for a new compilation unit
1008 //avoiding stack reallocation and all that....
1009 super.initialize(false);
1016 //private boolean isLocalDeclaration() {
1017 // int nestedDepth = nestedType;
1018 // while (nestedDepth >= 0) {
1019 // if (nestedMethod[nestedDepth] != 0) {
1027 * Investigate one entire unit.
1029 public void parseCompilationUnit(ICompilationUnit unit) {
1030 char[] regionSource = unit.getContents();
1033 goForCompilationUnit();
1037 new CompilationUnitDeclaration(
1039 new CompilationResult(unit, 0, 0, 10), //this.options.maxProblemsPerUnit),
1040 regionSource.length);
1041 scanner.resetTo(0, regionSource.length);
1042 scanner.setSource(regionSource);
1044 } catch (AbortCompilation ex) {
1048 * Investigate one constructor declaration.
1050 //public void parseConstructor(char[] regionSource) {
1053 // goForClassBodyDeclarations();
1054 // referenceContext =
1055 // compilationUnit =
1056 // compilationUnit =
1057 // new CompilationUnitDeclaration(
1058 // problemReporter(),
1059 // new CompilationResult(regionSource, 0, 0, 10), //this.options.maxProblemsPerUnit),
1060 // regionSource.length);
1061 // scanner.resetTo(0, regionSource.length);
1062 // scanner.setSource(regionSource);
1064 // } catch (AbortCompilation ex) {
1068 * Investigate one field declaration statement (might have multiple declarations in it).
1070 //public void parseField(char[] regionSource) {
1073 // goForFieldDeclaration();
1074 // referenceContext =
1075 // compilationUnit =
1076 // compilationUnit =
1077 // new CompilationUnitDeclaration(
1078 // problemReporter(),
1079 // new CompilationResult(regionSource, 0, 0, this.options.maxProblemsPerUnit),
1080 // regionSource.length);
1081 // scanner.resetTo(0, regionSource.length);
1082 // scanner.setSource(regionSource);
1084 // } catch (AbortCompilation ex) {
1089 // * Investigate one import statement declaration.
1091 //public void parseImport(char[] regionSource) {
1094 // goForImportDeclaration();
1095 // referenceContext =
1096 // compilationUnit =
1097 // compilationUnit =
1098 // new CompilationUnitDeclaration(
1099 // problemReporter(),
1100 // new CompilationResult(regionSource, 0, 0, this.options.maxProblemsPerUnit),
1101 // regionSource.length);
1102 // scanner.resetTo(0, regionSource.length);
1103 // scanner.setSource(regionSource);
1105 // } catch (AbortCompilation ex) {
1110 // * Investigate one initializer declaration.
1111 // * regionSource need to content exactly an initializer declaration.
1112 // * e.g: static { i = 4; }
1113 // * { name = "test"; }
1115 //public void parseInitializer(char[] regionSource) {
1118 // goForInitializer();
1119 // referenceContext =
1120 // compilationUnit =
1121 // compilationUnit =
1122 // new CompilationUnitDeclaration(
1123 // problemReporter(),
1124 // new CompilationResult(regionSource, 0, 0, this.options.maxProblemsPerUnit),
1125 // regionSource.length);
1126 // scanner.resetTo(0, regionSource.length);
1127 // scanner.setSource(regionSource);
1129 // } catch (AbortCompilation ex) {
1134 // * Investigate one method declaration.
1136 //public void parseMethod(char[] regionSource) {
1139 // goForGenericMethodDeclaration();
1140 // referenceContext =
1141 // compilationUnit =
1142 // compilationUnit =
1143 // new CompilationUnitDeclaration(
1144 // problemReporter(),
1145 // new CompilationResult(regionSource, 0, 0, this.options.maxProblemsPerUnit),
1146 // regionSource.length);
1147 // scanner.resetTo(0, regionSource.length);
1148 // scanner.setSource(regionSource);
1150 // } catch (AbortCompilation ex) {
1155 // * Investigate one package statement declaration.
1157 //public void parsePackage(char[] regionSource) {
1160 // goForPackageDeclaration();
1161 // referenceContext =
1162 // compilationUnit =
1163 // compilationUnit =
1164 // new CompilationUnitDeclaration(
1165 // problemReporter(),
1166 // new CompilationResult(regionSource, 0, 0, this.options.maxProblemsPerUnit),
1167 // regionSource.length);
1168 // scanner.resetTo(0, regionSource.length);
1169 // scanner.setSource(regionSource);
1171 // } catch (AbortCompilation ex) {
1176 // * Investigate one type declaration, its fields, methods and member types.
1178 //public void parseType(char[] regionSource) {
1181 // goForTypeDeclaration();
1182 // referenceContext =
1183 // compilationUnit =
1184 // compilationUnit =
1185 // new CompilationUnitDeclaration(
1186 // problemReporter(),
1187 // new CompilationResult(regionSource, 0, 0, this.options.maxProblemsPerUnit),
1188 // regionSource.length);
1189 // scanner.resetTo(0, regionSource.length);
1190 // scanner.setSource(regionSource);
1192 // } catch (AbortCompilation ex) {
1197 * Returns this parser's problem reporter initialized with its reference context.
1198 * Also it is assumed that a problem is going to be reported, so initializes
1199 * the compilation result's line positions.
1201 public ProblemReporter problemReporter() {
1202 problemReporter.referenceContext = referenceContext;
1203 return problemReporter;
1205 protected void pushOnIntArrayStack(int[] positions) {
1208 intArrayStack[++intArrayPtr] = positions;
1209 } catch (IndexOutOfBoundsException e) {
1211 int oldStackLength = intArrayStack.length;
1212 int oldStack[][] = intArrayStack;
1213 intArrayStack = new int[oldStackLength + StackIncrement][];
1214 System.arraycopy(oldStack, 0, intArrayStack, 0, oldStackLength);
1215 intArrayStack[intArrayPtr] = positions;
1218 //protected void resetModifiers() {
1219 // super.resetModifiers();
1220 // declarationSourceStart = -1;
1223 * Syntax error was detected. Will attempt to perform some recovery action in order
1224 * to resume to the regular parse loop.
1226 protected boolean resumeOnSyntaxError() {
1230 * Answer a char array representation of the type name formatted like:
1231 * - type name + dimensions
1233 * "A[][]".toCharArray()
1234 * "java.lang.String".toCharArray()
1236 //private char[] returnTypeName(TypeReference type) {
1237 // int dimension = type.dimensions();
1238 // if (dimension != 0) {
1239 // char[] dimensionsArray = new char[dimension * 2];
1240 // for (int i = 0; i < dimension; i++) {
1241 // dimensionsArray[i*2] = '[';
1242 // dimensionsArray[(i*2) + 1] = ']';
1244 // return CharOperation.concat(
1245 // CharOperation.concatWith(type.getTypeName(), '.'),
1246 // dimensionsArray);
1248 // return CharOperation.concatWith(type.getTypeName(), '.');
1250 //public String toString() {
1251 // StringBuffer buffer = new StringBuffer();
1252 // buffer.append("intArrayPtr = " + intArrayPtr + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
1253 // buffer.append(super.toString());
1254 // return buffer.toString();
1257 // * INTERNAL USE ONLY
1259 //protected TypeReference typeReference(
1261 // int localIdentifierPtr,
1262 // int localIdentifierLengthPtr) {
1263 // /* build a Reference on a variable that may be qualified or not
1264 // * This variable is a type reference and dim will be its dimensions.
1265 // * We don't have any side effect on the stacks' pointers.
1269 // TypeReference ref;
1270 // if ((length = identifierLengthStack[localIdentifierLengthPtr]) == 1) {
1271 // // single variable reference
1274 // new SingleTypeReference(
1275 // identifierStack[localIdentifierPtr],
1276 // identifierPositionStack[localIdentifierPtr--]);
1279 // new ArrayTypeReference(
1280 // identifierStack[localIdentifierPtr],
1282 // identifierPositionStack[localIdentifierPtr--]);
1283 // ref.sourceEnd = endPosition;
1286 // if (length < 0) { //flag for precompiled type reference on base types
1287 // ref = TypeReference.baseTypeReference(-length, dim);
1288 // ref.sourceStart = intStack[localIntPtr--];
1290 // ref.sourceEnd = intStack[localIntPtr--];
1293 // ref.sourceEnd = endPosition;
1295 // } else { //Qualified variable reference
1296 // char[][] tokens = new char[length][];
1297 // localIdentifierPtr -= length;
1298 // long[] positions = new long[length];
1299 // System.arraycopy(identifierStack, localIdentifierPtr + 1, tokens, 0, length);
1300 // System.arraycopy(
1301 // identifierPositionStack,
1302 // localIdentifierPtr + 1,
1307 // ref = new QualifiedTypeReference(tokens, positions);
1309 // ref = new ArrayQualifiedTypeReference(tokens, dim, positions);