1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package com.unitesk.atp.treedl;
33
34 import java.util.Iterator;
35 import java.util.Map;
36 import java.util.Properties;
37 import java.util.StringTokenizer;
38
39 import com.unitesk.atp.dynattrs.Accessor;
40 import com.unitesk.atp.treedl.TDL.*;
41 import com.unitesk.atp.tool.PluginClass;
42
43 /***
44 * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
45 * @version $Id: PrintVisitor.java,v 1.12 2005/11/23 11:28:52 all-x Exp $
46 */
47 public class PrintVisitor extends TextGenerator implements TDL_Visitor
48 {
49 /***
50 * {@inheritDoc}
51 */
52 public String getVersion()
53 {
54 return PluginClass.getRevision( "$Revision: 1.12 $" );
55 }
56
57 public void setOutDir( String dir ) { throw new UnsupportedOperationException(); }
58
59 protected void block()
60 {
61 txt( "{" );
62 incIndent();
63 }
64
65 protected void unblock()
66 {
67 popIndent();
68 txt( "}" );
69 }
70
71 protected void block_nl()
72 {
73 block(); nl();
74 }
75
76 protected void unblock_nl()
77 {
78 unblock(); nl();
79 }
80
81 protected void printDocomment()
82 {
83 if( Accessor.getAttribute( getNode(), "optDocomment" ) != null )
84 {
85 txt( "${optDocomment}" ); nl();
86 }
87 }
88
89 private void escapeKeywords( String s )
90 {
91 StringTokenizer st = new StringTokenizer( s, "." );
92 String t = null;
93 while( st.hasMoreTokens() )
94 {
95 if( t != null ) txtAsIs( "." );
96 t = st.nextToken();
97 if( TDL_Module.isKeyword( t ) ) txtAsIs( "@" );
98 txtAsIs( t );
99 }
100 }
101
102 protected void printProperties()
103 {
104 Properties props = (Properties)Accessor.getAttribute( getNode(), "optProperties" );
105 if( props == null ) return;
106 for( Iterator i = props.entrySet().iterator(); i.hasNext(); )
107 {
108 Map.Entry entry = (Map.Entry)i.next();
109 txtAsIs( "[ " );
110 escapeKeywords( (String)entry.getKey() );
111 txtAsIs( " = \"" + entry.getValue() + "\"; ]" ); nl();
112 }
113 }
114
115 public void visitModule( Module node )
116 {
117 printDocomment();
118 printProperties();
119
120 txtif( node.isTree(), "tree", "module" );
121 txt( " ${name}" );
122 if( node.sizeOptBaseModuleList() > 0 || node.sizeOptBaseCustomTypeList() > 0 )
123 {
124 txt( " : " );
125 list( "i", 0, node.sizeOptBaseModuleList(), "${optBaseModuleList[i]}", ", " );
126 txtif( node.sizeOptBaseModuleList() > 0 && node.sizeOptBaseCustomTypeList() > 0, ", " );
127 list( "i", 0, node.sizeOptBaseCustomTypeList(), "<${optBaseCustomTypeList[i]}>", ", " );
128 }
129 txt( ";" ); nl();
130 if( node.getOptHeader() != null )
131 {
132 nl();
133 txt( "header" ); nl();
134 block();
135 txt( "${optHeader}" );
136 unblock_nl();
137 }
138 if( node.getOptBody() != null )
139 {
140 nl();
141 txt( "body" ); nl();
142 block();
143 txt( "${optBody}" );
144 unblock_nl();
145 }
146 nl();
147 list( "i", 0, node.sizeOptMemberList(), "${optMemberList[i]}", endl );
148 }
149
150 public void visitBaseModule( BaseModule node )
151 {
152 txtif( node.getOptSynonym(), "${optSynonym} = " );
153 txt( "${name}" );
154 }
155
156 public void visitConstTypeDecl( ConstTypeDecl node )
157 {
158 printDocomment();
159 printProperties();
160 txtif( node.isFlags(), "flags", "enum" );
161 txt( " ${name}" );
162 txtif( node.getOptBaseType(), " : ${optBaseType}" ); nl();
163 block_nl();
164 list( "i", 0, node.sizeOptConstantList(), "${optConstantList[i]}", "," + endl ); nl();
165 unblock_nl();
166 }
167
168 public void visitNodeTypeDecl( NodeTypeDecl node )
169 {
170 printDocomment();
171 printProperties();
172 txtif( node.isRoot(), "root " );
173 txtif( node.isAbstract(), "abstract " );
174 txt( "node ${name}" );
175 if( node.getOptBaseType() != null || node.sizeOptBaseCustomTypeList() > 0 )
176 {
177 txt( " : " );
178 txtif( node.getOptBaseType(), "${optBaseType}" );
179 txtif( node.getOptBaseType() != null && node.sizeOptBaseCustomTypeList() > 0, ", " );
180 list( "i", 0, node.sizeOptBaseCustomTypeList(), "<${optBaseCustomTypeList[i]}>", ", " );
181 }
182 nl();
183 block_nl();
184 list( "i", 0, node.sizeOptMemberList(), "${optMemberList[i]}", endl );
185 unblock_nl();
186 }
187
188 public void visitConstructorCodeMember( ConstructorCodeMember node )
189 {
190 printDocomment();
191 printProperties();
192 txt( "constructor" ); nl();
193 block();
194 txt( "${code}" );
195 unblock_nl();
196 }
197
198 public void visitBodyCodeMember( BodyCodeMember node )
199 {
200 printDocomment();
201 printProperties();
202 txt( "body" ); nl();
203 block();
204 txt( "${code}" );
205 unblock_nl();
206 }
207
208 public void visitField( Field node )
209 {
210 printDocomment();
211 printProperties();
212 txtif( node.isChild(), "child", "attribute" );
213 txtif( node.checkModifiers( Modifiers.ABSTRACT ), " abstract" );
214 txtif( node.checkModifiers( Modifiers.CUSTOM ), " custom" );
215 txtif( node.checkModifiers( Modifiers.LATE ), " late" );
216 txtif( node.checkModifiers( Modifiers.OVERRIDE ), " override" );
217 txtif( node.checkModifiers( Modifiers.SETONCE ), " setonce" );
218 txtif( node.checkModifiers( Modifiers.NOSET ), " noset" );
219 txt( " ${type} ${name}" );
220 txtif( node.getOptInitCode(), " = {${optInitCode}}" );
221 if( node.getOptGetCode() != null )
222 {
223 nl();
224 txt( "get" ); nl();
225 txt( "{${optGetCode}}" );
226 }
227 if( node.getOptSetCode() != null )
228 {
229 nl();
230 txt( "set" ); nl();
231 txt( "{${optSetCode}}" );
232 }
233 txt( ";" ); nl();
234 }
235
236 public void visitOperationDecl( OperationDecl node )
237 {
238 printDocomment();
239 printProperties();
240 txtif( node.isVirtual(), "virtual " );
241 txt( "operation ${returnType?void} ${name}(" ); nl();
242 list( "i", 0, node.sizeOptParameterDeclList(), "${optParameterDeclList[i]}", endl + ", " ); nl();
243 txt( ")" ); nl();
244 if( node.sizeOptOperationRefList() > 0 )
245 {
246 incIndent();
247 txt( ": " );
248 list( "i", 0, node.sizeOptOperationRefList(), "${optOperationRefList[i]}", ", " ); nl();
249 popIndent();
250 }
251 block_nl();
252 list( "i", 0, node.sizeOptCaseList(), "${optCaseList[i]}", endl );
253 unblock_nl();
254 }
255
256 public void visitVirtualParameterDecl( VirtualParameterDecl node )
257 {
258 txt( "virtual ${type} ${name}" );
259 }
260
261 public void visitNonVirtualParameterDecl( NonVirtualParameterDecl node )
262 {
263 txt( "${type} ${name}" );
264 }
265
266 public void visitOperationRef( OperationRef node )
267 {
268 txtif( node.getOptModuleName(), "${optModuleName}." );
269 txt( "${name}" );
270 }
271
272 public void visitCase( Case node )
273 {
274 list( "i", 0, node.sizeCaseSignatureList(), "${caseSignatureList[i]}", endl ); nl();
275 block();
276 txt( "${code}" );
277 unblock_nl();
278 }
279
280 public void visitCaseSignature( CaseSignature node )
281 {
282 txt( "case( " );
283 list( "i", 0, node.sizeParameterList(), "${parameterList[i]}", ", " );
284 txt( " ):" );
285 }
286
287 public void visitParameter( Parameter node )
288 {
289 txtif( node.getOptType(), "${optType} " );
290 txt( "${name}" );
291 }
292
293 public void visitType( Type node )
294 {
295 switch( node.getCardinality().key() )
296 {
297 case Cardinality.Key.OPTIONAL:
298 txt( "?" );
299 break;
300 case Cardinality.Key.LIST:
301 txt( "+" );
302 break;
303 case Cardinality.Key.OPTIONAL | Cardinality.Key.LIST:
304 txt( "*" );
305 break;
306 default:
307 }
308 }
309
310 public void visitNodeType( NodeType node )
311 {
312 txt( "${type}" );
313 visitType( node );
314 }
315
316 public void visitNameType( NameType node )
317 {
318 txt( "<${name}>" );
319 visitType( node );
320 }
321
322 public void visitPredefinedType( PredefinedType node )
323 {
324 switch( node.getKind().key() )
325 {
326 case PredefinedTypeKind.Key.OBJECT:
327 txt( "object" );
328 break;
329 case PredefinedTypeKind.Key.BOOL:
330 txt( "bool" );
331 break;
332 case PredefinedTypeKind.Key.STRING:
333 txt( "string" );
334 break;
335 case PredefinedTypeKind.Key.CHAR:
336 txt( "char" );
337 break;
338 case PredefinedTypeKind.Key.SHORT:
339 txt( "short" );
340 break;
341 case PredefinedTypeKind.Key.INT:
342 txt( "int" );
343 break;
344 case PredefinedTypeKind.Key.LONG:
345 txt( "long" );
346 break;
347 case PredefinedTypeKind.Key.FLOAT:
348 txt( "float" );
349 break;
350 case PredefinedTypeKind.Key.DOUBLE:
351 txt( "double" );
352 break;
353 default:
354 }
355 visitType( node );
356 }
357
358 public void visitTypeRef( TypeRef node )
359 {
360 txtif( node.getOptModuleName(), "${optModuleName}." );
361 txt( "${name}" );
362 }
363
364 public void visitID( ID node )
365 {
366 txtif( TDL_Module.isKeyword( node.toString() ), "@" );
367 txt( "${value}" );
368 }
369
370 public void visitQID( QID node )
371 {
372 list( "i", 0, node.sizeIdList(), "${idList[i]}", "." );
373 }
374
375 public void visitCode( Code node )
376 {
377 txt( "${code}" );
378 }
379 }