author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 08 Oct 2014 21:56:20 +0100 | |
changeset 388 | 74c9c229033b |
parent 380 | 8fe3cb4e607f |
child 405 | 0470a5e6e712 |
permissions | -rw-r--r-- |
4 | 1 |
"{ Package: 'stx:goodies/petitparser' }" |
0 | 2 |
|
3 |
Object subclass:#PPToken |
|
160 | 4 |
instanceVariableNames:'collection start stop value' |
5 |
classVariableNames:'NewLineParser' |
|
0 | 6 |
poolDictionaries:'' |
7 |
category:'PetitParser-Core' |
|
8 |
! |
|
9 |
||
10 |
||
160 | 11 |
!PPToken class methodsFor:'initialization'! |
12 |
||
13 |
initialize |
|
380
8fe3cb4e607f
Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
377
diff
changeset
|
14 |
"Platform independent newline sequence. LF: Unix, CR+LF: Windows, and CR: Apple." |
8fe3cb4e607f
Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
377
diff
changeset
|
15 |
|
8fe3cb4e607f
Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
377
diff
changeset
|
16 |
| cr lf | |
160 | 17 |
|
380
8fe3cb4e607f
Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
377
diff
changeset
|
18 |
cr := Character codePoint: 13. |
8fe3cb4e607f
Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
377
diff
changeset
|
19 |
lf := Character codePoint: 10. |
8fe3cb4e607f
Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
377
diff
changeset
|
20 |
NewLineParser := lf asParser / (cr asParser , lf asParser optional) |
8fe3cb4e607f
Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
377
diff
changeset
|
21 |
|
8fe3cb4e607f
Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
377
diff
changeset
|
22 |
"Modified: / 04-10-2014 / 00:03:39 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
160 | 23 |
! ! |
24 |
||
0 | 25 |
!PPToken class methodsFor:'instance creation'! |
26 |
||
27 |
new |
|
28 |
self error: 'Token can only be created using a dedicated constructor.' |
|
29 |
! |
|
30 |
||
31 |
on: aSequenceableCollection |
|
160 | 32 |
^ self on: aSequenceableCollection start: 1 stop: aSequenceableCollection size value: nil |
0 | 33 |
! |
34 |
||
35 |
on: aSequenceableCollection start: aStartInteger stop: aStopInteger |
|
30 | 36 |
^ self basicNew |
0 | 37 |
initializeOn: aSequenceableCollection |
38 |
start: aStartInteger stop: aStopInteger |
|
160 | 39 |
! |
40 |
||
41 |
on: aSequenceableCollection start: aStartInteger stop: aStopInteger value: anObject |
|
42 |
^ self basicNew |
|
43 |
initializeOn: aSequenceableCollection |
|
44 |
start: aStartInteger stop: aStopInteger |
|
45 |
value: anObject |
|
0 | 46 |
! ! |
47 |
||
48 |
!PPToken methodsFor:'accessing'! |
|
49 |
||
50 |
collection |
|
51 |
"Answer the underlying collection of this token." |
|
52 |
||
53 |
^ collection |
|
54 |
! |
|
55 |
||
56 |
size |
|
160 | 57 |
"Answer the size of this token in the underlying collection." |
0 | 58 |
|
59 |
^ stop - start + 1 |
|
60 |
! |
|
61 |
||
62 |
start |
|
63 |
"Answer the start position of this token in the underlying collection." |
|
64 |
||
65 |
^ start |
|
66 |
! |
|
67 |
||
68 |
stop |
|
69 |
"Answer the stop position of this token in the underlying collection." |
|
30 | 70 |
|
0 | 71 |
^ stop |
160 | 72 |
! ! |
73 |
||
74 |
!PPToken methodsFor:'accessing-values'! |
|
75 |
||
76 |
inputValue |
|
77 |
"Answer the consumed input of this token." |
|
78 |
||
79 |
^ collection copyFrom: start to: stop |
|
80 |
! |
|
81 |
||
82 |
parsedValue |
|
83 |
"Answer the parsed value of this token." |
|
84 |
||
85 |
^ value |
|
0 | 86 |
! |
87 |
||
88 |
value |
|
160 | 89 |
self notify: 'Token>>#value is no longer supported. Instead use Token>>#inputValue or the more pragmatic #parsedValue.'. |
90 |
^ self inputValue |
|
0 | 91 |
! ! |
92 |
||
93 |
!PPToken methodsFor:'comparing'! |
|
94 |
||
95 |
= anObject |
|
160 | 96 |
^ self class = anObject class and: [ self parsedValue = anObject parsedValue ] |
0 | 97 |
! |
98 |
||
99 |
hash |
|
160 | 100 |
^ self parsedValue hash |
0 | 101 |
! ! |
102 |
||
103 |
!PPToken methodsFor:'copying'! |
|
104 |
||
105 |
copyFrom: aStartInteger to: aStopInteger |
|
160 | 106 |
^ self class on: collection start: start + aStartInteger - 1 stop: stop + aStopInteger - 3 value: value |
0 | 107 |
! ! |
108 |
||
109 |
!PPToken methodsFor:'initialization'! |
|
110 |
||
111 |
initializeOn: aSequenceableCollection start: aStartInteger stop: aStopInteger |
|
112 |
collection := aSequenceableCollection. |
|
113 |
start := aStartInteger. |
|
114 |
stop := aStopInteger |
|
160 | 115 |
! |
116 |
||
117 |
initializeOn: aSequenceableCollection start: aStartInteger stop: aStopInteger value: anObject |
|
118 |
collection := aSequenceableCollection. |
|
119 |
start := aStartInteger. |
|
120 |
stop := aStopInteger. |
|
121 |
value := anObject |
|
0 | 122 |
! ! |
123 |
||
124 |
!PPToken methodsFor:'printing'! |
|
125 |
||
126 |
printOn: aStream |
|
127 |
super printOn: aStream. |
|
160 | 128 |
aStream nextPut: $[; print: self start; nextPut: $,; print: self stop; nextPut: $]. |
129 |
aStream nextPut: $(; print: self parsedValue; nextPut: $) |
|
0 | 130 |
! ! |
131 |
||
132 |
!PPToken methodsFor:'private'! |
|
133 |
||
134 |
newline |
|
91 | 135 |
"Parser a platform independent newline sequence. LF: Unix, CR+LF: Windows, and CR: Apple." |
380
8fe3cb4e607f
Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
377
diff
changeset
|
136 |
| cr lf | |
0 | 137 |
|
380
8fe3cb4e607f
Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
377
diff
changeset
|
138 |
cr := Character codePoint: 13. |
8fe3cb4e607f
Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
377
diff
changeset
|
139 |
lf := Character codePoint: 10. |
8fe3cb4e607f
Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
377
diff
changeset
|
140 |
^ lf asParser |
8fe3cb4e607f
Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
377
diff
changeset
|
141 |
/ (cr asParser , lf asParser optional) |
8fe3cb4e607f
Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
377
diff
changeset
|
142 |
|
8fe3cb4e607f
Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
377
diff
changeset
|
143 |
"Modified: / 04-10-2014 / 00:02:15 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
0 | 144 |
! ! |
145 |
||
146 |
!PPToken methodsFor:'querying'! |
|
147 |
||
148 |
column |
|
149 |
"Answer the column number of this token in the underlying collection." |
|
30 | 150 |
|
0 | 151 |
| position | |
152 |
position := 0. |
|
160 | 153 |
(NewLineParser , [ :stream | |
0 | 154 |
start <= stream position |
155 |
ifTrue: [ ^ start - position ]. |
|
156 |
position := stream position ] asParser |
|
157 |
/ #any asParser) star |
|
158 |
parse: collection. |
|
159 |
^ start - position |
|
160 |
! |
|
161 |
||
162 |
line |
|
163 |
"Answer the line number of this token in the underlying collection." |
|
30 | 164 |
|
0 | 165 |
| line | |
166 |
line := 1. |
|
160 | 167 |
(NewLineParser , [ :stream | |
0 | 168 |
start <= stream position |
169 |
ifTrue: [ ^ line ]. |
|
170 |
line := line + 1 ] asParser |
|
171 |
/ #any asParser) star |
|
172 |
parse: collection. |
|
173 |
^ line |
|
174 |
! ! |
|
175 |
||
176 |
!PPToken class methodsFor:'documentation'! |
|
177 |
||
30 | 178 |
version |
258 | 179 |
^ '$Header: /cvs/stx/stx/goodies/petitparser/PPToken.st,v 1.6 2014-03-04 20:10:06 cg Exp $' |
30 | 180 |
! |
181 |
||
182 |
version_CVS |
|
258 | 183 |
^ '$Header: /cvs/stx/stx/goodies/petitparser/PPToken.st,v 1.6 2014-03-04 20:10:06 cg Exp $' |
30 | 184 |
! |
185 |
||
0 | 186 |
version_SVN |
258 | 187 |
^ '$Id: PPToken.st,v 1.6 2014-03-04 20:10:06 cg Exp $' |
0 | 188 |
! ! |
160 | 189 |
|
190 |
||
191 |
PPToken initialize! |