|
1 "{ Package: 'squeak:petitparser' }" |
|
2 |
|
3 Object subclass:#PPToken |
|
4 instanceVariableNames:'collection start stop' |
|
5 classVariableNames:'' |
|
6 poolDictionaries:'' |
|
7 category:'PetitParser-Core' |
|
8 ! |
|
9 |
|
10 PPToken comment:'PPToken represents a parsed part of the input stream. Contrary to a simple String it remembers where it came from, the original collection and its start and stop position. |
|
11 Instance Variables: |
|
12 collection <SequenceableCollection> The collection this token comes from. |
|
13 start <Integer> The start position in the collection. |
|
14 stop <Integer> The stop position in the collection.' |
|
15 ! |
|
16 |
|
17 |
|
18 !PPToken class methodsFor:'instance creation'! |
|
19 |
|
20 new |
|
21 self error: 'Token can only be created using a dedicated constructor.' |
|
22 ! |
|
23 |
|
24 on: aSequenceableCollection |
|
25 ^ self on: aSequenceableCollection start: 1 stop: aSequenceableCollection size |
|
26 ! |
|
27 |
|
28 on: aSequenceableCollection start: aStartInteger stop: aStopInteger |
|
29 ^ self basicNew |
|
30 initializeOn: aSequenceableCollection |
|
31 start: aStartInteger stop: aStopInteger |
|
32 ! ! |
|
33 |
|
34 !PPToken methodsFor:'accessing'! |
|
35 |
|
36 collection |
|
37 "Answer the underlying collection of this token." |
|
38 |
|
39 ^ collection |
|
40 ! |
|
41 |
|
42 size |
|
43 "Answer the size of this token." |
|
44 |
|
45 ^ stop - start + 1 |
|
46 ! |
|
47 |
|
48 start |
|
49 "Answer the start position of this token in the underlying collection." |
|
50 |
|
51 ^ start |
|
52 ! |
|
53 |
|
54 stop |
|
55 "Answer the stop position of this token in the underlying collection." |
|
56 |
|
57 ^ stop |
|
58 ! |
|
59 |
|
60 value |
|
61 "Answer the contents of this token." |
|
62 |
|
63 ^ collection copyFrom: start to: stop |
|
64 ! ! |
|
65 |
|
66 !PPToken methodsFor:'comparing'! |
|
67 |
|
68 = anObject |
|
69 ^ self class = anObject class and: [ self value = anObject value ] |
|
70 ! |
|
71 |
|
72 hash |
|
73 ^ self value hash |
|
74 ! ! |
|
75 |
|
76 !PPToken methodsFor:'copying'! |
|
77 |
|
78 copyFrom: aStartInteger to: aStopInteger |
|
79 ^ self class on: collection start: start + aStartInteger - 1 stop: stop + aStopInteger - 3 |
|
80 ! ! |
|
81 |
|
82 !PPToken methodsFor:'initialization'! |
|
83 |
|
84 initializeOn: aSequenceableCollection start: aStartInteger stop: aStopInteger |
|
85 collection := aSequenceableCollection. |
|
86 start := aStartInteger. |
|
87 stop := aStopInteger |
|
88 ! ! |
|
89 |
|
90 !PPToken methodsFor:'printing'! |
|
91 |
|
92 printOn: aStream |
|
93 super printOn: aStream. |
|
94 aStream nextPut: $(; nextPutAll: self value; nextPut: $) |
|
95 ! ! |
|
96 |
|
97 !PPToken methodsFor:'private'! |
|
98 |
|
99 newline |
|
100 "Parser a platform independent newline sequence. LF: Unix, CR+LF: Windows, and CR: Apple." |
|
101 |
|
102 ^ (Character lf asParser) |
|
103 / (Character cr asParser , Character lf asParser optional) |
|
104 ! ! |
|
105 |
|
106 !PPToken methodsFor:'querying'! |
|
107 |
|
108 column |
|
109 "Answer the column number of this token in the underlying collection." |
|
110 |
|
111 | position | |
|
112 position := 0. |
|
113 (self newline , [ :stream | |
|
114 start <= stream position |
|
115 ifTrue: [ ^ start - position ]. |
|
116 position := stream position ] asParser |
|
117 / #any asParser) star |
|
118 parse: collection. |
|
119 ^ start - position |
|
120 ! |
|
121 |
|
122 line |
|
123 "Answer the line number of this token in the underlying collection." |
|
124 |
|
125 | line | |
|
126 line := 1. |
|
127 (self newline , [ :stream | |
|
128 start <= stream position |
|
129 ifTrue: [ ^ line ]. |
|
130 line := line + 1 ] asParser |
|
131 / #any asParser) star |
|
132 parse: collection. |
|
133 ^ line |
|
134 ! ! |
|
135 |
|
136 !PPToken class methodsFor:'documentation'! |
|
137 |
|
138 version_SVN |
|
139 ^ '$Id: PPToken.st,v 1.1 2011-08-18 18:56:17 cg Exp $' |
|
140 ! ! |