1
|
1 |
"{ Package: 'stx:goodies/smaCC' }"
|
|
2 |
|
|
3 |
"{ NameSpace: SmaCC }"
|
|
4 |
|
|
5 |
Object subclass:#SmaCCScanner
|
|
6 |
instanceVariableNames:'stream start matchActions matchEnd currentCharacter outputStream
|
|
7 |
lastOutputStreamMatchPosition lastMatchWasEmpty returnMatchBlock'
|
|
8 |
classVariableNames:''
|
|
9 |
poolDictionaries:''
|
|
10 |
category:'SmaCC-Runtime'
|
|
11 |
!
|
|
12 |
|
|
13 |
SmaCCScanner class instanceVariableNames:'keywordMap'
|
|
14 |
|
|
15 |
"
|
|
16 |
No other class instance variables are inherited by this class.
|
|
17 |
"
|
|
18 |
!
|
|
19 |
|
|
20 |
SmaCCScanner comment:'SmaCCScanner is an abstract class that represents a scanner for the parser. The scanner converts its string input into SmaCCToken objects that the parser then uses for its parsing.
|
|
21 |
|
|
22 |
Subclasses must implement the following messages:
|
|
23 |
accessing
|
|
24 |
scanForToken
|
|
25 |
|
|
26 |
Instance Variables:
|
|
27 |
currentCharacter <Character> the current character we are scanning
|
|
28 |
lastMatchWasEmpty <Boolean> was our last scanning match an empty string -- don''t allow two empty matches in a row
|
|
29 |
lastOutputStreamMatchPosition <Integer> the position in the outputStream of the last match
|
|
30 |
matchActions <Array | Symbol> the actions for the last match (a symbol means that the action should be performed on the scanner)
|
|
31 |
matchEnd <Integer> the position of the last match in the stream (our input stream)
|
|
32 |
outputStream <PositionableStream> the matched characters go in this stream. After a match is made, we take this stream''s contents and create a token object.
|
|
33 |
returnMatchBlock <BlockClosure> when we match a token evaluate this block with the token (hack to return from multiple levels)
|
|
34 |
start <Integer> the starting position of a match in the stream
|
|
35 |
stream <Stream> our input
|
|
36 |
|
|
37 |
'
|
|
38 |
!
|
|
39 |
|
|
40 |
|
|
41 |
!SmaCCScanner class methodsFor:'instance creation'!
|
|
42 |
|
|
43 |
new
|
|
44 |
^(super new)
|
|
45 |
initialize;
|
|
46 |
yourself
|
|
47 |
!
|
|
48 |
|
|
49 |
on: aStream
|
|
50 |
^(self new)
|
|
51 |
on: (self needsLineNumbers
|
|
52 |
ifTrue: [SmaCCLineNumberStream on: aStream]
|
|
53 |
ifFalse: [aStream]);
|
|
54 |
yourself
|
|
55 |
! !
|
|
56 |
|
|
57 |
!SmaCCScanner class methodsFor:'accessing'!
|
|
58 |
|
|
59 |
frequencyTable
|
|
60 |
^#(1)
|
|
61 |
!
|
|
62 |
|
|
63 |
keywordMap
|
|
64 |
keywordMap isNil ifTrue: [self initializeKeywordMap].
|
|
65 |
^keywordMap
|
|
66 |
! !
|
|
67 |
|
|
68 |
!SmaCCScanner class methodsFor:'class initialization'!
|
|
69 |
|
|
70 |
initialize
|
|
71 |
self initializeKeywordMap
|
|
72 |
!
|
|
73 |
|
|
74 |
initializeKeywordMap
|
|
75 |
keywordMap := Dictionary new
|
|
76 |
! !
|
|
77 |
|
|
78 |
!SmaCCScanner class methodsFor:'public'!
|
|
79 |
|
|
80 |
needsLineNumbers
|
|
81 |
"Redefine to return true, if you need line number information"
|
|
82 |
|
|
83 |
^false
|
|
84 |
! !
|
|
85 |
|
|
86 |
!SmaCCScanner methodsFor:'accessing'!
|
|
87 |
|
|
88 |
contents
|
|
89 |
| writeStream token |
|
|
90 |
writeStream := WriteStream on: Array new.
|
|
91 |
[self atEnd] whileFalse:
|
|
92 |
[token := self next.
|
|
93 |
token notNil ifTrue: [writeStream nextPut: token]].
|
|
94 |
^writeStream contents
|
|
95 |
!
|
|
96 |
|
|
97 |
emptySymbolTokenId
|
|
98 |
^self subclassResponsibility
|
|
99 |
!
|
|
100 |
|
|
101 |
errorTokenId
|
|
102 |
^self subclassResponsibility
|
|
103 |
!
|
|
104 |
|
|
105 |
lineNumber
|
|
106 |
"This requires the stream to be a line number stream (see the #needsLineNumbers class method)."
|
|
107 |
|
|
108 |
^stream lineNumber
|
|
109 |
!
|
|
110 |
|
|
111 |
next
|
|
112 |
self resetScanner.
|
|
113 |
returnMatchBlock := [:match | ^match].
|
|
114 |
self scanForToken
|
|
115 |
!
|
|
116 |
|
|
117 |
position
|
|
118 |
^stream position
|
|
119 |
!
|
|
120 |
|
|
121 |
position: anInteger
|
|
122 |
^stream position: anInteger
|
|
123 |
!
|
|
124 |
|
|
125 |
scanForToken
|
|
126 |
^self subclassResponsibility
|
2
|
127 |
!
|
|
128 |
|
|
129 |
stream
|
|
130 |
|
|
131 |
^stream
|
|
132 |
|
|
133 |
"Created: / 06-06-2008 / 20:51:09 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
1
|
134 |
! !
|
|
135 |
|
|
136 |
!SmaCCScanner methodsFor:'default token handling'!
|
|
137 |
|
|
138 |
comment
|
|
139 |
"In case someone wants to record the comments"
|
|
140 |
|
|
141 |
self whitespace
|
|
142 |
!
|
|
143 |
|
|
144 |
whitespace
|
|
145 |
"By default, eat the whitespace"
|
|
146 |
|
|
147 |
self resetScanner.
|
|
148 |
self scanForToken
|
|
149 |
! !
|
|
150 |
|
|
151 |
!SmaCCScanner methodsFor:'initialize-release'!
|
|
152 |
|
|
153 |
initialize
|
|
154 |
outputStream := WriteStream on: (String new: self initialBufferSize).
|
|
155 |
lastMatchWasEmpty := true
|
|
156 |
!
|
|
157 |
|
|
158 |
on: aStream
|
|
159 |
stream := aStream.
|
|
160 |
start := stream position
|
|
161 |
! !
|
|
162 |
|
|
163 |
!SmaCCScanner methodsFor:'private'!
|
|
164 |
|
|
165 |
checkForKeyword: aString
|
|
166 |
| stateMap action |
|
|
167 |
action := matchActions isSymbol
|
|
168 |
ifTrue: [matchActions]
|
|
169 |
ifFalse: [matchActions first].
|
|
170 |
stateMap := self class keywordMap at: action ifAbsent: [nil].
|
|
171 |
stateMap isNil ifTrue: [^self].
|
|
172 |
matchActions := stateMap at: (self keywordFor: aString)
|
|
173 |
ifAbsent: [matchActions].
|
|
174 |
matchActions isInteger
|
|
175 |
ifTrue: [matchActions := Array with: matchActions with: action]
|
|
176 |
!
|
|
177 |
|
|
178 |
checkForValidMatch
|
|
179 |
matchActions isNil ifTrue: [self scannerError]
|
|
180 |
!
|
|
181 |
|
|
182 |
createTokenFor: string
|
|
183 |
| token |
|
|
184 |
token := SmaCCToken
|
|
185 |
value: string
|
|
186 |
start: start
|
|
187 |
id: matchActions.
|
|
188 |
outputStream reset.
|
|
189 |
matchActions := nil.
|
|
190 |
returnMatchBlock value: token
|
|
191 |
!
|
|
192 |
|
|
193 |
initialBufferSize
|
|
194 |
^128
|
|
195 |
!
|
|
196 |
|
|
197 |
recordAndReportMatch: aCollection
|
|
198 |
self
|
|
199 |
recordMatch: aCollection;
|
|
200 |
reportLastMatch
|
|
201 |
!
|
|
202 |
|
|
203 |
recordMatch: aCollection
|
|
204 |
matchActions := aCollection.
|
|
205 |
matchEnd := stream position.
|
|
206 |
lastOutputStreamMatchPosition := outputStream position
|
|
207 |
!
|
|
208 |
|
|
209 |
reportLastMatch
|
|
210 |
"The scanner has found the end of a token and must report it"
|
|
211 |
|
|
212 |
| string |
|
|
213 |
self checkForValidMatch.
|
|
214 |
self resetOutputToLastMatch.
|
|
215 |
stream position: matchEnd.
|
|
216 |
string := outputStream contents.
|
|
217 |
Smalltalk isSmalltalkX ifTrue:[
|
|
218 |
outputStream reset.
|
|
219 |
].
|
|
220 |
self checkForKeyword: string.
|
|
221 |
|
|
222 |
matchActions isSymbol
|
|
223 |
ifTrue: [self perform: matchActions]
|
|
224 |
ifFalse: [self createTokenFor: string]
|
|
225 |
!
|
|
226 |
|
|
227 |
resetOutputToLastMatch
|
|
228 |
|streamStartPosition|
|
|
229 |
|
|
230 |
outputStream position: lastOutputStreamMatchPosition.
|
|
231 |
Smalltalk isSmalltalkX ifTrue:[
|
|
232 |
streamStartPosition := outputStream class zeroPosition.
|
|
233 |
] ifFalse:[
|
|
234 |
streamStartPosition := 0.
|
|
235 |
].
|
|
236 |
lastOutputStreamMatchPosition == streamStartPosition
|
|
237 |
ifTrue:
|
|
238 |
[lastMatchWasEmpty ifTrue: [self scannerError].
|
|
239 |
lastMatchWasEmpty := true]
|
|
240 |
ifFalse: [lastMatchWasEmpty := false]
|
|
241 |
!
|
|
242 |
|
|
243 |
resetScanner
|
|
244 |
start := stream position.
|
|
245 |
outputStream reset.
|
|
246 |
Smalltalk isSmalltalkX ifTrue:[
|
|
247 |
lastOutputStreamMatchPosition := outputStream class zeroPosition .
|
|
248 |
] ifFalse:[
|
|
249 |
lastOutputStreamMatchPosition := 0 .
|
|
250 |
].
|
|
251 |
!
|
|
252 |
|
|
253 |
scannerError
|
|
254 |
(stream atEnd and: [start == stream position])
|
|
255 |
ifTrue:
|
|
256 |
[returnMatchBlock value: (SmaCCToken
|
|
257 |
value: ''
|
|
258 |
start: stream position
|
|
259 |
id: (Array with: self emptySymbolTokenId))].
|
|
260 |
stream position: start.
|
|
261 |
returnMatchBlock value: (SmaCCToken
|
|
262 |
value: (String with: stream next)
|
|
263 |
start: start
|
|
264 |
id: #(0))
|
|
265 |
!
|
|
266 |
|
|
267 |
step
|
|
268 |
stream atEnd ifTrue: [^self reportLastMatch].
|
|
269 |
currentCharacter := stream next.
|
|
270 |
outputStream nextPut: currentCharacter
|
|
271 |
! !
|
|
272 |
|
|
273 |
!SmaCCScanner methodsFor:'private-utility'!
|
|
274 |
|
|
275 |
keywordFor: aString
|
|
276 |
"Subclasses can override this to ignore case"
|
|
277 |
|
|
278 |
^aString
|
|
279 |
! !
|
|
280 |
|
|
281 |
!SmaCCScanner methodsFor:'public'!
|
|
282 |
|
|
283 |
atEnd
|
|
284 |
^stream atEnd
|
|
285 |
! !
|
|
286 |
|
|
287 |
!SmaCCScanner class methodsFor:'documentation'!
|
|
288 |
|
|
289 |
version
|
|
290 |
^ '$Header: /opt/data/cvs/stx/goodies/smaCC/SmaCC__SmaCCScanner.st,v 1.1 2006-02-09 21:15:31 vranyj1 Exp $'
|
15
|
291 |
!
|
|
292 |
|
|
293 |
version_SVN
|
|
294 |
^ '$Id$'
|
1
|
295 |
! !
|
|
296 |
|
|
297 |
SmaCCScanner initialize!
|