islands/extensions.st
changeset 387 e2b2ccaa4de6
equal deleted inserted replaced
386:a409905f7f2d 387:e2b2ccaa4de6
       
     1 "{ Package: 'stx:goodies/petitparser/islands' }"!
       
     2 
       
     3 !PPChoiceParser methodsFor:'*petitislands'!
       
     4 
       
     5 acceptsEpsilonOpenSet: set
       
     6 	set add: self.
       
     7 	^ self children anySatisfy: [:e | e acceptsEpsilonOpenSet: set ].
       
     8 ! !
       
     9 
       
    10 !PPContext methodsFor:'*petitislands'!
       
    11 
       
    12 waterPosition
       
    13 	^ self globalAt:  #waterPosition ifAbsentPut: [ nil ]
       
    14 ! !
       
    15 
       
    16 !PPContext methodsFor:'*petitislands'!
       
    17 
       
    18 waterPosition: position
       
    19 	^ self globalAt:  #waterPosition put: position
       
    20 ! !
       
    21 
       
    22 !PPDelegateParser methodsFor:'*petitislands'!
       
    23 
       
    24 acceptsEpsilon
       
    25 	^ parser acceptsEpsilonOpenSet: (IdentitySet with: self).
       
    26 ! !
       
    27 
       
    28 !PPDelegateParser methodsFor:'*petitislands'!
       
    29 
       
    30 acceptsEpsilonOpenSet: set
       
    31 	(set includes: parser) ifFalse: [ 
       
    32 		set add: parser.
       
    33 		^ parser acceptsEpsilonOpenSet: set 
       
    34 	].
       
    35 	^ false
       
    36 ! !
       
    37 
       
    38 !PPEpsilonParser methodsFor:'*petitislands'!
       
    39 
       
    40 acceptsEpsilon
       
    41 	^ true
       
    42 ! !
       
    43 
       
    44 !PPListParser methodsFor:'*petitislands'!
       
    45 
       
    46 acceptsEpsilon
       
    47 	^ self acceptsEpsilonOpenSet: IdentitySet new.
       
    48 ! !
       
    49 
       
    50 !PPLiteralParser methodsFor:'*petitislands'!
       
    51 
       
    52 acceptsEpsilon
       
    53 	^ false
       
    54 ! !
       
    55 
       
    56 !PPOptionalParser methodsFor:'*petitislands'!
       
    57 
       
    58 acceptsEpsilon
       
    59 	^ true
       
    60 ! !
       
    61 
       
    62 !PPOptionalParser methodsFor:'*petitislands'!
       
    63 
       
    64 acceptsEpsilonOpenSet: set
       
    65 	^ true
       
    66 ! !
       
    67 
       
    68 !PPParser methodsFor:'*petitislands'!
       
    69 
       
    70 acceptsEpsilon
       
    71 	"return true, if parser can accept epsilon without failure"
       
    72 	^ self subclassResponsibility
       
    73 ! !
       
    74 
       
    75 !PPParser methodsFor:'*petitislands'!
       
    76 
       
    77 acceptsEpsilonOpenSet: set
       
    78 	"private helper for acceptsEmpsilon that makes sure to avoid cycles (using open set)"
       
    79 	self children isEmpty ifTrue: [ ^ self acceptsEpsilon ].
       
    80 	
       
    81 	self shouldBeImplemented .
       
    82 ! !
       
    83 
       
    84 !PPParser methodsFor:'*petitislands'!
       
    85 
       
    86 island
       
    87 	^ PPIsland new
       
    88 		island: self;
       
    89 		yourself;
       
    90 		memoized
       
    91 ! !
       
    92 
       
    93 !PPParser methodsFor:'*petitislands'!
       
    94 
       
    95 island: water
       
    96 	^ PPIsland new
       
    97 		island: self;
       
    98 		water: water;
       
    99 		yourself;
       
   100 		memoized
       
   101 ! !
       
   102 
       
   103 !PPParser methodsFor:'*petitislands'!
       
   104 
       
   105 nextSets
       
   106 	| nextSets |
       
   107 	
       
   108 	nextSets := IdentityDictionary new.
       
   109 	self allParsersDo: [ :each | nextSets at: each put: IdentitySet new ].
       
   110 	
       
   111 	(nextSets at: self) add: PPSentinel instance.
       
   112 	
       
   113 	[ 	| changed |
       
   114 		changed := false.
       
   115 	
       
   116 		nextSets keysAndValuesDo: [:parser :next |
       
   117 			changed := (parser 
       
   118 				nextSets: nextSets
       
   119 				into: next) or: [ changed ].
       
   120 		].
       
   121 		changed ] whileTrue.
       
   122 	
       
   123 	^ nextSets
       
   124 ! !
       
   125 
       
   126 !PPParser methodsFor:'*petitislands'!
       
   127 
       
   128 nextSets: aNextDictionary into: aSet
       
   129 	"return true/false, if something has changed or not...."
       
   130 	| childSet change tally |
       
   131 	
       
   132 	change := false.
       
   133 
       
   134 	self children do: [:each | 
       
   135 		childSet := aNextDictionary at: each.
       
   136 		tally := childSet size.
       
   137 		childSet addAll: aSet.
       
   138 		change := change or: [ tally ~= childSet size ].
       
   139 	].
       
   140 
       
   141 	^ change
       
   142 	
       
   143 ! !
       
   144 
       
   145 !PPPredicateParser methodsFor:'*petitislands'!
       
   146 
       
   147 acceptsEpsilon
       
   148 	^ false
       
   149 ! !
       
   150 
       
   151 !PPRepeatingParser methodsFor:'*petitislands'!
       
   152 
       
   153 nextSets: aNextDictionary into: aSet
       
   154 	| tally childSet change |
       
   155 
       
   156 	change := super nextSets: aNextDictionary  into: aSet.
       
   157 
       
   158 	childSet := aNextDictionary at: parser.
       
   159 	tally := aSet size.
       
   160 	childSet add: parser.
       
   161 	^ change or: [ tally ~= aSet size ].
       
   162 ! !
       
   163 
       
   164 !PPSequenceParser methodsFor:'*petitislands'!
       
   165 
       
   166 acceptsEpsilonOpenSet: set
       
   167 	set add: self.
       
   168 	^ self children allSatisfy: [:e | e acceptsEpsilonOpenSet: set ]
       
   169 ! !
       
   170 
       
   171 !PPSequenceParser methodsFor:'*petitislands'!
       
   172 
       
   173 nextSets: aNextDictionary into: aSet
       
   174 	
       
   175 	| nextSet eachNextSet change tally |
       
   176 	nextSet := aSet copy.
       
   177 	
       
   178 	change := false.
       
   179 	
       
   180 	self children reverseDo: [:each |
       
   181 		eachNextSet := aNextDictionary at: each.	
       
   182 		tally := eachNextSet size.
       
   183 		eachNextSet addAll: nextSet.
       
   184 		change := change or: [ tally ~= eachNextSet size ].
       
   185 		
       
   186 		each acceptsEpsilon ifTrue: [
       
   187 			nextSet add: each.
       
   188 		] ifFalse: [
       
   189 			nextSet := IdentitySet with: each.
       
   190 		].
       
   191 	].
       
   192 
       
   193 	^ change
       
   194 ! !
       
   195 
       
   196 !stx_goodies_petitparser_islands class methodsFor:'documentation'!
       
   197 
       
   198 extensionsVersion_HG
       
   199 
       
   200     ^ '$Changeset: <not expanded> $'
       
   201 ! !