|
1 "{ Package: 'stx:goodies/petitparser/compiler' }" |
|
2 |
|
3 "{ NameSpace: Smalltalk }" |
|
4 |
|
5 PEGFsaAbstractDeterminizator subclass:#PEGFsaSequenceDeterminizator |
|
6 instanceVariableNames:'' |
|
7 classVariableNames:'' |
|
8 poolDictionaries:'' |
|
9 category:'PetitCompiler-FSA' |
|
10 ! |
|
11 |
|
12 !PEGFsaSequenceDeterminizator methodsFor:'determinization'! |
|
13 |
|
14 determinize |
|
15 super determinize. |
|
16 |
|
17 self markFailures. |
|
18 fsa removePriorities. |
|
19 ! |
|
20 |
|
21 markFailures |
|
22 fsa finalStates do: [ :fs | |
|
23 | priority | |
|
24 priority := fs priority. |
|
25 fs reachableStates do: [ :rs | |
|
26 (rs hasPriority and: [ (rs priority > fs priority) and: [ rs isFinal not ] ]) ifTrue: [ |
|
27 rs failure: true. |
|
28 rs final: true. |
|
29 ] |
|
30 ] |
|
31 ] |
|
32 ! ! |
|
33 |
|
34 !PEGFsaSequenceDeterminizator methodsFor:'joining'! |
|
35 |
|
36 joinInfo: info with: anotherInfo into: newInfo |
|
37 (info hasEqualPriorityTo: anotherInfo) ifTrue: [ |
|
38 newInfo final: (info isFinal or: [ anotherInfo isFinal ]). |
|
39 newInfo priority: info priority. |
|
40 ^ self |
|
41 ]. |
|
42 |
|
43 (info hasHigherPriorityThan: anotherInfo) ifTrue: [ |
|
44 newInfo priority: info priority. |
|
45 newInfo failure: info isFsaFailure. |
|
46 newInfo final: info isFinal. |
|
47 ^ self |
|
48 ]. |
|
49 |
|
50 newInfo priority: anotherInfo priority. |
|
51 newInfo failure: anotherInfo isFsaFailure. |
|
52 newInfo final: anotherInfo isFinal. |
|
53 ! |
|
54 |
|
55 joinRetval: state with: anotherState into: newState |
|
56 "Different retvals cannot merge their info" |
|
57 self assert: (state hasDifferentRetvalThan: anotherState) not. |
|
58 self assert: state retval == anotherState retval. |
|
59 |
|
60 newState retval: state retval. |
|
61 ! |
|
62 |
|
63 joinState: state with: anotherState |
|
64 self assert: state isMultivalue not. |
|
65 self assert: anotherState isMultivalue not. |
|
66 |
|
67 ^ super joinState: state with: anotherState |
|
68 ! |
|
69 |
|
70 joinTransitions: state with: anotherState into: newState |
|
71 self assert: newState isMultivalue not. |
|
72 |
|
73 newState hasPriority ifFalse: [ |
|
74 newState transitions addAll: (state transitions collect: #copy). |
|
75 newState transitions addAll: (anotherState transitions collect: #copy). |
|
76 ^ self |
|
77 ]. |
|
78 |
|
79 self assert: newState hasPriority. |
|
80 "This is a part when low priority branches are cut-out" |
|
81 (state priority == newState priority) ifTrue: [ |
|
82 newState transitions addAll: (state transitions collect: #copy). |
|
83 ] ifFalse: [ |
|
84 newState transitions addAll: (state transitions select: [ :t | t priority > newState priority ] thenCollect: #copy) |
|
85 ]. |
|
86 |
|
87 (anotherState priority == newState priority) ifTrue: [ |
|
88 newState transitions addAll: (anotherState transitions collect: #copy). |
|
89 ] ifFalse: [ |
|
90 newState transitions addAll: (anotherState transitions select: [ :t | t priority > newState priority ] thenCollect: #copy) |
|
91 ]. |
|
92 |
|
93 newState mergeTransitions. |
|
94 ! ! |
|
95 |