tests/PPArithmeticParser.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sun, 10 May 2015 14:20:24 +0100
changeset 454 a9cd5ea7cc36
parent 427 a7f5e6de19d2
child 502 1e45d3c96ec5
permissions -rw-r--r--
Portability: fixes for Smalltalk/X * Do not use detect:ifFound: - not present in Smalltalk/X * Removed leftover debugging code (Halt if:, ...) * Do not use `aClass methods`, use `aClass methodDictionary values` * Do not use #allPairsDo; - not present in Smalltalk/X * Do not use #crShow: - not present in Smalltalk/X * On Smalltalk?X use Filename - there's no FileReference in Smalltalk/X * Do not use CharacterSet, use String

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

"{ NameSpace: Smalltalk }"

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) trim) 
		foldLeft: [ :a :op :b | a perform: op asSymbol with: b ]
!

factors
	^ multiplication / power
!

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

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

parentheses
	^ $( asParser trim , terms , $) asParser trim
		==> [ :nodes | nodes at: 2 ]
!

power
	^ (primary separatedBy: $^ asParser 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.4 2014-03-04 14:33:59 cg Exp $'
!

version_CVS
    ^ '$Header: /cvs/stx/stx/goodies/petitparser/PPArithmeticParser.st,v 1.4 2014-03-04 14:33:59 cg Exp $'
!

version_SVN
    ^ '$Id: PPArithmeticParser.st,v 1.4 2014-03-04 14:33:59 cg Exp $'
! !