Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 077f94e395fbe3c1eb95bd2e2ea1b28ca5f426af (plain) (blame)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*******************************************************************************
 * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * CONTRIBUTORS:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.generator.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.eclipse.etrice.core.room.ActorClass;
import org.eclipse.etrice.core.room.Attribute;
import org.eclipse.etrice.core.room.DetailCode;
import org.eclipse.etrice.core.room.InterfaceItem;
import org.eclipse.etrice.core.room.Message;
import org.eclipse.etrice.core.room.Operation;
import org.eclipse.etrice.core.room.Port;
import org.eclipse.etrice.core.room.SAPRef;
import org.eclipse.etrice.core.room.SPPRef;
import org.eclipse.etrice.core.room.util.RoomHelpers;

/**
 * @author Henrik Rentz-Reichert
 *
 */
public class DetailCodeTranslator {

	public static interface ITranslationProvider {
		String getAttributeText(Attribute att, String orig);
		String getOperationText(Operation op, ArrayList<String> args, String orig);
		String getInterfaceItemMessageText(InterfaceItem item, Message msg, ArrayList<String> args, String orig);
		String getInterfaceItemMessageValue(InterfaceItem item, Message msg, String orig);
	}

	private static class Position {
		int pos = 0;
	}
	
	private ActorClass ac;
	private ITranslationProvider provider;
	private HashMap<String, InterfaceItem> name2item;
	private HashMap<String, Attribute> name2attr;
	private HashMap<String, Operation> name2op;
	
	public DetailCodeTranslator(ActorClass ac, ITranslationProvider provider) {
		this.ac = ac;
		this.provider = provider;
		
		prepare();
	}
	
	public String translateDetailCode(DetailCode code) {
		if (code==null)
			return null;
		
		// concatenate lines
		StringBuilder text = new StringBuilder();
		for (String line : code.getCommands()) {
			text.append(line+"\n");
		}

		StringBuilder result = new StringBuilder();
		translateText(text.substring(0, text.length()-1), result);
		
		return result.toString();
	}
	
	private void translateText(String text, StringBuilder result) {
		Position curr = new Position();
		int last = 0;
		
		while (curr.pos<text.length()) {
			proceedToToken(text, curr);
			
			last = appendParsed(text, curr, last, result);
			
			String token = getToken(text, curr);
			if (token.isEmpty()) {
				while (curr.pos<text.length() && !isTokenChar(text.charAt(curr.pos)))
					++curr.pos;
				last = appendParsed(text, curr, last, result);
			}
			else {
				// translate token if possible
				String translated = null;
				Attribute attribute = name2attr.get(token);
				if (attribute!=null) {
					String orig = text.substring(last, curr.pos);
					translated = provider.getAttributeText(attribute, orig);
				}
				else {
					Operation operation = name2op.get(token);
					if (operation!=null) {
						ArrayList<String> args = getArgs(text, curr);
						if (args!=null && operation.getArguments().size()==args.size()) {
							// recursively apply this algorithm to each argument
							for (int i=0; i<args.size(); ++i) {
								StringBuilder transArg = new StringBuilder();
								translateText(args.remove(i), transArg);
								args.add(i, transArg.toString());
							}
							String orig = text.substring(last, curr.pos);
							translated = provider.getOperationText(operation, args, orig);
						}
					}
					else {
						InterfaceItem item = name2item.get(token);
						if (item!=null) {
							Message msg = getMessage(text, curr, item);
							ArrayList<String> args = getArgs(text, curr);
							if (msg!=null) {
								if (args!=null) {
									if (argsMatching(msg, args)) {
										String orig = text.substring(last, curr.pos);
										translated = provider.getInterfaceItemMessageText(item, msg, args, orig);
									}
								}
								else {
									if (text.charAt(curr.pos)!='(') {
										String orig = text.substring(last, curr.pos);
										translated = provider.getInterfaceItemMessageValue(item, msg, orig);
									}
								}
							}
						}
					}
				}
				if (translated!=null) {
					last = curr.pos;
					result.append(translated);
				}
				else
					last = appendParsed(text, curr, last, result);
			}
		}
	}

	/**
	 * @param text
	 * @param result
	 * @return 
	 */
	private int appendParsed(String text, Position curr, int last, StringBuilder result) {
		String str = text.substring(last, curr.pos);
		result.append(str);
		return curr.pos;
	}

	private boolean argsMatching(Message msg, ArrayList<String> args) {
		if (msg.getData()==null && args.isEmpty())
			return true;
		if (msg.getData()!=null && args.size()==1)
			return true;
		
		return false;
	}

