PPSequenceParser.st
author sr
Thu, 05 Jul 2018 09:23:25 +0200
changeset 626 5159b1039a8f
parent 180 48a6757d5f01
child 377 6112a403a52d
child 644 0bf7cd45f7eb
permissions -rw-r--r--
order
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
90de244a7fa2 move to package
Claus Gittinger <cg@exept.de>
parents: 0
diff changeset
     1
"{ Package: 'stx:goodies/petitparser' }"
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
PPListParser subclass:#PPSequenceParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
	instanceVariableNames:''
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
	classVariableNames:''
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
	poolDictionaries:''
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
	category:'PetitParser-Parsers'
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
180
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    11
!PPSequenceParser methodsFor:'*petitanalyzer-private'!
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    12
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    13
cycleSet: aDictionary
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    14
	| firstSet |
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    15
	1 to: parsers size do: [ :index |
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    16
		firstSet := aDictionary at: (parsers at: index).
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    17
		(firstSet anySatisfy: [ :each | each isNullable ])
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    18
			ifFalse: [ ^ parsers copyFrom: 1 to: index ] ].
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    19
	^ parsers
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    20
!
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    21
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    22
firstSets: aFirstDictionary into: aSet
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    23
	| nullable |
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    24
	parsers do: [ :parser |
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    25
		nullable := false.
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    26
		(aFirstDictionary at: parser) do: [ :each |
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    27
			each isNullable
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    28
				ifTrue: [ nullable := true ]
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    29
				ifFalse: [ aSet add: each ] ].
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    30
		nullable
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    31
			ifFalse: [ ^ self ] ].
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    32
	aSet add: PPSentinel instance
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    33
!
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    34
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    35
followSets: aFollowDictionary firstSets: aFirstDictionary into: aSet
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    36
	parsers keysAndValuesDo: [ :index :parser |
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    37
		| followSet firstSet |
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    38
		followSet := aFollowDictionary at: parser.
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    39
		index = parsers size
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    40
			ifTrue: [ followSet addAll: aSet ]
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    41
			ifFalse: [
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    42
				(self class withAll: (parsers 
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    43
					copyFrom: index + 1 to: parsers size))
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    44
						firstSets: aFirstDictionary
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    45
						into: (firstSet := IdentitySet new).
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    46
				(firstSet anySatisfy: [ :each | each isNullable ])
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    47
					ifTrue: [ followSet addAll: aSet ].
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    48
				followSet addAll: (firstSet 
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    49
					reject: [ :each | each isNullable ]) ] ]
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    50
! !
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    51
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
!PPSequenceParser methodsFor:'operations'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
, aRule
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
	^ self copyWith: aRule
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
permutation: anArrayOfIntegers
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
	"Answer a permutation of the receivers sequence."
12
ace2bacc5f6a Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
    60
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
	anArrayOfIntegers do: [ :index |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
		(index isInteger and: [ index between: 1 and: parsers size ])
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
			ifFalse: [ self error: 'Invalid permutation index: ' , index printString ] ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
	^ self ==> [ :nodes | anArrayOfIntegers collect: [ :index | nodes at: index ] ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
180
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    67
!PPSequenceParser methodsFor:'operators-mapping'!
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    68
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    69
map: aBlock
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    70
	^ aBlock numArgs = self children size
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    71
		ifTrue: [ self ==> [ :nodes | aBlock valueWithArguments: nodes ] ]
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    72
		ifFalse: [ self error: aBlock numArgs asString , ' arguments expected.' ]
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    73
! !
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    74
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
!PPSequenceParser methodsFor:'parsing'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
parseOn: aStream
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
	"This is optimized code that avoids unnecessary block activations, do not change."
12
ace2bacc5f6a Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
    79
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
	| start elements element |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
	start := aStream position.
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
	elements := Array new: parsers size.
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
	1 to: parsers size do: [ :index |
12
ace2bacc5f6a Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
    84
		element := (parsers at: index) 
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
			parseOn: aStream.
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
		element isPetitFailure ifTrue: [
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
			aStream position: start.
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
			^ element ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
		elements at: index put: element ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
	^ elements
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
!PPSequenceParser class methodsFor:'documentation'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
12
ace2bacc5f6a Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
    95
version
180
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
    96
    ^ '$Header: /cvs/stx/stx/goodies/petitparser/PPSequenceParser.st,v 1.4 2014-03-04 14:33:25 cg Exp $'
12
ace2bacc5f6a Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
    97
!
ace2bacc5f6a Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
    98
ace2bacc5f6a Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
    99
version_CVS
180
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
   100
    ^ '$Header: /cvs/stx/stx/goodies/petitparser/PPSequenceParser.st,v 1.4 2014-03-04 14:33:25 cg Exp $'
12
ace2bacc5f6a Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   101
!
ace2bacc5f6a Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   102
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
version_SVN
180
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
   104
    ^ '$Id: PPSequenceParser.st,v 1.4 2014-03-04 14:33:25 cg Exp $'
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
! !
180
Claus Gittinger <cg@exept.de>
parents: 12
diff changeset
   106