tests/PPArithmeticParser.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Tue, 07 Oct 2014 09:42:03 +0100
changeset 385 44a36ed4e484
parent 376 a2656b27cace
child 427 a7f5e6de19d2
permissions -rw-r--r--
Commited a Smalltalk parser (MC package PetitSmalltalk) Name: PetitSmalltalk-JanKurs.71 Author: JanKurs Time: 19-08-2014, 02:18:05 AM UUID: d1d11836-f3e2-4709-abd3-e2ff3b72d7c4 Repository: http://smalltalkhub.com/mc/Moose/PetitParser/main Ancestors: Fixes to be compatible with PPContext

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

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 second ]
!

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 $'
! !