1 | package org.etsi.t3d.visitor; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.LinkedList; |
---|
5 | |
---|
6 | import org.etsi.t3d.DependencyPrinter; |
---|
7 | import org.etsi.t3d.exceptions.TTCN3BehaviorException; |
---|
8 | |
---|
9 | import de.ugoe.cs.swe.trex.core.analyzer.rfparser.ASTUtil; |
---|
10 | import de.ugoe.cs.swe.trex.core.analyzer.rfparser.LocationAST; |
---|
11 | import de.ugoe.cs.swe.trex.core.analyzer.rfparser.TTCN3ParserTokenTypes; |
---|
12 | import de.ugoe.cs.swe.trex.core.analyzer.rfparser.symboltable.EnumSymbol; |
---|
13 | import de.ugoe.cs.swe.trex.core.analyzer.rfparser.symboltable.Symbol; |
---|
14 | |
---|
15 | public class DependencyVisitor extends AbstractVisitor { |
---|
16 | |
---|
17 | private DependencyPrinter dependencyPrinter; |
---|
18 | |
---|
19 | public DependencyVisitor(DependencyPrinter dependencyPrinter) { |
---|
20 | this.dependencyPrinter = dependencyPrinter; |
---|
21 | } |
---|
22 | @Override |
---|
23 | public void finish() { |
---|
24 | } |
---|
25 | |
---|
26 | @Override |
---|
27 | public void init() { |
---|
28 | } |
---|
29 | |
---|
30 | |
---|
31 | /** |
---|
32 | * Extracts identifiers related to a module definition identifier within the |
---|
33 | * corresponding module definition subtree |
---|
34 | * TODO: filter out predefined functions |
---|
35 | * @param moduleDefinitionIdentifierNode - A module definition identifier node |
---|
36 | * @return - A list of referenced identifier nodes, excluding the identifier |
---|
37 | * nodes of the module definition itself; if node is not of type Identifier |
---|
38 | * an empty list is returned instead |
---|
39 | */ |
---|
40 | //TODO: this may need to be moved for reuse |
---|
41 | private static LinkedList<LocationAST> getRelatedIdentifierNodes( |
---|
42 | LocationAST identifierNode) { |
---|
43 | LinkedList<LocationAST> identifierNodes = new LinkedList<LocationAST>(); |
---|
44 | if (identifierNode.getType() != TTCN3ParserTokenTypes.Identifier) { |
---|
45 | return identifierNodes; |
---|
46 | } |
---|
47 | LocationAST moduleDefinitionNode = LocationAST.resolveParentsUntilType(identifierNode, |
---|
48 | TTCN3ParserTokenTypes.ModuleDefinition); |
---|
49 | |
---|
50 | LocationAST typeNode = LocationAST.getModuleDefinitionTypeNode(moduleDefinitionNode); |
---|
51 | if (typeNode.getType() == TTCN3ParserTokenTypes.ModuleParDef |
---|
52 | && typeNode.getFirstChild().getType() == TTCN3ParserTokenTypes.MultitypedModuleParList) { |
---|
53 | LocationAST sibling = identifierNode.getNextSibling(); |
---|
54 | if (sibling != null |
---|
55 | && sibling.getType() != TTCN3ParserTokenTypes.Identifier) { |
---|
56 | identifierNodes.addAll(ASTUtil.findTypeNodes(sibling, |
---|
57 | TTCN3ParserTokenTypes.Identifier)); |
---|
58 | |
---|
59 | } |
---|
60 | LocationAST moduleParNode = LocationAST.resolveParentsUntilType(identifierNode, |
---|
61 | TTCN3ParserTokenTypes.ModulePar); |
---|
62 | LocationAST moduleParTypeNode = moduleParNode.getFirstChild(); |
---|
63 | identifierNodes.addAll(ASTUtil.findTypeNodes(moduleParTypeNode, |
---|
64 | TTCN3ParserTokenTypes.Identifier)); |
---|
65 | |
---|
66 | } else { |
---|
67 | identifierNodes = ASTUtil.findTypeNodes(moduleDefinitionNode, |
---|
68 | TTCN3ParserTokenTypes.Identifier); |
---|
69 | ArrayList<LocationAST> moduleDefinitionIdentifierNodes = LocationAST.getModuleDefinitionIdentifiersList(moduleDefinitionNode); |
---|
70 | identifierNodes.removeAll(moduleDefinitionIdentifierNodes); |
---|
71 | } |
---|
72 | // TODO: exclude predefined functions |
---|
73 | return identifierNodes; |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | /** |
---|
78 | * Extracts referenced identifiers from within a module definition subtree |
---|
79 | * TODO: filter out predefined functions |
---|
80 | * @param moduleDefinitionNode - A module definition node |
---|
81 | * @return - A list of referenced identifier nodes, excluding the identifier nodes of the module definition itself; if node is not of type ModuleDefintion, an empty list is returned instead |
---|
82 | */ |
---|
83 | //TODO: This may have to be deprecated |
---|
84 | private static LinkedList<LocationAST> getReferencedIdentifierNodes(LocationAST moduleDefinitionNode){ |
---|
85 | if (moduleDefinitionNode.getType()!=TTCN3ParserTokenTypes.ModuleDefinition) { |
---|
86 | return new LinkedList<LocationAST>(); |
---|
87 | } |
---|
88 | LinkedList<LocationAST> identifierNodes = ASTUtil.findTypeNodes(moduleDefinitionNode, TTCN3ParserTokenTypes.Identifier); |
---|
89 | ArrayList<LocationAST> moduleDefinitionIdentifierNodes = LocationAST.getModuleDefinitionIdentifiersList(moduleDefinitionNode); |
---|
90 | identifierNodes.removeAll(moduleDefinitionIdentifierNodes); |
---|
91 | //TODO: exclude predefined functions |
---|
92 | return identifierNodes; |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | /** |
---|
97 | * Transforms a list of <LocationAST> identifier nodes into a list of <String> reference IDs |
---|
98 | * @param identifierNodes - the list of identifier nodes |
---|
99 | * @return - A list of reference IDs |
---|
100 | */ |
---|
101 | private LinkedList<String> getReferenceIds(LinkedList<LocationAST> identifierNodes){ |
---|
102 | LinkedList<String> referencedIds = new LinkedList<String>(); |
---|
103 | |
---|
104 | for (LocationAST identifierNode : identifierNodes) { |
---|
105 | LocationAST declarationNode = VisitorCommonFunctions.getDeclarationNodeFromIdentifier(identifierNode.getFirstChild()); |
---|
106 | if (declarationNode != null) { |
---|
107 | String id = VisitorCommonFunctions.getIdFromIdentifier(declarationNode); |
---|
108 | // TODO: move predefined filtering up |
---|
109 | if (!id.equals("#") && !id.startsWith("trexPredefined")) { |
---|
110 | Symbol symbol = identifierNode.getFirstChild().getSymbol(); |
---|
111 | if (symbol instanceof EnumSymbol) { |
---|
112 | //TODO: need to add handling in documentation and imports generators too probably |
---|
113 | declarationNode = symbol.getSurroundingScope().getScopeSymbol().getDeclarationNode(); |
---|
114 | id = VisitorCommonFunctions.getIdFromIdentifier(declarationNode); |
---|
115 | } |
---|
116 | if (VisitorCommonFunctions.isValidCrossReference(declarationNode)) { |
---|
117 | if (!referencedIds.contains(id)) { |
---|
118 | referencedIds.add(id); |
---|
119 | } |
---|
120 | } |
---|
121 | } |
---|
122 | } else { |
---|
123 | referencedIds.add("unresolvedReference---" |
---|
124 | + VisitorCommonFunctions.getName(identifierNode.getFirstChild())); |
---|
125 | } |
---|
126 | } |
---|
127 | return referencedIds; |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | private String getDependecies(LocationAST identifierNode) { |
---|
132 | //TODO: this should be a part of the printer |
---|
133 | LinkedList<String> referenceIds = getReferenceIds(getRelatedIdentifierNodes(identifierNode)); |
---|
134 | //sanitize list for slipped-through enumerated values |
---|
135 | referenceIds.remove(VisitorCommonFunctions.getIdFromIdentifier(identifierNode)); |
---|
136 | String refs = "\n\t\t<reflist>"; |
---|
137 | for (String id : referenceIds) |
---|
138 | refs += "\n\t\t\t<ref id=\"" + id + "\"/>"; |
---|
139 | refs += "\n\t\t</reflist>"; |
---|
140 | return refs; |
---|
141 | } |
---|
142 | |
---|
143 | private String getElementList(LocationAST node){ |
---|
144 | //TODO: this should be a part of the printer |
---|
145 | String elementList = "\n\t\t<elementlist>"; |
---|
146 | LinkedList <LocationAST> moduleDefinitionsList = ASTUtil.findTypeNodes(LocationAST.resolveParentsUntilType(node, TTCN3ParserTokenTypes.ModuleDefinition).getFirstChild(), TTCN3ParserTokenTypes.ModuleDefinition); |
---|
147 | for(LocationAST moduleDefinitionNode : moduleDefinitionsList){ |
---|
148 | ArrayList<LocationAST> identifiersList = LocationAST.getModuleDefinitionIdentifiersList(moduleDefinitionNode); |
---|
149 | for(LocationAST identifierNode : identifiersList) |
---|
150 | elementList += "\n\t\t\t<ref id=\"" + VisitorCommonFunctions.getIdFromIdentifier(identifierNode) + "\"/>"; |
---|
151 | } |
---|
152 | return elementList + "\n\t\t</elementlist>"; |
---|
153 | } |
---|
154 | |
---|
155 | public ContinueStatus visitModuleDefinition(LocationAST node) throws TTCN3BehaviorException { |
---|
156 | ArrayList<LocationAST> identifiersList = LocationAST.getModuleDefinitionIdentifiersList(node); |
---|
157 | LocationAST typeNode = LocationAST.getModuleDefinitionTypeNode(node); |
---|
158 | |
---|
159 | for (LocationAST identifierNode : identifiersList) { |
---|
160 | if (typeNode.getType()==TTCN3ParserTokenTypes.GroupDef) { |
---|
161 | dependencyPrinter.newGroup(VisitorCommonFunctions.getName(identifierNode), |
---|
162 | VisitorCommonFunctions.getIdFromIdentifier(identifierNode.getFirstChild()), |
---|
163 | typeNode.getType(), |
---|
164 | identifierNode.getLine(), |
---|
165 | VisitorCommonFunctions.getModuleName(identifierNode), |
---|
166 | getElementList(identifierNode)); |
---|
167 | }else { |
---|
168 | dependencyPrinter.newElement(VisitorCommonFunctions.getName(identifierNode), |
---|
169 | VisitorCommonFunctions.getIdFromIdentifier(identifierNode.getFirstChild()), |
---|
170 | typeNode.getType(), |
---|
171 | identifierNode.getLine(), |
---|
172 | VisitorCommonFunctions.getModuleName(identifierNode), |
---|
173 | getDependecies(identifierNode)); |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | return ContinueStatus.getInstance(true,true); |
---|
178 | } |
---|
179 | |
---|
180 | |
---|
181 | |
---|
182 | } |
---|