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