compiler/PEGFsaTransition.st
changeset 515 b5316ef15274
parent 502 1e45d3c96ec5
child 516 3b81c9e53352
child 524 f6f68d32de73
equal deleted inserted replaced
502:1e45d3c96ec5 515:b5316ef15274
     1 "{ Package: 'stx:goodies/petitparser/compiler' }"
     1 "{ Package: 'stx:goodies/petitparser/compiler' }"
     2 
     2 
     3 "{ NameSpace: Smalltalk }"
     3 "{ NameSpace: Smalltalk }"
     4 
     4 
     5 Object subclass:#PEGFsaTransition
     5 Object subclass:#PEGFsaTransition
     6 	instanceVariableNames:'characterSet destination priority'
     6 	instanceVariableNames:'destination priority characterSet'
     7 	classVariableNames:''
     7 	classVariableNames:''
     8 	poolDictionaries:''
     8 	poolDictionaries:''
     9 	category:'PetitCompiler-FSA'
     9 	category:'PetitCompiler-FSA'
    10 !
    10 !
       
    11 
       
    12 !PEGFsaTransition class methodsFor:'instance creation'!
       
    13 
       
    14 new
       
    15     "return an initialized instance"
       
    16 
       
    17     ^ self basicNew initialize.
       
    18 ! !
    11 
    19 
    12 !PEGFsaTransition methodsFor:'accessing'!
    20 !PEGFsaTransition methodsFor:'accessing'!
    13 
    21 
    14 characterSet
    22 characterSet
    15     ^ characterSet
    23     ^ characterSet
    47     (self == anotherTransition) ifTrue: [ ^ true ].
    55     (self == anotherTransition) ifTrue: [ ^ true ].
    48     (self class == anotherTransition class) ifFalse: [ ^ false ].
    56     (self class == anotherTransition class) ifFalse: [ ^ false ].
    49 
    57 
    50     (destination == anotherTransition destination) ifFalse: [ ^ false ].
    58     (destination == anotherTransition destination) ifFalse: [ ^ false ].
    51     (priority == anotherTransition priority) ifFalse: [ ^ false ].
    59     (priority == anotherTransition priority) ifFalse: [ ^ false ].
    52     (characterSet = anotherTransition characterSet) ifFalse: [ ^ false ].
       
    53     
    60     
    54     ^ true
    61     ^ true
    55 !
    62 !
    56 
    63 
    57 canBeIsomorphicTo: anotherTransition
    64 canBeIsomorphicTo: anotherTransition
       
    65     (self class == anotherTransition class) ifFalse: [ ^ false ].
    58     (priority == anotherTransition priority) ifFalse: [ ^ false ].
    66     (priority == anotherTransition priority) ifFalse: [ ^ false ].
    59     (characterSet = anotherTransition characterSet) ifFalse: [ ^ false ].
       
    60     
    67     
    61     ^ true
    68     ^ true
    62 !
    69 !
    63 
    70 
    64 equals: anotherTransition
    71 equals: anotherTransition
    65     "this method is used for minimization of the FSA"
    72     "this method is used for minimization of the FSA"
    66     
    73     
    67     (self == anotherTransition) ifTrue: [ ^ true ].
    74     (self == anotherTransition) ifTrue: [ ^ true ].
       
    75     (self class == anotherTransition class) ifFalse: [ ^ false ].
    68 
    76 
    69     (destination == anotherTransition destination) ifFalse: [ ^ false ].
    77     (destination == anotherTransition destination) ifFalse: [ ^ false ].
    70     (characterSet = anotherTransition characterSet) ifFalse: [ ^ false ].
       
    71 
    78 
    72     "JK: If character set and destination are the same, priority does not really matter"
    79     "JK: If character set and destination are the same, priority does not really matter"
    73     ^ true
    80     ^ true
    74 !
    81 !
    75 
    82 
    76 hash
    83 hash
    77     ^ destination hash bitXor: (priority hash bitXor: characterSet hash)
    84     ^ destination hash bitXor: priority hash
    78 !
       
    79 
       
    80 isIsomorphicTo: object resolvedSet: set
       
    81     (set includes: (PEGFsaPair with: self with: object)) ifTrue: [ 
       
    82         ^ true
       
    83     ].
       
    84     set add: (PEGFsaPair with: self with: object).
       
    85 
       
    86     (self == object) ifTrue: [ ^ true ].
       
    87     (self class == object class) ifFalse: [ ^ false ].
       
    88 
       
    89     (priority == object priority) ifFalse: [ ^ false ].
       
    90     (characterSet = object characterSet) ifFalse: [ ^ false ].
       
    91     (destination isIsomorphicTo: object destination resolvedSet: set) ifFalse: [ ^ false ].
       
    92     
       
    93     ^ true
       
    94 ! !
    85 ! !
    95 
    86 
    96 !PEGFsaTransition methodsFor:'copying'!
    87 !PEGFsaTransition methodsFor:'copying'!
    97 
    88 
    98 postCopy
    89 postCopy
   111 
   102 
   112 !PEGFsaTransition methodsFor:'initialization'!
   103 !PEGFsaTransition methodsFor:'initialization'!
   113 
   104 
   114 initialize
   105 initialize
   115     super initialize.
   106     super initialize.
   116     characterSet := Array new: 255 withAll: false.
       
   117     priority := 0.
   107     priority := 0.
   118 ! !
   108 ! !
   119 
   109 
   120 !PEGFsaTransition methodsFor:'modifications'!
   110 !PEGFsaTransition methodsFor:'modifications'!
   121 
   111 
   122 addCharacter: character
   112 addCharacter: character
   123     characterSet at: character codePoint put: true
   113     characterSet at: character codePoint put: true
   124 !
   114 !
   125 
   115 
   126 decreasePriority
   116 decreasePriority
   127     priority := priority - 1
   117     self decreasePriorityBy: 1
   128 ! !
   118 !
   129 
   119 
   130 !PEGFsaTransition methodsFor:'printing'!
   120 decreasePriorityBy: value
   131 
   121     priority := priority - value
   132 characterSetAsString
       
   133     | stream |
       
   134     stream := WriteStream on: ''.
       
   135     self printCharacterSetOn: stream.
       
   136     ^ stream contents
       
   137 !
       
   138 
       
   139 printCharacterSetOn: stream
       
   140     self isEpsilon ifTrue: [ 
       
   141         stream nextPutAll: '<epsilon>'.
       
   142         ^ self
       
   143     ].
       
   144 
       
   145     stream nextPut: $[.
       
   146     32 to: 127 do: [ :index |
       
   147         (characterSet at: index) ifTrue: [ 
       
   148             stream nextPut: (Character codePoint: index)
       
   149         ]
       
   150     ].
       
   151     stream nextPut: $].
       
   152 !
       
   153 
       
   154 printOn: stream
       
   155     self printCharacterSetOn: stream.
       
   156     stream nextPutAll: ' ('.
       
   157     priority printOn: stream.
       
   158     stream nextPutAll: ')'.		
       
   159     stream nextPutAll: '-->'.
       
   160     destination printOn: stream.
       
   161     stream nextPutAll: '(ID: '.
       
   162     stream nextPutAll: self identityHash asString.
       
   163     stream nextPutAll: ')'.
       
   164 ! !
   122 ! !
   165 
   123 
   166 !PEGFsaTransition methodsFor:'set operations'!
   124 !PEGFsaTransition methodsFor:'set operations'!
   167 
   125 
   168 complement: transition
   126 complement: transition
   221 
   179 
   222 accepts: character
   180 accepts: character
   223     ^ characterSet at: character codePoint
   181     ^ characterSet at: character codePoint
   224 !
   182 !
   225 
   183 
       
   184 isCharacterTransition
       
   185     ^ false
       
   186 !
       
   187 
   226 isEpsilon
   188 isEpsilon
   227     ^ characterSet allSatisfy: [ :e | e not ]
   189     ^ self isEpsilonTransition
       
   190 !
       
   191 
       
   192 isEpsilonTransition
       
   193     ^ false
       
   194 !
       
   195 
       
   196 isPredicateTransition
       
   197     ^ false
   228 !
   198 !
   229 
   199 
   230 overlapsWith: transition
   200 overlapsWith: transition
   231     ^ (self intersection: transition) anySatisfy: [ :bool | bool ]
   201     ^ (self intersection: transition) anySatisfy: [ :bool | bool ]
   232 ! !
   202 ! !