compiler/PEGFsaTransition.st
changeset 516 3b81c9e53352
parent 504 0fb1f0799fc1
parent 515 b5316ef15274
child 523 09afcf28ed60
equal deleted inserted replaced
514:46dd1237b20a 516:3b81c9e53352
     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 
    11 
    55     (self == anotherTransition) ifTrue: [ ^ true ].
    55     (self == anotherTransition) ifTrue: [ ^ true ].
    56     (self class == anotherTransition class) ifFalse: [ ^ false ].
    56     (self class == anotherTransition class) ifFalse: [ ^ false ].
    57 
    57 
    58     (destination == anotherTransition destination) ifFalse: [ ^ false ].
    58     (destination == anotherTransition destination) ifFalse: [ ^ false ].
    59     (priority == anotherTransition priority) ifFalse: [ ^ false ].
    59     (priority == anotherTransition priority) ifFalse: [ ^ false ].
    60     (characterSet = anotherTransition characterSet) ifFalse: [ ^ false ].
       
    61     
    60     
    62     ^ true
    61     ^ true
    63 !
    62 !
    64 
    63 
    65 canBeIsomorphicTo: anotherTransition
    64 canBeIsomorphicTo: anotherTransition
       
    65     (self class == anotherTransition class) ifFalse: [ ^ false ].
    66     (priority == anotherTransition priority) ifFalse: [ ^ false ].
    66     (priority == anotherTransition priority) ifFalse: [ ^ false ].
    67     (characterSet = anotherTransition characterSet) ifFalse: [ ^ false ].
       
    68     
    67     
    69     ^ true
    68     ^ true
    70 !
    69 !
    71 
    70 
    72 equals: anotherTransition
    71 equals: anotherTransition
    73     "this method is used for minimization of the FSA"
    72     "this method is used for minimization of the FSA"
    74     
    73     
    75     (self == anotherTransition) ifTrue: [ ^ true ].
    74     (self == anotherTransition) ifTrue: [ ^ true ].
       
    75     (self class == anotherTransition class) ifFalse: [ ^ false ].
    76 
    76 
    77     (destination == anotherTransition destination) ifFalse: [ ^ false ].
    77     (destination == anotherTransition destination) ifFalse: [ ^ false ].
    78     (characterSet = anotherTransition characterSet) ifFalse: [ ^ false ].
       
    79 
    78 
    80     "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"
    81     ^ true
    80     ^ true
    82 !
    81 !
    83 
    82 
    84 hash
    83 hash
    85     ^ destination hash bitXor: (priority hash bitXor: characterSet hash)
    84     ^ destination hash bitXor: priority hash
    86 !
       
    87 
       
    88 isIsomorphicTo: object resolvedSet: set
       
    89     (set includes: (PEGFsaPair with: self with: object)) ifTrue: [ 
       
    90         ^ true
       
    91     ].
       
    92     set add: (PEGFsaPair with: self with: object).
       
    93 
       
    94     (self == object) ifTrue: [ ^ true ].
       
    95     (self class == object class) ifFalse: [ ^ false ].
       
    96 
       
    97     (priority == object priority) ifFalse: [ ^ false ].
       
    98     (characterSet = object characterSet) ifFalse: [ ^ false ].
       
    99     (destination isIsomorphicTo: object destination resolvedSet: set) ifFalse: [ ^ false ].
       
   100     
       
   101     ^ true
       
   102 ! !
    85 ! !
   103 
    86 
   104 !PEGFsaTransition methodsFor:'copying'!
    87 !PEGFsaTransition methodsFor:'copying'!
   105 
    88 
   106 postCopy
    89 postCopy
   119 
   102 
   120 !PEGFsaTransition methodsFor:'initialization'!
   103 !PEGFsaTransition methodsFor:'initialization'!
   121 
   104 
   122 initialize
   105 initialize
   123     super initialize.
   106     super initialize.
   124     characterSet := Array new: 255 withAll: false.
       
   125     priority := 0.
   107     priority := 0.
   126 ! !
   108 ! !
   127 
   109 
   128 !PEGFsaTransition methodsFor:'modifications'!
   110 !PEGFsaTransition methodsFor:'modifications'!
   129 
   111 
   130 addCharacter: character
   112 addCharacter: character
   131     characterSet at: character codePoint put: true
   113     characterSet at: character codePoint put: true
   132 !
   114 !
   133 
   115 
   134 decreasePriority
   116 decreasePriority
   135     priority := priority - 1
   117     self decreasePriorityBy: 1
   136 ! !
   118 !
   137 
   119 
   138 !PEGFsaTransition methodsFor:'printing'!
   120 decreasePriorityBy: value
   139 
   121     priority := priority - value
   140 characterSetAsString
       
   141     | stream |
       
   142     stream := WriteStream on: ''.
       
   143     self printCharacterSetOn: stream.
       
   144     ^ stream contents
       
   145 !
       
   146 
       
   147 printCharacterSetOn: stream
       
   148     self isEpsilon ifTrue: [ 
       
   149         stream nextPutAll: '<epsilon>'.
       
   150         ^ self
       
   151     ].
       
   152 
       
   153     stream nextPut: $[.
       
   154     32 to: 127 do: [ :index |
       
   155         (characterSet at: index) ifTrue: [ 
       
   156             stream nextPut: (Character codePoint: index)
       
   157         ]
       
   158     ].
       
   159     stream nextPut: $].
       
   160 !
       
   161 
       
   162 printOn: stream
       
   163     self printCharacterSetOn: stream.
       
   164     stream nextPutAll: ' ('.
       
   165     priority printOn: stream.
       
   166     stream nextPutAll: ')'.		
       
   167     stream nextPutAll: '-->'.
       
   168     destination printOn: stream.
       
   169     stream nextPutAll: '(ID: '.
       
   170     stream nextPutAll: self identityHash asString.
       
   171     stream nextPutAll: ')'.
       
   172 ! !
   122 ! !
   173 
   123 
   174 !PEGFsaTransition methodsFor:'set operations'!
   124 !PEGFsaTransition methodsFor:'set operations'!
   175 
   125 
   176 complement: transition
   126 complement: transition
   229 
   179 
   230 accepts: character
   180 accepts: character
   231     ^ characterSet at: character codePoint
   181     ^ characterSet at: character codePoint
   232 !
   182 !
   233 
   183 
       
   184 isCharacterTransition
       
   185     ^ false
       
   186 !
       
   187 
   234 isEpsilon
   188 isEpsilon
   235     ^ characterSet allSatisfy: [ :e | e not ]
   189     ^ self isEpsilonTransition
       
   190 !
       
   191 
       
   192 isEpsilonTransition
       
   193     ^ false
       
   194 !
       
   195 
       
   196 isPredicateTransition
       
   197     ^ false
   236 !
   198 !
   237 
   199 
   238 overlapsWith: transition
   200 overlapsWith: transition
   239     ^ (self intersection: transition) anySatisfy: [ :bool | bool ]
   201     ^ (self intersection: transition) anySatisfy: [ :bool | bool ]
   240 ! !
   202 ! !