PPSequenceParser.st
author Stefan Vogel <sv@exept.de>
Thu, 07 Mar 2019 17:13:31 +0100
changeset 634 e63445bb2c9a
parent 180 48a6757d5f01
child 377 6112a403a52d
child 644 0bf7cd45f7eb
permissions -rw-r--r--
#value: moved to libbasic

"{ Package: 'stx:goodies/petitparser' }"

PPListParser subclass:#PPSequenceParser
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'PetitParser-Parsers'
!


!PPSequenceParser methodsFor:'*petitanalyzer-private'!

cycleSet: aDictionary
	| firstSet |
	1 to: parsers size do: [ :index |
		firstSet := aDictionary at: (parsers at: index).
		(firstSet anySatisfy: [ :each | each isNullable ])
			ifFalse: [ ^ parsers copyFrom: 1 to: index ] ].
	^ parsers
!

firstSets: aFirstDictionary into: aSet
	| nullable |
	parsers do: [ :parser |
		nullable := false.
		(aFirstDictionary at: parser) do: [ :each |
			each isNullable
				ifTrue: [ nullable := true ]
				ifFalse: [ aSet add: each ] ].
		nullable
			ifFalse: [ ^ self ] ].
	aSet add: PPSentinel instance
!

followSets: aFollowDictionary firstSets: aFirstDictionary into: aSet
	parsers keysAndValuesDo: [ :index :parser |
		| followSet firstSet |
		followSet := aFollowDictionary at: parser.
		index = parsers size
			ifTrue: [ followSet addAll: aSet ]
			ifFalse: [
				(self class withAll: (parsers 
					copyFrom: index + 1 to: parsers size))
						firstSets: aFirstDictionary
						into: (firstSet := IdentitySet new).
				(firstSet anySatisfy: [ :each | each isNullable ])
					ifTrue: [ followSet addAll: aSet ].
				followSet addAll: (firstSet 
					reject: [ :each | each isNullable ]) ] ]
! !

!PPSequenceParser methodsFor:'operations'!

, aRule
	^ self copyWith: aRule
!

permutation: anArrayOfIntegers
	"Answer a permutation of the receivers sequence."
	
	anArrayOfIntegers do: [ :index |
		(index isInteger and: [ index between: 1 and: parsers size ])
			ifFalse: [ self error: 'Invalid permutation index: ' , index printString ] ].
	^ self ==> [ :nodes | anArrayOfIntegers collect: [ :index | nodes at: index ] ]
! !

!PPSequenceParser methodsFor:'operators-mapping'!

map: aBlock
	^ aBlock numArgs = self children size
		ifTrue: [ self ==> [ :nodes | aBlock valueWithArguments: nodes ] ]
		ifFalse: [ self error: aBlock numArgs asString , ' arguments expected.' ]
! !

!PPSequenceParser methodsFor:'parsing'!

parseOn: aStream
	"This is optimized code that avoids unnecessary block activations, do not change."
	
	| start elements element |
	start := aStream position.
	elements := Array new: parsers size.
	1 to: parsers size do: [ :index |
		element := (parsers at: index) 
			parseOn: aStream.
		element isPetitFailure ifTrue: [
			aStream position: start.
			^ element ].
		elements at: index put: element ].
	^ elements
! !

!PPSequenceParser class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/goodies/petitparser/PPSequenceParser.st,v 1.4 2014-03-04 14:33:25 cg Exp $'
!

version_CVS
    ^ '$Header: /cvs/stx/stx/goodies/petitparser/PPSequenceParser.st,v 1.4 2014-03-04 14:33:25 cg Exp $'
!

version_SVN
    ^ '$Id: PPSequenceParser.st,v 1.4 2014-03-04 14:33:25 cg Exp $'
! !