001: /**************************************************************************/
002: /* N I C E */
003: /* A high-level object-oriented research language */
004: /* (c) Daniel Bonniot 2000 */
005: /* */
006: /* This program is free software; you can redistribute it and/or modify */
007: /* it under the terms of the GNU General Public License as published by */
008: /* the Free Software Foundation; either version 2 of the License, or */
009: /* (at your option) any later version. */
010: /* */
011: /**************************************************************************/package bossa.syntax;
012:
013: import java.util.*;
014: import bossa.util.*;
015:
016: /**
017: A Scope level for variables.
018:
019: @version $Date: 2005/03/12 02:41:47 $
020: @author Daniel Bonniot (d.bonniot@mail.dotcom.fr)
021: */
022: public abstract class VarScope {
023: abstract void addSymbol(/*VarSymbol*/Symbol s);
024:
025: void addSymbol(/*VarSymbol*/Symbol s,
026: nice.tools.visibility.Visibility v) {
027: this .addSymbol(s);
028: }
029:
030: /**
031: Adds a collection of VarSymbols
032: */
033: void addSymbols(Collection c) {
034: if (c == null)
035: return;
036:
037: Iterator i = c.iterator();
038: while (i.hasNext()) {
039: /*VarSymbol*/Symbol s = (/*VarSymbol*/Symbol) i.next();
040: addSymbol(s);
041:
042: }
043: }
044:
045: abstract void removeSymbol(/*VarSymbol*/Symbol sym);
046:
047: public abstract List lookup(LocatedString i);
048:
049: abstract List globalLookup(LocatedString i);
050:
051: static VarScope create(VarScope outer) {
052: return new LocalVarScope(outer);
053: }
054:
055: static VarScope create(VarScope outer,
056: Collection /* of VarSymbol */defs) {
057: return new LocalVarScope(outer, defs);
058: }
059: }
060:
061: class LocalVarScope extends VarScope {
062: public LocalVarScope(VarScope outer) {
063: this .outer = outer;
064: this .defs = new HashMap();
065: }
066:
067: public LocalVarScope(VarScope outer,
068: Collection /* of VarSymbol */defs) {
069: this (outer);
070: addSymbols(defs);
071: }
072:
073: void addSymbol(/*VarSymbol*/Symbol s) {
074: this .defs.put(s.name, s);
075: }
076:
077: void removeSymbol(/*VarSymbol*/Symbol sym) {
078: defs.remove(sym.name);
079: }
080:
081: /**
082: * The lookup method to call when you need to get a VarSymbol
083: * from its name
084: *
085: * @param i the identifier to lookup
086: * @return the symbols if some were found, null otherwise
087: */
088: public List lookup(LocatedString i) {
089: Object res = defs.get(i);
090:
091: if (res != null) {
092: LinkedList l = new LinkedList();
093: l.add(res);
094: return l;
095: }
096:
097: if (outer != null)
098: return outer.lookup(i);
099:
100: return Collections.EMPTY_LIST;
101: }
102:
103: List globalLookup(LocatedString i) {
104: if (outer != null)
105: return outer.globalLookup(i);
106: else
107: return null;
108: }
109:
110: /****************************************************************
111: * Debugging
112: ****************************************************************/
113:
114: public String toString() {
115: return defs.size() + ";;\n" + outer;
116: }
117:
118: private VarScope outer;
119: private HashMap defs;
120: }
|