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
|
package org.momut.ooas.visitors;
|
29
|
|
30
|
import java.util.ArrayList;
|
31
|
import java.util.EnumMap;
|
32
|
import java.util.List;
|
33
|
import java.util.Set;
|
34
|
import java.util.TreeMap;
|
35
|
|
36
|
import org.momut.ooas.CompilerConfiguration.LabelCompression;
|
37
|
import org.momut.ooas.ast.identifiers.AttributeIdentifier;
|
38
|
import org.momut.ooas.ast.identifiers.Identifier;
|
39
|
import org.momut.ooas.ast.identifiers.IdentifierKind;
|
40
|
import org.momut.ooas.ast.identifiers.MethodIdentifier;
|
41
|
import org.momut.ooas.ast.identifiers.NamedActionIdentifier;
|
42
|
import org.momut.ooas.ast.types.FunctionType;
|
43
|
import org.momut.ooas.ast.types.OoActionSystemInstance;
|
44
|
import org.momut.ooas.ast.types.FunctionType.FunctionTypeEnum;
|
45
|
import org.momut.ooas.parser.ParserState;
|
46
|
import org.momut.ooas.utils.exceptions.ArgumentException;
|
47
|
import org.momut.ooas.utils.exceptions.NotImplementedException;
|
48
|
|
49
|
public class OoaIdentifierCompressVisitor extends OoaCompleteAstTraversalVisitor {
|
50
|
|
51
|
public static final String s_methodPrefix = "m_";
|
52
|
public static final String s_actionPrefix = "a_";
|
53
|
public static final String s_attributePrefix = "at_";
|
54
|
public static final String s_objectPrefix = "o_";
|
55
|
public static final String s_typePrefix = "t_";
|
56
|
|
57
|
private final EnumMap<IdentifierKind, TreeMap<String, String> > m_identifierMappings = new EnumMap<>(IdentifierKind.class);
|
58
|
private final TreeMap<String, String> m_objectMappings = new TreeMap<>();
|
59
|
private final LabelCompression m_compression;
|
60
|
|
61
|
private void putMapping(String label, Identifier identifier) {
|
62
|
TreeMap<String, String> map = m_identifierMappings.get(identifier.kind());
|
63
|
if (map == null) {
|
64
|
map = new TreeMap<>();
|
65
|
m_identifierMappings.put(identifier.kind(), map);
|
66
|
}
|
67
|
map.put(label, identifier.tokenText());
|
68
|
}
|
69
|
|
70
|
public String mappingFor(Identifier identifier) {
|
71
|
final IdentifierKind kind = identifier.kind();
|
72
|
final TreeMap<String, String> map = m_identifierMappings.get(kind);
|
73
|
return map.get(identifier.tokenText());
|
74
|
}
|
75
|
|
76
|
@Override
|
77
|
public void visit(AttributeIdentifier attributeIdentifier) {
|
78
|
if (m_compression != LabelCompression.All)
|
79
|
return;
|
80
|
final String attributeName = labelFor(attributeIdentifier);
|
81
|
attributeIdentifier.SetTokenText(attributeName);
|
82
|
}
|
83
|
|
84
|
@Override
|
85
|
public void visit(MethodIdentifier methodIdentifier) {
|
86
|
final String renamedName = labelFor(methodIdentifier);
|
87
|
methodIdentifier.SetTokenText(renamedName);
|
88
|
}
|
89
|
|
90
|
@Override
|
91
|
public void visit(NamedActionIdentifier namedActionIdentifier) {
|
92
|
final FunctionType functionType = (FunctionType) namedActionIdentifier.type();
|
93
|
if (functionType.functionType() == FunctionTypeEnum.Internal) {
|
94
|
final String renamedName = labelFor(namedActionIdentifier);
|
95
|
namedActionIdentifier.SetTokenText(renamedName);
|
96
|
}
|
97
|
}
|
98
|
|
99
|
public String labelFor(Identifier identifier) {
|
100
|
String label = identifier.tokenText();
|
101
|
|
102
|
switch(identifier.kind()) {
|
103
|
case MethodIdentifier:
|
104
|
label = s_methodPrefix + convert(methodCount++);
|
105
|
break;
|
106
|
case NamedActionIdentifier:
|
107
|
label = s_actionPrefix + convert(actionCount++);
|
108
|
break;
|
109
|
case AttributeIdentifier:
|
110
|
label = s_attributePrefix + convert(attributeCount++);
|
111
|
break;
|
112
|
default:
|
113
|
throw new NotImplementedException();
|
114
|
}
|
115
|
putMapping(label, identifier);
|
116
|
return label;
|
117
|
}
|
118
|
|
119
|
public String labelFor(OoActionSystemInstance instance) {
|
120
|
final String label = s_objectPrefix + convert(objectCount++);
|
121
|
m_objectMappings.put(label, instance.Name);
|
122
|
return label;
|
123
|
}
|
124
|
|
125
|
public String originalName(String key) {
|
126
|
if(m_objectMappings.get(key) != null) {
|
127
|
return m_objectMappings.get(key);
|
128
|
}
|
129
|
return key;
|
130
|
}
|
131
|
|
132
|
public List<String> mappingInformation() {
|
133
|
final List<String> lines = new ArrayList<>();
|
134
|
final Set<IdentifierKind> kinds = m_identifierMappings.keySet();
|
135
|
|
136
|
for (final IdentifierKind kind: kinds) {
|
137
|
final TreeMap<String, String> map = m_identifierMappings.get(kind);
|
138
|
final Set<String> keys = map.keySet();
|
139
|
lines.add(String.format("[ Mappings for '%s' ]", kind.name()));
|
140
|
for(final String key: keys) {
|
141
|
lines.add(String.format("'%s' => '%s'", key, map.get(key)));
|
142
|
}
|
143
|
lines.add("");
|
144
|
}
|
145
|
|
146
|
if (m_objectMappings.isEmpty())
|
147
|
return lines;
|
148
|
|
149
|
lines.add(String.format("[ Object mappings ]"));
|
150
|
final Set<String> keys = m_objectMappings.keySet();
|
151
|
for (final String key: keys) {
|
152
|
lines.add(String.format("'%s' => '%s'", key, originalName(key)));
|
153
|
}
|
154
|
lines.add("");
|
155
|
return lines;
|
156
|
}
|
157
|
|
158
|
public LabelCompression compression() {
|
159
|
return m_compression;
|
160
|
}
|
161
|
|
162
|
static final String CHARS = "abcdefghijklmnopqrstuvwxyz";
|
163
|
static final int CHARS_LENGTH = CHARS.length();
|
164
|
|
165
|
int actionCount = 0;
|
166
|
int methodCount = 0;
|
167
|
int attributeCount = 0;
|
168
|
int objectCount = 0;
|
169
|
|
170
|
private String convert(int id) {
|
171
|
final StringBuilder sb = new StringBuilder();
|
172
|
do {
|
173
|
sb.append(CHARS.charAt(id % CHARS_LENGTH));
|
174
|
id = id / CHARS_LENGTH;
|
175
|
} while(id != 0);
|
176
|
return sb.toString();
|
177
|
}
|
178
|
|
179
|
public OoaIdentifierCompressVisitor(ParserState aState, LabelCompression compression)
|
180
|
{
|
181
|
super (aState);
|
182
|
if (aState == null || compression == null)
|
183
|
throw new ArgumentException();
|
184
|
m_compression = compression;
|
185
|
}
|
186
|
}
|