	private void proceedToToken(String text, Position curr) {
		boolean stop = false;
		while (curr.pos<text.length() && !stop) {
			if (text.charAt(curr.pos)=='"') {
				skipString(text, curr);
			}
			else if (text.charAt(curr.pos)=='/') {
				if (curr.pos+1<text.length()) {
					if (text.charAt(curr.pos+1)=='/') {
						skipSingleComment(text, curr);
					}
					else if (text.charAt(curr.pos+1)=='*') {
						skipMultiComment(text, curr);
					}
				}
			}
			else if (Character.isWhitespace(text.charAt(curr.pos))) {
				skipWhiteSpace(text, curr);
			}
			else
				stop = true;
		}
	}
	
	private Message getMessage(String text, Position curr, InterfaceItem item) {
		proceedToToken(text, curr);

		if (text.charAt(curr.pos)!='.')
			return null;
		++curr.pos;
		
		proceedToToken(text, curr);
		
		String token = getToken(text, curr);
		
		List<Message> messages = null;
		if (item instanceof Port) {
			if (((Port) item).isConjugated())
				messages = ((Port) item).getProtocol().getIncomingMessages();
			else
				messages = ((Port) item).getProtocol().getOutgoingMessages();
		}
		else if (item instanceof SAPRef) {
			messages = ((SAPRef)item).getProtocol().getIncomingMessages();
		}
		else if (item instanceof SPPRef) {
			messages = ((SPPRef)item).getProtocol().getOutgoingMessages();
		}
		else {
			assert(false): "unexpected sub type";
			return null;
		}
		
		for (Message message : messages) {
			if (message.getName().equals(token))
				return message;
		}
		
		return null;
	}
	
	private ArrayList<String> getArgs(String text, Position curr) {
		proceedToToken(text, curr);

		if (text.charAt(curr.pos)!='(')
			return null;
		++curr.pos;
		
		ArrayList<String> result = new ArrayList<String>();
		
		boolean stop = false;
		do {
			proceedToToken(text, curr);
			if (text.charAt(curr.pos)!=')') {
				String arg = getParam(text, curr);
				result.add(arg);
				proceedToToken(text, curr);
			}
			if (text.charAt(curr.pos)==',')
				++curr.pos;
			else
				stop = true;
		}
		while (!stop);

		if (text.charAt(curr.pos)!=')')
			return null;
		
		++curr.pos;
		
		return result;
	}

	private String getToken(String text, Position curr) {
		int begin = curr.pos;
		while (curr.pos<text.length() && isTokenChar(text.charAt(curr.pos)))
			++curr.pos;
		String token = text.substring(begin, curr.pos);
		return token;
	}

	private String getParam(String text, Position curr) {
		int begin = curr.pos;
		int parenthesisLevel = 0;
		while (curr.pos<text.length()) {
			if (text.charAt(curr.pos)=='(')
				++parenthesisLevel;
			else if (text.charAt(curr.pos)==')') {
				if (parenthesisLevel==0)
					break;
				else
					--parenthesisLevel;
			}
			else if (parenthesisLevel==0) {
				if (text.charAt(curr.pos)==',')
					break;
			}
			++curr.pos;
		}
		String token = text.substring(begin, curr.pos).trim();
		return token;
	}

	private void skipWhiteSpace(String text, Position curr) {
		while (curr.pos<text.length() && Character.isWhitespace(text.charAt(curr.pos)))
			++curr.pos;
	}

	private void skipMultiComment(String text, Position curr) {
		curr.pos += 2;
		while (curr.pos<text.length()-1 && text.charAt(curr.pos++)!='*')
			if (text.charAt(curr.pos)=='/')
				break;
		if (curr.pos<text.length())
			++curr.pos;
	}

	private void skipSingleComment(String text, Position curr) {
		while (curr.pos<text.length() && text.charAt(curr.pos)!='\n')
			++curr.pos;
		if (curr.pos<text.length())
			++curr.pos;
	}

	private void skipString(String text, Position curr) {
		while (++curr.pos<text.length() && text.charAt(curr.pos)!='"')
			if (text.charAt(curr.pos)=='\\')
				++curr.pos;
	}

	private boolean isTokenChar(char c) {
		return Character.isDigit(c) || Character.isLetter(c) || c=='_';
	}

	private void prepare() {
		name2item = new HashMap<String, InterfaceItem>();
		List<InterfaceItem> items = RoomHelpers.getAllInterfaceItems(ac);
		for (InterfaceItem item : items) {
			name2item.put(item.getName(), item);
		}

		name2attr = new HashMap<String, Attribute>();
		List<Attribute> attributes = RoomHelpers.getAllAttributes(ac);
		for (Attribute attribute : attributes) {
			name2attr.put(attribute.getName(), attribute);
		}
		
		name2op = new HashMap<String, Operation>();
		List<Operation> operations = RoomHelpers.getAllOperations(ac);
		for (Operation operation : operations) {
			name2op.put(operation.getName(), operation);
		}
	}
}

Back to the top