4
|
1 |
"{ Package: 'stx:goodies/petitparser' }"
|
0
|
2 |
|
|
3 |
PPDelegateParser subclass:#PPExpressionParser
|
|
4 |
instanceVariableNames:'operators'
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'PetitParser-Tools'
|
|
8 |
!
|
|
9 |
|
|
10 |
|
|
11 |
!PPExpressionParser methodsFor:'private'!
|
|
12 |
|
|
13 |
build: aParser left: aChoiceParser
|
|
14 |
^ (aParser separatedBy: aChoiceParser) foldLeft: [ :a :op :b | op first value: a value: op second value: b ]
|
|
15 |
!
|
|
16 |
|
|
17 |
build: aParser postfix: aChoiceParser
|
|
18 |
^ aParser , aChoiceParser star map: [ :term :ops | ops inject: term into: [ :result :operator | operator first value: result value: operator second ] ]
|
|
19 |
!
|
|
20 |
|
|
21 |
build: aParser prefix: aChoiceParser
|
|
22 |
^ aChoiceParser star , aParser map: [ :ops :term | ops reversed inject: term into: [ :result :operator | operator first value: operator second value: result ] ]
|
|
23 |
!
|
|
24 |
|
|
25 |
build: aParser right: aChoiceParser
|
|
26 |
^ (aParser separatedBy: aChoiceParser) foldRight: [ :a :op :b | op first value: a value: op second value: b ]
|
|
27 |
!
|
|
28 |
|
|
29 |
buildOn: aParser
|
|
30 |
^ self buildSelectors inject: aParser into: [ :term :selector |
|
|
31 |
| list |
|
|
32 |
list := operators at: selector ifAbsent: [ #() ].
|
|
33 |
list isEmpty
|
|
34 |
ifTrue: [ term ]
|
|
35 |
ifFalse: [
|
|
36 |
self
|
11
|
37 |
perform: selector with: term
|
0
|
38 |
with: (list size = 1
|
|
39 |
ifTrue: [ list first first ==> [ :operator | Array with: list first second with: operator ] ]
|
11
|
40 |
ifFalse: [
|
0
|
41 |
list
|
|
42 |
inject: PPChoiceParser new
|
|
43 |
into: [ :choice :each | choice / (each first ==> [ :operator | Array with: each second with: operator ]) ] ]) ] ]
|
|
44 |
!
|
|
45 |
|
|
46 |
buildSelectors
|
|
47 |
^ #(build:prefix: build:postfix: build:right: build:left:)
|
|
48 |
!
|
|
49 |
|
|
50 |
operator: aSymbol parser: aParser do: aBlock
|
|
51 |
parser isNil
|
|
52 |
ifTrue: [ ^ self error: 'You did not specify a term when creating the receiver.' ].
|
|
53 |
operators isNil
|
|
54 |
ifTrue: [ ^ self error: 'Use #group: to define precedence groups in descending order.' ].
|
|
55 |
(operators at: aSymbol ifAbsentPut: [ OrderedCollection new ])
|
|
56 |
addLast: (Array with: aParser asParser with: aBlock)
|
|
57 |
! !
|
|
58 |
|
|
59 |
!PPExpressionParser methodsFor:'specifying'!
|
|
60 |
|
|
61 |
group: aOneArgumentBlock
|
|
62 |
"Defines a priority group by evaluating aOneArgumentBlock."
|
11
|
63 |
|
0
|
64 |
operators := Dictionary new.
|
11
|
65 |
parser := [
|
0
|
66 |
aOneArgumentBlock value: self.
|
11
|
67 |
self buildOn: parser ]
|
0
|
68 |
ensure: [ operators := nil ]
|
|
69 |
!
|
|
70 |
|
|
71 |
left: aParser do: aThreeArgumentBlock
|
|
72 |
"Define an operator aParser that is left-associative. Evaluate aThreeArgumentBlock with the first term, the operator, and the second term."
|
11
|
73 |
|
0
|
74 |
self operator: #build:left: parser: aParser do: aThreeArgumentBlock
|
|
75 |
!
|
|
76 |
|
|
77 |
postfix: aParser do: aTwoArgumentBlock
|
|
78 |
"Define a postfix operator aParser. Evaluate aTwoArgumentBlock with the term and the operator."
|
|
79 |
|
|
80 |
self operator: #build:postfix: parser: aParser do: aTwoArgumentBlock
|
|
81 |
!
|
|
82 |
|
|
83 |
prefix: aParser do: aTwoArgumentBlock
|
|
84 |
"Define a prefix operator aParser. Evaluate aTwoArgumentBlock with the operator and the term."
|
|
85 |
|
|
86 |
self operator: #build:prefix: parser: aParser do: aTwoArgumentBlock
|
|
87 |
!
|
|
88 |
|
|
89 |
right: aParser do: aThreeArgumentBlock
|
|
90 |
"Define an operator aParser that is right-associative. Evaluate aThreeArgumentBlock with the first term, the operator, and the second term."
|
11
|
91 |
|
0
|
92 |
self operator: #build:right: parser: aParser do: aThreeArgumentBlock
|
|
93 |
!
|
|
94 |
|
|
95 |
term: aParser
|
|
96 |
"Defines the initial term aParser of the receiver."
|
11
|
97 |
|
0
|
98 |
parser isNil
|
|
99 |
ifTrue: [ parser := aParser ]
|
|
100 |
ifFalse: [ self error: 'Unable to redefine the term.' ]
|
|
101 |
! !
|
|
102 |
|
|
103 |
!PPExpressionParser class methodsFor:'documentation'!
|
|
104 |
|
11
|
105 |
version
|
|
106 |
^ '$Header: /cvs/stx/stx/goodies/petitparser/PPExpressionParser.st,v 1.3 2012-05-04 21:59:01 vrany Exp $'
|
|
107 |
!
|
|
108 |
|
|
109 |
version_CVS
|
|
110 |
^ '$Header: /cvs/stx/stx/goodies/petitparser/PPExpressionParser.st,v 1.3 2012-05-04 21:59:01 vrany Exp $'
|
|
111 |
!
|
|
112 |
|
0
|
113 |
version_SVN
|
11
|
114 |
^ '§Id: PPExpressionParser.st 2 2010-12-17 18:44:23Z vranyj1 §'
|
0
|
115 |
! !
|