1
|
1 |
"{ Package: 'stx:goodies/smaCC' }"
|
|
2 |
|
|
3 |
"{ NameSpace: SmaCC }"
|
|
4 |
|
|
5 |
Object subclass:#SmaCCScannerCompiler
|
|
6 |
instanceVariableNames:'grammar scannerClass scannerDefinitionString'
|
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
|
9 |
category:'SmaCC-Parser Generator'
|
|
10 |
!
|
|
11 |
|
|
12 |
SmaCCScannerCompiler comment:'SmaCCScannerCompiler represents the compiler for the scanner part of the parser.
|
|
13 |
|
|
14 |
Instance Variables:
|
|
15 |
grammar <SmaCCGrammar> the grammar that defines the scanner
|
|
16 |
scannerClass <RBAbstractClass> the class to compile the scanner into
|
|
17 |
scannerDefinitionString <String> the string that defines the scanner'
|
|
18 |
!
|
|
19 |
|
|
20 |
|
|
21 |
!SmaCCScannerCompiler methodsFor:'accessing'!
|
|
22 |
|
|
23 |
scannerClass
|
|
24 |
^scannerClass
|
|
25 |
!
|
|
26 |
|
|
27 |
scannerClass: anObject
|
|
28 |
scannerClass := anObject
|
|
29 |
!
|
|
30 |
|
|
31 |
scannerDefinitionString: aString
|
|
32 |
scannerDefinitionString := aString
|
|
33 |
!
|
|
34 |
|
|
35 |
symbols
|
|
36 |
^grammar symbols
|
|
37 |
! !
|
|
38 |
|
|
39 |
!SmaCCScannerCompiler methodsFor:'compiling-scanner'!
|
|
40 |
|
|
41 |
addActionsForSymbols
|
|
42 |
| selectorMap |
|
|
43 |
selectorMap := Dictionary new.
|
|
44 |
(1 to: self symbols size) with: self symbols
|
|
45 |
do:
|
|
46 |
[:index :each | |eachRegex selector|
|
|
47 |
|
|
48 |
eachRegex := each regularExpression.
|
|
49 |
eachRegex notNil ifTrue:
|
|
50 |
[
|
|
51 |
selector := (self scannerActionFor: each name).
|
|
52 |
selector notNil ifTrue: [selectorMap at: index put: selector].
|
|
53 |
eachRegex action: index].
|
|
54 |
each createIdMethod ifTrue: [self compileTokenIdMethodFor: each]].
|
|
55 |
^selectorMap
|
|
56 |
!
|
|
57 |
|
|
58 |
addSpecialSymbols
|
|
59 |
grammar tokens keysDo:
|
|
60 |
[:each |
|
|
61 |
(self scannerActionFor: each) notNil
|
|
62 |
ifTrue: [grammar terminalSymbolNamed: each]]
|
|
63 |
!
|
|
64 |
|
|
65 |
compileEmptySymbolTokenId
|
|
66 |
| stream |
|
|
67 |
stream := WriteStream on: (String new: 100).
|
|
68 |
stream
|
|
69 |
nextPutAll: 'emptySymbolTokenId';
|
|
70 |
cr;
|
|
71 |
tab;
|
|
72 |
nextPutAll: '^';
|
|
73 |
nextPutAll: (self symbols identityIndexOf: SmaCCSymbol empty) printString.
|
|
74 |
self scannerClass compile: stream contents classified: #'generated-tokens'
|
|
75 |
|
|
76 |
"Modified: / 06-09-2005 / 20:41:47 / janfrog"
|
|
77 |
!
|
|
78 |
|
|
79 |
compileErrorSymbolTokenId
|
|
80 |
| stream |
|
|
81 |
stream := WriteStream on: (String new: 100).
|
|
82 |
stream
|
|
83 |
nextPutAll: 'errorTokenId';
|
|
84 |
cr;
|
|
85 |
tab;
|
|
86 |
nextPutAll: '^';
|
|
87 |
nextPutAll: (self symbols identityIndexOf: SmaCCSymbol error) printString.
|
|
88 |
self scannerClass compile: stream contents classified: #'generated-tokens'
|
|
89 |
|
|
90 |
"Modified: / 06-09-2005 / 20:41:58 / janfrog"
|
|
91 |
!
|
|
92 |
|
|
93 |
compileKeywordInitializerUsing: aDictionary selectorMap: selectorMapDictionary
|
|
94 |
| stream dataStream |
|
|
95 |
aDictionary isEmpty ifTrue: [^self].
|
|
96 |
stream := WriteStream on: String new.
|
|
97 |
stream nextPutAll: 'initializeKeywordMap'.
|
|
98 |
stream nextPutAll: ' keywordMap := Dictionary new. '.
|
|
99 |
dataStream := WriteStream on: Array new.
|
|
100 |
aDictionary keysAndValuesDo:
|
|
101 |
[:key :value |
|
|
102 |
value keys asSortedCollection do:
|
|
103 |
[:each |
|
|
104 |
dataStream nextPut: (Array
|
|
105 |
with: (selectorMapDictionary at: key ifAbsent: [key])
|
|
106 |
with: each
|
|
107 |
with: (selectorMapDictionary at: (value at: each) ifAbsent: [value at: each]))]].
|
|
108 |
stream
|
|
109 |
nextPutAll: '#(';
|
|
110 |
cr.
|
|
111 |
dataStream contents do:
|
|
112 |
[:each |
|
|
113 |
stream nextPutAll: '#('.
|
|
114 |
each do: [:item | item storeOn: stream] separatedBy: [stream nextPut: $ ].
|
|
115 |
stream nextPut: $)]
|
|
116 |
separatedBy: [stream cr].
|
|
117 |
stream nextPut: $).
|
|
118 |
stream
|
|
119 |
nextPutAll: ' do: [:each | (keywordMap at: each first ifAbsentPut: [Dictionary new]) at: (each at: 2) put: each last]. ^keywordMap'.
|
|
120 |
scannerClass metaclass
|
|
121 |
compile: (RBParser parseMethod: stream contents) formattedCode
|
|
122 |
classified: #'generated-initialization'
|
|
123 |
!
|
|
124 |
|
|
125 |
compileScanner
|
|
126 |
| regex selectorMap regexPartition dfa keywordmap |
|
|
127 |
regex := nil.
|
|
128 |
self addSpecialSymbols.
|
|
129 |
grammar
|
|
130 |
symbols: (self symbols asSortedCollection:
|
|
131 |
[:a :b |
|
|
132 |
| aRegex bRegex |
|
|
133 |
aRegex := a regularExpression.
|
|
134 |
bRegex := b regularExpression.
|
|
135 |
bRegex isNil or:
|
|
136 |
[aRegex notNil
|
|
137 |
and: [(aRegex position ifNil: [0]) < (bRegex position ifNil: [0])]]]).
|
|
138 |
selectorMap := self addActionsForSymbols.
|
|
139 |
regexPartition := self regularExpressionsPartitionedByIsKeyword.
|
|
140 |
regex := self generalRegexesFrom: regexPartition.
|
|
141 |
dfa := regex asDFA.
|
|
142 |
keywordmap := Dictionary new.
|
|
143 |
(regexPartition at: true ifAbsent: [#()]) do:
|
|
144 |
[:each |
|
|
145 |
| action strings newAction |
|
|
146 |
strings := each possibleMatches.
|
|
147 |
1 to: strings size
|
|
148 |
do:
|
|
149 |
[:i |
|
|
150 |
newAction := dfa simulate: (ReadStream on: (strings at: i)).
|
|
151 |
i = 1
|
|
152 |
ifTrue: [action := newAction]
|
|
153 |
ifFalse: [action = newAction ifFalse: [action := nil]]].
|
|
154 |
action isNil
|
|
155 |
ifTrue: [regex := regex | each]
|
|
156 |
ifFalse:
|
|
157 |
[action do:
|
|
158 |
[:state |
|
|
159 |
strings do:
|
|
160 |
[:string |
|
|
161 |
(keywordmap at: state ifAbsentPut: [Dictionary new]) at: string
|
|
162 |
put: each action]]]].
|
|
163 |
self compileKeywordInitializerUsing: keywordmap selectorMap: selectorMap.
|
|
164 |
regex asDFA compileInto: scannerClass usingSelectorMap: selectorMap.
|
|
165 |
self compileEmptySymbolTokenId.
|
|
166 |
self compileErrorSymbolTokenId.
|
|
167 |
SmaCCGrammar ignoreCase
|
|
168 |
ifTrue:
|
|
169 |
[scannerClass compile: 'keywordFor: aString
|
|
170 |
^aString asUppercase'
|
|
171 |
classified: #'generated-scanner']
|
|
172 |
!
|
|
173 |
|
|
174 |
compileScannerDefinitionComment
|
|
175 |
| stream |
|
|
176 |
stream := WriteStream on: (String new: 1000).
|
|
177 |
stream
|
|
178 |
nextPutAll: 'scannerDefinitionComment';
|
|
179 |
cr;
|
|
180 |
cr;
|
|
181 |
tab;
|
|
182 |
nextPut: $";
|
|
183 |
nextPutAll: (scannerDefinitionString copyReplaceAll: $" withAll:'""');
|
|
184 |
nextPut: $".
|
|
185 |
self scannerClass metaclass compile: stream contents
|
|
186 |
classified: #'generated-comments'
|
|
187 |
!
|
|
188 |
|
|
189 |
compileTokenIdMethodFor: aTerminalSymbol
|
|
190 |
| stream |
|
|
191 |
stream := WriteStream on: (String new: 1000).
|
|
192 |
stream
|
|
193 |
nextPutAll: (aTerminalSymbol name copyFrom: 2
|
|
194 |
to: aTerminalSymbol name size - 1);
|
|
195 |
nextPutAll: 'Id';
|
|
196 |
cr;
|
|
197 |
tab;
|
|
198 |
nextPutAll: '^';
|
|
199 |
nextPutAll: (self symbols identityIndexOf: aTerminalSymbol) printString.
|
|
200 |
scannerClass compile: stream contents classified: #'generated-tokens'
|
|
201 |
|
|
202 |
"Modified: / 06-09-2005 / 20:42:43 / janfrog"
|
|
203 |
!
|
|
204 |
|
|
205 |
generalRegexesFrom: regexPartition
|
|
206 |
| regexs combinedRegex |
|
|
207 |
regexs := regexPartition at: false ifAbsent: [regexPartition at: true].
|
|
208 |
combinedRegex := regexs first.
|
|
209 |
2 to: regexs size
|
|
210 |
do: [:i | combinedRegex := combinedRegex | (regexs at: i)].
|
|
211 |
^combinedRegex
|
|
212 |
!
|
|
213 |
|
|
214 |
regularExpressionsPartitionedByIsKeyword
|
|
215 |
| regexPartition |
|
|
216 |
regexPartition := Dictionary new.
|
|
217 |
self symbols do:
|
|
218 |
[:each | |regex|
|
|
219 |
regex := each regularExpression.
|
|
220 |
regex notNil ifTrue:
|
|
221 |
[
|
|
222 |
(regexPartition at: regex isKeywordLiteral
|
|
223 |
ifAbsentPut: [OrderedCollection new]) add: regex]].
|
|
224 |
^regexPartition
|
|
225 |
!
|
|
226 |
|
|
227 |
scannerActionFor: aString
|
|
228 |
| selector |
|
|
229 |
aString size > 2 ifFalse: [^nil].
|
|
230 |
selector := (aString copyFrom: 2 to: aString size - 1) asSymbol.
|
|
231 |
^((scannerClass definesMethod: selector)
|
|
232 |
and: [(Object canUnderstand: selector) not and: [selector argumentCount = 0]])
|
|
233 |
ifTrue: [selector]
|
|
234 |
ifFalse: [nil]
|
|
235 |
! !
|
|
236 |
|
|
237 |
!SmaCCScannerCompiler methodsFor:'initialize-release'!
|
|
238 |
|
|
239 |
grammar: aGrammar
|
|
240 |
grammar := aGrammar
|
|
241 |
!
|
|
242 |
|
|
243 |
parseTokens
|
|
244 |
grammar tokens: (SmaCCScannerParser parse: scannerDefinitionString)
|
|
245 |
! !
|
|
246 |
|
|
247 |
!SmaCCScannerCompiler class methodsFor:'documentation'!
|
|
248 |
|
|
249 |
version
|
|
250 |
^ '$Header: /opt/data/cvs/stx/goodies/smaCC/SmaCC__SmaCCScannerCompiler.st,v 1.1 2006-02-09 21:14:22 vranyj1 Exp $'
|
15
|
251 |
!
|
|
252 |
|
|
253 |
version_SVN
|
|
254 |
^ '$Id$'
|
1
|
255 |
! !
|