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