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
|
183
|
21 |
^ (factors separatedBy: ($+ asParser / $- asParser) trim)
|
|
22 |
foldLeft: [ :a :op :b | a perform: op asSymbol with: b ]
|
0
|
23 |
!
|
|
24 |
|
|
25 |
factors
|
|
26 |
^ multiplication / power
|
|
27 |
!
|
|
28 |
|
|
29 |
multiplication
|
183
|
30 |
^ (power separatedBy: ($* asParser / $/ asParser) trim)
|
|
31 |
foldLeft: [ :a :op :b | a perform: op asSymbol with: b ]
|
0
|
32 |
!
|
|
33 |
|
|
34 |
number
|
183
|
35 |
^ ($- asParser optional , #digit asParser plus , ($. asParser , #digit asParser plus) optional) flatten trim
|
|
36 |
==> [ :value | value asNumber ]
|
0
|
37 |
!
|
|
38 |
|
|
39 |
parentheses
|
183
|
40 |
^ $( asParser trim , terms , $) asParser trim
|
|
41 |
==> [ :nodes | nodes second ]
|
0
|
42 |
!
|
|
43 |
|
|
44 |
power
|
183
|
45 |
^ (primary separatedBy: $^ asParser trim)
|
|
46 |
foldRight: [ :a :op :b | a raisedTo: b ]
|
0
|
47 |
!
|
|
48 |
|
|
49 |
primary
|
|
50 |
^ number / parentheses
|
|
51 |
!
|
|
52 |
|
|
53 |
terms
|
|
54 |
^ addition / factors
|
|
55 |
! !
|
|
56 |
|
|
57 |
!PPArithmeticParser class methodsFor:'documentation'!
|
|
58 |
|
31
|
59 |
version
|
183
|
60 |
^ '$Header: /cvs/stx/stx/goodies/petitparser/PPArithmeticParser.st,v 1.4 2014-03-04 14:33:59 cg Exp $'
|
31
|
61 |
!
|
|
62 |
|
|
63 |
version_CVS
|
183
|
64 |
^ '$Header: /cvs/stx/stx/goodies/petitparser/PPArithmeticParser.st,v 1.4 2014-03-04 14:33:59 cg Exp $'
|
31
|
65 |
!
|
|
66 |
|
0
|
67 |
version_SVN
|
183
|
68 |
^ '$Id: PPArithmeticParser.st,v 1.4 2014-03-04 14:33:59 cg Exp $'
|
0
|
69 |
! !
|
183
|
70 |
|