01: /*
02: * %W% %E%
03: *
04: * Copyright (c) 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.lang;
10:
11: import pnuts.compiler.*;
12: import pnuts.lang.*;
13: import java.util.*;
14:
15: public class DeclarationHelper extends ScopeAnalyzer {
16:
17: public static void preprocess(SimpleNode node) {
18: new DeclarationHelper().analyze(node);
19: }
20:
21: static boolean isConditionalNode(SimpleNode node) {
22: return node.id == PnutsParserTreeConstants.JJTIFSTATEMENT
23: || node.id == PnutsParserTreeConstants.JJTSWITCHSTATEMENT
24: || node.id == PnutsParserTreeConstants.JJTWHILESTATEMENT
25: || node.id == PnutsParserTreeConstants.JJTDOSTATEMENT
26: || node.id == PnutsParserTreeConstants.JJTFORSTATEMENT
27: || node.id == PnutsParserTreeConstants.JJTFOREACHSTATEMENT;
28: }
29:
30: protected void declared(SimpleNode node, Context context,
31: String symbol) {
32: SimpleNode n = node.jjtGetParent();
33: while (n != null
34: && n.id != PnutsParserTreeConstants.JJTFUNCTIONSTATEMENT) {
35: n = n.jjtGetParent();
36: }
37: if (n != null) { // node is in local scope
38: n = node;
39: // Set symbols = null;
40: SimpleNode block = null;
41: while (n != null
42: && n.id != PnutsParserTreeConstants.JJTFUNCTIONSTATEMENT) {
43: if (n.id == PnutsParserTreeConstants.JJTBLOCK
44: || n.id == PnutsParserTreeConstants.JJTSWITCHBLOCK) {
45: block = n;
46: }
47: if (isConditionalNode(n)) {
48: Set s = (Set) n.getAttribute("declaredSymbols");
49: if (s == null) {
50: s = new HashSet();
51: n.setAttribute("declaredSymbols", s);
52: /*
53: if (symbols == null){
54: symbols = s = new HashSet();
55: } else {
56: s = symbols;
57: }
58: */
59: }
60: // node.setAttribute("declaredSymbols", s);
61: s.add(symbol);
62:
63: // if (block != null){
64: s = (Set) block.getAttribute("declaredSymbols");
65: if (s == null) {
66: s = new HashSet();
67: block.setAttribute("declaredSymbols", s);
68: }
69: s.add(symbol);
70: // }
71: }
72: n = n.jjtGetParent();
73: }
74: }
75: }
76: }
|