author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Tue, 12 May 2015 01:57:37 +0100 | |
changeset 461 | 5986bf6d7d60 |
parent 421 | 7e08b31e0dae |
permissions | -rw-r--r-- |
4 | 1 |
"{ Package: 'stx:goodies/petitparser' }"! |
0 | 2 |
|
3 |
!Block methodsFor:'*petitparser-core-converting'! |
|
4 |
||
5 |
asParser |
|
213 | 6 |
"Answer a parser implemented in the receiving one-argument block." |
7 |
||
0 | 8 |
^ PPPluggableParser on: self |
9 |
! ! |
|
3 | 10 |
|
0 | 11 |
!BlockContext methodsFor:'*petitparser-core-converting'! |
12 |
||
13 |
asParser |
|
14 |
^ PPPluggableParser on: self |
|
15 |
! ! |
|
3 | 16 |
|
213 | 17 |
!Character methodsFor:'*petitparser-core-converting'! |
0 | 18 |
|
154
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
19 |
asParser |
213 | 20 |
"Answer a parser that accepts the receiving character." |
21 |
||
154
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
22 |
^ PPLiteralObjectParser on: self |
0 | 23 |
! ! |
3 | 24 |
|
226 | 25 |
!Collection methodsFor:'*petitparser-core-converting'! |
26 |
||
27 |
asChoiceParser |
|
28 |
^ PPChoiceParser withAll: (self collect: [ :each | each asParser ]) |
|
29 |
! ! |
|
30 |
||
405
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
31 |
!Collection methodsFor:'*petitparser-converting'! |
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
32 |
|
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
33 |
asParser |
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
34 |
"Create a range of characters between start and stop." |
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
35 |
|
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
36 |
(self allSatisfy: [ :e | e isCharacter ]) ifTrue: [ |
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
37 |
| charSet | |
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
38 |
charSet := PPCharSetPredicate on: [ :char | self includes: char ] . |
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
39 |
^ PPPredicateObjectParser on: charSet message: 'One of these charactes expected: ', self printString. |
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
40 |
]. |
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
41 |
|
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
42 |
|
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
43 |
^ super asParser |
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
44 |
" |
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
45 |
($a to:$f) asParser parse:'a' |
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
46 |
($a to:$f) asParser parse:'g' |
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
47 |
" |
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
48 |
! ! |
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
49 |
|
226 | 50 |
!Collection methodsFor:'*petitparser-core-converting'! |
51 |
||
52 |
asSequenceParser |
|
53 |
^ PPSequenceParser withAll: (self collect: [ :each | each asParser ]) |
|
54 |
! ! |
|
55 |
||
405
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
56 |
!Interval methodsFor:'*petitparser-core-converting'! |
0 | 57 |
|
405
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
58 |
asParser |
421
7e08b31e0dae
Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
408
diff
changeset
|
59 |
"Create a range of characters between start and stop." |
152 | 60 |
|
61 |
self assert:start isCharacter. |
|
62 |
self assert:stop isCharacter. |
|
63 |
self assert:step == 1. |
|
64 |
^ PPPredicateObjectParser between: start and: stop |
|
65 |
||
66 |
" |
|
421
7e08b31e0dae
Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
408
diff
changeset
|
67 |
($a to: $f) asParser parse: 'a' |
7e08b31e0dae
Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
408
diff
changeset
|
68 |
($a to: $f) asParser parse: 'g' |
152 | 69 |
" |
70 |
! ! |
|
71 |
||
153 | 72 |
!Object methodsFor:'*petitparser-core-converting'! |
73 |
||
154
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
74 |
asParser |
213 | 75 |
"Answer a parser accepting the receiving object." |
76 |
||
154
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
77 |
^ PPPredicateObjectParser expect: self |
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
78 |
! ! |
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
79 |
|
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
80 |
!Object methodsFor:'*petitparser-core-testing'! |
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
81 |
|
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
82 |
isPetitFailure |
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
83 |
^ false |
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
84 |
! ! |
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
85 |
|
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
86 |
!Object methodsFor:'*petitparser-core-testing'! |
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
87 |
|
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
88 |
isPetitParser |
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
89 |
^ false |
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
90 |
! ! |
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
91 |
|
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
92 |
!PositionableStream methodsFor:'*petitparser-core-converting'! |
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
93 |
|
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
94 |
asPetitStream |
405
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
95 |
"Some of my subclasses do not use the instance-variables collection, position and readLimit but instead have a completely different internal representation. In these cases just use the super implementation that is inefficient but should work in all cases." |
154
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
96 |
|
421
7e08b31e0dae
Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
408
diff
changeset
|
97 |
" |
7e08b31e0dae
Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
408
diff
changeset
|
98 |
Disabled until we agree on some way how to optimize this |
7e08b31e0dae
Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
408
diff
changeset
|
99 |
|
405
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
100 |
^ (collection isNil or: [ position isNil or: [ readLimit isNil ] ]) |
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
101 |
ifFalse: [ PPStream on: collection from: ( position + 1 ) to: readLimit ] |
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
383
diff
changeset
|
102 |
ifTrue: [ super asPetitStream ] |
421
7e08b31e0dae
Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
408
diff
changeset
|
103 |
" |
7e08b31e0dae
Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
408
diff
changeset
|
104 |
^ super asPetitStream |
153 | 105 |
! ! |
106 |
||
379 | 107 |
!PositionableStream methodsFor:'*petitparser-core'! |
108 |
||
109 |
peekTwice |
|
110 |
"Answer what would be returned if the message next were sent to the |
|
111 |
receiver. If the receiver is at the end, answer nil." |
|
112 |
||
113 |
| array | |
|
114 |
self atEnd |
|
115 |
ifTrue: [^Array with: nil with: nil]. |
|
116 |
array := Array with: (self next) with: (self peek). |
|
117 |
position := position - 1. |
|
118 |
^array |
|
119 |
! ! |
|
120 |
||
153 | 121 |
!SequenceableCollection methodsFor:'*petitparser-core-converting'! |
122 |
||
154
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
123 |
asPetitStream |
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
124 |
^ PPStream on: self |
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
125 |
! ! |
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
126 |
|
0 | 127 |
!Set methodsFor:'*petitparser-core-converting'! |
128 |
||
154
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
129 |
asParser |
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
130 |
^ PPChoiceParser withAll: (self collect: [ :each | each asParser ]) |
0 | 131 |
! ! |
3 | 132 |
|
0 | 133 |
!Stream methodsFor:'*petitparser-core-converting'! |
134 |
||
154
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
135 |
asPetitStream |
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
136 |
^ self contents asPetitStream |
0 | 137 |
! ! |
3 | 138 |
|
0 | 139 |
!String methodsFor:'*petitparser-core-converting'! |
140 |
||
154
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
141 |
asParser |
213 | 142 |
"Answer a parser that accepts the receiving string." |
143 |
||
154
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
144 |
^ PPLiteralSequenceParser on: self |
0 | 145 |
! ! |
3 | 146 |
|
0 | 147 |
!Symbol methodsFor:'*petitparser-core-converting'! |
148 |
||
154
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
149 |
asParser |
213 | 150 |
"Answer a predicate parser named after the receiving symbol. Possible symbols are the method selectors on the class-side of PPPredicateObjectParser." |
151 |
||
154
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
152 |
^ PPPredicateObjectParser perform: self |
0 | 153 |
! ! |
3 | 154 |
|
0 | 155 |
!Symbol methodsFor:'Compatibility-Squeak'! |
156 |
||
154
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
157 |
value:anObject |
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
158 |
^ anObject perform: self. |
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
159 |
|
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
160 |
"Created: / 18-12-2010 / 16:47:22 / Jan Kurs <kurs.jan@post.cz>" |
0 | 161 |
! ! |
3 | 162 |
|
0 | 163 |
!Text methodsFor:'*petitparser-core'! |
164 |
||
154
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
165 |
asPetitStream |
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
166 |
^ string asPetitStream |
0 | 167 |
! ! |
3 | 168 |
|
0 | 169 |
!UndefinedObject methodsFor:'*petitparser-converting'! |
170 |
||
154
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
171 |
asParser |
213 | 172 |
"Answer a parser that succeeds and does not consume anything." |
173 |
||
154
5c8f5da40d90
manually fixed bad checkin version
Claus Gittinger <cg@exept.de>
parents:
153
diff
changeset
|
174 |
^ PPEpsilonParser new |
0 | 175 |
! ! |
3 | 176 |
|
4 | 177 |
!stx_goodies_petitparser class methodsFor:'documentation'! |
3 | 178 |
|
376
a2656b27cace
Added monticelloName to package definition to ease export to .mcz
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
309
diff
changeset
|
179 |
extensionsVersion_HG |
a2656b27cace
Added monticelloName to package definition to ease export to .mcz
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
309
diff
changeset
|
180 |
|
a2656b27cace
Added monticelloName to package definition to ease export to .mcz
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
309
diff
changeset
|
181 |
^ '$Changeset: <not expanded> $' |
150
24e8d10ff041
Character>>-not needed here - already in libbasic
Claus Gittinger <cg@exept.de>
parents:
58
diff
changeset
|
182 |
! ! |