compiler/PPCompiledParser.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sun, 26 Oct 2014 01:03:31 +0000
changeset 391 553a5456963b
child 392 9b297f0d949c
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/compiler' }"

PPParser subclass:#PPCompiledParser
	instanceVariableNames:'startSymbol context failure error'
	classVariableNames:''
	poolDictionaries:''
	category:'PetitCompiler-Core'
!

PPCompiledParser class instanceVariableNames:'parsers constants referringParser'

"
 No other class instance variables are inherited by this class.
"
!

PPCompiledParser comment:''
!

!PPCompiledParser class methodsFor:'as yet unclassified'!

addConstant: value as: id
	self constants at: id ifPresent: [ 
		((self constants at: id) = value) ifFalse: [self error: 'ooups']].	
	
	self constants at: id put: value.
!

addParser: aPPParser as: id
	
	"(self parsers includesKey: id) ifTrue: [self error: 'Ooups' ]."
	self parsers at: id put: aPPParser.
!

constants
	constants ifNil: [ constants := IdentityDictionary new ].
	^ constants
!

parse: input
	^ self new parse: input
!

parsers
	parsers ifNil: [ parsers := IdentityDictionary new ].
	^ parsers
!

referringParser
	^ referringParser 
!

referringParser: aPPParser
	referringParser := aPPParser
! !

!PPCompiledParser methodsFor:'as yet unclassified'!

callParser: id
	| retval |
	retval := (self class parsers at: id) parseOn: context.
	retval isPetitFailure 	ifTrue: [ self error: retval message at: retval position ]
									ifFalse: [ self clearError ].
	^ retval
!

clearError
	error := false.
!

error
	^ self error: '' at: context position
!

error: message
	^ self error: message at: context position
!

error: aMessage at: position
	failure position < position ifTrue: [
		failure message: aMessage.
		failure position: position
	].
	error := true.
	^ failure
!

initialize
	super initialize.
	
	self class constants keysAndValuesDo: [ :key :value |
		self instVarNamed: key put: value.
	].

	startSymbol := #start.

	

!

isCompiled
	^ true
!

isError
	^ error
!

parse: input rule: symbol
	startSymbol := symbol.
	^ self parse: input.
!

start
	^ self subclassResponsibility
!

startSymbol: aSymbol
	startSymbol := aSymbol
!

updateContext: aPPContext
	self class referringParser allParsersDo: [ :p | p updateContext: aPPContext ].
! !

!PPCompiledParser methodsFor:'parsing'!

parseOn: aPPContext
	| retval |
"	context := aPPContext asCompiledParserContext."
	context := aPPContext.
	context compiledParser: self.
	failure := PPFailure new message: nil; context: context; position: -1.
	context noteFailure: failure.
	error := false.

	retval := self perform: startSymbol.
	(retval isPetitFailure) ifTrue: [ aPPContext noteFailure: failure ].
	error ifTrue: [ aPPContext noteFailure: failure. retval := failure ].
	
"	aPPContext position: context position."
	^ retval
! !