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