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