1
|
1 |
"{ Package: 'stx:goodies/smaCC' }"
|
|
2 |
|
|
3 |
"{ NameSpace: SmaCC }"
|
|
4 |
|
|
5 |
Object subclass:#SmaCCRHS
|
|
6 |
instanceVariableNames:'collection reduceAction variableNames'
|
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
|
9 |
category:'SmaCC-Parser Generator'
|
|
10 |
!
|
|
11 |
|
|
12 |
SmaCCRHS comment:'SmaCCRHS represents the right hand side of the production.
|
|
13 |
|
|
14 |
Instance variables:
|
|
15 |
collection <OrderedCollection of: SmaCCSymbol> the collection of symbols that represent the rhs
|
|
16 |
grammar <SmaCCGrammar> the grammar that the production is in
|
|
17 |
variableNames <Dictionary key: String value: Integer> the name of each symbol in the rhs. These names can be used in the {} code blocks.'
|
|
18 |
!
|
|
19 |
|
|
20 |
|
|
21 |
!SmaCCRHS class methodsFor:'instance creation'!
|
|
22 |
|
|
23 |
new
|
|
24 |
^(super new)
|
|
25 |
initialize;
|
|
26 |
yourself
|
|
27 |
! !
|
|
28 |
|
|
29 |
!SmaCCRHS methodsFor:'accessing'!
|
|
30 |
|
|
31 |
add: aGrammarSymbol
|
|
32 |
collection add: aGrammarSymbol
|
|
33 |
!
|
|
34 |
|
|
35 |
at: anInteger
|
|
36 |
^collection at: anInteger
|
|
37 |
!
|
|
38 |
|
|
39 |
do: aBlock
|
|
40 |
collection do: aBlock
|
|
41 |
!
|
|
42 |
|
|
43 |
firstTerminals
|
|
44 |
| items |
|
|
45 |
collection isEmpty ifTrue: [^Set with: SmaCCSymbol empty].
|
|
46 |
items := Set new.
|
|
47 |
1 to: collection size
|
|
48 |
do:
|
|
49 |
[:index |
|
|
50 |
items addAll: (collection at: index) firstTerminals.
|
|
51 |
(items includes: SmaCCSymbol empty)
|
|
52 |
ifTrue: [index < collection size ifTrue: [items remove: SmaCCSymbol empty]]
|
|
53 |
ifFalse: [^items]].
|
|
54 |
^items
|
|
55 |
!
|
|
56 |
|
|
57 |
nameLastItem: aString
|
|
58 |
variableNames at: aString put: collection size
|
|
59 |
!
|
|
60 |
|
|
61 |
reduceAction
|
|
62 |
^reduceAction
|
|
63 |
!
|
|
64 |
|
|
65 |
reduceAction: anObject
|
|
66 |
reduceAction := anObject
|
|
67 |
!
|
|
68 |
|
|
69 |
size
|
|
70 |
^collection size
|
|
71 |
! !
|
|
72 |
|
|
73 |
!SmaCCRHS methodsFor:'comparing'!
|
|
74 |
|
|
75 |
= aRHS
|
|
76 |
^self class = aRHS class and: [collection = aRHS collection]
|
|
77 |
!
|
|
78 |
|
|
79 |
hash
|
|
80 |
^self class hash bitXor: (collection hash bitShift: 14)
|
|
81 |
! !
|
|
82 |
|
|
83 |
!SmaCCRHS methodsFor:'initialize-release'!
|
|
84 |
|
|
85 |
initialize
|
|
86 |
collection := OrderedCollection new.
|
|
87 |
reduceAction := nil.
|
|
88 |
variableNames := Dictionary new
|
|
89 |
! !
|
|
90 |
|
|
91 |
!SmaCCRHS methodsFor:'printing'!
|
|
92 |
|
|
93 |
printOn: aStream
|
|
94 |
^collection do: [:each | each printOn: aStream]
|
|
95 |
separatedBy: [aStream space]
|
|
96 |
! !
|
|
97 |
|
|
98 |
!SmaCCRHS methodsFor:'private'!
|
|
99 |
|
|
100 |
collection
|
|
101 |
^collection
|
|
102 |
!
|
|
103 |
|
|
104 |
defaultReduceAction
|
|
105 |
^#reduceFor:
|
|
106 |
! !
|
|
107 |
|
|
108 |
!SmaCCRHS methodsFor:'public'!
|
|
109 |
|
|
110 |
compileSourceFor: aGrammarSymbol in: aClass
|
|
111 |
| action rewriter parseTree methodName |
|
|
112 |
action := self reduceAction.
|
|
113 |
action isNil ifTrue: [^self defaultReduceAction].
|
|
114 |
parseTree := RBParser parseExpression: action
|
|
115 |
onError:
|
|
116 |
[:s :p |
|
|
117 |
Smalltalk isSmalltalkX ifTrue:[
|
|
118 |
SmaCCCompilationNotification raiseSignal: 'Invalid Smalltalk code in reduction rule'
|
|
119 |
with: aGrammarSymbol name , ' : ' , self printString , '\\' withCRs , action.
|
|
120 |
] ifFalse:[
|
|
121 |
SmaCCCompilationNotification signal: 'Invalid Smalltalk code in reduction rule'
|
|
122 |
with: aGrammarSymbol name , ' : ' , self printString , '\\' withCRs , action.
|
|
123 |
].
|
|
124 |
^self defaultReduceAction].
|
|
125 |
(parseTree isLiteral and:
|
|
126 |
[parseTree value isSymbol
|
|
127 |
and: [parseTree value argumentCount <= 1 and: [aClass definesMethod: parseTree value]]])
|
|
128 |
ifTrue: [^parseTree value].
|
|
129 |
rewriter := self parseTreeRewriter.
|
|
130 |
rewriter executeTree: parseTree.
|
|
131 |
parseTree := rewriter tree.
|
|
132 |
methodName := ('reduceActionFor' , (self safeMethodNameFor: aGrammarSymbol)
|
|
133 |
, (aGrammarSymbol positionOf: self) printString , ':')
|
|
134 |
asSymbol.
|
|
135 |
parseTree isSequence
|
|
136 |
ifFalse: [parseTree := RBSequenceNode statements: (Array with: parseTree)].
|
|
137 |
parseTree := RBMethodNode
|
|
138 |
selector: methodName
|
|
139 |
arguments: (Array with: (RBVariableNode named: 'nodes'))
|
|
140 |
body: parseTree.
|
|
141 |
parseTree addReturn.
|
|
142 |
aClass compile: parseTree formattedCode classified: 'generated-reduction actions'.
|
|
143 |
^methodName
|
|
144 |
!
|
|
145 |
|
|
146 |
parseTreeRewriter
|
|
147 |
| rewriter |
|
|
148 |
rewriter := ParseTreeRewriter new.
|
|
149 |
1 to: self size
|
|
150 |
do:
|
|
151 |
[:i |
|
|
152 |
rewriter replace: i printString storeString
|
|
153 |
with: '(nodes at: ' , i printString , ')'].
|
|
154 |
variableNames keysAndValuesDo:
|
|
155 |
[:key :value |
|
|
156 |
rewriter replace: key with: '(nodes at: ' , value printString , ')'].
|
|
157 |
^rewriter
|
|
158 |
!
|
|
159 |
|
|
160 |
safeMethodNameFor: aGrammarSymbol
|
|
161 |
^aGrammarSymbol printString
|
|
162 |
collect: [:each | each isAlphaNumeric ifTrue: [each] ifFalse: [$_]]
|
|
163 |
! !
|
|
164 |
|
|
165 |
!SmaCCRHS class methodsFor:'documentation'!
|
|
166 |
|
|
167 |
version
|
|
168 |
^ '$Header: /opt/data/cvs/stx/goodies/smaCC/SmaCC__SmaCCRHS.st,v 1.1 2006-02-09 21:14:42 vranyj1 Exp $'
|
15
|
169 |
!
|
|
170 |
|
|
171 |
version_SVN
|
|
172 |
^ '$Id$'
|
1
|
173 |
! !
|