Explorar el Código

Added valueconverter for float values. The converter provides "f" suffix for float values so rename refactorings can be done for variables of type float. (#1932)

Robert Rudi hace 7 años
padre
commit
baeda2b189

+ 9 - 1
plugins/org.yakindu.base.expressions/src/org/yakindu/base/expressions/terminals/ExpressionsValueConverterService.java

@@ -28,6 +28,7 @@ public class ExpressionsValueConverterService extends DefaultTerminalConverters
 	public static final String BOOL = "BOOL";
 	public static final String HEX = "HEX";
 	public static final String BINARY = "BINARY";
+	public static final String FLOAT = "FLOAT";
 	
 	@Inject
 	protected AbstractIDValueConverter idValueConverter;
@@ -44,6 +45,9 @@ public class ExpressionsValueConverterService extends DefaultTerminalConverters
 	@Inject
 	protected QIDValueConverter qidConverter;
 	
+	@Inject
+	protected FloatValueConverter floatConverter;
+	
 	@ValueConverter(rule = BOOL)
 	public IValueConverter<Boolean> BOOL() {
 		return boolConverter;
@@ -63,6 +67,10 @@ public class ExpressionsValueConverterService extends DefaultTerminalConverters
 	public IValueConverter<String> QID() {
 		return qidConverter;
 	}
-
+	
+	@ValueConverter(rule = FLOAT)
+	public IValueConverter<Float> FLOAT() {
+		return floatConverter;
+	}
 
 }

+ 48 - 0
plugins/org.yakindu.base.expressions/src/org/yakindu/base/expressions/terminals/FloatValueConverter.java

@@ -0,0 +1,48 @@
+/** 
+ * Copyright (c) 2018 committers of YAKINDU and others. 
+ * 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:
+ * 	committers of YAKINDU - initial API and implementation
+ *
+*/
+package org.yakindu.base.expressions.terminals;
+
+import org.eclipse.xtext.conversion.ValueConverterException;
+import org.eclipse.xtext.conversion.impl.AbstractLexerBasedConverter;
+import org.eclipse.xtext.nodemodel.INode;
+import org.eclipse.xtext.util.Strings;
+
+/**
+ * This float value converter provides float values with 'f' as suffix
+ * character.
+ * 
+ * @author robert rudi
+ *
+ */
+public class FloatValueConverter extends AbstractLexerBasedConverter<Float> {
+
+	protected static final String FLOAT_SUFFIX = "f";
+
+	@Override
+	public Float toValue(String string, INode node) throws ValueConverterException {
+		if (Strings.isEmpty(string))
+			throw new ValueConverterException("Couldn't convert empty string to float.", node, null);
+		try {
+			if (string.endsWith(FLOAT_SUFFIX))
+				return Float.parseFloat(string.substring(0, string.length() - 1));
+			else
+				return Float.parseFloat(string);
+		} catch (NumberFormatException e) {
+			throw new ValueConverterException("Couldn't convert '" + string + "' to float.", node, null);
+		}
+	}
+
+	@Override
+	protected String toEscapedString(Float value) {
+		return super.toEscapedString(value) + FLOAT_SUFFIX;
+	}
+
+}