islands/XmlFeedParser.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 08 Oct 2014 21:56:20 +0100
changeset 388 74c9c229033b
parent 387 e2b2ccaa4de6
child 454 a9cd5ea7cc36
permissions -rw-r--r--
Workaround to fix PPComposedTest>>testLeftRecursion - make PPStream>>size returning size of the input. For whatever reason, in Smalltalk/X ReadStream>>size returns size remaning data, not the size of a whole stream. This is a bug and should be fixed in Smalltalk/X libbasic. Meanwhile, override PPStream>>size to return proper value. In Pharo, the method is not needed as the inherited method is exactly the same.

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

PPCompositeParser subclass:#XmlFeedParser
	instanceVariableNames:'item openItem itemContent closeItem openShoplist shoplistContent
		closeShoplist shoplist simpleElement stringValue'
	classVariableNames:''
	poolDictionaries:''
	category:'PetitIslands-Examples'
!

XmlFeedParser comment:'A XmlFeedParser is Parser of a fake shop feed. Shop feed is a XML file with information about shop products. Shop feed can be malformed (for any reason) the parser will recover from malformed item and will continue on the next valid item.

Instance Variables
	closeItem:		<Object>
	closeName:		<Object>
	closeShoplist:		<Object>
	item:		<Object>
	itemContent:		<Object>
	name:		<Object>
	nameContent:		<Object>
	openItem:		<Object>
	openName:		<Object>
	openShoplist:		<Object>
	shoplist:		<Object>
	shoplistContent:		<Object>
	simpleElement:		<Object>
	stringValue:		<Object>

closeItem
	- xxxxx

closeName
	- xxxxx

closeShoplist
	- xxxxx

item
	- xxxxx

itemContent
	- xxxxx

name
	- xxxxx

nameContent
	- xxxxx

openItem
	- xxxxx

openName
	- xxxxx

openShoplist
	- xxxxx

shoplist
	- xxxxx

shoplistContent
	- xxxxx

simpleElement
	- xxxxx

stringValue
	- xxxxx
'
!

!XmlFeedParser methodsFor:'grammar'!

item
	^ (openItem, itemContent trim, closeItem) trim ==> #second
!

itemContent
	^ (simpleElement trim star) ==> self elementsToDictionaryBlock
!

shoplist
	^ (openShoplist, shoplistContent, closeShoplist) trim ==> #second
!

shoplistContent
	^ (item island ==> #second) star
!

start
	^ shoplist
!

stringValue
	^ (#letter asParser / #digit asParser) star flatten trim
! !

!XmlFeedParser methodsFor:'tags'!

closeItem
	^ '</' asParser, 'item' asParser trim, '>' asParser
!

closeShoplist
	^ '</' asParser, 'shoplist' asParser trim, '>' asParser
!

openItem
	^ '<' asParser, 'item' asParser trim, '>' asParser
!

openShoplist
	^ '<' asParser, 'shoplist' asParser trim, '>' asParser
! !

!XmlFeedParser methodsFor:'xmlSupport'!

elementsToDictionaryBlock
 	^ [ :elements | | d | 
		d := Dictionary new.
		elements do: [ :e | d at: e first asSymbol put: e second ].
		d
	]
!

simpleElement
	^ (
		(('<' asParser, stringValue trim, '>' asParser) ==> #second),
		stringValue,
		(('</' asParser, stringValue trim, '>' asParser) ==> #second)
	) ==> [ :elements |
		(elements first = elements third) 
		ifTrue: [ Array with: elements first with: elements second ]
		ifFalse: [ PPFailure message: 'malformed element' ]
 	]
! !