4
|
1 |
"{ Package: 'stx:goodies/petitparser' }"
|
0
|
2 |
|
|
3 |
PPCompositeParser subclass:#PPArithmeticParser
|
|
4 |
instanceVariableNames:'terms addition factors multiplication power primary parentheses
|
|
5 |
number'
|
|
6 |
classVariableNames:''
|
|
7 |
poolDictionaries:''
|
|
8 |
category:'PetitTests-Examples'
|
|
9 |
!
|
|
10 |
|
|
11 |
|
|
12 |
!PPArithmeticParser methodsFor:'accessing'!
|
|
13 |
|
|
14 |
start
|
|
15 |
^ terms end
|
|
16 |
! !
|
|
17 |
|
|
18 |
!PPArithmeticParser methodsFor:'grammar'!
|
|
19 |
|
|
20 |
addition
|
31
|
21 |
^ (factors separatedBy: ($+ asParser / $- asParser) token trim)
|
0
|
22 |
foldLeft: [ :a :op :b | a perform: op value asSymbol with: b ]
|
|
23 |
!
|
|
24 |
|
|
25 |
factors
|
|
26 |
^ multiplication / power
|
|
27 |
!
|
|
28 |
|
|
29 |
multiplication
|
|
30 |
^ (power separatedBy: ($* asParser / $/ asParser) token trim)
|
|
31 |
foldLeft: [ :a :op :b | a perform: op value asSymbol with: b ]
|
|
32 |
!
|
|
33 |
|
|
34 |
number
|
|
35 |
^ ($- asParser optional , #digit asParser plus , ($. asParser , #digit asParser plus) optional) token trim ==> [ :token | token value asNumber ]
|
|
36 |
!
|
|
37 |
|
|
38 |
parentheses
|
|
39 |
^ $( asParser flatten trim , terms , $) asParser flatten trim ==> #second
|
|
40 |
!
|
|
41 |
|
|
42 |
power
|
|
43 |
^ (primary separatedBy: $^ asParser token trim) foldRight: [ :a :op :b | a raisedTo: b ]
|
|
44 |
!
|
|
45 |
|
|
46 |
primary
|
|
47 |
^ number / parentheses
|
|
48 |
!
|
|
49 |
|
|
50 |
terms
|
|
51 |
^ addition / factors
|
|
52 |
! !
|
|
53 |
|
|
54 |
!PPArithmeticParser class methodsFor:'documentation'!
|
|
55 |
|
31
|
56 |
version
|
|
57 |
^ '$Header: /cvs/stx/stx/goodies/petitparser/PPArithmeticParser.st,v 1.3 2012-05-04 22:03:26 vrany Exp $'
|
|
58 |
!
|
|
59 |
|
|
60 |
version_CVS
|
|
61 |
^ '$Header: /cvs/stx/stx/goodies/petitparser/PPArithmeticParser.st,v 1.3 2012-05-04 22:03:26 vrany Exp $'
|
|
62 |
!
|
|
63 |
|
0
|
64 |
version_SVN
|
31
|
65 |
^ '§Id: PPArithmeticParser.st 4 2010-12-18 17:02:23Z kursjan §'
|
0
|
66 |
! !
|