Package lepl :: Package support :: Module state
[hide private]
[frames] | no frames]

Source Code for Module lepl.support.state

  1   
  2  # The contents of this file are subject to the Mozilla Public License 
  3  # (MPL) Version 1.1 (the "License"); you may not use this file except 
  4  # in compliance with the License. You may obtain a copy of the License 
  5  # at http://www.mozilla.org/MPL/ 
  6  # 
  7  # Software distributed under the License is distributed on an "AS IS" 
  8  # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
  9  # the License for the specific language governing rights and 
 10  # limitations under the License. 
 11  # 
 12  # The Original Code is LEPL (http://www.acooke.org/lepl) 
 13  # The Initial Developer of the Original Code is Andrew Cooke. 
 14  # Portions created by the Initial Developer are Copyright (C) 2009-2010 
 15  # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 
 16  # 
 17  # Alternatively, the contents of this file may be used under the terms 
 18  # of the LGPL license (the GNU Lesser General Public License, 
 19  # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 
 20  # of the LGPL License are applicable instead of those above. 
 21  # 
 22  # If you wish to allow use of your version of this file only under the 
 23  # terms of the LGPL License and not to allow others to use your version 
 24  # of this file under the MPL, indicate your decision by deleting the 
 25  # provisions above and replace them with the notice and other provisions 
 26  # required by the LGPL License.  If you do not delete the provisions 
 27  # above, a recipient may use your version of this file under either the 
 28  # MPL or the LGPL License. 
 29   
 30  ''' 
 31  Encapsulate global (per thread) state. 
 32   
 33  This is for state that can affect the current parse.  It's probably simplest to 
 34  explain an example of what it can be used for.  Memoization records results 
 35  for a particular state to avoid repeating matches needlessly.  The state used 
 36  to identify when "the same thing is happening" is based on: 
 37  - the matcher being called 
 38  - the stream passed to the matcher 
 39  - this state 
 40   
 41  So a good shorthand for deciding whether or not this state should be used is 
 42  to ask whether the state will affect whether or not memoisation will work 
 43  correctly. 
 44   
 45  For example, with offside parsing, the current indentation level should be 
 46  stored here, because a (matcher, stream) combination that has previously failed 
 47  may work correctly when it changes.  
 48  ''' 
 49   
 50  from threading import local 
 51   
 52  from lepl.support.lib import singleton 
53 54 55 -class State(local):
56 ''' 57 A thread local map from key (typically calling class) to value. The hash 58 attribute is updated on each mutation and cached for rapid access. 59 ''' 60
61 - def __init__(self):
62 ''' 63 Do not call directly - use the singleton. 64 ''' 65 super(State, self).__init__() 66 self.__map = {} 67 self.hash = self.__hash()
68 69 @classmethod
70 - def singleton(cls):
71 ''' 72 Get a singleton instance. 73 ''' 74 return singleton(cls)
75
76 - def __hash(self):
77 ''' 78 Calculate the hash for the current dict. 79 ''' 80 value = 0 81 for key in self.__map: 82 value ^= hash(key) ^ hash(self.__map[key]) 83 return value
84
85 - def __getitem__(self, key):
86 return self.__map[key]
87
88 - def get(self, key, default=None):
89 ''' 90 As for dict (lookup with default). 91 ''' 92 return self.__map.get(key, default)
93
94 - def __setitem__(self, key, value):
95 self.__map[key] = value 96 self.hash = self.__hash()
97
98 - def __delitem__(self, key):
99 del self.__map[key] 100 self.hash = self.__hash()
101
102 - def __hash__(self):
103 return self.hash
104