PPArithmeticParser.st
author Claus Gittinger <cg@exept.de>
Sat, 01 Dec 2012 15:27:17 +0100
changeset 91 ed96c98bff4a
parent 31 38fde57e4ea0
child 183 553dad635f6d
permissions -rw-r--r--
class: PPToken changed: #newline fix for stx's stupid cr/return incompatibility

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

PPCompositeParser subclass:#PPArithmeticParser
	instanceVariableNames:'terms addition factors multiplication power primary parentheses
		number'
	classVariableNames:''
	poolDictionaries:''
	category:'PetitTests-Examples'
!


!PPArithmeticParser methodsFor:'accessing'!

start
	^ terms end
! !

!PPArithmeticParser methodsFor:'grammar'!

addition
	^ (factors separatedBy: ($+ asParser / $- asParser) token trim) 
		foldLeft: [ :a :op :b | a perform: op value asSymbol with: b ]
!

factors
	^ multiplication / power
!

multiplication
	^ (power separatedBy: ($* asParser / $/ asParser) token trim)
		foldLeft: [ :a :op :b | a perform: op value asSymbol with: b ]
!

number
	^ ($- asParser optional , #digit asParser plus , ($. asParser , #digit asParser plus) optional) token trim ==> [ :token | token value asNumber ]
!

parentheses
	^ $( asParser flatten trim , terms , $) asParser flatten trim ==> #second
!

power
	^ (primary separatedBy: $^ asParser token trim) foldRight: [ :a :op :b | a raisedTo: b ]
!

primary
	^ number / parentheses
!

terms
	^ addition / factors
! !

!PPArithmeticParser class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/goodies/petitparser/PPArithmeticParser.st,v 1.3 2012-05-04 22:03:26 vrany Exp $'
!

version_CVS
    ^ '$Header: /cvs/stx/stx/goodies/petitparser/PPArithmeticParser.st,v 1.3 2012-05-04 22:03:26 vrany Exp $'
!

version_SVN
    ^ '§Id: PPArithmeticParser.st 4 2010-12-18 17:02:23Z kursjan §'
! !