1
|
1 |
"{ Package: 'stx:goodies/smaCC' }"
|
|
2 |
|
|
3 |
"{ NameSpace: SmaCC }"
|
|
4 |
|
|
5 |
SmaCCScanner subclass:#CScanner
|
|
6 |
instanceVariableNames:'typeNames'
|
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
|
9 |
category:'SmaCC-Example Parsers'
|
|
10 |
!
|
|
11 |
|
|
12 |
|
|
13 |
!CScanner class methodsFor:'generated-comments'!
|
|
14 |
|
|
15 |
scannerDefinitionComment
|
|
16 |
|
|
17 |
"<H>: [a-fA-F0-9] ;
|
|
18 |
<IS>: [uUlL] ;
|
|
19 |
<D>: [0-9] ;
|
|
20 |
<E>: [Ee][\+\-]?<D>+ ;
|
|
21 |
<FS>: [fFlL] ;
|
|
22 |
<ELLIPSIS> : \.\.\. ;
|
|
23 |
<comment>: \/ \* ;
|
|
24 |
<LETTER> : [a-zA-Z_] ;
|
|
25 |
<DIGIT> : [0-9] ;
|
|
26 |
<IDENTIFIER>: <LETTER> (<LETTER>|<DIGIT>)* ;
|
|
27 |
<CONSTANT>: (0[xX]<H>+<IS>?) |
|
|
28 |
(<D>+<IS>?) |
|
|
29 |
(<D>+<E><FS>?) |
|
|
30 |
<D>*\.<D>+<E>?<FS>? |
|
|
31 |
(L? \' ( (\\ .) | ([^\\\""]) )+ \') ;
|
|
32 |
|
|
33 |
<STRING_LITERAL>: L? \"" ( (\\ .) | ([^\\\""]) )* \"" ;
|
|
34 |
|
|
35 |
<LEFT_BLOCK>: \[ | (\< \:) ;
|
|
36 |
<RIGHT_BLOCK>: \] | (\: \>) ;
|
|
37 |
<LEFT_BRACE>: \{ | (\< \%) ;
|
|
38 |
<RIGHT_BRACE>: \} | (\% \>) ;
|
|
39 |
|
|
40 |
<whitespace>: \s+ ;"
|
|
41 |
! !
|
|
42 |
|
|
43 |
!CScanner class methodsFor:'generated-initialization'!
|
|
44 |
|
|
45 |
initializeKeywordMap
|
|
46 |
keywordMap := Dictionary new.
|
|
47 |
#(#(#IDENTIFIER 'auto' 62) #(#IDENTIFIER 'break' 37) #(#IDENTIFIER 'case' 45) #(#IDENTIFIER 'char' 67) #(#IDENTIFIER 'const' 38) #(#IDENTIFIER 'continue' 49) #(#IDENTIFIER 'default' 41) #(#IDENTIFIER 'do' 48) #(#IDENTIFIER 'double' 52) #(#IDENTIFIER 'else' 43) #(#IDENTIFIER 'enum' 42) #(#IDENTIFIER 'extern' 60) #(#IDENTIFIER 'float' 71) #(#IDENTIFIER 'for' 47) #(#IDENTIFIER 'goto' 50) #(#IDENTIFIER 'if' 51) #(#IDENTIFIER 'int' 69) #(#IDENTIFIER 'long' 68) #(#IDENTIFIER 'register' 73) #(#IDENTIFIER 'return' 65) #(#IDENTIFIER 'short' 66) #(#IDENTIFIER 'signed' 46) #(#IDENTIFIER 'sizeof' 32) #(#IDENTIFIER 'static' 70) #(#IDENTIFIER 'struct' 44) #(#IDENTIFIER 'switch' 54) #(#IDENTIFIER 'typedef' 64) #(#IDENTIFIER 'union' 39) #(#IDENTIFIER 'unsigned' 55) #(#IDENTIFIER 'void' 72) #(#IDENTIFIER 'volatile' 40) #(#IDENTIFIER 'while' 53))
|
|
48 |
do:
|
|
49 |
[:each |
|
|
50 |
(keywordMap at: each first ifAbsentPut: [Dictionary new]) at: (each at: 2) put: each last].
|
|
51 |
^keywordMap
|
|
52 |
! !
|
|
53 |
|
|
54 |
!CScanner class methodsFor:'public'!
|
|
55 |
|
|
56 |
needsLineNumbers
|
|
57 |
^true
|
|
58 |
! !
|
|
59 |
|
|
60 |
!CScanner methodsFor:'default token handling'!
|
|
61 |
|
|
62 |
comment
|
|
63 |
| char |
|
|
64 |
|
|
65 |
[[(char := stream next) isNil or: [char == $*]] whileFalse.
|
|
66 |
stream atEnd ifTrue: [self scannerError].
|
|
67 |
stream peekFor: $/]
|
|
68 |
whileFalse.
|
|
69 |
^self whitespace
|
|
70 |
! !
|
|
71 |
|
|
72 |
!CScanner methodsFor:'generated-scanner'!
|
|
73 |
|
|
74 |
scan1
|
|
75 |
self step.
|
|
76 |
currentCharacter isDigit ifTrue: [^self scan2].
|
|
77 |
^self reportLastMatch
|
|
78 |
!
|
|
79 |
|
|
80 |
scan2
|
|
81 |
|
|
82 |
[self recordMatch: #(77).
|
|
83 |
self step.
|
|
84 |
currentCharacter isDigit] whileTrue.
|
|
85 |
(currentCharacter == $F
|
|
86 |
or: [currentCharacter == $L or: [currentCharacter == $f or: [currentCharacter == $l]]])
|
|
87 |
ifTrue: [^self recordAndReportMatch: #(77)].
|
|
88 |
(currentCharacter == $E or: [currentCharacter == $e]) ifTrue: [^self scan3].
|
|
89 |
^self reportLastMatch
|
|
90 |
!
|
|
91 |
|
|
92 |
scan3
|
|
93 |
self step.
|
|
94 |
currentCharacter isDigit ifTrue: [^self scan4].
|
|
95 |
(currentCharacter == $+ or: [currentCharacter == $-])
|
|
96 |
ifTrue:
|
|
97 |
[self step.
|
|
98 |
currentCharacter isDigit ifTrue: [^self scan4].
|
|
99 |
^self reportLastMatch].
|
|
100 |
^self reportLastMatch
|
|
101 |
!
|
|
102 |
|
|
103 |
scan4
|
|
104 |
|
|
105 |
[self recordMatch: #(77).
|
|
106 |
self step.
|
|
107 |
currentCharacter isDigit] whileTrue.
|
|
108 |
(currentCharacter == $F
|
|
109 |
or: [currentCharacter == $L or: [currentCharacter == $f or: [currentCharacter == $l]]])
|
|
110 |
ifTrue: [^self recordAndReportMatch: #(77)].
|
|
111 |
^self reportLastMatch
|
|
112 |
!
|
|
113 |
|
|
114 |
scan5
|
|
115 |
self step.
|
|
116 |
(currentCharacter <= $!!
|
|
117 |
or: [(currentCharacter between: $# and: $[) or: [currentCharacter >= $]]])
|
|
118 |
ifTrue: [^self scan7].
|
|
119 |
currentCharacter == $\ ifTrue: [^self scan6].
|
|
120 |
^self reportLastMatch
|
|
121 |
!
|
|
122 |
|
|
123 |
scan6
|
|
124 |
self step.
|
|
125 |
^self scan7
|
|
126 |
!
|
|
127 |
|
|
128 |
scan7
|
|
129 |
|
|
130 |
[self step.
|
|
131 |
currentCharacter <= $!! or:
|
|
132 |
[(currentCharacter between: $# and: $&)
|
|
133 |
or: [(currentCharacter between: $( and: $[) or: [currentCharacter >= $]]]]]
|
|
134 |
whileTrue.
|
|
135 |
currentCharacter == $'
|
|
136 |
ifTrue:
|
|
137 |
[
|
|
138 |
[self recordMatch: #(77).
|
|
139 |
self step.
|
|
140 |
(currentCharacter <= $!! or:
|
|
141 |
[(currentCharacter between: $# and: $&)
|
|
142 |
or: [(currentCharacter between: $( and: $[) or: [currentCharacter >= $]]]])
|
|
143 |
ifTrue: [^self scan7].
|
|
144 |
currentCharacter == $']
|
|
145 |
whileTrue.
|
|
146 |
currentCharacter == $\ ifTrue: [^self scan6].
|
|
147 |
^self reportLastMatch].
|
|
148 |
currentCharacter == $\ ifTrue: [^self scan6].
|
|
149 |
^self reportLastMatch
|
|
150 |
!
|
|
151 |
|
|
152 |
scan8
|
|
153 |
|
|
154 |
[
|
|
155 |
[self step.
|
|
156 |
currentCharacter <= $!!
|
|
157 |
or: [(currentCharacter between: $# and: $[) or: [currentCharacter >= $]]]]
|
|
158 |
whileTrue.
|
|
159 |
currentCharacter == $" ifTrue: [^self recordAndReportMatch: #(78)].
|
|
160 |
currentCharacter == $\]
|
|
161 |
whileTrue: [self step].
|
|
162 |
^self reportLastMatch
|
|
163 |
!
|
|
164 |
|
|
165 |
scan9
|
|
166 |
|
|
167 |
[self recordMatch: #(77).
|
|
168 |
self step.
|
|
169 |
currentCharacter isDigit] whileTrue.
|
|
170 |
(currentCharacter == $L
|
|
171 |
or: [currentCharacter == $U or: [currentCharacter == $l or: [currentCharacter == $u]]])
|
|
172 |
ifTrue: [^self recordAndReportMatch: #(77)].
|
|
173 |
(currentCharacter == $E or: [currentCharacter == $e]) ifTrue: [^self scan3].
|
|
174 |
currentCharacter == $. ifTrue: [^self scan1].
|
|
175 |
^self reportLastMatch
|
|
176 |
!
|
|
177 |
|
|
178 |
scanForToken
|
|
179 |
self step.
|
|
180 |
((currentCharacter between: $A and: $K) or:
|
|
181 |
[(currentCharacter between: $M and: $Z)
|
|
182 |
or: [currentCharacter == $_ or: [currentCharacter between: $a and: $z]]])
|
|
183 |
ifTrue:
|
|
184 |
[
|
|
185 |
[self recordMatch: #IDENTIFIER.
|
|
186 |
self step.
|
|
187 |
currentCharacter isHexDigit or:
|
|
188 |
[(currentCharacter between: $G and: $Z)
|
|
189 |
or: [currentCharacter == $_ or: [currentCharacter between: $a and: $z]]]]
|
|
190 |
whileTrue.
|
|
191 |
^self reportLastMatch].
|
|
192 |
(currentCharacter between: $1 and: $9) ifTrue: [^self scan9].
|
|
193 |
((currentCharacter between: $ and: $
|
|
194 |
) or: [currentCharacter == $ ])
|
|
195 |
ifTrue:
|
|
196 |
[
|
|
197 |
[self recordMatch: #whitespace.
|
|
198 |
self step.
|
|
199 |
(currentCharacter between: $ and: $
|
|
200 |
) or: [currentCharacter == $ ]]
|
|
201 |
whileTrue.
|
|
202 |
^self reportLastMatch].
|
|
203 |
currentCharacter == $!!
|
|
204 |
ifTrue:
|
|
205 |
[self recordMatch: #(31).
|
|
206 |
self step.
|
|
207 |
currentCharacter == $= ifTrue: [^self recordAndReportMatch: #(3)].
|
|
208 |
^self reportLastMatch].
|
|
209 |
currentCharacter == $" ifTrue: [^self scan8].
|
|
210 |
currentCharacter == $%
|
|
211 |
ifTrue:
|
|
212 |
[self recordMatch: #(13).
|
|
213 |
self step.
|
|
214 |
currentCharacter == $= ifTrue: [^self recordAndReportMatch: #(36)].
|
|
215 |
currentCharacter == $> ifTrue: [^self recordAndReportMatch: #(82)].
|
|
216 |
^self reportLastMatch].
|
|
217 |
currentCharacter == $&
|
|
218 |
ifTrue:
|
|
219 |
[self recordMatch: #(24).
|
|
220 |
self step.
|
|
221 |
currentCharacter == $& ifTrue: [^self recordAndReportMatch: #(6)].
|
|
222 |
currentCharacter == $= ifTrue: [^self recordAndReportMatch: #(58)].
|
|
223 |
^self reportLastMatch].
|
|
224 |
currentCharacter == $' ifTrue: [^self scan5].
|
|
225 |
currentCharacter == $( ifTrue: [^self recordAndReportMatch: #(25)].
|
|
226 |
currentCharacter == $) ifTrue: [^self recordAndReportMatch: #(20)].
|
|
227 |
currentCharacter == $*
|
|
228 |
ifTrue:
|
|
229 |
[self recordMatch: #(35).
|
|
230 |
self step.
|
|
231 |
currentCharacter == $= ifTrue: [^self recordAndReportMatch: #(10)].
|
|
232 |
^self reportLastMatch].
|
|
233 |
currentCharacter == $+
|
|
234 |
ifTrue:
|
|
235 |
[self recordMatch: #(34).
|
|
236 |
self step.
|
|
237 |
currentCharacter == $+ ifTrue: [^self recordAndReportMatch: #(21)].
|
|
238 |
currentCharacter == $= ifTrue: [^self recordAndReportMatch: #(18)].
|
|
239 |
^self reportLastMatch].
|
|
240 |
currentCharacter == $, ifTrue: [^self recordAndReportMatch: #(22)].
|
|
241 |
currentCharacter == $-
|
|
242 |
ifTrue:
|
|
243 |
[self recordMatch: #(29).
|
|
244 |
self step.
|
|
245 |
currentCharacter == $- ifTrue: [^self recordAndReportMatch: #(26)].
|
|
246 |
currentCharacter == $= ifTrue: [^self recordAndReportMatch: #(56)].
|
|
247 |
currentCharacter == $> ifTrue: [^self recordAndReportMatch: #(19)].
|
|
248 |
^self reportLastMatch].
|
|
249 |
currentCharacter == $.
|
|
250 |
ifTrue:
|
|
251 |
[self recordMatch: #(23).
|
|
252 |
self step.
|
|
253 |
currentCharacter isDigit ifTrue: [^self scan2].
|
|
254 |
currentCharacter == $.
|
|
255 |
ifTrue:
|
|
256 |
[self step.
|
|
257 |
currentCharacter == $. ifTrue: [^self recordAndReportMatch: #(74)].
|
|
258 |
^self reportLastMatch].
|
|
259 |
^self reportLastMatch].
|
|
260 |
currentCharacter == $/
|
|
261 |
ifTrue:
|
|
262 |
[self recordMatch: #(30).
|
|
263 |
self step.
|
|
264 |
currentCharacter == $* ifTrue: [^self recordAndReportMatch: #comment].
|
|
265 |
currentCharacter == $= ifTrue: [^self recordAndReportMatch: #(12)].
|
|
266 |
^self reportLastMatch].
|
|
267 |
currentCharacter == $0
|
|
268 |
ifTrue:
|
|
269 |
[self recordMatch: #(77).
|
|
270 |
self step.
|
|
271 |
currentCharacter isDigit ifTrue: [^self scan9].
|
|
272 |
(currentCharacter == $L
|
|
273 |
or: [currentCharacter == $U or: [currentCharacter == $l or: [currentCharacter == $u]]])
|
|
274 |
ifTrue: [^self recordAndReportMatch: #(77)].
|
|
275 |
(currentCharacter == $E or: [currentCharacter == $e]) ifTrue: [^self scan3].
|
|
276 |
(currentCharacter == $X or: [currentCharacter == $x])
|
|
277 |
ifTrue:
|
|
278 |
[self step.
|
|
279 |
(currentCharacter isHexDigit or: [currentCharacter between: $a and: $f])
|
|
280 |
ifTrue:
|
|
281 |
[
|
|
282 |
[self recordMatch: #(77).
|
|
283 |
self step.
|
|
284 |
currentCharacter isHexDigit or: [currentCharacter between: $a and: $f]]
|
|
285 |
whileTrue.
|
|
286 |
(currentCharacter == $L
|
|
287 |
or: [currentCharacter == $U or: [currentCharacter == $l or: [currentCharacter == $u]]])
|
|
288 |
ifTrue: [^self recordAndReportMatch: #(77)].
|
|
289 |
^self reportLastMatch].
|
|
290 |
^self reportLastMatch].
|
|
291 |
currentCharacter == $. ifTrue: [^self scan1].
|
|
292 |
^self reportLastMatch].
|
|
293 |
currentCharacter == $:
|
|
294 |
ifTrue:
|
|
295 |
[self recordMatch: #(11).
|
|
296 |
self step.
|
|
297 |
currentCharacter == $> ifTrue: [^self recordAndReportMatch: #(80)].
|
|
298 |
^self reportLastMatch].
|
|
299 |
currentCharacter == $; ifTrue: [^self recordAndReportMatch: #(59)].
|
|
300 |
currentCharacter == $<
|
|
301 |
ifTrue:
|
|
302 |
[self recordMatch: #(17).
|
|
303 |
self step.
|
|
304 |
currentCharacter == $% ifTrue: [^self recordAndReportMatch: #(81)].
|
|
305 |
currentCharacter == $: ifTrue: [^self recordAndReportMatch: #(79)].
|
|
306 |
currentCharacter == $<
|
|
307 |
ifTrue:
|
|
308 |
[self recordMatch: #(27).
|
|
309 |
self step.
|
|
310 |
currentCharacter == $= ifTrue: [^self recordAndReportMatch: #(33)].
|
|
311 |
^self reportLastMatch].
|
|
312 |
currentCharacter == $= ifTrue: [^self recordAndReportMatch: #(2)].
|
|
313 |
^self reportLastMatch].
|
|
314 |
currentCharacter == $=
|
|
315 |
ifTrue:
|
|
316 |
[self recordMatch: #(14).
|
|
317 |
self step.
|
|
318 |
currentCharacter == $= ifTrue: [^self recordAndReportMatch: #(1)].
|
|
319 |
^self reportLastMatch].
|
|
320 |
currentCharacter == $>
|
|
321 |
ifTrue:
|
|
322 |
[self recordMatch: #(7).
|
|
323 |
self step.
|
|
324 |
currentCharacter == $= ifTrue: [^self recordAndReportMatch: #(5)].
|
|
325 |
currentCharacter == $>
|
|
326 |
ifTrue:
|
|
327 |
[self recordMatch: #(9).
|
|
328 |
self step.
|
|
329 |
currentCharacter == $= ifTrue: [^self recordAndReportMatch: #(63)].
|
|
330 |
^self reportLastMatch].
|
|
331 |
^self reportLastMatch].
|
|
332 |
currentCharacter == $? ifTrue: [^self recordAndReportMatch: #(16)].
|
|
333 |
currentCharacter == $L
|
|
334 |
ifTrue:
|
|
335 |
[self recordMatch: #IDENTIFIER.
|
|
336 |
self step.
|
|
337 |
(currentCharacter isHexDigit or:
|
|
338 |
[(currentCharacter between: $G and: $Z)
|
|
339 |
or: [currentCharacter == $_ or: [currentCharacter between: $a and: $z]]])
|
|
340 |
ifTrue:
|
|
341 |
[
|
|
342 |
[self recordMatch: #IDENTIFIER.
|
|
343 |
self step.
|
|
344 |
currentCharacter isHexDigit or:
|
|
345 |
[(currentCharacter between: $G and: $Z)
|
|
346 |
or: [currentCharacter == $_ or: [currentCharacter between: $a and: $z]]]]
|
|
347 |
whileTrue.
|
|
348 |
^self reportLastMatch].
|
|
349 |
currentCharacter == $" ifTrue: [^self scan8].
|
|
350 |
currentCharacter == $' ifTrue: [^self scan5].
|
|
351 |
^self reportLastMatch].
|
|
352 |
currentCharacter == $[ ifTrue: [^self recordAndReportMatch: #(79)].
|
|
353 |
currentCharacter == $] ifTrue: [^self recordAndReportMatch: #(80)].
|
|
354 |
currentCharacter == $^
|
|
355 |
ifTrue:
|
|
356 |
[self recordMatch: #(8).
|
|
357 |
self step.
|
|
358 |
currentCharacter == $= ifTrue: [^self recordAndReportMatch: #(61)].
|
|
359 |
^self reportLastMatch].
|
|
360 |
currentCharacter == ${ ifTrue: [^self recordAndReportMatch: #(81)].
|
|
361 |
currentCharacter == $|
|
|
362 |
ifTrue:
|
|
363 |
[self recordMatch: #(4).
|
|
364 |
self step.
|
|
365 |
currentCharacter == $= ifTrue: [^self recordAndReportMatch: #(57)].
|
|
366 |
currentCharacter == $| ifTrue: [^self recordAndReportMatch: #(15)].
|
|
367 |
^self reportLastMatch].
|
|
368 |
currentCharacter == $} ifTrue: [^self recordAndReportMatch: #(82)].
|
|
369 |
currentCharacter == $~ ifTrue: [^self recordAndReportMatch: #(28)].
|
|
370 |
^self reportLastMatch
|
|
371 |
! !
|
|
372 |
|
|
373 |
!CScanner methodsFor:'generated-tokens'!
|
|
374 |
|
|
375 |
CONSTANTId
|
|
376 |
^77
|
|
377 |
!
|
|
378 |
|
|
379 |
IDENTIFIERId
|
|
380 |
^76
|
|
381 |
!
|
|
382 |
|
|
383 |
TYPE_NAMEId
|
|
384 |
^131
|
|
385 |
!
|
|
386 |
|
|
387 |
emptySymbolTokenId
|
|
388 |
^146
|
|
389 |
!
|
|
390 |
|
|
391 |
errorTokenId
|
|
392 |
^147
|
|
393 |
! !
|
|
394 |
|
|
395 |
!CScanner methodsFor:'initialize-release'!
|
|
396 |
|
|
397 |
initialize
|
|
398 |
super initialize.
|
|
399 |
typeNames := Set new
|
|
400 |
! !
|
|
401 |
|
|
402 |
!CScanner methodsFor:'public'!
|
|
403 |
|
|
404 |
IDENTIFIER
|
|
405 |
| name token |
|
|
406 |
name := outputStream contents.
|
|
407 |
matchActions := (typeNames includes: name)
|
|
408 |
ifTrue: [Array with: self TYPE_NAMEId]
|
|
409 |
ifFalse: [Array with: self IDENTIFIERId].
|
|
410 |
outputStream reset.
|
|
411 |
token := SmaCCToken
|
|
412 |
value: name
|
|
413 |
start: start
|
|
414 |
id: matchActions.
|
|
415 |
matchActions := nil.
|
|
416 |
returnMatchBlock value: token
|
|
417 |
!
|
|
418 |
|
|
419 |
addTypeName: aString
|
|
420 |
typeNames add: aString
|
|
421 |
! !
|
|
422 |
|
|
423 |
!CScanner class methodsFor:'documentation'!
|
|
424 |
|
|
425 |
version
|
|
426 |
^ '$Header: /opt/data/cvs/stx/goodies/smaCC/SmaCC__CScanner.st,v 1.1 2006-02-09 21:13:27 vranyj1 Exp $'
|
|
427 |
! !
|