|
1 "{ Package: 'squeak:petitparser' }" |
|
2 |
|
3 PPPredicateParser subclass:#PPPredicateObjectParser |
|
4 instanceVariableNames:'' |
|
5 classVariableNames:'' |
|
6 poolDictionaries:'' |
|
7 category:'PetitParser-Parsers' |
|
8 ! |
|
9 |
|
10 PPPredicateObjectParser comment:'A parser that accepts if a given predicate on one element of the input sequence holds.' |
|
11 ! |
|
12 |
|
13 |
|
14 !PPPredicateObjectParser class methodsFor:'instance creation'! |
|
15 |
|
16 on: aBlock message: aString |
|
17 ^ self on: aBlock message: aString negated: [ :each | (aBlock value: each) not ] message: 'no ' , aString |
|
18 ! |
|
19 |
|
20 on: aBlock message: aString negated: aNegatedBlock message: aNegatedString |
|
21 ^ self new initializeOn: aBlock message: aString negated: aNegatedBlock message: aNegatedString |
|
22 ! ! |
|
23 |
|
24 !PPPredicateObjectParser class methodsFor:'factory-chars'! |
|
25 |
|
26 blank |
|
27 ^ self chars: (String with: Character space with: Character tab) message: 'blank expected' |
|
28 ! |
|
29 |
|
30 char: aCharacter |
|
31 ^ self expect: aCharacter message: (String with: $" with: aCharacter with: $") , ' expected' |
|
32 ! |
|
33 |
|
34 char: aCharacter message: aString |
|
35 ^ self expect: aCharacter message: aString |
|
36 ! |
|
37 |
|
38 chars: aCollection message: aString |
|
39 ^ self on: (PPCharSetPredicate on: [ :char | aCollection includes: char ]) message: aString |
|
40 ! |
|
41 |
|
42 cr |
|
43 ^ self char: Character cr message: 'carriage return expected' |
|
44 ! |
|
45 |
|
46 digit |
|
47 ^ self on: (PPCharSetPredicate on: [ :char | char isDigit ]) message: 'digit expected' |
|
48 ! |
|
49 |
|
50 hex |
|
51 ^ self |
|
52 on: (PPCharSetPredicate on: [ :char | |
|
53 (char between: $0 and: $9) |
|
54 or: [ (char between: $a and: $f) |
|
55 or: [ (char between: $A and: $F) ] ] ]) |
|
56 message: 'hex digit expected' |
|
57 ! |
|
58 |
|
59 letter |
|
60 ^ self on: (PPCharSetPredicate on: [ :char | char isLetter ]) message: 'letter expected' |
|
61 ! |
|
62 |
|
63 lf |
|
64 ^ self char: Character lf |
|
65 ! |
|
66 |
|
67 lowercase |
|
68 ^ self on: (PPCharSetPredicate on: [ :char | char isLowercase ]) message: 'lowercase letter expected' |
|
69 ! |
|
70 |
|
71 newline |
|
72 ^ self chars: (String with: Character cr with: Character lf) message: 'newline expected' |
|
73 ! |
|
74 |
|
75 punctuation |
|
76 ^ self chars: '.,"''?!!;:#$%&()*+-/<>=@[]\^_{}|~' message: 'punctuation expected' |
|
77 ! |
|
78 |
|
79 space |
|
80 ^ self on: (PPCharSetPredicate on: [ :char | char isSeparator ]) message: 'separator expected' |
|
81 ! |
|
82 |
|
83 tab |
|
84 ^ self char: Character tab message: 'tab expected' |
|
85 ! |
|
86 |
|
87 uppercase |
|
88 ^ self on: (PPCharSetPredicate on: [ :char | char isUppercase ]) message: 'uppercase letter expected' |
|
89 ! |
|
90 |
|
91 word |
|
92 ^ self on: (PPCharSetPredicate on: [ :char | char isAlphaNumeric ]) message: 'letter or digit expected' |
|
93 ! ! |
|
94 |
|
95 !PPPredicateObjectParser class methodsFor:'factory-objects'! |
|
96 |
|
97 any |
|
98 ^ self |
|
99 on: [ :each | true ] message: 'input expected' |
|
100 negated: [ :each | false ] message: 'no input expected' |
|
101 ! |
|
102 |
|
103 anyExceptAnyOf: aCollectionOfChars |
|
104 ^ self |
|
105 on: [ :each | (aCollectionOfChars includes: each) not ] message: 'any except ' , aCollectionOfChars printString , ' expected' |
|
106 negated: [ :each | aCollectionOfChars includes: each ] message: aCollectionOfChars printString , ' not expected' |
|
107 ! |
|
108 |
|
109 anyOf: anArray |
|
110 ^ self |
|
111 on: [ :each | anArray includes: each ] message: 'any of ' , anArray printString , ' expected' |
|
112 negated: [ :each | (anArray includes: each) not ] message: 'none of ' , anArray printString , 'expected' |
|
113 ! |
|
114 |
|
115 between: min and: max |
|
116 ^ self |
|
117 on: [ :each | each >= min and: [ each <= max ] ] message: min printString , '..' , max printString , ' expected' |
|
118 negated: [ :each | each < min or: [ each > max ] ] message: min printString , '..' , max printString , ' not expected' |
|
119 ! |
|
120 |
|
121 expect: anObject |
|
122 ^ self expect: anObject message: anObject printString , ' expected' |
|
123 ! |
|
124 |
|
125 expect: anObject message: aString |
|
126 ^ self |
|
127 on: [ :each | each = anObject ] message: aString |
|
128 negated: [ :each | each ~= anObject ] message: 'no ' , aString |
|
129 ! ! |
|
130 |
|
131 !PPPredicateObjectParser methodsFor:'initialization'! |
|
132 |
|
133 initializeOn: aBlock message: aString negated: aNegatedBlock message: aNegatedString |
|
134 predicate := aBlock. |
|
135 predicateMessage := aString. |
|
136 negated := aNegatedBlock. |
|
137 negatedMessage := aNegatedString |
|
138 ! ! |
|
139 |
|
140 !PPPredicateObjectParser methodsFor:'operators'! |
|
141 |
|
142 negate |
|
143 "Answer a parser that is the negation of the receiving predicate parser." |
|
144 |
|
145 ^ self class |
|
146 on: negated message: negatedMessage |
|
147 negated: predicate message: predicateMessage |
|
148 ! ! |
|
149 |
|
150 !PPPredicateObjectParser methodsFor:'parsing'! |
|
151 |
|
152 parseOn: aStream |
|
153 ^ (aStream atEnd not and: [ predicate value: aStream uncheckedPeek ]) |
|
154 ifFalse: [ PPFailure message: predicateMessage at: aStream position ] |
|
155 ifTrue: [ aStream next ] |
|
156 ! ! |
|
157 |
|
158 !PPPredicateObjectParser class methodsFor:'documentation'! |
|
159 |
|
160 version_SVN |
|
161 ^ '$Id: PPPredicateObjectParser.st,v 1.1 2011-08-18 18:56:17 cg Exp $' |
|
162 ! ! |