author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 07 Dec 2016 13:18:16 +0000 | |
changeset 26 | b2c091b8cea1 |
parent 25 | 5a6921729520 |
permissions | -rw-r--r-- |
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 |
||
25
5a6921729520
UI improvements: added menu items to show FIRST() and LAST() sets for non-terminal symbols.
vranyj1
parents:
23
diff
changeset
|
57 |
lastTerminals |
5a6921729520
UI improvements: added menu items to show FIRST() and LAST() sets for non-terminal symbols.
vranyj1
parents:
23
diff
changeset
|
58 |
| items | |
5a6921729520
UI improvements: added menu items to show FIRST() and LAST() sets for non-terminal symbols.
vranyj1
parents:
23
diff
changeset
|
59 |
collection isEmpty ifTrue: [^Set with: SmaCCSymbol empty]. |
5a6921729520
UI improvements: added menu items to show FIRST() and LAST() sets for non-terminal symbols.
vranyj1
parents:
23
diff
changeset
|
60 |
items := Set new. |
5a6921729520
UI improvements: added menu items to show FIRST() and LAST() sets for non-terminal symbols.
vranyj1
parents:
23
diff
changeset
|
61 |
collection size downTo: 1 |
5a6921729520
UI improvements: added menu items to show FIRST() and LAST() sets for non-terminal symbols.
vranyj1
parents:
23
diff
changeset
|
62 |
do: |
5a6921729520
UI improvements: added menu items to show FIRST() and LAST() sets for non-terminal symbols.
vranyj1
parents:
23
diff
changeset
|
63 |
[:index | |
5a6921729520
UI improvements: added menu items to show FIRST() and LAST() sets for non-terminal symbols.
vranyj1
parents:
23
diff
changeset
|
64 |
items addAll: (collection at: index) lastTerminals. |
5a6921729520
UI improvements: added menu items to show FIRST() and LAST() sets for non-terminal symbols.
vranyj1
parents:
23
diff
changeset
|
65 |
(items includes: SmaCCSymbol empty) |
5a6921729520
UI improvements: added menu items to show FIRST() and LAST() sets for non-terminal symbols.
vranyj1
parents:
23
diff
changeset
|
66 |
ifTrue: [index > 1 ifTrue: [items remove: SmaCCSymbol empty]] |
5a6921729520
UI improvements: added menu items to show FIRST() and LAST() sets for non-terminal symbols.
vranyj1
parents:
23
diff
changeset
|
67 |
ifFalse: [^items]]. |
5a6921729520
UI improvements: added menu items to show FIRST() and LAST() sets for non-terminal symbols.
vranyj1
parents:
23
diff
changeset
|
68 |
^items |
5a6921729520
UI improvements: added menu items to show FIRST() and LAST() sets for non-terminal symbols.
vranyj1
parents:
23
diff
changeset
|
69 |
|
5a6921729520
UI improvements: added menu items to show FIRST() and LAST() sets for non-terminal symbols.
vranyj1
parents:
23
diff
changeset
|
70 |
"Created: / 24-02-2016 / 09:51:46 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
5a6921729520
UI improvements: added menu items to show FIRST() and LAST() sets for non-terminal symbols.
vranyj1
parents:
23
diff
changeset
|
71 |
! |
5a6921729520
UI improvements: added menu items to show FIRST() and LAST() sets for non-terminal symbols.
vranyj1
parents:
23
diff
changeset
|
72 |
|
1 | 73 |
nameLastItem: aString |
74 |
variableNames at: aString put: collection size |
|
75 |
! |
|
76 |
||
77 |
reduceAction |
|
78 |
^reduceAction |
|
79 |
! |
|
80 |
||
81 |
reduceAction: anObject |
|
82 |
reduceAction := anObject |
|
83 |
! |
|
84 |
||
85 |
size |
|
86 |
^collection size |
|
87 |
! ! |
|
88 |
||
89 |
!SmaCCRHS methodsFor:'comparing'! |
|
90 |
||
91 |
= aRHS |
|
92 |
^self class = aRHS class and: [collection = aRHS collection] |
|
93 |
! |
|
94 |
||
95 |
hash |
|
96 |
^self class hash bitXor: (collection hash bitShift: 14) |
|
97 |
! ! |
|
98 |
||
99 |
!SmaCCRHS methodsFor:'initialize-release'! |
|
100 |
||
101 |
initialize |
|
102 |
collection := OrderedCollection new. |
|
103 |
reduceAction := nil. |
|
104 |
variableNames := Dictionary new |
|
105 |
! ! |
|
106 |
||
107 |
!SmaCCRHS methodsFor:'printing'! |
|
108 |
||
109 |
printOn: aStream |
|
110 |
^collection do: [:each | each printOn: aStream] |
|
111 |
separatedBy: [aStream space] |
|
112 |
! ! |
|
113 |
||
114 |
!SmaCCRHS methodsFor:'private'! |
|
115 |
||
116 |
collection |
|
117 |
^collection |
|
118 |
! |
|
119 |
||
120 |
defaultReduceAction |
|
121 |
^#reduceFor: |
|
122 |
! ! |
|
123 |
||
124 |
!SmaCCRHS methodsFor:'public'! |
|
125 |
||
126 |
compileSourceFor: aGrammarSymbol in: aClass |
|
127 |
| action rewriter parseTree methodName | |
|
128 |
action := self reduceAction. |
|
129 |
action isNil ifTrue: [^self defaultReduceAction]. |
|
130 |
parseTree := RBParser parseExpression: action |
|
131 |
onError: |
|
132 |
[:s :p | |
|
133 |
Smalltalk isSmalltalkX ifTrue:[ |
|
134 |
SmaCCCompilationNotification raiseSignal: 'Invalid Smalltalk code in reduction rule' |
|
135 |
with: aGrammarSymbol name , ' : ' , self printString , '\\' withCRs , action. |
|
136 |
] ifFalse:[ |
|
137 |
SmaCCCompilationNotification signal: 'Invalid Smalltalk code in reduction rule' |
|
138 |
with: aGrammarSymbol name , ' : ' , self printString , '\\' withCRs , action. |
|
139 |
]. |
|
140 |
^self defaultReduceAction]. |
|
141 |
(parseTree isLiteral and: |
|
142 |
[parseTree value isSymbol |
|
143 |
and: [parseTree value argumentCount <= 1 and: [aClass definesMethod: parseTree value]]]) |
|
144 |
ifTrue: [^parseTree value]. |
|
145 |
rewriter := self parseTreeRewriter. |
|
146 |
rewriter executeTree: parseTree. |
|
147 |
parseTree := rewriter tree. |
|
148 |
methodName := ('reduceActionFor' , (self safeMethodNameFor: aGrammarSymbol) |
|
149 |
, (aGrammarSymbol positionOf: self) printString , ':') |
|
150 |
asSymbol. |
|
151 |
parseTree isSequence |
|
152 |
ifFalse: [parseTree := RBSequenceNode statements: (Array with: parseTree)]. |
|
153 |
parseTree := RBMethodNode |
|
154 |
selector: methodName |
|
155 |
arguments: (Array with: (RBVariableNode named: 'nodes')) |
|
156 |
body: parseTree. |
|
157 |
parseTree addReturn. |
|
158 |
aClass compile: parseTree formattedCode classified: 'generated-reduction actions'. |
|
159 |
^methodName |
|
160 |
! |
|
161 |
||
162 |
parseTreeRewriter |
|
23 | 163 |
| rewriter | |
164 |
rewriter := ParseTreeRewriter basicNew initialize. |
|
165 |
1 to: self size |
|
166 |
do: |
|
167 |
[:i | |
|
168 |
rewriter replace: i printString storeString |
|
169 |
with: '(nodes at: ' , i printString , ')']. |
|
170 |
variableNames keysAndValuesDo: |
|
171 |
[:key :value | |
|
172 |
rewriter replace: key with: '(nodes at: ' , value printString , ')']. |
|
173 |
^rewriter |
|
174 |
||
175 |
"Modified: / 10-09-2012 / 10:04:36 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
1 | 176 |
! |
177 |
||
178 |
safeMethodNameFor: aGrammarSymbol |
|
179 |
^aGrammarSymbol printString |
|
180 |
collect: [:each | each isAlphaNumeric ifTrue: [each] ifFalse: [$_]] |
|
181 |
! ! |
|
182 |
||
183 |
!SmaCCRHS class methodsFor:'documentation'! |
|
184 |
||
185 |
version |
|
23 | 186 |
^ '$Id$' |
15 | 187 |
! |
188 |
||
189 |
version_SVN |
|
190 |
^ '$Id$' |
|
1 | 191 |
! ! |
25
5a6921729520
UI improvements: added menu items to show FIRST() and LAST() sets for non-terminal symbols.
vranyj1
parents:
23
diff
changeset
|
192 |