129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2007 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.text.templates.TemplateTranslator;
|
|
14
|
131
|
15 import dwtx.jface.text.templates.SimpleTemplateVariableResolver; // packageimport
|
|
16 import dwtx.jface.text.templates.TemplateBuffer; // packageimport
|
|
17 import dwtx.jface.text.templates.TemplateContext; // packageimport
|
|
18 import dwtx.jface.text.templates.TemplateContextType; // packageimport
|
|
19 import dwtx.jface.text.templates.Template; // packageimport
|
|
20 import dwtx.jface.text.templates.TemplateVariable; // packageimport
|
|
21 import dwtx.jface.text.templates.PositionBasedCompletionProposal; // packageimport
|
|
22 import dwtx.jface.text.templates.TemplateException; // packageimport
|
|
23 import dwtx.jface.text.templates.DocumentTemplateContext; // packageimport
|
|
24 import dwtx.jface.text.templates.GlobalTemplateVariables; // packageimport
|
|
25 import dwtx.jface.text.templates.InclusivePositionUpdater; // packageimport
|
|
26 import dwtx.jface.text.templates.TemplateProposal; // packageimport
|
|
27 import dwtx.jface.text.templates.ContextTypeRegistry; // packageimport
|
|
28 import dwtx.jface.text.templates.JFaceTextTemplateMessages; // packageimport
|
|
29 import dwtx.jface.text.templates.TemplateCompletionProcessor; // packageimport
|
|
30 import dwtx.jface.text.templates.TextTemplateMessages; // packageimport
|
|
31 import dwtx.jface.text.templates.TemplateVariableType; // packageimport
|
|
32 import dwtx.jface.text.templates.TemplateVariableResolver; // packageimport
|
|
33
|
|
34
|
129
|
35 import dwt.dwthelper.utils;
|
|
36
|
|
37 import java.util.ArrayList;
|
|
38 import java.util.Iterator;
|
|
39 import java.util.LinkedHashMap;
|
|
40 import java.util.List;
|
|
41 import java.util.Map;
|
|
42 import java.util.regex.Matcher;
|
|
43 import java.util.regex.Pattern;
|
|
44
|
|
45 /**
|
|
46 * The template translator translates a string into a template buffer. Regions marked as variables
|
|
47 * are translated into <code>TemplateVariable</code>s.
|
|
48 * <p>
|
|
49 * The EBNF grammar of a valid string is as follows:
|
|
50 * </p>
|
|
51 * <p>
|
|
52 * template := (text | escape)*. <br />
|
|
53 * text := character - dollar. <br />
|
|
54 * escape := dollar ('{' variable '}' | dollar). <br />
|
|
55 * dollar := '$'. <br />
|
|
56 * variable := identifier | identifier ':' type. <br />
|
|
57 * type := qualifiedname | qualifiedname '(' arguments ')'. <br />
|
|
58 * arguments := (argument ',')* argument. <br />
|
|
59 * argument := qualifiedname | argumenttext. <br />
|
|
60 * qualifiedname := (identifier '.')* identifier. <br />
|
|
61 * argumenttext := "'" (character - "'" | "'" "'")* "'". <br />
|
|
62 * </p>
|
|
63 * <p>
|
|
64 * Clients may only replace the <code>createVariable</code> method of this class.
|
|
65 * </p>
|
|
66 *
|
|
67 * @since 3.0
|
|
68 */
|
|
69 public class TemplateTranslator {
|
|
70 /**
|
|
71 * Regex pattern for qualifiedname
|
|
72 * @since 3.4
|
|
73 */
|
|
74 private static final String QUALIFIED_NAME= "(?:\\w++\\.)*\\w++"; //$NON-NLS-1$
|
|
75
|
|
76 /**
|
|
77 * Regex pattern for argumenttext
|
|
78 * @since 3.4
|
|
79 */
|
|
80 private static final String ARGUMENT_TEXT= "'(?:(?:'')|(?:[^']))*'"; //$NON-NLS-1$
|
|
81
|
|
82 /**
|
|
83 * Regex pattern for argument
|
|
84 * @since 3.4
|
|
85 */
|
|
86 private static final String ARGUMENT= "(?:" + QUALIFIED_NAME + ")|(?:" + ARGUMENT_TEXT + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
87
|
|
88 /**
|
|
89 * Precompiled regex pattern for qualified names.
|
|
90 * @since 3.3
|
|
91 */
|
|
92 private static final Pattern PARAM_PATTERN= Pattern.compile(ARGUMENT);
|
|
93 /**
|
|
94 * Precompiled regex pattern for valid dollar escapes (dollar literals and variables) and
|
|
95 * (invalid) single dollars.
|
|
96 * @since 3.3
|
|
97 */
|
|
98 private static final Pattern ESCAPE_PATTERN= Pattern.compile(
|
|
99 "\\$\\$|\\$\\{\\s*+" + // $$|${ //$NON-NLS-1$
|
|
100 "(\\w*+)" + // variable id group (1) //$NON-NLS-1$
|
|
101 "\\s*+(?::\\s*+" + // : //$NON-NLS-1$
|
|
102 "(" + QUALIFIED_NAME + ")" + // variable type group (2) //$NON-NLS-1$ //$NON-NLS-2$
|
|
103 "\\s*+(?:\\(\\s*+" + // ( //$NON-NLS-1$
|
|
104 "((?:(?:" + ARGUMENT + ")\\s*+,\\s*+)*(?:" + ARGUMENT + "))" + // arguments group (3) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
105 "\\s*+\\))?\\s*+)?\\}|\\$"); // )}|$ //$NON-NLS-1$
|
|
106 /**
|
|
107 * @since 3.3
|
|
108 */
|
|
109 private final class VariableDescription {
|
|
110 final List fOffsets= new ArrayList(5);
|
|
111 final String fName;
|
|
112 TemplateVariableType fType;
|
|
113
|
|
114 VariableDescription(String name, TemplateVariableType type) {
|
|
115 fName= name;
|
|
116 fType= type;
|
|
117 }
|
|
118
|
|
119 void mergeType(TemplateVariableType type) throws TemplateException {
|
|
120 if (type is null)
|
|
121 return;
|
|
122 if (fType is null)
|
|
123 fType= type;
|
|
124 if (!type.equals(fType))
|
|
125 fail(TextTemplateMessages.getFormattedString("TemplateTranslator.error.incompatible.type", fName)); //$NON-NLS-1$
|
|
126 }
|
|
127 }
|
|
128
|
|
129 /** Last translation error. */
|
|
130 private String fErrorMessage;
|
|
131 /**
|
|
132 * Used to ensure compatibility with subclasses overriding
|
|
133 * {@link #createVariable(String, String, int[])}.
|
|
134 * @since 3.3
|
|
135 */
|
|
136 private TemplateVariableType fCurrentType;
|
|
137
|
|
138 /**
|
|
139 * Returns an error message if an error occurred for the last translation, <code>null</code>
|
|
140 * otherwise.
|
|
141 *
|
|
142 * @return the error message if an error occurred during the most recent translation,
|
|
143 * <code>null</code> otherwise
|
|
144 */
|
|
145 public String getErrorMessage() {
|
|
146 return fErrorMessage;
|
|
147 }
|
|
148
|
|
149 /**
|
|
150 * Translates a template to a <code>TemplateBuffer</code>. <code>null</code> is returned if
|
|
151 * there was an error. <code>getErrorMessage()</code> retrieves the associated error message.
|
|
152 *
|
|
153 * @param template the template to translate.
|
|
154 * @return returns the template buffer corresponding to the string
|
|
155 * @see #getErrorMessage()
|
|
156 * @throws TemplateException if translation failed
|
|
157 */
|
|
158 public TemplateBuffer translate(Template template) throws TemplateException {
|
|
159 return parse(template.getPattern());
|
|
160 }
|
|
161
|
|
162 /**
|
|
163 * Translates a template string to <code>TemplateBuffer</code>. <code>null</code> is
|
|
164 * returned if there was an error. <code>getErrorMessage()</code> retrieves the associated
|
|
165 * error message.
|
|
166 *
|
|
167 * @param string the string to translate.
|
|
168 * @return returns the template buffer corresponding to the string
|
|
169 * @see #getErrorMessage()
|
|
170 * @throws TemplateException if translation failed
|
|
171 */
|
|
172 public TemplateBuffer translate(String string) throws TemplateException {
|
|
173 return parse(string);
|
|
174 }
|
|
175
|
|
176 /**
|
|
177 * Internal parser.
|
|
178 *
|
|
179 * @param string the string to parse
|
|
180 * @return the parsed <code>TemplateBuffer</code>
|
|
181 * @throws TemplateException if the string does not conform to the template format
|
|
182 */
|
|
183 private TemplateBuffer parse(String string) throws TemplateException {
|
|
184
|
|
185 fErrorMessage= null;
|
|
186 final StringBuffer buffer= new StringBuffer(string.length());
|
|
187 final Matcher matcher= ESCAPE_PATTERN.matcher(string);
|
|
188 final Map variables= new LinkedHashMap();
|
|
189
|
|
190 int complete= 0;
|
|
191 while (matcher.find()) {
|
|
192 // append any verbatim text
|
|
193 buffer.append(string.substring(complete, matcher.start()));
|
|
194
|
|
195 // check the escaped sequence
|
|
196 if ("$".equals(matcher.group())) { //$NON-NLS-1$
|
|
197 fail(TextTemplateMessages.getString("TemplateTranslator.error.incomplete.variable")); //$NON-NLS-1$
|
|
198 } else if ("$$".equals(matcher.group())) { //$NON-NLS-1$
|
|
199 // escaped $
|
|
200 buffer.append('$');
|
|
201 } else {
|
|
202 // parse variable
|
|
203 String name= matcher.group(1);
|
|
204 String typeName= matcher.group(2);
|
|
205 String params= matcher.group(3);
|
|
206 TemplateVariableType type= createType(typeName, params);
|
|
207
|
|
208 updateOrCreateVariable(variables, name, type, buffer.length());
|
|
209
|
|
210 buffer.append(name);
|
|
211 }
|
|
212 complete= matcher.end();
|
|
213 }
|
|
214 // append remaining verbatim text
|
|
215 buffer.append(string.substring(complete));
|
|
216
|
|
217 TemplateVariable[] vars= createVariables(variables);
|
|
218 return new TemplateBuffer(buffer.toString(), vars);
|
|
219 }
|
|
220
|
|
221 private TemplateVariableType createType(String typeName, String paramString) {
|
|
222 if (typeName is null)
|
|
223 return null;
|
|
224
|
|
225 if (paramString is null)
|
|
226 return new TemplateVariableType(typeName);
|
|
227
|
|
228 final Matcher matcher= PARAM_PATTERN.matcher(paramString);
|
|
229 List params= new ArrayList(5);
|
|
230 while (matcher.find()) {
|
|
231 String argument= matcher.group();
|
|
232 if (argument.charAt(0) is '\'') {
|
|
233 // argumentText
|
|
234 argument= argument.substring(1, argument.length() - 1).replaceAll("''", "'"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
235 }
|
|
236
|
|
237 params.add(argument);
|
|
238 }
|
|
239
|
|
240 return new TemplateVariableType(typeName, (String[]) params.toArray(new String[params.size()]));
|
|
241 }
|
|
242
|
|
243 private void fail(String message) throws TemplateException {
|
|
244 fErrorMessage= message;
|
|
245 throw new TemplateException(message);
|
|
246 }
|
|
247
|
|
248 /**
|
|
249 * If there is no variable named <code>name</code>, a new variable with the given type, name
|
|
250 * and offset is created. If one exists, the offset is added to the variable and the type is
|
|
251 * merged with the existing type.
|
|
252 *
|
|
253 * @param variables the variables by variable name
|
|
254 * @param name the name of the variable
|
|
255 * @param type the variable type, <code>null</code> for not defined
|
|
256 * @param offset the buffer offset of the variable
|
|
257 * @throws TemplateException if merging the type fails
|
|
258 * @since 3.3
|
|
259 */
|
|
260 private void updateOrCreateVariable(Map variables, String name, TemplateVariableType type, int offset) throws TemplateException {
|
|
261 VariableDescription varDesc= (VariableDescription) variables.get(name);
|
|
262 if (varDesc is null) {
|
|
263 varDesc= new VariableDescription(name, type);
|
|
264 variables.put(name, varDesc);
|
|
265 } else {
|
|
266 varDesc.mergeType(type);
|
|
267 }
|
|
268 varDesc.fOffsets.add(new Integer(offset));
|
|
269 }
|
|
270
|
|
271 /**
|
|
272 * Creates proper {@link TemplateVariable}s from the variable descriptions.
|
|
273 *
|
|
274 * @param variables the variable descriptions by variable name
|
|
275 * @return the corresponding variables
|
|
276 * @since 3.3
|
|
277 */
|
|
278 private TemplateVariable[] createVariables(Map variables) {
|
|
279 TemplateVariable[] result= new TemplateVariable[variables.size()];
|
|
280 int idx= 0;
|
|
281 for (Iterator it= variables.values().iterator(); it.hasNext(); idx++) {
|
|
282 VariableDescription desc= (VariableDescription) it.next();
|
|
283 TemplateVariableType type= desc.fType is null ? new TemplateVariableType(desc.fName) : desc.fType;
|
|
284 int[] offsets= new int[desc.fOffsets.size()];
|
|
285 int i= 0;
|
|
286 for (Iterator intIt= desc.fOffsets.iterator(); intIt.hasNext(); i++) {
|
|
287 Integer offset= (Integer) intIt.next();
|
|
288 offsets[i]= offset.intValue();
|
|
289 }
|
|
290 fCurrentType= type;
|
|
291 /*
|
|
292 * Call the deprecated version of createVariable. When not overridden, it will delegate
|
|
293 * to the new version using fCurrentType.
|
|
294 */
|
|
295 TemplateVariable var= createVariable(type.getName(), desc.fName, offsets);
|
|
296 result[idx]= var;
|
|
297 }
|
|
298 fCurrentType= null; // avoid dangling reference
|
|
299 return result;
|
|
300 }
|
|
301
|
|
302 /**
|
|
303 * Hook method to create new variables. Subclasses may override to supply their custom variable
|
|
304 * type.
|
|
305 * <p>
|
|
306 * Clients may replace this method.
|
|
307 * </p>
|
|
308 *
|
|
309 * @param type the type of the new variable.
|
|
310 * @param name the name of the new variable.
|
|
311 * @param offsets the offsets where the variable occurs in the template
|
|
312 * @return a new instance of <code>TemplateVariable</code>
|
|
313 * @deprecated as of 3.3 use {@link #createVariable(TemplateVariableType, String, int[])} instead
|
|
314 */
|
|
315 protected TemplateVariable createVariable(String type, String name, int[] offsets) {
|
|
316 return createVariable(fCurrentType, name, offsets);
|
|
317 }
|
|
318
|
|
319 /**
|
|
320 * Hook method to create new variables. Subclasses may override to supply their custom variable
|
|
321 * type.
|
|
322 * <p>
|
|
323 * Clients may replace this method.
|
|
324 * </p>
|
|
325 *
|
|
326 * @param type the type of the new variable.
|
|
327 * @param name the name of the new variable.
|
|
328 * @param offsets the offsets where the variable occurs in the template
|
|
329 * @return a new instance of <code>TemplateVariable</code>
|
|
330 * @since 3.3
|
|
331 */
|
|
332 protected TemplateVariable createVariable(TemplateVariableType type, String name, int[] offsets) {
|
|
333 return new TemplateVariable(type, name, name, offsets);
|
|
334 }
|
|
335 }
|