islands/PPIsland.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sun, 26 Oct 2014 01:03:31 +0000
changeset 391 553a5456963b
parent 387 e2b2ccaa4de6
child 454 a9cd5ea7cc36
permissions -rw-r--r--
Ported PetitCompiler-(Tests). Name: PetitCompiler-JanKurs.41 Author: JanKurs Time: 25-10-2014, 03:30:28 AM UUID: 105186d1-1187-4ca6-8d66-3d2d47def4d3 Repository: http://smalltalkhub.com/mc/JanKurs/PetitParser/main Name: PetitCompiler-Tests-JanKurs.4 Author: JanKurs Time: 25-10-2014, 03:30:58 AM UUID: 3e798fad-d5f6-4881-a583-f0bbffe27869 Repository: http://smalltalkhub.com/mc/JanKurs/PetitParser/main In addition, fixed some problems to make it compilable under Smalltalk/X: * Fixed PPCTokenNode>>initialize - there's no children instvar, it's initialization removed. * Fixed PPCContextMemento>>propertyAt:ifAbsent: - removed return-in-return, not compilable under Smalltalk/X (C issues) * Fixed PPCContextMemento>>hash - there's no stream instvar, access to it removed. * Fixed PPCAbstractCharacterNode>>compileWith:effect:id: - removed dot after method selector (stc does not like it)

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

PPParser subclass:#PPIsland
	instanceVariableNames:'island afterWaterParser beforeWaterParser context
		afterWaterDelegate beforeWaterDelegate water'
	classVariableNames:''
	poolDictionaries:''
	category:'PetitIslands-Parsers'
!

PPIsland comment:'A PPIsland allows for imprecise parsing. One can create it on a parser p by calling: ''p island'' E.g.:

p := x, a island, y              accepts following inputs:
x.....a.....b 
xab

yet fails on:
x....a....c
xb
xac
x..b....a....b

The input represented by dots is called water and water can appear before and after the island. Use it, if you don''t want to define all the grammar rules and you want to skip something.

I am still an experiment, but if you know how to improve me, please contact Jan Kurs at: kurs@iam.unibe.ch

Instance Variables
	afterWaterParser:		<Object>
	awp:		<Object>
	beforeWaterParser:		<Object>
	bwp:		<Object>
	context:		<Object>
	island:		<Object>

afterWaterParser
	- xxxxx

awp
	- xxxxx

beforeWaterParser
	- xxxxx

bwp
	- xxxxx

context
	- xxxxx

island
	- xxxxx
'
!

!PPIsland methodsFor:'accessing'!

children

	^ Array with: water with: island with: water
!

followSet: aPPContext

	^ aPPContext root followSets at: self.	
!

initialize 
	super initialize.
	water := #any asParser name: 'water'; yourself.
!

island

	^ island
!

island: anObject
	island := anObject.
!

nextSet: aPPContext

	^ aPPContext root nextSets at: self.
!

replace: parser with: anotherParser 
	super replace: parser with: anotherParser.
	
	(water == parser) ifTrue: [ water := anotherParser ].
	(island == parser) ifTrue: [ island := anotherParser ].
!

water
	^ water
!

water: aPPParser
	water := aPPParser
! !

!PPIsland methodsFor:'memoization'!

memoized 
	^ PPMemoizingIsland new
		island: self island;
		water: water;
		yourself
!

reset: aPPContext
	context := aPPContext.
	beforeWaterParser := nil.
	afterWaterParser := nil.
! !

!PPIsland methodsFor:'parsing'!

afterWaterParser: aPPContext
	context == aPPContext ifFalse: [ self reset: aPPContext ].

	afterWaterParser ifNil: [
		afterWaterParser := self createAfterWaterParser: aPPContext.
	].
	^ afterWaterParser
!

beforeWaterParser: aPPContext
	context == aPPContext ifFalse: [ self reset: aPPContext ].

	beforeWaterParser ifNil: [
		beforeWaterParser := self createBeforeWaterParser: aPPContext.
	].
	^ beforeWaterParser
!

createAfterWaterParser: aPPContext
	|  nextSet  p |

	nextSet := Set new.
	nextSet addAll: (self nextSet: aPPContext).
	nextSet add: PPInputEnds new.
	
	nextSet := nextSet collect: [ :e | PPNonEmptyParser on: e ].
	
	p := (PPChoiceParser withAll: nextSet) not.
	^ PPWater on: p waterToken: water
!

createBeforeWaterParser: aPPContext
	| nextSet p |
	nextSet := Set new.
	nextSet addAll: (self nextSet: aPPContext).
	nextSet add: PPInputEnds new.
	
	nextSet := nextSet collect: [:e | PPNonEmptyParser on: e].
	
	p := (PPChoiceParser withAll: nextSet) not, (PPNonEmptyParser on: island) not.
	^ PPWater on: p waterToken: water.
!

exampleOn: aStream
	aStream nextPutAll: '~~~~ '.
	island exampleOn: aStream .
	aStream nextPutAll:  ' ~~~~'.
!

parseAfterWater: aPPContext
	^ (self afterWaterParser: aPPContext) parseOn: aPPContext .
!

parseBeforeWater: aPPContext
	^ (self beforeWaterParser: aPPContext) parseOn: aPPContext.
!

parseOn: aPPContext 
	|  bwr awr result retval memento |

	memento := aPPContext remember.
	"Halt ifShiftPressed."
	
	bwr := self parseBeforeWater: aPPContext.
	bwr isPetitFailure ifTrue: 
	[
		self error: 'IMO should never happen'.
	].

	"JK: HACK ALERT, FIX!!"
	(aPPContext waterPosition == aPPContext position) ifTrue:[
		result := (PPNonEmptyParser on: island) parseOn: aPPContext.
	] ifFalse: [
		result := island parseOn: aPPContext.
	].
	

	result isPetitFailure ifTrue: [ 
		retval := PPFailure message: 'Island not found between ', memento position asString, ' and ', aPPContext position asString context: aPPContext.
		aPPContext restore: memento.
		^ retval
	].


	awr := self parseAfterWater: aPPContext.	
	awr isPetitFailure ifTrue: 
	[
		retval := PPFailure message: 'IMO should not happen :(' context: aPPContext.
		aPPContext restore: memento.
		^ retval.
	].

	retval := OrderedCollection with: bwr with: result with: awr.
	^ retval


!

waterToken
	| waterObjects |
	self halt: 'deprecated?'.
	waterObjects := self globalAt: #waterObjects ifAbsent: [ OrderedCollection new ].
	waterObjects add: #any asParser.
	^ PPChoiceParser withAll: waterObjects.
! !

!PPIsland methodsFor:'queries'!

acceptsEpsilon
	"JK: Hack alert?"
	"Let us suppose island is always nullable, it helps to sequences of islands"
	^ true
	"^ island isNullableOpenSet: (IdentitySet with: self)"
!

acceptsEpsilonOpenSet: set
	"JK: Hack alert?"
	^ true
"	^ island isNullableOpenSet: set"
!

name
	^ super name ifNil: [ 'an island '].
! !