Fixed: [Issue 151] Negative hexadecimal numbers are not supported.
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/conversion/HEXValueConverter_toValue_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/conversion/HEXValueConverter_toValue_Test.java
index 8356c23..aeb48ee 100644
--- a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/conversion/HEXValueConverter_toValue_Test.java
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/conversion/HEXValueConverter_toValue_Test.java
@@ -44,6 +44,7 @@
{ "0x1", 1L },
{ "0xA", 10L },
{ "0xFF", 255L },
+ { "-0x80000000", -2147483648L },
{ "0xffffffffffffffff", -1L } // overflow
});
}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/Protobuf.xtext b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/Protobuf.xtext
index 7e2d303..c9aeb75 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/Protobuf.xtext
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/Protobuf.xtext
@@ -162,7 +162,7 @@
('[' fieldOptions+=FieldOption (',' fieldOptions+=FieldOption)* ']')? ';';
terminal HEX returns ecore::ELong:
- '0x' (NUMBER | 'a'..'f' | 'A'..'F')+;
+ ('-')? '0x' (NUMBER | 'a'..'f' | 'A'..'F')+;
terminal NUMBER:
'0'..'9';
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/HEXValueConverter.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/HEXValueConverter.java
index d87eb69..43a7618 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/HEXValueConverter.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/HEXValueConverter.java
@@ -23,6 +23,8 @@
*/
public class HEXValueConverter extends AbstractLexerBasedConverter<Long> {
+ private static final String[] VALID_PREFIXES = { "0x", "-0x", "0X", "-0x" };
+
/**
* Creates an {@code int} from the given input, if the given input represents an hexadecimal number.
* @param string the given input.
@@ -33,8 +35,8 @@
*/
@Override public Long toValue(String string, INode node) throws ValueConverterException {
if (isEmpty(string)) throw new ValueConverterException("Couldn't convert empty string to long.", node, null);
- if (!string.startsWith("0x") && !string.startsWith("0X")) throw parsingError(string, node);
- String withoutZeroX = string.substring(2, string.length());
+ if (!startsWithValidPrefix(string)) throw parsingError(string, node);
+ String withoutZeroX = removeZeroX(string);
try {
BigInteger value = new BigInteger(withoutZeroX, 16);
return value.longValue();
@@ -43,6 +45,21 @@
}
}
+ private boolean startsWithValidPrefix(String string) {
+ for (String prefix : VALID_PREFIXES) {
+ if (string.startsWith(prefix)) return true;
+ }
+ return false;
+ }
+
+ private String removeZeroX(String string) {
+ if (string.startsWith("-")) {
+ String withoutSign = string.substring(3, string.length());
+ return "-" + withoutSign;
+ }
+ return string.substring(2, string.length());
+ }
+
private ValueConverterException parsingError(String string, INode node) {
return parsingError(string, node, null);
}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/Messages.properties b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/Messages.properties
index 688c504..c84ff3f 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/Messages.properties
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/Messages.properties
@@ -9,5 +9,5 @@
importNotFound = Import \"%s\" was not found.
missingFieldNumber = Missing field number.
multiplePackages = Multiple package definitions.
-nonProto2 = Protocol buffer with deprecated syntax. This parser only recognizes \"proto2\".
+nonProto2 = Deprecated syntax. This parser only recognizes \"proto2\".
unrecognizedSyntaxIdentifier = Unrecognized syntax identifier \"%s\". This parser only recognizes \"proto2\".