In progress: [ Issue 32 ] Mapping Xtext syntax errors to protoc ones.
https://code.google.com/p/protobuf-dt/issues/detail?id=32
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/ProtobufRuntimeModule.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/ProtobufRuntimeModule.java
index a8b9ace..833d9db 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/ProtobufRuntimeModule.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/ProtobufRuntimeModule.java
@@ -9,10 +9,12 @@
package com.google.eclipse.protobuf;
import org.eclipse.xtext.naming.IQualifiedNameProvider;
+import org.eclipse.xtext.parser.antlr.ISyntaxErrorMessageProvider;
import org.eclipse.xtext.scoping.impl.ImportUriResolver;
import com.google.eclipse.protobuf.naming.ProtobufQualifiedNameProvider;
import com.google.eclipse.protobuf.scoping.ImportUriFixerAndResolver;
+import com.google.eclipse.protobuf.validation.ProtobufSyntaxErrorMessageProvider;
import com.google.inject.Binder;
/**
@@ -28,4 +30,8 @@
public void configureImportUriResolver(Binder binder) {
binder.bind(ImportUriResolver.class).to(ImportUriFixerAndResolver.class);
}
+
+ public void configureSyntaxErrorMessageProvider(Binder binder) {
+ binder.bind(ISyntaxErrorMessageProvider.class).to(ProtobufSyntaxErrorMessageProvider.class);
+ }
}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufScopeProvider.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufScopeProvider.java
index 8a0cc00..2bdff31 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufScopeProvider.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufScopeProvider.java
@@ -81,12 +81,12 @@
for (Import anImport : root.getImports()) {
URI importUri = createURI(uriResolver.apply(anImport));
Resource imported = resourceSet.getResource(importUri, true);
- descriptions.addAll(describeTypesUsingQualifiedNames(imported));
+ descriptions.addAll(innerTypes(imported));
}
return descriptions;
}
- private Collection<IEObjectDescription> describeTypesUsingQualifiedNames(Resource resource) {
+ private Collection<IEObjectDescription> innerTypes(Resource resource) {
List<IEObjectDescription> descriptions = new ArrayList<IEObjectDescription>();
TreeIterator<Object> contents = getAllContents(resource, true);
while (contents.hasNext()) {
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
new file mode 100644
index 0000000..52c5409
--- /dev/null
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/Messages.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2011 Google Inc.
+ *
+ * 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
+ */
+package com.google.eclipse.protobuf.validation;
+
+import org.eclipse.osgi.util.NLS;
+
+/**
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class Messages extends NLS {
+
+ static {
+ Class<Messages> targetType = Messages.class;
+ NLS.initializeMessages(targetType.getName(), targetType);
+ }
+
+ private Messages() {}
+
+ public static String Error_expectedFieldName;
+ public static String Error_expectedFieldNumber;
+ public static String Error_missingFieldNumber;
+ public static String Error_fieldNumbersMustBePositive;
+}
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
new file mode 100644
index 0000000..6ececf8
--- /dev/null
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/Messages.properties
@@ -0,0 +1,4 @@
+Error_expectedFieldName = Expected field name.
+Error_expectedFieldNumber = Expected field number.
+Error_missingFieldNumber = Missing field number.
+Error_fieldNumbersMustBePositive = Field numbers must be positive integers.
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 158aa1b..94a11a8 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,8 @@
*/
package com.google.eclipse.protobuf.validation;
+import static com.google.eclipse.protobuf.validation.Messages.*;
+
import org.eclipse.xtext.validation.Check;
import com.google.eclipse.protobuf.protobuf.Property;
@@ -22,9 +24,10 @@
* @param property the given property.
*/
@Check public void checkTagNumberIsGreaterThanZero(Property property) {
+ if (property.getName() == null) return; // we already show an error if name is null, no need to go further.
int index = property.getIndex();
if (index > 0) return;
- String msg = (index == 0) ? "Field numbers must be positive integers." : "Expected field number.";
+ String msg = (index == 0) ? Error_fieldNumbersMustBePositive : Error_expectedFieldNumber;
error(msg, property.eClass().getEStructuralFeature("index"));
}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufSyntaxErrorMessageProvider.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufSyntaxErrorMessageProvider.java
new file mode 100644
index 0000000..b35c79a
--- /dev/null
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufSyntaxErrorMessageProvider.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2011 Google Inc.
+ *
+ * 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
+ */
+package com.google.eclipse.protobuf.validation;
+
+import static com.google.eclipse.protobuf.validation.Messages.*;
+import static org.eclipse.xtext.diagnostics.Diagnostic.SYNTAX_DIAGNOSITC;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.xtext.nodemodel.SyntaxErrorMessage;
+import org.eclipse.xtext.parser.antlr.SyntaxErrorMessageProvider;
+
+import com.google.eclipse.protobuf.protobuf.Property;
+
+/**
+ * Messages for syntax errors.
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class ProtobufSyntaxErrorMessageProvider extends SyntaxErrorMessageProvider {
+
+ @Override public SyntaxErrorMessage getSyntaxErrorMessage(IParserErrorContext context) {
+ String message = context.getDefaultMessage();
+ EObject currentContext = context.getCurrentContext();
+ if (currentContext instanceof Property) message = mapToProtocMessage((Property) currentContext, message);
+ return new SyntaxErrorMessage(message, SYNTAX_DIAGNOSITC);
+ }
+
+ private String mapToProtocMessage(Property property, String message) {
+ if (message.contains("RULE_ID") && property.getName() == null) return Error_expectedFieldName;
+ if (message.equals("mismatched input ';' expecting '='") && property.getIndex() == 0) return Error_missingFieldNumber;
+ return message;
+ }
+
+ @Override public SyntaxErrorMessage getSyntaxErrorMessage(IValueConverterErrorContext context) {
+ return new SyntaxErrorMessage(context.getDefaultMessage(), SYNTAX_DIAGNOSITC);
+ }
+
+}