compiler/PPCContextMemento.st
changeset 391 553a5456963b
child 392 9b297f0d949c
equal deleted inserted replaced
390:17ba167b8ee1 391:553a5456963b
       
     1 "{ Package: 'stx:goodies/petitparser/compiler' }"
       
     2 
       
     3 Object subclass:#PPCContextMemento
       
     4 	instanceVariableNames:'position properties'
       
     5 	classVariableNames:''
       
     6 	poolDictionaries:''
       
     7 	category:'PetitCompiler-Context'
       
     8 !
       
     9 
       
    10 PPCContextMemento comment:''
       
    11 !
       
    12 
       
    13 !PPCContextMemento methodsFor:'accessing'!
       
    14 
       
    15 position
       
    16 	^ position
       
    17 !
       
    18 
       
    19 position: anInteger
       
    20 	position := anInteger 
       
    21 ! !
       
    22 
       
    23 !PPCContextMemento methodsFor:'accessing - properties'!
       
    24 
       
    25 hasProperty: aKey
       
    26 	"Test if the property aKey is present."
       
    27 	
       
    28 	^ properties notNil and: [ properties includesKey: aKey ]
       
    29 !
       
    30 
       
    31 keysAndValuesDo: aBlock
       
    32 	properties ifNil: [ ^ self ].
       
    33 	properties keysAndValuesDo: [ :key :value | aBlock value: key value: value copy ] 
       
    34 !
       
    35 
       
    36 propertiesSize
       
    37 	properties ifNil: [ ^ 0 ].
       
    38 	^ properties size.
       
    39 !
       
    40 
       
    41 propertyAt: aKey
       
    42 	"Answer the property value associated with aKey."
       
    43 	
       
    44 	^ self propertyAt: aKey ifAbsent: [ self error: 'Property not found' ]
       
    45 !
       
    46 
       
    47 propertyAt: aKey ifAbsent: aBlock
       
    48         "Answer the property value associated with aKey or, if aKey isn't found, answer the result of evaluating aBlock."
       
    49 
       
    50         properties isNil
       
    51                 ifTrue: [ ^ aBlock value ]
       
    52                 ifFalse: [ 
       
    53                         (properties includesKey: aKey) ifTrue: [ 
       
    54                                 ^ (properties at: aKey) copy
       
    55                         ].
       
    56                         ^ aBlock value
       
    57                 ]
       
    58 
       
    59     "Created: / 26-10-2014 / 01:23:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    60 !
       
    61 
       
    62 propertyAt: aKey ifAbsentPut: aBlock
       
    63 	"Answer the property associated with aKey or, if aKey isn't found store the result of evaluating aBlock as new value."
       
    64 	
       
    65 	^ self propertyAt: aKey ifAbsent: [ self propertyAt: aKey put: aBlock value ]
       
    66 !
       
    67 
       
    68 propertyAt: aKey put: anObject
       
    69 	"Set the property at aKey to be anObject. If aKey is not found, create a new entry for aKey and set is value to anObject. Answer anObject."
       
    70 
       
    71 	^ (properties ifNil: [ properties := Dictionary new: 1 ])
       
    72 		at: aKey put: (anObject copy)
       
    73 !
       
    74 
       
    75 removeProperty: aKey
       
    76 	"Remove the property with aKey. Answer the property or raise an error if aKey isn't found."
       
    77 	
       
    78 	^ self removeProperty: aKey ifAbsent: [ self error: 'Property not found' ]
       
    79 !
       
    80 
       
    81 removeProperty: aKey ifAbsent: aBlock
       
    82 	"Remove the property with aKey. Answer the value or, if aKey isn't found, answer the result of evaluating aBlock."
       
    83 	
       
    84 	| answer |
       
    85 	properties isNil ifTrue: [ ^ aBlock value ].
       
    86 	answer := properties removeKey: aKey ifAbsent: aBlock.
       
    87 	properties isEmpty ifTrue: [ properties := nil ].
       
    88 	^ answer
       
    89 ! !
       
    90 
       
    91 !PPCContextMemento methodsFor:'comparing'!
       
    92 
       
    93 = anObject
       
    94 	
       
    95 	(self == anObject) ifTrue: [ ^ true ].
       
    96 	(anObject class = PPCContextMemento) ifFalse: [ ^ false ].
       
    97 	
       
    98 	(anObject position = position) ifFalse: [ ^ false ].
       
    99 
       
   100 	(self propertiesSize = anObject propertiesSize) ifFalse: [ ^ false ].
       
   101 
       
   102 	self keysAndValuesDo: [ :key :value |
       
   103 		(anObject hasProperty: key) ifFalse: [ ^ false ].
       
   104 		((anObject propertyAt: key) = value) ifFalse: [ ^ false ]. 
       
   105  	].
       
   106 	
       
   107 	^ true.
       
   108 !
       
   109 
       
   110 hash
       
   111         ^ position hash bitXor: properties hash.
       
   112 
       
   113     "Modified: / 26-10-2014 / 01:46:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   114 ! !
       
   115