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