author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Tue, 24 Nov 2015 19:37:14 +0100 | |
changeset 553 | d83cb4945d5e |
parent 421 | 7e08b31e0dae |
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 |
||
160 | 31 |
on: aSequenceableCollection start: aStartInteger stop: aStopInteger value: anObject |
32 |
^ self basicNew |
|
33 |
initializeOn: aSequenceableCollection |
|
34 |
start: aStartInteger stop: aStopInteger |
|
35 |
value: anObject |
|
0 | 36 |
! ! |
37 |
||
405
0470a5e6e712
Merged PetitParser and PetitTests
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
380
diff
changeset
|
38 |
|
0 | 39 |
!PPToken methodsFor:'accessing'! |
40 |
||
41 |
collection |
|
42 |
"Answer the underlying collection of this token." |
|
43 |
||
44 |
^ collection |
|
45 |
! |
|
46 |
||
47 |
size |
|
160 | 48 |
"Answer the size of this token in the underlying collection." |
0 | 49 |
|
50 |
^ stop - start + 1 |
|
51 |
! |
|
52 |
||
53 |
start |
|
54 |
"Answer the start position of this token in the underlying collection." |
|
55 |
||
56 |
^ start |
|
57 |
! |
|
58 |
||
59 |
stop |
|
60 |
"Answer the stop position of this token in the underlying collection." |
|
30 | 61 |
|
0 | 62 |
^ stop |
160 | 63 |
! ! |
64 |
||
65 |
!PPToken methodsFor:'accessing-values'! |
|
66 |
||
67 |
inputValue |
|
68 |
"Answer the consumed input of this token." |
|
69 |
||
70 |
^ collection copyFrom: start to: stop |
|
71 |
! |
|
72 |
||
73 |
parsedValue |
|
74 |
"Answer the parsed value of this token." |
|
75 |
||
76 |
^ value |
|
0 | 77 |
! |
78 |
||
79 |
value |
|
160 | 80 |
self notify: 'Token>>#value is no longer supported. Instead use Token>>#inputValue or the more pragmatic #parsedValue.'. |
81 |
^ self inputValue |
|
0 | 82 |
! ! |
83 |
||
421
7e08b31e0dae
Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
407
diff
changeset
|
84 |
!PPToken methodsFor:'comparing'! |
7e08b31e0dae
Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
407
diff
changeset
|
85 |
|
7e08b31e0dae
Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
407
diff
changeset
|
86 |
= anObject |
7e08b31e0dae
Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
407
diff
changeset
|
87 |
^ self class = anObject class and: [ self parsedValue = anObject parsedValue ] |
7e08b31e0dae
Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
407
diff
changeset
|
88 |
! |
7e08b31e0dae
Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
407
diff
changeset
|
89 |
|
7e08b31e0dae
Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
407
diff
changeset
|
90 |
hash |
7e08b31e0dae
Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
407
diff
changeset
|
91 |
^ self parsedValue hash |
7e08b31e0dae
Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
407
diff
changeset
|
92 |
! ! |
7e08b31e0dae
Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
407
diff
changeset
|
93 |
|
0 | 94 |
!PPToken methodsFor:'copying'! |
95 |
||
96 |
copyFrom: aStartInteger to: aStopInteger |
|
160 | 97 |
^ self class on: collection start: start + aStartInteger - 1 stop: stop + aStopInteger - 3 value: value |
0 | 98 |
! ! |
99 |
||
100 |
!PPToken methodsFor:'initialization'! |
|
101 |
||
160 | 102 |
initializeOn: aSequenceableCollection start: aStartInteger stop: aStopInteger value: anObject |
103 |
collection := aSequenceableCollection. |
|
104 |
start := aStartInteger. |
|
105 |
stop := aStopInteger. |
|
106 |
value := anObject |
|
0 | 107 |
! ! |
108 |
||
109 |
!PPToken methodsFor:'printing'! |
|
110 |
||
111 |
printOn: aStream |
|
112 |
super printOn: aStream. |
|
160 | 113 |
aStream nextPut: $[; print: self start; nextPut: $,; print: self stop; nextPut: $]. |
114 |
aStream nextPut: $(; print: self parsedValue; nextPut: $) |
|
0 | 115 |
! ! |
116 |
||
117 |
!PPToken methodsFor:'private'! |
|
118 |
||
119 |
newline |
|
91 | 120 |
"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
|
121 |
| cr lf | |
0 | 122 |
|
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
|
123 |
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
|
124 |
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
|
125 |
^ 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
|
126 |
/ (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
|
127 |
|
8fe3cb4e607f
Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
377
diff
changeset
|
128 |
"Modified: / 04-10-2014 / 00:02:15 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
0 | 129 |
! ! |
130 |
||
131 |
!PPToken methodsFor:'querying'! |
|
132 |
||
133 |
column |
|
134 |
"Answer the column number of this token in the underlying collection." |
|
30 | 135 |
|
0 | 136 |
| position | |
137 |
position := 0. |
|
160 | 138 |
(NewLineParser , [ :stream | |
0 | 139 |
start <= stream position |
140 |
ifTrue: [ ^ start - position ]. |
|
141 |
position := stream position ] asParser |
|
142 |
/ #any asParser) star |
|
143 |
parse: collection. |
|
144 |
^ start - position |
|
145 |
! |
|
146 |
||
147 |
line |
|
148 |
"Answer the line number of this token in the underlying collection." |
|
30 | 149 |
|
0 | 150 |
| line | |
151 |
line := 1. |
|
160 | 152 |
(NewLineParser , [ :stream | |
0 | 153 |
start <= stream position |
154 |
ifTrue: [ ^ line ]. |
|
155 |
line := line + 1 ] asParser |
|
156 |
/ #any asParser) star |
|
157 |
parse: collection. |
|
158 |
^ line |
|
159 |
! ! |
|
160 |
||
161 |
!PPToken class methodsFor:'documentation'! |
|
162 |
||
30 | 163 |
version |
258 | 164 |
^ '$Header: /cvs/stx/stx/goodies/petitparser/PPToken.st,v 1.6 2014-03-04 20:10:06 cg Exp $' |
30 | 165 |
! |
166 |
||
167 |
version_CVS |
|
258 | 168 |
^ '$Header: /cvs/stx/stx/goodies/petitparser/PPToken.st,v 1.6 2014-03-04 20:10:06 cg Exp $' |
30 | 169 |
! |
170 |
||
0 | 171 |
version_SVN |
258 | 172 |
^ '$Id: PPToken.st,v 1.6 2014-03-04 20:10:06 cg Exp $' |
0 | 173 |
! ! |
160 | 174 |
|
175 |
||
176 |
PPToken initialize! |