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