Fixed: [ Issue 67 ] 'nan' is a valid default value for double and float https://code.google.com/p/protobuf-dt/issues/detail?id=67 Also added validations for default string and boolean values.
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/grammar/CommonKeyword_hasValue_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/grammar/CommonKeyword_hasValue_Test.java similarity index 91% rename from com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/grammar/CommonKeyword_hasValue_Test.java rename to com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/grammar/CommonKeyword_hasValue_Test.java index 902a757..0e0da1f 100644 --- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/grammar/CommonKeyword_hasValue_Test.java +++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/grammar/CommonKeyword_hasValue_Test.java
@@ -6,9 +6,9 @@ * * http://www.eclipse.org/legal/epl-v10.html */ -package com.google.eclipse.protobuf.ui.grammar; +package com.google.eclipse.protobuf.grammar; -import static com.google.eclipse.protobuf.ui.grammar.CommonKeyword.*; +import static com.google.eclipse.protobuf.grammar.CommonKeyword.*; import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertThat;
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/grammar/CommonKeyword_toString_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/grammar/CommonKeyword_toString_Test.java similarity index 84% rename from com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/grammar/CommonKeyword_toString_Test.java rename to com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/grammar/CommonKeyword_toString_Test.java index 9895bfc..50ec6f0 100644 --- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/grammar/CommonKeyword_toString_Test.java +++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/grammar/CommonKeyword_toString_Test.java
@@ -6,9 +6,9 @@ * * http://www.eclipse.org/legal/epl-v10.html */ -package com.google.eclipse.protobuf.ui.grammar; +package com.google.eclipse.protobuf.grammar; -import static com.google.eclipse.protobuf.ui.grammar.CommonKeyword.BOOL; +import static com.google.eclipse.protobuf.grammar.CommonKeyword.BOOL; import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertThat;
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_isBool_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/Properties_isBool_Test.java similarity index 97% rename from com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_isBool_Test.java rename to com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/Properties_isBool_Test.java index 54930eb..e30879e 100644 --- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_isBool_Test.java +++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/Properties_isBool_Test.java
@@ -6,7 +6,7 @@ * * http://www.eclipse.org/legal/epl-v10.html */ -package com.google.eclipse.protobuf.ui.util; +package com.google.eclipse.protobuf.util; import static com.google.eclipse.protobuf.junit.util.Finder.findProperty; import static org.hamcrest.core.IsEqual.equalTo;
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_isPrimitive_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/Properties_isPrimitive_Test.java similarity index 97% rename from com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_isPrimitive_Test.java rename to com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/Properties_isPrimitive_Test.java index db24e18..384664d 100644 --- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_isPrimitive_Test.java +++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/Properties_isPrimitive_Test.java
@@ -6,7 +6,7 @@ * * http://www.eclipse.org/legal/epl-v10.html */ -package com.google.eclipse.protobuf.ui.util; +package com.google.eclipse.protobuf.util; import static com.google.eclipse.protobuf.junit.util.Finder.allProperties; import static org.hamcrest.core.IsEqual.equalTo;
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_isString_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/Properties_isString_Test.java similarity index 97% rename from com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_isString_Test.java rename to com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/Properties_isString_Test.java index 4ef01fc..595b89b 100644 --- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_isString_Test.java +++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/Properties_isString_Test.java
@@ -6,7 +6,7 @@ * * http://www.eclipse.org/legal/epl-v10.html */ -package com.google.eclipse.protobuf.ui.util; +package com.google.eclipse.protobuf.util; import static com.google.eclipse.protobuf.junit.util.Finder.findProperty; import static org.hamcrest.core.IsEqual.equalTo;
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_typeNameOf_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/Properties_typeNameOf_Test.java similarity index 97% rename from com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_typeNameOf_Test.java rename to com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/Properties_typeNameOf_Test.java index 8b50b0d..1a08d4b 100644 --- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_typeNameOf_Test.java +++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/Properties_typeNameOf_Test.java
@@ -6,7 +6,7 @@ * * http://www.eclipse.org/legal/epl-v10.html */ -package com.google.eclipse.protobuf.ui.util; +package com.google.eclipse.protobuf.util; import static com.google.eclipse.protobuf.junit.util.Finder.findProperty; import static org.hamcrest.core.IsEqual.equalTo;
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/grammar/CompoundElement_indexOf_Test.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/grammar/CompoundElement_indexOf_Test.java index 15ef28d..647bbdf 100644 --- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/grammar/CompoundElement_indexOf_Test.java +++ b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/grammar/CompoundElement_indexOf_Test.java
@@ -8,13 +8,15 @@ */ package com.google.eclipse.protobuf.ui.grammar; -import static com.google.eclipse.protobuf.ui.grammar.CommonKeyword.*; +import static com.google.eclipse.protobuf.grammar.CommonKeyword.*; import static com.google.eclipse.protobuf.ui.grammar.CompoundElement.DEFAULT_EQUAL; import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertThat; import org.junit.Test; +import com.google.eclipse.protobuf.grammar.CommonKeyword; + /** * Tests for <code>{@link CompoundElement#indexOf(CommonKeyword)}</code>. *
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/commands/SmartSemicolonHandler.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/commands/SmartSemicolonHandler.java index 6f52c7e..35c6bf8 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/commands/SmartSemicolonHandler.java +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/commands/SmartSemicolonHandler.java
@@ -8,7 +8,7 @@ */ package com.google.eclipse.protobuf.ui.commands; -import static com.google.eclipse.protobuf.ui.grammar.CommonKeyword.SEMICOLON; +import static com.google.eclipse.protobuf.grammar.CommonKeyword.SEMICOLON; import java.util.regex.Pattern;
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/ProtobufProposalProvider.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/ProtobufProposalProvider.java index ed93126..d74ff54 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/ProtobufProposalProvider.java +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/ProtobufProposalProvider.java
@@ -8,15 +8,15 @@ */ package com.google.eclipse.protobuf.ui.contentassist; +import static com.google.eclipse.protobuf.grammar.CommonKeyword.*; import static com.google.eclipse.protobuf.protobuf.Modifier.*; -import static com.google.eclipse.protobuf.protobuf.ScalarType.STRING; -import static com.google.eclipse.protobuf.ui.grammar.CommonKeyword.*; import static com.google.eclipse.protobuf.ui.grammar.CompoundElement.*; import static com.google.eclipse.protobuf.ui.util.Strings.firstCharToLowerCase; import static java.lang.String.valueOf; import static java.util.Collections.emptyList; -import java.util.*; +import java.util.ArrayList; +import java.util.List; import org.eclipse.emf.ecore.EObject; import org.eclipse.jface.text.contentassist.ICompletionProposal; @@ -27,14 +27,16 @@ import org.eclipse.xtext.ui.editor.contentassist.*; import com.google.common.collect.ImmutableList; +import com.google.eclipse.protobuf.grammar.CommonKeyword; import com.google.eclipse.protobuf.protobuf.*; import com.google.eclipse.protobuf.protobuf.Enum; -import com.google.eclipse.protobuf.scoping.*; -import com.google.eclipse.protobuf.ui.grammar.*; +import com.google.eclipse.protobuf.scoping.ProtoDescriptor; +import com.google.eclipse.protobuf.scoping.ProtoDescriptorProvider; import com.google.eclipse.protobuf.ui.grammar.CompoundElement; import com.google.eclipse.protobuf.ui.labeling.Images; -import com.google.eclipse.protobuf.ui.util.*; -import com.google.eclipse.protobuf.ui.util.Properties; +import com.google.eclipse.protobuf.ui.util.Fields; +import com.google.eclipse.protobuf.ui.util.Literals; +import com.google.eclipse.protobuf.util.Properties; import com.google.eclipse.protobuf.util.ProtobufElementFinder; import com.google.inject.Inject; @@ -131,7 +133,7 @@ ICompletionProposalAcceptor acceptor) { if (model instanceof Property && isProposalForDefaultValue(context)) { Property p = (Property) model; - if (!isStringProperty(p)) return; + if (!properties.isString(p)) return; proposeEmptyString(context, acceptor); return; } @@ -223,7 +225,7 @@ if (OPTIONAL.equals(modifier)) { CompoundElement display = DEFAULT_EQUAL_IN_BRACKETS; int cursorPosition = display.indexOf(CLOSING_BRACKET); - if (isStringProperty(p)) { + if (properties.isString(p)) { display = DEFAULT_EQUAL_STRING_IN_BRACKETS; cursorPosition++; } @@ -243,7 +245,7 @@ CompoundElement display = DEFAULT_EQUAL; int cursorPosition = display.charCount(); Property property = extractPropertyFrom(context); - if (isStringProperty(property)) { + if (properties.isString(property)) { display = DEFAULT_EQUAL_STRING; cursorPosition++; } @@ -277,10 +279,6 @@ return type.cast(model); } - private boolean isStringProperty(Property p) { - return STRING.equals(finder.scalarTypeOf(p)); - } - private ICompletionProposal createCompletionProposal(CompoundElement proposal, ContentAssistContext context) { return createCompletionProposal(proposal.toString(), context); } @@ -398,6 +396,7 @@ @Override public void completeFieldOption_Value(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) { + if (!(model instanceof FieldOption)) return; FieldOption option = (FieldOption) model; ProtoDescriptor descriptor = descriptorProvider.get(); Enum enumType = descriptor.enumTypeOf(option);
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/CompoundElement.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/CompoundElement.java index 418b253..62b1046 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/CompoundElement.java +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/CompoundElement.java
@@ -8,7 +8,9 @@ */ package com.google.eclipse.protobuf.ui.grammar; -import static com.google.eclipse.protobuf.ui.grammar.CommonKeyword.*; +import static com.google.eclipse.protobuf.grammar.CommonKeyword.*; + +import com.google.eclipse.protobuf.grammar.CommonKeyword; /** * Element composed of one or more keywords.
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/labeling/Labels.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/labeling/Labels.java index d8319e4..ae469a2 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/labeling/Labels.java +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/labeling/Labels.java
@@ -12,8 +12,8 @@ import static org.eclipse.jface.viewers.StyledString.DECORATIONS_STYLER; import com.google.eclipse.protobuf.protobuf.*; -import com.google.eclipse.protobuf.ui.util.Properties; import com.google.eclipse.protobuf.util.ModelNodes; +import com.google.eclipse.protobuf.util.Properties; import com.google.inject.*; import org.eclipse.jface.viewers.StyledString;
diff --git a/com.google.eclipse.protobuf/META-INF/MANIFEST.MF b/com.google.eclipse.protobuf/META-INF/MANIFEST.MF index f4159fc..be7f077 100644 --- a/com.google.eclipse.protobuf/META-INF/MANIFEST.MF +++ b/com.google.eclipse.protobuf/META-INF/MANIFEST.MF
@@ -21,6 +21,7 @@ org.apache.commons.logging Bundle-RequiredExecutionEnvironment: J2SE-1.5 Export-Package: com.google.eclipse.protobuf, + com.google.eclipse.protobuf.grammar, com.google.eclipse.protobuf.parseTreeConstruction, com.google.eclipse.protobuf.parser.antlr, com.google.eclipse.protobuf.parser.antlr.internal,
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 a14878e..0f0c32e 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
@@ -108,7 +108,9 @@ | BooleanRef | IntRef | FloatRef - | StringRef; + | DoubleRef + | StringRef + | Nan; LiteralRef: literal=[Literal]; @@ -126,9 +128,15 @@ FloatRef: float=FLOAT; +DoubleRef: + double=DOUBLE; + StringRef: string=STRING; +Nan: + number='nan'; + Enum: 'enum' name=Name '{' literals+=Literal* @@ -163,4 +171,5 @@ terminal INT returns ecore::EInt: ('-')? ('0'..'9')+; terminal FLOAT returns ecore::EFloat: ('-')? ('0'..'9')* ('.' ('0'..'9')+)?; +terminal DOUBLE returns ecore::EDouble: ('-')? ('0'..'9')* ('.' ('0'..'9')+)?; \ No newline at end of file
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/CommonKeyword.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/grammar/CommonKeyword.java similarity index 96% rename from com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/CommonKeyword.java rename to com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/grammar/CommonKeyword.java index 0807913..d00648c 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/CommonKeyword.java +++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/grammar/CommonKeyword.java
@@ -6,7 +6,7 @@ * * http://www.eclipse.org/legal/epl-v10.html */ -package com.google.eclipse.protobuf.ui.grammar; +package com.google.eclipse.protobuf.grammar; /** * A commonly used keyword.
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Properties.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/util/Properties.java similarity index 93% rename from com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Properties.java rename to com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/util/Properties.java index f9668cd..3ddcd47 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Properties.java +++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/util/Properties.java
@@ -6,12 +6,12 @@ * * http://www.eclipse.org/legal/epl-v10.html */ -package com.google.eclipse.protobuf.ui.util; +package com.google.eclipse.protobuf.util; -import static com.google.eclipse.protobuf.ui.grammar.CommonKeyword.*; +import static com.google.eclipse.protobuf.grammar.CommonKeyword.*; +import com.google.eclipse.protobuf.grammar.CommonKeyword; import com.google.eclipse.protobuf.protobuf.*; -import com.google.eclipse.protobuf.ui.grammar.CommonKeyword; import com.google.inject.Singleton; /**
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/Messages.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/Messages.java index cbd6fa0..7bbe1d8 100644 --- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/Messages.java +++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/Messages.java
@@ -17,7 +17,9 @@ public static String expectedFieldName; public static String expectedFieldNumber; + public static String expectedString; public static String expectedSyntaxIdentifier; + public static String expectedTrueOrFalse; public static String fieldNumberAlreadyUsed; public static String fieldNumbersMustBePositive; public static String importNotFound;
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 dbb5a26..cc7c6ed 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
@@ -1,6 +1,8 @@ expectedFieldName = Expected field name. expectedFieldNumber = Expected field number. +expectedString = Expected string. expectedSyntaxIdentifier = Expected syntax identifier. +expectedTrueOrFalse = Expected "true" or "false". fieldNumberAlreadyUsed = Field number %d has already been used in \"%s\" by field \"%s\". fieldNumbersMustBePositive = Field numbers must be positive integers. importNotFound = Import \"%s\" was not found.
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator.java index 22a6323..a818e82 100644 --- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator.java +++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator.java
@@ -8,6 +8,7 @@ */ package com.google.eclipse.protobuf.validation; +import static com.google.eclipse.protobuf.protobuf.Modifier.OPTIONAL; import static com.google.eclipse.protobuf.protobuf.ProtobufPackage.Literals.*; import static com.google.eclipse.protobuf.validation.Messages.*; import static java.lang.String.format; @@ -15,11 +16,13 @@ import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.EObject; -import org.eclipse.xtext.naming.*; +import org.eclipse.xtext.naming.IQualifiedNameProvider; +import org.eclipse.xtext.naming.QualifiedName; import org.eclipse.xtext.validation.Check; import com.google.eclipse.protobuf.protobuf.*; import com.google.eclipse.protobuf.protobuf.Package; +import com.google.eclipse.protobuf.util.Properties; import com.google.inject.Inject; /** @@ -28,6 +31,20 @@ public class ProtobufJavaValidator extends AbstractProtobufJavaValidator { @Inject private IQualifiedNameProvider qualifiedNameProvider; + @Inject private Properties properties; + + @Check public void checkDefaultValueType(Property property) { + ValueRef defaultValue = property.getDefault(); + if (!OPTIONAL.equals(property.getModifier()) || defaultValue == null) return; + if (properties.isString(property)) { + if (defaultValue instanceof StringRef) return; + error(expectedString, PROPERTY__DEFAULT); + } + if (properties.isBool(property)) { + if (defaultValue instanceof BooleanRef) return; + error(expectedTrueOrFalse, PROPERTY__DEFAULT); + } + } @Check public void checkImportIsResolved(Import anImport) { String importUri = anImport.getImportURI();