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