01: package org.acm.seguin.pmd.symboltable;
02:
03: public class Search {
04: private static final boolean TRACE = false;
05:
06: private NameOccurrence occ;
07: private NameDeclaration decl;
08:
09: public Search(NameOccurrence occ) {
10: if (TRACE)
11: System.out.println("new search for " + occ);
12: this .occ = occ;
13: }
14:
15: public void execute() {
16: decl = searchUpward(occ, occ.getScope());
17: if (TRACE)
18: System.out.println("found " + decl);
19: }
20:
21: public void execute(Scope startingScope) {
22: decl = searchUpward(occ, startingScope);
23: if (TRACE)
24: System.out.println("found " + decl);
25: }
26:
27: public NameDeclaration getResult() {
28: return decl;
29: }
30:
31: private NameDeclaration searchUpward(NameOccurrence nameOccurrence,
32: Scope scope) {
33: if (!scope.contains(nameOccurrence)
34: && scope.getParent() != null) {
35: if (TRACE)
36: System.out.println("moving up fm " + scope + " to "
37: + scope.getParent());
38: return searchUpward(nameOccurrence, (Scope) scope
39: .getParent());
40: }
41: if (scope.contains(nameOccurrence)) {
42: return scope.addVariableNameOccurrence(nameOccurrence);
43: }
44: return null;
45: }
46: }
|