Debian Codegraph @

src/main/java/com/eprosima/idl/context/Context.java

raw ◀ ▶
1 Debian patch touch this file
1 // Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package com.eprosima.idl.context;
16
17 import java.io.File;
18 import java.io.StringReader;
19 import java.nio.file.Paths;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.LinkedHashSet;
27 import java.util.LinkedList;
28 import java.util.Map;
29 import java.util.NoSuchElementException;
30 import java.util.Properties;
31 import java.util.Scanner;
32 import java.util.Stack;
33 import javax.script.ScriptEngineManager;
34 import javax.script.ScriptEngine;
35 import javax.script.ScriptException;
36 import org.antlr.v4.runtime.Token;
37 import org.stringtemplate.v4.STGroupFile;
38
39 import com.eprosima.idl.generator.manager.TemplateGroup;
40 import com.eprosima.idl.generator.manager.TemplateManager;
41 import com.eprosima.idl.parser.exception.ParseException;
42 import com.eprosima.idl.parser.tree.Annotation;
43 import com.eprosima.idl.parser.tree.AnnotationDeclaration;
44 import com.eprosima.idl.parser.tree.AnnotationMember;
45 import com.eprosima.idl.parser.tree.ConstDeclaration;
46 import com.eprosima.idl.parser.tree.Definition;
47 import com.eprosima.idl.parser.tree.Interface;
48 import com.eprosima.idl.parser.tree.Operation;
49 import com.eprosima.idl.parser.tree.Param;
50 import com.eprosima.idl.parser.tree.TypeDeclaration;
51 import com.eprosima.idl.parser.tree.TreeNode;
52 import com.eprosima.idl.parser.typecode.AliasTypeCode;
53 import com.eprosima.idl.parser.typecode.AnyTypeCode;
54 import com.eprosima.idl.parser.typecode.ArrayTypeCode;
55 import com.eprosima.idl.parser.typecode.BitfieldSpec;
56 import com.eprosima.idl.parser.typecode.BitsetTypeCode;
57 import com.eprosima.idl.parser.typecode.Bitmask;
58 import com.eprosima.idl.parser.typecode.BitmaskTypeCode;
59 import com.eprosima.idl.parser.typecode.EnumMember;
60 import com.eprosima.idl.parser.typecode.EnumTypeCode;
61 import com.eprosima.idl.parser.typecode.Kind;
62 import com.eprosima.idl.parser.typecode.MapTypeCode;
63 import com.eprosima.idl.parser.typecode.PrimitiveTypeCode;
64 import com.eprosima.idl.parser.typecode.SequenceTypeCode;
65 import com.eprosima.idl.parser.typecode.SetTypeCode;
66 import com.eprosima.idl.parser.typecode.StringTypeCode;
67 import com.eprosima.idl.parser.typecode.StructTypeCode;
68 import com.eprosima.idl.parser.typecode.TypeCode;
69 import com.eprosima.idl.parser.typecode.UnionTypeCode;
70 import com.eprosima.idl.util.Pair;
71 import com.eprosima.idl.util.Util;
72
73 public class Context
74 {
75 /*!
76 * Built-in custom property to specify a TemplateGroup is using explicitly modules (in C++ when opening the
77 * namespace "namespace module {")
78 */
79 public static final String using_explicitly_modules_custom_property = "using_explicitly_modules";
80
81 public Context(
82 TemplateManager tmanager,
83 String file,
84 ArrayList<String> includePaths,
85 boolean generate_typesc
86 )
87 {
88 tmanager_ = tmanager;
89 generate_typesc_ = generate_typesc;
90
91 // Detect OS
92 m_os = System.getProperty("os.name");
93 m_userdir = System.getProperty("user.dir");
94
95 m_filename = Util.getIDLFileNameOnly(file).replace('.', '_').replace('-', '_');
96 m_directoryFile = Util.getIDLFileDirectoryOnly(file);
97 m_file = file;
98
99 // Remove absolute directory where the application was executed
100 if (startsWith(m_file, m_userdir))
101 {
102 m_file = m_file.substring(m_userdir.length());
103
104 // Remove possible separator
105 if (startsWith(m_file, java.io.File.separator))
106 {
107 m_file = m_file.substring(1);
108 }
109 }
110 /*
111 // Remove relative directory if is equal that where the processed IDL is.
112 if(m_directoryFile != null && startsWith(m_file, m_directoryFile))
113 m_file = m_file.substring(m_directoryFile.length());
114 */
115
116 m_keywords = new HashSet<String>();
117 fillKeywords();
118
119 // TODO Quitar porque solo es para tipos (previous c) (usado para las excepciones). Mirar alternativa.
120 m_includedependency = new HashSet<String>();
121
122 // The scope file has to be initialized because could occur the preprocessor
123 // is not called (using -ppDisable).
124 m_scopeFile = Paths.get(m_file).normalize().toString();
125
126 m_includePaths = new ArrayList<String>();
127 m_dependencies = new LinkedHashSet<String>();
128 m_directIncludeDependencies = new HashSet<String>();
129 m_scopeFilesStack = new Stack<Pair<String, Integer>>();
130 for (int i = 0; i < includePaths.size(); ++i)
131 {
132 String include = includePaths.get(i);
133 if (startsWith(include, includeFlag))
134 {
135 include = include.substring(includeFlag.length());
136 }
137 if (startsWith(include, m_userdir))
138 {
139 include = include.substring(m_userdir.length());
140
141 // Remove possible separator
142 if (startsWith(include, java.io.File.separator))
143 {
144 include = include.substring(1);
145 }
146 }
147 if (m_directoryFile != null && startsWith(include, m_directoryFile))
148 {
149 include = include.substring(m_directoryFile.length());
150 }
151 m_includePaths.add(Paths.get(include).normalize().toString() + java.io.File.separator);
152 }
153
154 // Reorder include paths;
155 int pointer = 0;
156 while (pointer < m_includePaths.size())
157 {
158 int count = pointer + 1;
159
160 while (count < m_includePaths.size())
161 {
162 if (startsWith(m_includePaths.get(count), m_includePaths.get(pointer)))
163 {
164 String first = m_includePaths.get(pointer);
165 String second = m_includePaths.get(count);
166 m_includePaths.set(pointer, second);
167 m_includePaths.set(count, first);
168 break;
169 }
170 ++count;
171 }
172
173 if (count == m_includePaths.size())
174 {
175 ++pointer;
176 }
177 }
178
179 // Load IDL types for stringtemplates
180 TypeCode.ctx = this;
181 TypeCode.idltypesgr = new STGroupFile("com/eprosima/idl/templates/idlTypes.stg", '$', '$');
182 if (generate_typesc)
183 {
184 TypeCode.cpptypesgr = new STGroupFile("com/eprosima/idl/templates/TypesCInterface.stg", '$', '$');
185 }
186 else
187 {
188 TypeCode.cpptypesgr = new STGroupFile("com/eprosima/idl/templates/Types.stg", '$', '$');
189 }
190 TypeCode.ctypesgr = new STGroupFile("com/eprosima/idl/templates/CTypes.stg", '$', '$');
191 TypeCode.javatypesgr = new STGroupFile("com/eprosima/idl/templates/JavaTypes.stg", '$', '$');
192
193 // Builtin annotations (IDL 4.2 - 8.3 section & XTypes 1.3 - 7.3.1.2.1 section)
194
195 //{{{ @ami
196 AnnotationDeclaration amiann = createAnnotationDeclaration(Annotation.ami_str, null);
197 amiann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_BOOLEAN), Annotation.true_str));
198 //}}}
199
200 //{{{ @autoid
201 AnnotationDeclaration autoidann = createAnnotationDeclaration(Annotation.autoid_str, null);
202 EnumTypeCode autoidannenum = new EnumTypeCode(autoidann.getScopedname(), Annotation.autoid_enum_str);
203 autoidannenum.addMember(new EnumMember(Annotation.autoid_sequential_str));
204 autoidannenum.addMember(new EnumMember(Annotation.autoid_hash_str));
205 autoidann.addMember(new AnnotationMember(Annotation.value_str, autoidannenum, Annotation.autoid_hash_str));
206 //}}}
207
208 //{{{ @bit_bound
209 AnnotationDeclaration bit_boundann = createAnnotationDeclaration(Annotation.bit_bound_str, null);
210 bit_boundann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_USHORT), Annotation.null_default_value));
211 //}}}
212
213 //{{{ @data_representation
214 BitmaskTypeCode data_representation_mask_bitmask = new BitmaskTypeCode(null, Annotation.data_representation_mask_str);
215 Bitmask xcdr1_bitmask = new Bitmask(data_representation_mask_bitmask, Annotation.xcdr1_bitflag_str);
216 xcdr1_bitmask.setPosition(0);
217 data_representation_mask_bitmask.addBitmask(xcdr1_bitmask);
218 Bitmask xml_bitmask = new Bitmask(data_representation_mask_bitmask, Annotation.xml_bitflag_str);
219 xml_bitmask.setPosition(1);
220 data_representation_mask_bitmask.addBitmask(xml_bitmask);
221 Bitmask xcdr2_bitmask = new Bitmask(data_representation_mask_bitmask, Annotation.xcdr2_bitflag_str);
222 xcdr2_bitmask.setPosition(2);
223 data_representation_mask_bitmask.addBitmask(xcdr2_bitmask);
224
225 AnnotationDeclaration data_representation_annotation = createAnnotationDeclaration(Annotation.data_representation_str, null);
226 data_representation_annotation.addMember(new AnnotationMember(Annotation.allowed_kinds_str, data_representation_mask_bitmask, Annotation.empty_str));
227 //}}}
228
229 //{{{ @default
230 AnnotationDeclaration defaultann = createAnnotationDeclaration(Annotation.default_str, null);
231 defaultann.addMember(new AnnotationMember(Annotation.value_str, new AnyTypeCode(), null));
232 //}}}
233
234 //{{{ @default_literal
235 createAnnotationDeclaration(Annotation.default_literal_str, null);
236 //}}}
237
238 //{{{ @default_nested
239 AnnotationDeclaration default_nested_annotation = createAnnotationDeclaration(Annotation.default_nested_str, null);
240 default_nested_annotation.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_BOOLEAN), Annotation.true_str));
241 //}}}
242
243 //{{{ @extensibility
244 AnnotationDeclaration extensibilityann = createAnnotationDeclaration(Annotation.extensibility_str, null);
245 EnumTypeCode extensibilityannenum = new EnumTypeCode(extensibilityann.getScopedname(), Annotation.extensibility_enum_str);
246 extensibilityannenum.addMember(new EnumMember(Annotation.ex_final_str));
247 extensibilityannenum.addMember(new EnumMember(Annotation.ex_appendable_str));
248 extensibilityannenum.addMember(new EnumMember(Annotation.ex_mutable_str));
249 extensibilityann.addMember(new AnnotationMember(Annotation.value_str, extensibilityannenum,
250 extensibilityannenum.getInitialValue()));
251
252 createAnnotationDeclaration(Annotation.final_str, null);
253 createAnnotationDeclaration(Annotation.appendable_str, null);
254 createAnnotationDeclaration(Annotation.mutable_str, null);
255 //}}}
256
257 //{{{ @external
258 AnnotationDeclaration externalann = createAnnotationDeclaration(Annotation.external_str, null);
259 externalann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_BOOLEAN), Annotation.true_str));
260 //}}}
261
262 //{{{ @hashid
263 AnnotationDeclaration hashid_annotation = createAnnotationDeclaration(Annotation.hashid_str, null);
264 hashid_annotation.addMember(new AnnotationMember(Annotation.value_str, new StringTypeCode(Kind.KIND_STRING, null, null), Annotation.empty_str));
265 //}}}
266
267 //{{{ @id
268 AnnotationDeclaration idann = createAnnotationDeclaration(Annotation.id_str, null);
269 idann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_ULONG), Annotation.null_default_value));
270 //}}}
271
272 //{{{ @ignore_literal_names
273 AnnotationDeclaration ignore_literal_names_annotation = createAnnotationDeclaration(Annotation.ignore_literal_names_str, null);
274 ignore_literal_names_annotation.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_BOOLEAN), Annotation.true_str));
275 //}}}
276
277 //{{{ @key
278 // Create default @key annotation (@Key annotation also supported and registered in parseIDL)
279 AnnotationDeclaration keyann = createAnnotationDeclaration(Annotation.key_str, null);
280 keyann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_BOOLEAN), Annotation.true_str));
281 //}}}
282
283 //{{{ @max
284 AnnotationDeclaration maxann = createAnnotationDeclaration(Annotation.max_str, null);
285 maxann.addMember(new AnnotationMember(Annotation.value_str, new AnyTypeCode(), null));
286 //}}}
287
288 //{{{ @min
289 AnnotationDeclaration minann = createAnnotationDeclaration(Annotation.min_str, null);
290 minann.addMember(new AnnotationMember(Annotation.value_str, new AnyTypeCode(), null));
291 //}}}
292
293 //{{{ @must_understand
294 AnnotationDeclaration mustundann = createAnnotationDeclaration(Annotation.must_understand_str, null);
295 mustundann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_BOOLEAN), Annotation.true_str));
296 //}}}
297
298 //{{{ @nested
299 AnnotationDeclaration nestedann = createAnnotationDeclaration(Annotation.nested_str, null);
300 nestedann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_BOOLEAN), Annotation.true_str));
301 //}}}
302
303 //{{{ @non_serialized
304 AnnotationDeclaration non_serializedann = createAnnotationDeclaration(Annotation.non_serialized_str, null);
305 non_serializedann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_BOOLEAN), Annotation.true_str));
306 //}}}
307
308 //{{{ @oneway
309 AnnotationDeclaration onewayann = createAnnotationDeclaration(Annotation.oneway_str, null);
310 onewayann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_BOOLEAN), Annotation.true_str));
311 //}}}
312
313 //{{{ @optional
314 AnnotationDeclaration optionalann = createAnnotationDeclaration(Annotation.optional_str, null);
315 optionalann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_BOOLEAN), Annotation.true_str));
316 //}}}
317
318 //{{{ @position
319 AnnotationDeclaration positionann = createAnnotationDeclaration(Annotation.position_str, null);
320 positionann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_USHORT), Annotation.null_default_value));
321 //}}}
322
323 //{{{ @range
324 AnnotationDeclaration rangeann = createAnnotationDeclaration(Annotation.range_str, null);
325 rangeann.addMember(new AnnotationMember(Annotation.min_str, new AnyTypeCode(), null));
326 rangeann.addMember(new AnnotationMember(Annotation.max_str, new AnyTypeCode(), null));
327 //}}}
328
329 //{{{ @service
330 AnnotationDeclaration serviceann = createAnnotationDeclaration(Annotation.service_str, null);
331 serviceann.addMember(new AnnotationMember(Annotation.platform_str, new PrimitiveTypeCode(Kind.KIND_STRING), Annotation.any_str));
332 // CORBA, DDS, * (any), or custom value
333 //}}}
334
335 //{{{ topic
336 AnnotationDeclaration topic_annotation = createAnnotationDeclaration(Annotation.topic_str, null);
337 topic_annotation.addMember(new AnnotationMember(Annotation.name_str, new PrimitiveTypeCode(Kind.KIND_STRING), Annotation.empty_str));
338 topic_annotation.addMember(new AnnotationMember(Annotation.platform_str, new PrimitiveTypeCode(Kind.KIND_STRING), Annotation.any_str));
339 //}}}
340
341 //{{{ @try_construct
342 EnumTypeCode try_construct_fail_action_enum = new EnumTypeCode(null, Annotation.try_construct_enum_str);
343 try_construct_fail_action_enum.addMember(new EnumMember(Annotation.try_construct_discard_str));
344 try_construct_fail_action_enum.addMember(new EnumMember(Annotation.try_construct_use_default_str));
345 try_construct_fail_action_enum.addMember(new EnumMember(Annotation.try_construct_trim_str));
346
347 AnnotationDeclaration try_construct_annotation = createAnnotationDeclaration(Annotation.try_construct_str, null);
348 try_construct_annotation.addMember(new AnnotationMember(Annotation.value_str, try_construct_fail_action_enum, Annotation.try_construct_use_default_str));
349 //}}}
350
351 //{{{ @unit
352 AnnotationDeclaration unitsann = createAnnotationDeclaration(Annotation.unit_str, null);
353 unitsann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_STRING), Annotation.empty_str));
354 //}}}
355
356 //{{{ @value
357 AnnotationDeclaration valueann = createAnnotationDeclaration(Annotation.value_str, null);
358 valueann.addMember(new AnnotationMember(Annotation.value_str, new AnyTypeCode(), null));
359 //}}}
360
361 //{{{ @verbatim
362 AnnotationDeclaration verbatimann = createAnnotationDeclaration(Annotation.verbatim_str, null);
363 EnumTypeCode verbatimannenum = new EnumTypeCode(verbatimann.getScopedname(), Annotation.placement_enum_str);
364 verbatimannenum.addMember(new EnumMember(Annotation.begin_file_str));
365 verbatimannenum.addMember(new EnumMember(Annotation.before_declaration_str));
366 verbatimannenum.addMember(new EnumMember(Annotation.begin_declaration_str));
367 verbatimannenum.addMember(new EnumMember(Annotation.end_declaration_str));
368 verbatimannenum.addMember(new EnumMember(Annotation.after_declaration_str));
369 verbatimannenum.addMember(new EnumMember(Annotation.end_file_str));
370 verbatimann.addMember(new AnnotationMember(Annotation.language_str, new PrimitiveTypeCode(Kind.KIND_STRING), Annotation.any_str));
371 // c, c++, java, idl, * (any), or custom value
372 verbatimann.addMember(new AnnotationMember(Annotation.placement_str, verbatimannenum, Annotation.before_declaration_str));
373 verbatimann.addMember(new AnnotationMember(Annotation.text_str, new PrimitiveTypeCode(Kind.KIND_STRING), Annotation.empty_str));
374 //}}}
375 }
376
377 public String getFilename()
378 {
379 return m_filename;
380 }
381
382 public void setFilename(
383 String filename)
384 {
385 m_filename = filename;
386 }
387
388 public String getTrimfilename()
389 {
390 return Util.stringTrimAll(m_filename);
391 }
392
393 public String getScope()
394 {
395 return m_scope;
396 }
397
398 public void setScope(
399 String scope)
400 {
401 m_scope = scope;
402 }
403
404 public String getRelativeDir(String dependant_idl_dir)
405 {
406 String relative_dir = Util.getIDLFileDirectoryOnly(m_file);
407
408 if(null != relative_dir)
409 {
410 File rel_dir = new File(relative_dir);
411
412 if (rel_dir.isAbsolute())
413 {
414 if (null != dependant_idl_dir && relative_dir.startsWith(dependant_idl_dir))
415 {
416 relative_dir = relative_dir.substring(dependant_idl_dir.length());
417 }
418 else
419 {
420 relative_dir = "";
421 }
422 }
423 }
424 else
425 {
426 relative_dir = "";
427 }
428
429 return relative_dir;
430 }
431
432 /*!
433 * @return True if current call is in scoped file.
434 */
435 public boolean isInScopedFile()
436 {
437 return m_scopeFile.equals(m_file);
438 }
439
440 public String getScopeFile()
441 {
442 return m_scopeFile;
443 }
444
445 public boolean isScopeLimitToAll()
446 {
447 return m_scopeLimitToAll;
448 }
449
450 public void setScopeLimitToAll(
451 boolean scopeLimitToAll)
452 {
453 m_scopeLimitToAll = scopeLimitToAll;
454 }
455
456 public int getCurrentIncludeLine()
457 {
458 return m_currentincludeline;
459 }
460
461 public Stack<Pair<String, Integer>> getScopeFilesStack()
462 {
463 return m_scopeFilesStack;
464 }
465
466 /*!
467 * @brief This function stores a global definition of the IDL file.
468 */
469 public void addDefinition(
470 Definition definition)
471 {
472 m_definitions.add(definition);
473 }
474
475 public ArrayList<Definition> getDefinitions()
476 {
477 return m_definitions;
478 }
479
480 /*!
481 * @brief This function adds a module to the context.
482 * This function is used in the parser.
483 */
484 public TemplateGroup addModule(
485 com.eprosima.idl.parser.tree.Module module)
486 {
487 TemplateGroup moduleTemplates = null;
488
489 if (!m_modules.containsKey(module.getScopedname()))
490 {
491 m_modules.put(module.getScopedname(), module);
492 }
493
494 if(isInScopedFile() || isScopeLimitToAll()) {
495 if(tmanager_ != null) {
496 moduleTemplates = tmanager_.createTemplateGroup("module");
497 moduleTemplates.setAttribute("ctx", this);
498 // Set the module object to the TemplateGroup of the module.
499 moduleTemplates.setAttribute("module", module);
500 }
501 }
502
503 // Change context scope.
504 if (m_scope.isEmpty())
505 {
506 setScope(module.getName());
507 }
508 else
509 {
510 setScope(m_scope + "::" + module.getName());
511 }
512
513 return moduleTemplates;
514 }
515
516 public com.eprosima.idl.parser.tree.Module existsModule(
517 String scopedName)
518 {
519 if (m_modules.containsKey(scopedName))
520 {
521 return m_modules.get(scopedName);
522 }
523
524 return null;
525 }
526
527 public Interface createInterface(
528 String name,
529 Token token)
530 {
531 Interface interfaceObject = new Interface(m_scopeFile, isInScopedFile(), m_scope, name, token);
532 addInterface(interfaceObject);
533 return interfaceObject;
534 }
535
536 /*!
537 * @brief This function adds a interface to the context.
538 * This function is used in the parser.
539 */
540 protected void addInterface(
541 Interface interf)
542 {
543 Interface prev = m_interfaces.put(interf.getScopedname(), interf);
544
545 // TODO: Excepcion
546 if (prev != null)
547 {
548 System.out.println("Warning: Redefined interface " + prev.getScopedname());
549 }
550 }
551
552 public Interface getInterface(
553 String name)
554 {
555 int lastIndex = -1;
556 Interface returnedValue = m_interfaces.get(name);
557
558 if (returnedValue == null)
559 {
560 String scope = m_scope;
561
562 while (returnedValue == null && !scope.isEmpty())
563 {
564 returnedValue = m_interfaces.get(scope + "::" + name);
565 lastIndex = scope.lastIndexOf("::");
566
567 if (lastIndex != -1)
568 {
569 scope = scope.substring(0, lastIndex);
570 }
571 else
572 {
573 scope = "";
574 }
575 }
576 }
577
578 return returnedValue;
579 }
580
581 /*!
582 * @brief This function returns all interfaces.
583 * This function is used in string templates.
584 */
585 public ArrayList<Interface> getInterfaces()
586 {
587 return new ArrayList<Interface>(m_interfaces.values());
588 }
589
590 public ArrayList<Interface> getScopedInterfaces()
591 {
592 ArrayList<Interface> ret = new ArrayList<Interface>();
593
594 for (Interface interf : m_interfaces.values())
595 {
596 if (interf.isInScope())
597 {
598 ret.add(interf);
599 }
600 }
601
602 return ret;
603 }
604
605 public com.eprosima.idl.parser.tree.Exception createException(
606 String name,
607 Token token)
608 {
609 com.eprosima.idl.parser.tree.Exception exceptionObject = new com.eprosima.idl.parser.tree.Exception(m_scopeFile,
610 isInScopedFile(), m_scope, name, token);
611 addException(exceptionObject);
612 return exceptionObject;
613 }
614
615 /*!
616 * @brief This function adds a global exception to the context.
617 */
618 protected void addException(
619 com.eprosima.idl.parser.tree.Exception exception)
620 {
621 com.eprosima.idl.parser.tree.Exception prev = m_exceptions.put(exception.getScopedname(), exception);
622
623 // TODO: Exception.
624 if (prev != null)
625 {
626 System.out.println("Warning: Redefined exception " + prev.getScopedname());
627 }
628 }
629
630 /*!
631 * @brief This function tries to retrieve a global typecode.
632 */
633 public com.eprosima.idl.parser.tree.Exception getException(
634 String name)
635 {
636 int lastIndex = -1;
637 com.eprosima.idl.parser.tree.Exception returnedValue = m_exceptions.get(name);
638
639 // Probar si no tiene scope, con el scope actual.
640 if (returnedValue == null)
641 {
642 String scope = m_scope;
643
644 while (returnedValue == null && !scope.isEmpty())
645 {
646 returnedValue = m_exceptions.get(scope + "::" + name);
647 lastIndex = scope.lastIndexOf("::");
648
649 if (lastIndex != -1)
650 {
651 scope = scope.substring(0, lastIndex);
652 }
653 else
654 {
655 scope = "";
656 }
657 }
658 }
659
660 return returnedValue;
661 }
662
663 public com.eprosima.idl.parser.tree.Module createModule(
664 String scope_file,
665 boolean is_in_scope,
666 String scope,
667 String name,
668 Token token)
669 {
670 return new com.eprosima.idl.parser.tree.Module (scope_file, is_in_scope, scope, name, token);
671 }
672
673 public Operation createOperation(
674 String name,
675 Token token)
676 {
677 Operation operationObject = new Operation(m_scopeFile, isInScopedFile(), null, name, token);
678 return operationObject;
679 }
680
681 public Param createParam(
682 String name,
683 TypeCode typecode,
684 Param.Kind kind)
685 {
686 Param paramObject = new Param(name, typecode, kind);
687 return paramObject;
688 }
689
690 public Param createParam(
691 String name,
692 Definition definition,
693 Param.Kind kind)
694 {
695 Param paramObject = new Param(name, definition, kind);
696 return paramObject;
697 }
698
699 public AliasTypeCode createAliasTypeCode(
700 String scope,
701 String name)
702 {
703 return new AliasTypeCode(scope, name);
704 }
705
706 public ArrayTypeCode createArrayTypeCode()
707 {
708 return new ArrayTypeCode();
709 }
710
711 public BitmaskTypeCode createBitmaskTypeCode(
712 String scope,
713 String name)
714 {
715 return new BitmaskTypeCode(scope, name);
716 }
717
718 public BitsetTypeCode createBitsetTypeCode(
719 String scope,
720 String name)
721 {
722 return new BitsetTypeCode(scope, name);
723 }
724
725 public EnumTypeCode createEnumTypeCode(
726 String scope,
727 String name)
728 {
729 return new EnumTypeCode(scope, name);
730 }
731
732 public MapTypeCode createMapTypeCode(
733 String maxsize)
734 {
735 return new MapTypeCode(maxsize, evaluate_literal(maxsize));
736 }
737
738 public PrimitiveTypeCode createPrimitiveTypeCode(
739 int kind)
740 {
741 return new PrimitiveTypeCode(kind);
742 }
743
744 public SequenceTypeCode createSequenceTypeCode(
745 String maxsize)
746 {
747 return new SequenceTypeCode(maxsize, evaluate_literal(maxsize));
748 }
749
750 public SetTypeCode createSetTypeCode(
751 String maxsize)
752 {
753 return new SetTypeCode(maxsize, evaluate_literal(maxsize));
754 }
755
756 public StringTypeCode createStringTypeCode(
757 int kind,
758 String maxsize)
759 {
760 return new StringTypeCode(kind, maxsize, evaluate_literal(maxsize));
761 }
762
763 public StructTypeCode createStructTypeCode(
764 String name)
765 {
766 return new StructTypeCode(m_scope, name);
767 }
768
769 public UnionTypeCode createUnionTypeCode(
770 String scope,
771 String name)
772 {
773 return new UnionTypeCode(m_scope, name);
774 }
775
776 public UnionTypeCode createUnionTypeCode(
777 String scope,
778 String name,
779 TypeCode discriminatorTypeCode)
780 {
781 return new UnionTypeCode(m_scope, name, discriminatorTypeCode);
782 }
783
784 public BitfieldSpec createBitfieldSpec(
785 String size,
786 TypeCode type)
787 {
788 BitfieldSpec object = new BitfieldSpec(m_scope, size, type);
789 return object;
790 }
791
792 public Collection<com.eprosima.idl.parser.tree.Exception> getExceptions()
793 {
794 return m_exceptions.values();
795 }
796
797 public Collection<TypeDeclaration> getTypes()
798 {
799 return m_types.values();
800 }
801
802 /*!
803 * @brief This function adds a global typecode to the context.
804 */
805 public void addTypeDeclaration(
806 TypeDeclaration typedecl)
807 {
808 TypeDeclaration prev = m_types.put(typedecl.getScopedname(), typedecl);
809
810 if (prev != null)
811 {
812 throw new ParseException(typedecl.getToken(), "was redefined");
813 }
814 }
815
816 /*!
817 * @brief This function returns a global typecode of the context.
818 */
819 public TypeDeclaration getTypeDeclaration(
820 String scopedName)
821 {
822 return m_types.get(scopedName);
823 }
824
825 /*!
826 * @brief This function tries to retrieve a global typecode.
827 */
828 public TypeCode getTypeCode(
829 String name)
830 {
831 int lastIndex = -1;
832 TypeCode returnedValue = null;
833 TypeDeclaration typedecl = m_types.get(name);
834
835 // Wether the name doesn't contain scope, test with the current scope.
836 if (typedecl == null)
837 {
838 String scope = m_scope;
839
840 while (typedecl == null && scope != null && !scope.isEmpty())
841 {
842 typedecl = m_types.get(scope + "::" + name);
843 lastIndex = scope.lastIndexOf("::");
844
845 if (lastIndex != -1)
846 {
847 scope = scope.substring(0, lastIndex);
848 }
849 else
850 {
851 scope = "";
852 }
853 }
854 }
855
856 if (typedecl != null)
857 {
858 returnedValue = typedecl.getTypeCode();
859 }
860
861
862 return returnedValue;
863 }
864
865 public AnnotationDeclaration createAnnotationDeclaration(
866 String name,
867 Token token)
868 {
869 AnnotationDeclaration annotationObject = new AnnotationDeclaration(m_scopeFile,
870 isInScopedFile(), m_scope, name, token);
871 addAnnotationDeclaration(annotationObject);
872 return annotationObject;
873 }
874
875 /*!
876 * @brief This function adds an annotation to the context.
877 */
878 protected void addAnnotationDeclaration(
879 AnnotationDeclaration annotation)
880 {
881 AnnotationDeclaration prev = m_annotations.put(annotation.getScopedname(), annotation);
882
883 // TODO: Exception.
884 if (prev != null)
885 {
886 System.out.println("Warning: Redefined annotation " + prev.getScopedname());
887 }
888 }
889
890 public AnnotationDeclaration getAnnotationDeclaration(
891 String name)
892 {
893 int lastIndex = -1;
894 AnnotationDeclaration returnedValue = m_annotations.get(name);
895
896 // Probar si no tiene scope, con el scope actual.
897 if (returnedValue == null)
898 {
899 String scope = m_scope;
900
901 while (returnedValue == null && !scope.isEmpty())
902 {
903 returnedValue = m_annotations.get(scope + "::" + name);
904 lastIndex = scope.lastIndexOf("::");
905
906 if (lastIndex != -1)
907 {
908 scope = scope.substring(0, lastIndex);
909 }
910 else
911 {
912 scope = "";
913 }
914 }
915 }
916
917 return returnedValue;
918 }
919
920 /*!
921 * @brief This function add a new library dependency to the project.
922 */
923 public void addDependency(
924 String dependency)
925 {
926 m_dependencies.add(dependency);
927 }
928
929 /*!
930 * @brief This function get the library dependencies of a project.
931 */
932 public LinkedHashSet<String> getDependencies()
933 {
934 // At this level the dependencies are in reverse order. Return them
935 // in correct order.
936 LinkedHashSet<String> set = new LinkedHashSet<String>();
937 LinkedList<String> list = new LinkedList<String>(m_dependencies);
938 Iterator<String> it = list.descendingIterator();
939
940 while (it.hasNext())
941 {
942 String dep = it.next();
943
944 if (getOS().contains("Windows"))
945 {
946 // In windows substitute \\ by /
947 int count = 0;
948 while ((count = dep.indexOf("/")) != -1)
949 {
950 dep = dep.substring(0, count) + "\\" + dep.substring(count + 1);
951 }
952 }
953
954 set.add(dep);
955 }
956
957 return set;
958 }
959
960 /*!
961 * @brief This function is used in the stringtemplates. For these reason this function
962 * returns an ArrayList
963 */
964 public ArrayList<String> getDirectIncludeDependencies()
965 {
966 return new ArrayList<String>(m_directIncludeDependencies);
967 }
968
969 // TODO Quitar porque solo es para tipos (previous c) (usado para las excepciones). Mirar alternativa.
970 /*!
971 * @brief This function add a new include dependency to the project.
972 * This dependency will be needed to include our generated file with the types that
973 * the (previous c) DDS middleware doesn't generate (right now only exceptions).
974 * The include dependencies are added without the .idl extension.
975 */
976 public void addIncludeDependency(
977 String dependency)
978 {
979 // Remove .idl extension.
980 String dep = dependency.substring(0, dependency.length() - 4);
981 // Remove directory if it is the same than main IDL file.
982 if (m_directoryFile != null && startsWith(dep, m_directoryFile))
983 {
984 dep = dep.substring(m_directoryFile.length());
985 }
986 m_includedependency.add(dep);
987 }
988
989 // TODO Quitar porque solo es para tipos (previous c) (usado para las excepciones). Mirar alternativa.
990 /*!
991 * @brief This function is used in the stringtemplates. For these reason this function
992 * returns an ArrayList
993 */
994 public ArrayList<String> getIncludeDependencies()
995 {
996 return new ArrayList<String>(m_includedependency);
997 }
998
999 /*!
1000 * @brief This function is call when a preprocessor line was found by the lexer.
1001 * In case the line referring to the content included file, this function sets this file as current scope file.
1002 * Also this function saves the scope file in the library dependecy map.
1003 * In case it is a #include directive, this is saved as direct include dependency.
1004 */
1005 public void processPreprocessorLine(
1006 String line,
1007 int nline)
1008 {
1009 // If there is a line referring to the content of an included file.
1010 if (line.startsWith("# "))
1011 {
1012 String line_ = line.substring(2);
1013
1014 /* The received preprocessor line has the following form:
1015 * ' numline filename flags'
1016 * where:
1017 * - numline Number of the line where the include was.
1018 * - filename The filename whose content was included.
1019 * - flags
1020 */
1021 Scanner scanner = new Scanner(line_);
1022
1023 // Read numline
1024 int numline = scanner.nextInt();
1025
1026 line_ = scanner.nextLine();
1027 scanner = new Scanner(line_).useDelimiter("\"");
1028
1029 // Read filename
1030 scanner.next();
1031 String file = Paths.get(scanner.next()).normalize().toString();
1032
1033 // Read flags.
1034 boolean systemFile = false, enteringFile = false, exitingFile = false;
1035
1036 if (m_os.contains("Linux"))
1037 {
1038 try
1039 {
1040 line_ = scanner.nextLine();
1041 scanner = new Scanner(line_);
1042 scanner.next();
1043
1044 while (true)
1045 {
1046 Integer flag = scanner.nextInt();
1047
1048 if (flag == 1)
1049 {
1050 enteringFile = true;
1051 }
1052 else if (flag == 2)
1053 {
1054 exitingFile = true;
1055 }
1056 else if (flag == 3)
1057 {
1058 systemFile = true;
1059 }
1060 }
1061 }
1062 catch (NoSuchElementException ex)
1063 {
1064 // The line finishes.
1065 }
1066 }
1067
1068 // Only not system files are processed.
1069 if (!systemFile)
1070 {
1071 if (!m_scopeFile.equals(file))
1072 {
1073 // Remove absolute directory where the application was executed
1074 if (startsWith(file, m_userdir))
1075 {
1076 file = file.substring(m_userdir.length());
1077
1078 // Remove possible separator
1079 if (startsWith(file, java.io.File.separator))
1080 {
1081 file = file.substring(1);
1082 }
1083 }
1084 // Remove relative ./ directory.
1085 if (startsWith(file, currentDirS))
1086 {
1087 file = file.substring(currentDirS.length());
1088 // Remove possible separator
1089 if (startsWith(file, java.io.File.separator))
1090 {
1091 file = file.substring(1);
1092 }
1093 }
1094
1095
1096 //if it is a idl file.
1097 if (file.substring(file.length() - 4, file.length()).equals(".idl"))
1098 {
1099 if (!m_scopeFile.equals(file))
1100 {
1101 if (!m_scopeFilesStack.empty() && m_scopeFilesStack.peek().first().equals(file))
1102 {
1103 m_scopeFilesStack.pop();
1104
1105 // Add to dependency if there is different IDL file than the processed
1106 addDependency(m_scopeFile);
1107
1108 // See if it is a direct dependency.
1109 if (file.equals(m_file))
1110 {
1111 String includeFile = m_scopeFile;
1112 // Remove relative directory if is equal that where the processed IDL is.
1113 if (m_directoryFile != null && startsWith(includeFile, m_directoryFile))
1114 {
1115 includeFile = includeFile.substring(m_directoryFile.length());
1116 }
1117 // Remove relative directory if is equal to a include path.
1118 for (int i = 0; i < m_includePaths.size(); ++i)
1119 {
1120 if (startsWith(includeFile, m_includePaths.get(i)))
1121 {
1122 includeFile = includeFile.substring(m_includePaths.get(i).length());
1123 break;
1124 }
1125 }
1126
1127 String dependency_to_add =
1128 includeFile.substring(0, includeFile.length() - 4).replace('.', '_');
1129 // Change back to "." for relative paths.
1130 dependency_to_add = dependency_to_add.replaceAll("__/", "../");
1131 // Substitute "\\" with File separator.
1132 dependency_to_add = dependency_to_add.replace('\\', '/');
1133
1134 m_directIncludeDependencies.add(dependency_to_add);
1135 }
1136 }
1137 else
1138 {
1139 m_scopeFilesStack.push(new Pair<String, Integer>(m_scopeFile,
1140 nline - m_currentincludeline - 1));
1141 }
1142
1143 m_scopeFile = file;
1144 }
1145 }
1146 }
1147
1148 //Update the current line.
1149 m_currentincludeline = nline - numline;
1150 }
1151 }
1152 }
1153
1154 protected String getOS()
1155 {
1156 return m_os;
1157 }
1158
1159 protected boolean startsWith(
1160 String st,
1161 String prefix)
1162 {
1163 if (m_os.contains("Windows"))
1164 {
1165 return st.toLowerCase().startsWith(prefix.toLowerCase());
1166 }
1167
1168 return st.startsWith(prefix);
1169 }
1170
1171 /*** Function to generate random loop variables in string templates ***/
1172 public String getNewLoopVarName()
1173 {
1174 m_loopVarName = 'a';
1175 return Character.toString(m_loopVarName);
1176 }
1177
1178 public String getNextLoopVarName()
1179 {
1180 return Character.toString(++m_loopVarName);
1181 }
1182
1183 public String removeEscapeCharacter(
1184 String id)
1185 {
1186 if (id.startsWith("_")) // Escaped identifier?
1187 {
1188 id = id.substring(1);
1189 }
1190 return id;
1191 }
1192
1193 public String checkIdentifier(
1194 Definition.Kind kind,
1195 String scope,
1196 String id)
1197 {
1198 if (checkKeyword(id))
1199 {
1200 return id + " is a keyword, use escape character if you want to use it as identifier (_" + id + ")";
1201 }
1202
1203 String scopedname = (scope == null || scope.isEmpty()) ? id : scope + "::" + id;
1204
1205 // Check definitions
1206 for (Definition def : m_definitions)
1207 {
1208 if (def instanceof TreeNode)
1209 {
1210 TreeNode tn = (TreeNode)def;
1211 if (m_ignore_case
1212 ? tn.getScopedname().equalsIgnoreCase(scopedname)
1213 : tn.getScopedname().equals(scopedname)
1214 )
1215 {
1216 boolean error = true;
1217
1218 if (kind == Definition.Kind.MODULE && tn instanceof com.eprosima.idl.parser.tree.Module)
1219 {
1220 error = false;
1221 }
1222 else if (kind == Definition.Kind.TYPE_DECLARATION &&
1223 tn instanceof com.eprosima.idl.parser.tree.TypeDeclaration &&
1224 !((com.eprosima.idl.parser.tree.TypeDeclaration)tn).getTypeCode().isDefined())
1225 {
1226 error = false;
1227 }
1228
1229 if (error)
1230 {
1231 return scopedname + " is already defined (Definition: " + def + ")";
1232 }
1233 }
1234 }
1235 }
1236
1237 // Check modules
1238 for (String type : m_modules.keySet())
1239 {
1240 if (m_ignore_case
1241 ? type.equalsIgnoreCase(scopedname)
1242 : type.equals(scopedname)
1243 )
1244 {
1245 if (kind != Definition.Kind.MODULE)
1246 {
1247 return scopedname + " is already defined (Module: " + type + ")";
1248 }
1249 }
1250 }
1251
1252 // Check interfaces
1253 for (String type : m_interfaces.keySet())
1254 {
1255 if (m_ignore_case
1256 ? type.equalsIgnoreCase(scopedname)
1257 : type.equals(scopedname)
1258 )
1259 {
1260 return scopedname + " is already defined (Interface: " + type + ")";
1261 }
1262 }
1263
1264 // Check Exceptions
1265 for (String type : m_exceptions.keySet())
1266 {
1267 if (m_ignore_case
1268 ? type.equalsIgnoreCase(scopedname)
1269 : type.equals(scopedname)
1270 )
1271 {
1272 return scopedname + " is already defined (Exception: " + type + ")";
1273 }
1274 }
1275
1276 // Check TypeDeclarations
1277 for (Map.Entry<String, TypeDeclaration> type : m_types.entrySet())
1278 {
1279 if (m_ignore_case
1280 ? type.getKey().equalsIgnoreCase(scopedname)
1281 : type.getKey().equals(scopedname)
1282 )
1283 {
1284 if (type.getValue().getTypeCode().isDefined())
1285 {
1286 return scopedname + " is already defined (Type: " + type + ")";
1287 }
1288 }
1289 }
1290
1291 // Check Annotations, only check annotations against other annotations
1292 if (kind == Definition.Kind.ANNOTATION)
1293 {
1294 for (String anno : m_annotations.keySet())
1295 {
1296 if (m_ignore_case
1297 ? anno.equalsIgnoreCase(scopedname)
1298 : anno.equals(scopedname)
1299 )
1300 {
1301 return scopedname + " is already defined (Annotation: " + anno + ")";
1302 }
1303 }
1304 }
1305
1306 if (id.startsWith("_")) // Escaped identifier?
1307 {
1308 id = id.substring(1);
1309 }
1310
1311 return null;
1312 }
1313
1314 public void ignore_case(
1315 boolean ignore_case)
1316 {
1317 m_ignore_case = ignore_case;
1318 }
1319
1320 protected void fillKeywords()
1321 {
1322 m_keywords.add("setraises");
1323 m_keywords.add("out");
1324 m_keywords.add("emits");
1325 m_keywords.add("string");
1326 m_keywords.add("switch");
1327 m_keywords.add("publishes");
1328 m_keywords.add("typedef");
1329 m_keywords.add("uses");
1330 m_keywords.add("primarykey");
1331 m_keywords.add("custom");
1332 m_keywords.add("octet");
1333 m_keywords.add("sequence");
1334 m_keywords.add("import");
1335 m_keywords.add("struct");
1336 m_keywords.add("native");
1337 m_keywords.add("readonly");
1338 m_keywords.add("finder");
1339 m_keywords.add("raises");
1340 m_keywords.add("void");
1341 m_keywords.add("private");
1342 m_keywords.add("eventtype");
1343 m_keywords.add("wchar");
1344 m_keywords.add("in");
1345 m_keywords.add("default");
1346 m_keywords.add("public");
1347 m_keywords.add("short");
1348 m_keywords.add("long");
1349 m_keywords.add("enum");
1350 m_keywords.add("wstring");
1351 m_keywords.add("context");
1352 m_keywords.add("home");
1353 m_keywords.add("factory");
1354 m_keywords.add("exception");
1355 m_keywords.add("getraises");
1356 m_keywords.add("const");
1357 m_keywords.add("ValueBase");
1358 m_keywords.add("valuetype");
1359 m_keywords.add("supports");
1360 m_keywords.add("module");
1361 m_keywords.add("Object");
1362 m_keywords.add("truncatable");
1363 m_keywords.add("unsigned");
1364 m_keywords.add("fixed");
1365 m_keywords.add("union");
1366 m_keywords.add("oneway");
1367 m_keywords.add("any");
1368 m_keywords.add("char");
1369 m_keywords.add("case");
1370 m_keywords.add("float");
1371 m_keywords.add("boolean");
1372 m_keywords.add("multiple");
1373 m_keywords.add("abstract");
1374 m_keywords.add("inout");
1375 m_keywords.add("provides");
1376 m_keywords.add("consumes");
1377 m_keywords.add("double");
1378 m_keywords.add("typeprefix");
1379 m_keywords.add("typeid");
1380 m_keywords.add("attribute");
1381 m_keywords.add("local");
1382 m_keywords.add("manages");
1383 m_keywords.add("interface");
1384 m_keywords.add("component");
1385 m_keywords.add("set");
1386 m_keywords.add("map");
1387 m_keywords.add("bitfield");
1388 m_keywords.add("bitset");
1389 m_keywords.add("bitmask");
1390 m_keywords.add("annotation");
1391 }
1392
1393 protected boolean checkKeyword(
1394 String id)
1395 {
1396 boolean return_value = false;
1397
1398 for (String keyword : m_keywords)
1399 {
1400 if (m_ignore_case
1401 ? keyword.equalsIgnoreCase(id)
1402 : keyword.equals(id)
1403 )
1404 {
1405 return_value = true;
1406 break;
1407 }
1408 }
1409
1410 return return_value;
1411 }
1412
1413 public String concatStringLiterals(
1414 String literal)
1415 {
1416 // Split into separated strings
1417 String[] substrings = literal.split("\"([ \r\t\u000C\n])*\"");
1418
1419 String result = "";
1420 boolean escapeHex = false;
1421 for (String str : substrings)
1422 {
1423 if (escapeHex)
1424 {
1425 str = "\"" + str;
1426 }
1427 escapeHex = false;
1428 if (str.matches("\\\\x[a-fA-F0-9]+$"))
1429 {
1430 escapeHex = true;
1431 }
1432 result += str + (escapeHex ? "\"" : "");
1433 }
1434
1435 return result;
1436 }
1437
1438 /*** End ***/
1439
1440 public static ScriptEngine getJSScriptEngine()
1441 {
•1442 return new ScriptEngineManager().getEngineByName("js");
1443 }
1444
1445 public String evaluate_literal(
1446 String str)
1447 {
1448 if (null == str)
1449 {
1450 return null;
1451 }
1452
1453 String aux_str = "(" + str.replace("::", "_") + ") | 0";
1454 String const_str = "";
1455
1456 // Add all constants.
1457
1458 for (Definition definition : m_definitions)
1459 {
1460 if (definition.isIsConstDeclaration())
1461 {
1462 ConstDeclaration const_decl = (ConstDeclaration)definition;
1463
1464 if ((const_decl.getTypeCode().isPrimitive() && !const_decl.getTypeCode().isIsEnumType())
1465 || const_decl.getTypeCode().isIsStringType()
1466 || const_decl.getTypeCode().isIsWStringType())
1467 {
1468 const_str = const_str + ";" + const_decl.getFormatedScopedname() + "=" + const_decl.getEvaluatedValue();
1469
1470 if (const_decl.getScope() == getScope())
1471 {
1472 const_str = const_str + ";" + const_decl.getName() + "=" + const_decl.getEvaluatedValue();
1473 }
1474 }
1475 }
1476 }
1477
1478 const_str = const_str.replace("=L'", "='").replace("=L\"", "=\""); // remove "L" from wchar and wstring declarations
1479 aux_str = const_str + ";" + aux_str;
1480
1481 // Process the math expression
1482 try
1483 {
1484 ScriptEngine engine = getJSScriptEngine();
•1485 if (engine == null)
1486 {
1487 // Fallback for simple expressions
1488 for (Definition definition : m_definitions)
1489 {
1490 if (definition.isIsConstDeclaration())
1491 {
1492 ConstDeclaration const_decl = (ConstDeclaration)definition;
1493 if (const_decl.getTypeCode().isPrimitive() || const_decl.getTypeCode().isIsStringType() || const_decl.getTypeCode().isIsWStringType())
1494 {
1495 if (str == const_decl.getScopedname())
1496 return const_decl.getValue();
1497 if (const_decl.getScope() == getScope() && str == const_decl.getName())
1498 return const_decl.getValue();
1499 }
1500 }
1501 }
1502 return str;
1503 }
1504 aux_str = engine.eval(aux_str).toString();
1505 }
1506 catch (ScriptException se)
1507 {
1508 throw new ParseException(null, "Error evaluating expression: " + aux_str);
1509 }
1510
1511 return aux_str;
1512 }
1513
1514 public boolean isGenerateTypesC()
1515 {
1516 return generate_typesc_;
1517 }
1518
1519 /*!
1520 * @brief Checks a custom property was enables for a TemplateGroup.
1521 *
1522 * There are built-in custom properties:
1523 * - Context.using_explicitly_modules_custom_property: specifies a TemplateGroup is using explicitly modules.
1524 */
1525 public boolean is_enabled_custom_property_in_current_group(String custom_property)
1526 {
1527 if (null != tmanager_)
1528 {
1529 return tmanager_.is_enabled_custom_property_in_current_group(custom_property);
1530 }
1531
1532 return false;
1533 }
1534
1535 // OS
1536 String m_os = null;
1537 String m_userdir = null;
1538
1539 private String m_filename = "";
1540 private String m_file = "";
1541 private String m_directoryFile = "";
1542
1543 private String m_scope = "";
1544 private String m_scopeFile = "";
1545 private boolean m_scopeLimitToAll = false;
1546
1547 private int m_currentincludeline = 0;
1548
1549 final String currentDirS = "." + File.separator;
1550 final String includeFlag = "-I";
1551
1552 //! Store all global definitions.
1553 private ArrayList<Definition> m_definitions = new ArrayList<Definition>();
1554 //! Map that contains all modules that were found processing the IDL file (after preprocessing):
1555 private HashMap<String, com.eprosima.idl.parser.tree.Module> m_modules = new HashMap<String, com.eprosima.idl.parser.tree.Module>();
1556 //! Map that contains all interfaces that were found processing the IDL file (after preprocessing):
1557 private HashMap<String, Interface> m_interfaces = new HashMap<String, Interface>();
1558 //! Map that contains all global exceptions that were found processing the IDL file (after preprocessing).
1559 private HashMap<String, com.eprosima.idl.parser.tree.Exception> m_exceptions = new HashMap<String, com.eprosima.idl.parser.tree.Exception>();
1560 //! Map that contains all types that were found processing the IDL file (after preprocessing).
1561 private HashMap<String, TypeDeclaration> m_types = new HashMap<String, TypeDeclaration>();
1562 //! Map that contains all annotations that where found processing the IDL file.
1563 private HashMap<String, AnnotationDeclaration> m_annotations = new HashMap<String, AnnotationDeclaration>();
1564
1565 private ArrayList<String> m_includePaths = null;
1566 //! Set that contains the library dependencies that were found because there was a line of the preprocessor.
1567 private LinkedHashSet<String> m_dependencies = null;
1568
1569 //! Set that contains the direct include dependencies in the IDL file. Used to regenerate the IDL in a supported form.
1570 private HashSet<String> m_directIncludeDependencies = null;
1571
1572 // TODO Quitar porque solo es para tipos (previous c) (usado para las excepciones). Mirar alternativa.
1573 //! Set that contains the include dependencies that force to include our type generated file (right now only with exceptions in (previous c) DDS types).
1574 private HashSet<String> m_includedependency = null;
1575
1576 // TODO Lleva la cuenta del nombre de variables para bucles anidados.
1577 private char m_loopVarName = 'a';
1578
1579 private Stack<Pair<String, Integer>> m_scopeFilesStack;
1580
1581 // All grammar keywords
1582 private HashSet<String> m_keywords = null;
1583
1584 private boolean m_ignore_case = true;
1585
1586 private boolean generate_typesc_ = false;
1587
1588 protected TemplateManager tmanager_ = null;
1589 }