001/* An evaluation scope that consists of a list of nested scopes. 002 003 Copyright (c) 2002-2014 The Regents of the University of California. 004 All rights reserved. 005 Permission is hereby granted, without written agreement and without 006 license or royalty fees, to use, copy, modify, and distribute this 007 software and its documentation for any purpose, provided that the above 008 copyright notice and the following two paragraphs appear in all copies 009 of this software. 010 011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 015 SUCH DAMAGE. 016 017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 022 ENHANCEMENTS, OR MODIFICATIONS. 023 024 PT_COPYRIGHT_VERSION_2 025 COPYRIGHTENDKEY 026 027 028 */ 029package ptolemy.data.expr; 030 031import java.util.HashSet; 032import java.util.Iterator; 033import java.util.List; 034import java.util.Set; 035 036import ptolemy.graph.InequalityTerm; 037import ptolemy.kernel.util.IllegalActionException; 038 039////////////////////////////////////////////////////////////////////////// 040//// NestedScope 041 042/** 043 An implementation of ParserScope that consists of a list of nested scopes. 044 A lookup starts from the first scope in the list, and proceeds through the 045 list until a mapping is found. 046 047 @author Xiaojun Liu 048 @version $Id$ 049 @since Ptolemy II 2.1 050 @Pt.ProposedRating Red (liuxj) 051 @Pt.AcceptedRating Red (liuxj) 052 */ 053public class NestedScope implements ParserScope { 054 /** Construct a new scope that consists of the given list of scopes. 055 * @param scopeList The list of scopes. 056 */ 057 public NestedScope(List scopeList) { 058 _scopeList = scopeList; 059 } 060 061 /** Look up and return the value with the specified name in the 062 * scope. Start from the first scope in the list supplied to the 063 * constructor. If a value is found, return the value, otherwise 064 * continue to look up through the list. Return null if no mapping 065 * is defined in any scope in the list. 066 * @return The token associated with the given name in the scope. 067 * @exception IllegalActionException If a value in the scope 068 * exists with the given name, but cannot be evaluated. 069 */ 070 @Override 071 public ptolemy.data.Token get(String name) throws IllegalActionException { 072 Iterator scopes = _scopeList.iterator(); 073 074 while (scopes.hasNext()) { 075 ParserScope scope = (ParserScope) scopes.next(); 076 ptolemy.data.Token result = scope.get(name); 077 078 if (result != null) { 079 return result; 080 } 081 } 082 083 return null; 084 } 085 086 /** Look up and return the type of the value with the specified 087 * name in the scope. Return null if the name is not defined in 088 * this scope. 089 * @return The token associated with the given name in the scope. 090 * @exception IllegalActionException If a value in the scope 091 * exists with the given name, but cannot be evaluated. 092 */ 093 @Override 094 public ptolemy.data.type.Type getType(String name) 095 throws IllegalActionException { 096 Iterator scopes = _scopeList.iterator(); 097 098 while (scopes.hasNext()) { 099 ParserScope scope = (ParserScope) scopes.next(); 100 ptolemy.data.type.Type result = scope.getType(name); 101 102 if (result != null) { 103 return result; 104 } 105 } 106 107 return null; 108 } 109 110 /** Look up and return the type term for the specified name 111 * in the scope. Return null if the name is not defined in this 112 * scope, or is a constant type. 113 * @return The InequalityTerm associated with the given name in 114 * the scope. 115 * @exception IllegalActionException If a value in the scope 116 * exists with the given name, but cannot be evaluated. 117 */ 118 @Override 119 public ptolemy.graph.InequalityTerm getTypeTerm(String name) 120 throws IllegalActionException { 121 Iterator scopes = _scopeList.iterator(); 122 123 while (scopes.hasNext()) { 124 ParserScope scope = (ParserScope) scopes.next(); 125 InequalityTerm result = scope.getTypeTerm(name); 126 127 if (result != null) { 128 return result; 129 } 130 } 131 132 return null; 133 } 134 135 /** Return the list of identifiers within the scope. 136 * @return The union of the identifierSets of the contained scopes. 137 * @exception IllegalActionException If the identifierSet() 138 * method of a contained scope throws it. 139 */ 140 @Override 141 public Set identifierSet() throws IllegalActionException { 142 Set set = new HashSet(); 143 144 for (Iterator scopes = _scopeList.iterator(); scopes.hasNext();) { 145 ParserScope scope = (ParserScope) scopes.next(); 146 set.addAll(scope.identifierSet()); 147 } 148 149 return set; 150 } 151 152 private List _scopeList; 153}