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