compiler/PEGFsaState.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:#PEGFsaState
     5 Object subclass:#PEGFsaState
     6 	instanceVariableNames:'name retval priority transitions final multivalue'
     6 	instanceVariableNames:'name infos transitions'
     7 	classVariableNames:''
     7 	classVariableNames:''
     8 	poolDictionaries:''
     8 	poolDictionaries:''
     9 	category:'PetitCompiler-FSA'
     9 	category:'PetitCompiler-FSA'
    10 !
    10 !
    11 
    11 
       
    12 !PEGFsaState class methodsFor:'instance creation'!
       
    13 
       
    14 new
       
    15     "return an initialized instance"
       
    16 
       
    17     ^ self basicNew initialize.
       
    18 ! !
       
    19 
       
    20 !PEGFsaState class methodsFor:'as yet unclassified'!
       
    21 
       
    22 named: aName
       
    23     ^ self new
       
    24         name: aName;
       
    25         yourself
       
    26 ! !
       
    27 
    12 !PEGFsaState methodsFor:'accessing'!
    28 !PEGFsaState methodsFor:'accessing'!
    13 
    29 
    14 destination
    30 destination
    15     self assert: transitions size = 1.
    31     self assert: transitions size = 1.
    16     ^ transitions anyOne destination
    32     ^ transitions anyOne destination
    18 
    34 
    19 destinations
    35 destinations
    20     ^ (transitions collect: #destination) asIdentitySet
    36     ^ (transitions collect: #destination) asIdentitySet
    21 !
    37 !
    22 
    38 
       
    39 failure: boolean
       
    40     self info failure: boolean
       
    41 !
       
    42 
    23 final
    43 final
    24     ^ final
    44     ^ self info final
    25 !
    45 !
    26 
    46 
    27 final: anObject
    47 final: boolean
    28     final := anObject
    48     self info final: boolean
       
    49 !
       
    50 
       
    51 infoFor: retval
       
    52     ^ infos at: retval
       
    53 !
       
    54 
       
    55 infoFor: retval ifAbsent: block
       
    56     ^ infos at: retval ifAbsent: block
       
    57 !
       
    58 
       
    59 isFsaFailure
       
    60     ^ self isFinal and: [ self info isFsaFailure ]
    29 !
    61 !
    30 
    62 
    31 multivalue
    63 multivalue
    32     ^ multivalue
    64     <resource: #obsolete>
       
    65     ^ self isMultivalue
       
    66 
       
    67     "Modified: / 17-08-2015 / 12:03:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    33 !
    68 !
    34 
    69 
    35 multivalue: anObject
    70 multivalue: anObject
    36     multivalue := anObject
    71     self flag: 'JK: Obsolete?'.
       
    72     "multivalue := anObject"
       
    73 
       
    74     "Modified: / 17-08-2015 / 12:03:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    37 !
    75 !
    38 
    76 
    39 name
    77 name
    40     ^ name
    78     ^ name
    41 !
    79 !
    42 
    80 
    43 name: anObject
    81 name: anObject
    44     name := anObject asString
    82     name := anObject asString
    45 !
    83 !
    46 
    84 
    47 prefix
       
    48     ^ 'state'
       
    49 !
       
    50 
       
    51 priority
    85 priority
    52     ^ priority
    86     ^ self info priority
    53 !
    87 !
    54 
    88 
    55 priority: anObject
    89 priority: anObject
    56     priority := anObject
    90     self info priority: anObject
       
    91 !
       
    92 
       
    93 priorityFor: retval
       
    94     ^ (self infoFor: retval) priority
    57 !
    95 !
    58 
    96 
    59 priorityIfNone: value
    97 priorityIfNone: value
    60     ^ self hasPriority ifTrue: [ self priority ] ifFalse: [ value ]
    98     ^ self hasPriority ifTrue: [ self priority ] ifFalse: [ value ]
    61 !
    99 !
    62 
   100 
    63 retval
   101 retval
    64     ^ retval
   102     self assert: self isMultivalue not.
       
   103     ^ infos keys anyOne
    65 !
   104 !
    66 
   105 
    67 retval: anObject
   106 retval: anObject
    68     retval := anObject
   107     | info |
       
   108     info := self info.
       
   109     infos removeAll.
       
   110     infos at: anObject put: info.
    69 !
   111 !
    70 
   112 
    71 retvalAsCollection
   113 retvalAsCollection
    72     ^ self isMultivalue ifTrue: [ 
   114     ^ infos keys
    73         self retval
   115 !
    74     ] ifFalse: [ 
   116 
    75         Array with: self retval
   117 retvals
    76     ]
   118     ^ infos keys
    77 !
   119 !
    78 
   120 
    79 suffix
   121 retvalsAndInfosDo: twoArgBlock
    80     ^ ''
   122  	infos keysAndValuesDo: twoArgBlock
       
   123 !
       
   124 
       
   125 stateInfos
       
   126     ^ infos values
    81 !
   127 !
    82 
   128 
    83 transitions
   129 transitions
    84     ^ transitions
   130     ^ transitions
    85 ! !
   131 ! !
    86 
   132 
    87 !PEGFsaState methodsFor:'analysis'!
   133 !PEGFsaState methodsFor:'analysis'!
       
   134 
       
   135 collectNonEpsilonTransitionsOf: state to: collection
       
   136     state transitions do: [ :t | 
       
   137         t isEpsilon ifTrue: [ 
       
   138             self collectNonEpsilonTransitionsOf: t destination to: collection
       
   139         ] ifFalse: [ 
       
   140             collection add: t
       
   141         ]
       
   142     ].
       
   143     ^ collection
       
   144 !
       
   145 
       
   146 nonEpsilonTransitionPairs
       
   147     | size pairs collection |
       
   148     pairs := OrderedCollection new.
       
   149     
       
   150     collection := OrderedCollection new.
       
   151     self collectNonEpsilonTransitionsOf: self to: collection.
       
   152     size := collection size.
       
   153 
       
   154     1 to: (size - 1) do: [ :index1 |
       
   155         (index1 + 1 to: size) do: [ :index2 | 
       
   156             pairs add: (PEGFsaPair 
       
   157                 with: (collection at: index1)
       
   158                 with: (collection at: index2)).
       
   159         ]
       
   160     ].
       
   161     ^ pairs
       
   162 !
    88 
   163 
    89 reachableStates
   164 reachableStates
    90     | openSet |
   165     | openSet |
    91     openSet := IdentitySet new.
   166     openSet := IdentitySet new.
    92     self reachableStatesOpenSet: openSet.
   167     self reachableStatesOpenSet: openSet.
   107 !
   182 !
   108 
   183 
   109 transitionPairs
   184 transitionPairs
   110     | size pairs collection |
   185     | size pairs collection |
   111     size := transitions size.
   186     size := transitions size.
   112     pairs := OrderedCollection new: (size - 1) * size / 2.
   187     pairs := OrderedCollection new.
   113     
   188     
   114     collection := transitions asOrderedCollection.
   189     collection := transitions asOrderedCollection.
   115 
   190 
   116     1 to: (size - 1) do: [ :index1 |
   191     1 to: (size - 1) do: [ :index1 |
   117         (index1 + 1 to: size) do: [ :index2 | 
   192         (index1 + 1 to: size) do: [ :index2 | 
   118             pairs add: (PEGFsaPair new 
   193             pairs add: (PEGFsaPair 
   119                 first: (collection at: index1);
   194                 with: (collection at: index1)
   120                 second: (collection at: index2);
   195                 with: (collection at: index2)).
   121                 yourself).
       
   122         ]
   196         ]
   123     ].
   197     ].
   124     ^ pairs
   198     ^ pairs
   125 ! !
   199 ! !
   126 
   200 
   127 !PEGFsaState methodsFor:'comparing'!
   201 !PEGFsaState methodsFor:'comparing'!
   128 
   202 
   129 = anotherState
   203 = anotherState
   130     (self == anotherState) ifTrue: [ ^ true ].
   204     (self == anotherState) ifTrue: [ ^ true ].
   131     (self class == anotherState class) ifFalse: [ ^ true ].
   205     (self class == anotherState class) ifFalse: [ ^ false ].
   132     
   206     
   133     (name == anotherState name) ifFalse: [ ^ false ].
   207     (name == anotherState name) ifFalse: [ ^ false ].
   134     (priority == anotherState priority) ifFalse: [ ^ false ].
   208 
   135     (multivalue == anotherState multivalue) ifFalse: [ ^ false ].
   209     (self stateInfos size == anotherState stateInfos size) ifFalse: [ ^ false ].
   136     (retval = anotherState retval) ifFalse: [ ^ false ].
   210     self retvals do: [:retval |
   137     (final = anotherState final) ifFalse: [ ^ false ].
   211         ((self infoFor: retval) = (anotherState infoFor: retval  ifAbsent: [ ^ false ])) ifFalse: [ ^ false ]
       
   212     ].
   138 
   213 
   139     (transitions size = anotherState transitions size) ifFalse: [ ^ false ].
   214     (transitions size = anotherState transitions size) ifFalse: [ ^ false ].
   140     transitions do: [:t |
   215     transitions do: [:t |
   141         (anotherState transitions contains: [:at | at = t]) ifFalse: [ ^ false ].
   216         (anotherState transitions contains: [:at | at = t]) ifFalse: [ ^ false ].
   142     ].
   217     ].
   144     ^ true
   219     ^ true
   145 !
   220 !
   146 
   221 
   147 canBeIsomorphicTo: anotherState
   222 canBeIsomorphicTo: anotherState
   148     (name == anotherState name) ifFalse: [ ^ false ].
   223     (name == anotherState name) ifFalse: [ ^ false ].
   149     (priority == anotherState priority) ifFalse: [ ^ false ].
       
   150     (multivalue == anotherState multivalue) ifFalse: [ ^ false ].
       
   151     (final == anotherState final) ifFalse: [ ^ false ].
       
   152     (transitions size == anotherState transitions size) ifFalse: [ ^ false ].
   224     (transitions size == anotherState transitions size) ifFalse: [ ^ false ].
   153     (retval = anotherState retval) ifFalse: [ ^ false ].
   225 
       
   226     (self stateInfos size == anotherState stateInfos size) ifFalse: [ ^ false ].
       
   227     self retvals do: [:retval |
       
   228         ((self infoFor: retval) = (anotherState infoFor: retval ifAbsent: [ ^ false ])) ifFalse: [ ^ false ]
       
   229     ].
   154     
   230     
   155     ^ true
   231     ^ true
   156 !
   232 !
   157 
   233 
   158 equals: anotherState
   234 equals: anotherState
       
   235     self error: 'deprecated'.
       
   236     "
       
   237         JK: there is a bit mess between equals, isomorphic and =
       
   238         
       
   239         JK: I should clean it, but the idea behind is:
       
   240             - for minimization, I use equals 
       
   241             - for comparing, I use canBeIsomorphicTo: (because it can handle nested structures)
       
   242             - I have no idea, why I override =     O:)
       
   243     "
       
   244 
   159     (self == anotherState) ifTrue: [ ^ true ].
   245     (self == anotherState) ifTrue: [ ^ true ].
   160     (anotherState class == PEGFsaState) ifFalse: [ ^ false ].
   246     (self class == anotherState class) ifFalse: [ ^ false ].
   161     
   247     
   162     (retval = anotherState retval) ifFalse: [ ^ false ].
       
   163     (multivalue = anotherState multivalue) ifFalse: [ ^ false ].
       
   164     (self isFinal = anotherState isFinal) ifFalse: [ ^ false ].
   248     (self isFinal = anotherState isFinal) ifFalse: [ ^ false ].
   165 
   249 
   166     (self hasPriority and: [anotherState hasPriority]) ifTrue: [ 	
   250     (self stateInfos size == anotherState stateInfos size) ifFalse: [ ^ false ].
   167         (priority == anotherState priority) ifFalse: [ ^ false ].
   251     self retvals do: [:retval |
       
   252         ((self infoFor: retval) equals: (anotherState infoFor: retval ifAbsent: [ ^ false ])) ifFalse: [ ^ false ]
   168     ].
   253     ].
   169 
   254 
   170     (transitions size == anotherState transitions size) ifFalse: [ ^ false ].
   255     (transitions size == anotherState transitions size) ifFalse: [ ^ false ].
   171     anotherState transitions do: [ :t | 
   256     anotherState transitions do: [ :t | 
   172         (transitions contains: [ :e | e equals: t]) ifFalse: [ ^ false ]
   257         (transitions contains: [ :e | e equals: t]) ifFalse: [ ^ false ]
   174     
   259     
   175     ^ true
   260     ^ true
   176 !
   261 !
   177 
   262 
   178 hash
   263 hash
   179     ^ retval hash bitXor: (
   264     "JK: Size is not the best option here, but it one gets infinite loops otherwise"
   180         priority hash bitXor: (
   265     ^ infos hash bitXor: transitions size hash
   181         multivalue hash bitXor:
       
   182         "JK: Size is not the best option here, but it one gets infinite loops otherwise"
       
   183         transitions size hash)).
       
   184 !
   266 !
   185 
   267 
   186 isIsomorphicTo: anotherState resolvedSet: set
   268 isIsomorphicTo: anotherState resolvedSet: set
       
   269     self error: 'depracated?'.
   187     (self == anotherState) ifTrue: [ ^ true ].
   270     (self == anotherState) ifTrue: [ ^ true ].
   188     
   271     
   189     (name == anotherState name) ifFalse: [ ^ false ].
   272 "	(name == anotherState name) ifFalse: [ ^ false ].
   190     (priority == anotherState priority) ifFalse: [ ^ false ].
   273     (priority == anotherState priority) ifFalse: [ ^ false ].
   191     (multivalue == anotherState multivalue) ifFalse: [ ^ false ].
   274     (multivalue == anotherState isMultivalue) ifFalse: [ ^ false ].
   192     (retval = anotherState retval) ifFalse: [ ^ false ].
   275     (retval = anotherState retval) ifFalse: [ ^ false ].
   193     (final = anotherState final) ifFalse: [ ^ false ].
   276     (final = anotherState final) ifFalse: [ ^ false ].
   194 
   277 "
   195     (transitions size = anotherState transitions size) ifFalse: [ ^ false ].
   278     (transitions size = anotherState transitions size) ifFalse: [ ^ false ].
   196     transitions do: [:t |
   279     transitions do: [:t |
   197         (anotherState transitions contains: [:at | t isIsomorphicto: at]) ifFalse: [ ^ false ].
   280         (anotherState transitions contains: [:at | t isIsomorphicto: at]) ifFalse: [ ^ false ].
   198     ].
   281     ].
   199     
   282     
   201 ! !
   284 ! !
   202 
   285 
   203 !PEGFsaState methodsFor:'copying'!
   286 !PEGFsaState methodsFor:'copying'!
   204 
   287 
   205 postCopy
   288 postCopy
       
   289     | newInfos |
   206     super postCopy.
   290     super postCopy.
   207     transitions := (transitions collect: [ :t | t copy ]).
   291     transitions := (transitions collect: [ :t | t copy ]).
   208     retval := retval copy.
   292     
       
   293     newInfos := IdentityDictionary new.
       
   294     infos keysAndValuesDo: [ :key :value | 
       
   295         newInfos at: key put: value copy
       
   296     ].
       
   297 
       
   298     infos := newInfos.
   209 ! !
   299 ! !
   210 
   300 
   211 !PEGFsaState methodsFor:'gt'!
   301 !PEGFsaState methodsFor:'gt'!
   212 
   302 
   213 gtName
   303 gtName
   214     | gtName |
   304     |  gtStream |
   215     gtName := name.
   305     gtStream := '' writeStream.
   216 
   306     self printNameOn: gtStream.
       
   307     
   217     self hasPriority ifTrue: [ 
   308     self hasPriority ifTrue: [ 
   218         gtName := gtName asString, ',', self priority asString.
   309         self retvalsAndInfosDo: [ :retval :info | 
   219     ].
   310             gtStream nextPut: (Character codePoint: 13). 
   220 
   311             gtStream nextPutAll: retval asString.
   221     ^ gtName
   312             gtStream nextPutAll: '->'.
       
   313             info printOn: gtStream. 
       
   314         ].
       
   315     ].
       
   316 
       
   317     ^ gtStream contents trim
       
   318 ! !
       
   319 
       
   320 !PEGFsaState methodsFor:'ids'!
       
   321 
       
   322 defaultName
       
   323     ^ #state
       
   324 !
       
   325 
       
   326 hasName
       
   327     ^ name isNil not
       
   328 !
       
   329 
       
   330 prefix
       
   331     ^ nil
       
   332 !
       
   333 
       
   334 suffix
       
   335     ^ nil
       
   336 ! !
       
   337 
       
   338 !PEGFsaState methodsFor:'infos'!
       
   339 
       
   340 info
       
   341     self assert: infos size = 1.
       
   342     ^ infos anyOne
   222 ! !
   343 ! !
   223 
   344 
   224 !PEGFsaState methodsFor:'initialization'!
   345 !PEGFsaState methodsFor:'initialization'!
   225 
   346 
   226 initialize
   347 initialize
   227     super initialize.
   348     super initialize.
   228     
   349     
   229     transitions := OrderedCollection new.
   350     transitions := OrderedCollection new.
   230     multivalue := false.
   351 
       
   352     infos := IdentityDictionary new.
       
   353     infos at: nil put: PEGFsaStateInfo new.
   231 ! !
   354 ! !
   232 
   355 
   233 !PEGFsaState methodsFor:'modifications'!
   356 !PEGFsaState methodsFor:'modifications'!
       
   357 
       
   358 addInfo: info for: retval
       
   359     infos removeKey: nil ifAbsent: [ "not a big deal" ].
       
   360     infos at: retval put: info
       
   361 !
   234 
   362 
   235 addTransition: t
   363 addTransition: t
   236     self assert: (transitions identityIncludes: t) not.
   364     self assert: (transitions identityIncludes: t) not.
   237     transitions add: t
   365     transitions add: t
   238 !
   366 !
   239 
   367 
   240 decreasePriority
   368 decreasePriority
       
   369     self decreasePriorityBy: 1.
       
   370 !
       
   371 
       
   372 decreasePriorityBy: value
   241     (self isFinal and: [ self hasPriority not ]) ifTrue: [ 
   373     (self isFinal and: [ self hasPriority not ]) ifTrue: [ 
   242         priority := 0.
   374         self error: 'Final States Should have priority!!'
   243     ].
   375     ].
   244     priority isNil ifFalse: [ 
   376 
   245         priority := priority - 1
   377     self priority isNil ifFalse: [ 
       
   378         self priority: self priority - value
       
   379     ]
       
   380 !
       
   381 
       
   382 join: state
       
   383     ^ self join: state joinDictionary: Dictionary new
       
   384 !
       
   385 
       
   386 mergeInfo: state into: newState
       
   387     self info merge: state info into: newState info.
       
   388 !
       
   389 
       
   390 mergeTransitions
       
   391     | toRemove |
       
   392     toRemove := OrderedCollection new.
       
   393     self transitionPairs do:[ :pair | 
       
   394         (pair first destination = pair second destination) ifTrue: [ 
       
   395             (pair first isPredicateTransition not and: [pair second isPredicateTransition not]) ifTrue: [ 
       
   396                 pair first mergeWith: pair second.
       
   397                 toRemove add: pair second.
       
   398             ]
       
   399         ]
       
   400     ].
       
   401 
       
   402     toRemove do: [ :t |
       
   403         self removeTransition: t
   246     ]
   404     ]
   247 !
   405 !
   248 
   406 
   249 removeTransition: t
   407 removeTransition: t
   250     self assert: (transitions includes: t).
   408     self assert: (transitions includes: t).
   251     transitions remove: t
   409     transitions remove: t
       
   410 ! !
       
   411 
       
   412 !PEGFsaState methodsFor:'modifications - determinization'!
       
   413 
       
   414 determinize
       
   415     ^ PEGFsaAbstractDeterminizator new determinizeState: self
       
   416 !
       
   417 
       
   418 join: state joinDictionary: dictionary
       
   419     | pair newState |
       
   420     self error: 'deprecated'.
       
   421     pair := PEGFsaPair with: self with: state.
       
   422     (dictionary includesKey: pair) ifTrue: [ ^ dictionary at: pair ].
       
   423     
       
   424     newState := PEGFsaState new.
       
   425     
       
   426     dictionary at: pair put: newState.
       
   427 
       
   428     self joinRetval: state into: newState.
       
   429     self joinName: state into: newState.
       
   430     self joinTransitions: state into: newState.	
       
   431 
       
   432     newState determinize: dictionary.
       
   433     
       
   434     ^ dictionary at: pair put: newState
       
   435 !
       
   436 
       
   437 joinInfo: state into: newState
       
   438     self info join: state info into: newState info.
       
   439 !
       
   440 
       
   441 joinName: state into: newState
       
   442     newState name: self name asString, '_', state name asString.
       
   443 !
       
   444 
       
   445 joinRetval: state into: newState
       
   446     "Different retvals cannot merge their info"
       
   447     (self hasDifferentRetvalThan: state) ifTrue: [  
       
   448         newState addInfo: self info for: self retval.
       
   449         newState addInfo: state info for: state retval.
       
   450         ^ self
       
   451     ].
       
   452 
       
   453 
       
   454     (self hasHigherPriorityThan: state) ifTrue: [ 
       
   455         newState retval: self retval	
       
   456     ].
       
   457 
       
   458     (state hasHigherPriorityThan: self) ifTrue: [ 
       
   459         newState retval: state retval	
       
   460     ].
       
   461 
       
   462     (state priority == self priority) ifTrue: [ 
       
   463         self hasRetval ifTrue: [newState retval: self retval].
       
   464         state hasRetval ifTrue: [newState retval: state retval].
       
   465     ].
       
   466 
       
   467     self joinInfo: state into: newState.
       
   468 !
       
   469 
       
   470 joinTransitions: state into: newState.	
       
   471     newState isMultivalue ifTrue: [ 
       
   472         newState transitions addAll: (self transitions collect: #copy).
       
   473         newState transitions addAll: (state transitions collect: #copy).
       
   474         ^ self
       
   475     ].
       
   476     
       
   477     newState hasPriority ifFalse: [ 
       
   478         newState transitions addAll: (self transitions collect: #copy).
       
   479         newState transitions addAll: (state transitions collect: #copy).
       
   480         ^ self
       
   481     ].
       
   482 
       
   483     
       
   484     self assert: newState hasPriority.
       
   485     
       
   486     "This is a part when low priority branches are cut"
       
   487     (self priority == newState priority) ifTrue: [ 
       
   488         newState transitions addAll: (self transitions collect: #copy).
       
   489     ] ifFalse: [
       
   490         newState transitions addAll: (self transitions select: [ :t | t priority > newState priority ] thenCollect: #copy)
       
   491     ].
       
   492 
       
   493     (state priority == newState priority) ifTrue: [ 
       
   494         newState transitions addAll: (state transitions collect: #copy).
       
   495     ] ifFalse: [
       
   496         newState transitions addAll: (state transitions select: [ :t | t priority > newState priority ] thenCollect: #copy)
       
   497     ].
   252 ! !
   498 ! !
   253 
   499 
   254 !PEGFsaState methodsFor:'printing'!
   500 !PEGFsaState methodsFor:'printing'!
   255 
   501 
   256 printNameOn: aStream
   502 printNameOn: aStream
   263     super printOn: aStream.
   509     super printOn: aStream.
   264     aStream nextPut: $(.
   510     aStream nextPut: $(.
   265     self printNameOn: aStream.
   511     self printNameOn: aStream.
   266     aStream nextPut: Character space.
   512     aStream nextPut: Character space.
   267     aStream nextPutAll: self identityHash asString.
   513     aStream nextPutAll: self identityHash asString.
   268     self isFinal ifTrue: [ 
   514 
   269         aStream nextPutAll: ' FINAL'.
   515     self retvalsAndInfosDo: [ :retval :info | 
   270     ].
   516         retval printOn: aStream.
   271     aStream nextPut: (Character codePoint: 32).
   517         aStream nextPutAll: '->'.
   272     aStream nextPutAll: priority asString.
   518         info printOn: aStream.
       
   519         aStream nextPutAll: ';'.
       
   520     ].
       
   521 
   273     aStream nextPut: $)
   522     aStream nextPut: $)
   274 ! !
   523 ! !
   275 
   524 
   276 !PEGFsaState methodsFor:'testing'!
   525 !PEGFsaState methodsFor:'testing'!
   277 
   526 
   278 canHavePPCId
   527 canHavePPCId
   279     ^ true
   528     ^ true
   280 !
   529 !
   281 
   530 
       
   531 hasDifferentRetvalThan: anotherState
       
   532     "returns true only if both hav retval and both retvals are different"
       
   533     self hasRetval ifFalse: [ ^ false ].	
       
   534     anotherState hasRetval ifFalse: [ ^ false ].
       
   535 
       
   536     "`retval value` is called in order to obtain retval from FsaFailure (if any)"
       
   537     ^ (self retval value == anotherState retval value) not
       
   538 !
       
   539 
   282 hasEqualPriorityTo: state
   540 hasEqualPriorityTo: state
   283     "nil - nil"
   541     ^ self info hasEqualPriorityTo: state info
   284     (self hasPriority not and: [state hasPriority not]) ifTrue: [ ^ true ].
       
   285     
       
   286     "nil - priority"
       
   287     (self hasPriority) ifFalse: [ ^ false ].
       
   288     
       
   289     "priority - nil"
       
   290     state hasPriority ifFalse: [ ^ false ].
       
   291     
       
   292     "priority - priority"
       
   293     ^ self priority = state priority 
       
   294 !
   542 !
   295 
   543 
   296 hasHigherPriorityThan: state
   544 hasHigherPriorityThan: state
   297     "nil - nil"
   545     ^ self info hasHigherPriorityThan: state info
   298     (self hasPriority not and: [state hasPriority not]) ifTrue: [ ^ true ].
       
   299     
       
   300     "nil - priority"
       
   301     (self hasPriority) ifFalse: [ ^ false ].
       
   302     
       
   303     "priority - nil"
       
   304     state hasPriority ifFalse: [ ^ true ].
       
   305     
       
   306     "priority - priority"
       
   307     ^ self priority > state priority 
       
   308 !
   546 !
   309 
   547 
   310 hasPriority
   548 hasPriority
   311     ^ priority isNil not
   549     ^ self stateInfos anySatisfy: [ :info | info hasPriority ]
       
   550 !
       
   551 
       
   552 hasRetval
       
   553     ^ self retval isNil not
       
   554 !
       
   555 
       
   556 hasZeroPriorityOnly
       
   557     ^ self stateInfos allSatisfy: [ :si | si hasPriority not or: [ si priority == 0 ] ].
   312 !
   558 !
   313 
   559 
   314 isFailure
   560 isFailure
       
   561     self error: 'Obsolete?'.
       
   562     "
   315     ^ self isFinal and: [ retval class == PEGFsaFailure ]
   563     ^ self isFinal and: [ retval class == PEGFsaFailure ]
       
   564     "
       
   565 
       
   566     "Modified: / 17-08-2015 / 12:01:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   316 !
   567 !
   317 
   568 
   318 isFinal
   569 isFinal
   319     final isNil ifTrue: [ ^ false ].
   570     ^ self stateInfos anySatisfy: [ :info | info isFinal ].
   320     
   571 !
   321     final ifTrue: [
   572 
   322 "		self assert: self hasPriority. "
   573 isMultivalue
   323         ^ true
   574     ^ infos size > 1
   324     ].
   575 !
   325 
   576 
       
   577 isStub
   326     ^ false
   578     ^ false
   327 !
       
   328 
       
   329 isMultivalue
       
   330     ^ multivalue
       
   331 ! !
   579 ! !
   332 
   580 
   333 !PEGFsaState methodsFor:'transformation'!
   581 !PEGFsaState methodsFor:'transformation'!
   334 
       
   335 determinize
       
   336     ^ self determinize: Dictionary new.
       
   337 !
       
   338 
       
   339 determinize: dictionary
       
   340     self transitionPairs do: [ :pair |
       
   341         self assert: (pair first destination = pair second destination) not.
       
   342         (pair first overlapsWith: pair second) ifTrue: [ 
       
   343             self determinizeOverlap: pair first second: pair second joinDictionary: dictionary
       
   344         ]
       
   345     ].
       
   346 !
       
   347 
       
   348 determinizeOverlap: t1 second: t2 joinDictionary: dictionary
       
   349     | pair t1Prime t2Prime tIntersection |
       
   350     pair := PEGFsaPair with: t1 with: t2.
       
   351 
       
   352     (dictionary includes: pair) ifTrue: [ self error: 'should not happen'.].
       
   353     dictionary at: pair put: nil.
       
   354     
       
   355     tIntersection := t1 join: t2 joinDictionary: dictionary.
       
   356     t1Prime := PEGFsaTransition new
       
   357                     destination: t1 destination;
       
   358                     characterSet: (t1 complement: t2);
       
   359                     yourself.
       
   360     t2Prime := PEGFsaTransition new
       
   361                     destination: t2 destination;
       
   362                     characterSet: (t2 complement: t1);
       
   363                     yourself.					
       
   364                                     
       
   365                                 
       
   366     self removeTransition: t1.
       
   367     self removeTransition: t2.
       
   368     
       
   369     tIntersection isEpsilon ifFalse: [ self addTransition: tIntersection  ].
       
   370     t1Prime isEpsilon ifFalse: [ self addTransition: t1Prime ].
       
   371     t2Prime isEpsilon ifFalse: [ self addTransition: t2Prime ].
       
   372     
       
   373     dictionary at: pair put: (Array 
       
   374                                         with: tIntersection 
       
   375                                         with: t1Prime
       
   376                                         with: t2Prime
       
   377                                     )
       
   378 !
       
   379 
       
   380 join: state
       
   381     ^ self join: state joinDictionary: Dictionary new
       
   382 !
       
   383 
       
   384 join: state joinDictionary: dictionary
       
   385     | pair newState |
       
   386     pair := PEGFsaPair with: self with: state.
       
   387     (dictionary includesKey: pair) ifTrue: [ ^ dictionary at: pair ].
       
   388     
       
   389     newState := PEGFsaState new.
       
   390     
       
   391     dictionary at: pair put: newState.
       
   392     
       
   393     self joinFinal: state newState: newState.
       
   394     self joinPriority: state newState: newState.
       
   395     self joinRetval: state newState: newState.
       
   396     self joinName: state newState: newState.
       
   397     
       
   398     newState transitions addAll: (self transitions collect: #copy).
       
   399     newState transitions addAll: (state transitions collect: #copy).
       
   400     newState determinize: dictionary.
       
   401     
       
   402     ^ dictionary at: pair put: newState
       
   403 !
       
   404 
   582 
   405 joinFinal: state newState: newState
   583 joinFinal: state newState: newState
   406     (self hasEqualPriorityTo: state) ifTrue: [ 
   584     (self hasEqualPriorityTo: state) ifTrue: [ 
   407         ^ newState final: (self isFinal or: [ state isFinal ]).
   585         ^ newState final: (self isFinal or: [ state isFinal ]).
   408     ].
   586     ].