Still working on having keywords as identifiers.
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/conversion/NameValueConverter_toValue_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/conversion/NameValueConverter_toValue_Test.java
new file mode 100644
index 0000000..755fb40
--- /dev/null
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/conversion/NameValueConverter_toValue_Test.java
@@ -0,0 +1,55 @@
+/*
+ * 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.conversion;
+
+import static com.google.eclipse.protobuf.junit.core.Setups.unitTestSetup;
+import static com.google.eclipse.protobuf.junit.core.XtextRule.createWith;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+
+import com.google.eclipse.protobuf.junit.core.XtextRule;
+import com.google.eclipse.protobuf.protobuf.Name;
+
+import org.eclipse.xtext.nodemodel.INode;
+import org.junit.*;
+
+/**
+ * Tests for <code>{@link NameValueConverter#toValue(String, INode)}</code>
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class NameValueConverter_toValue_Test {
+
+ @Rule public XtextRule xtext = createWith(unitTestSetup());
+
+ private INode node;
+ private NameValueConverter converter;
+
+ @Before public void setUp() {
+ node = mock(INode.class);
+ converter = xtext.getInstanceOf(NameValueConverter.class);
+ }
+
+ @Test public void should_return_name_using_given_value_if_given_value_is_not_null() {
+ Name name = converter.toValue("hello", node);
+ assertThat(name.getValue(), equalTo("hello"));
+ }
+
+ @Test public void should_return_name_using_text_from_node_if_value_is_null_and_text_in_node_is_keyword() {
+ when(node.getText()).thenReturn("message");
+ Name name = converter.toValue(null, node);
+ assertThat(name.getValue(), equalTo("message"));
+ }
+
+ @Test public void should_return_null_if_given_value_is_null_and_text_in_node_is_not_keyword() {
+ when(node.getText()).thenReturn("hello");
+ assertNull(converter.toValue(null, node));
+ }
+}
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 7a0f0fe..ca69dd0 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
@@ -38,9 +38,6 @@
PublicImport:
'import' 'public' importURI=STRING ';';
-QualifiedName:
- '.'? ID ('.' ID)*;
-
ComplexType:
Message | Enum | Group;
@@ -75,9 +72,7 @@
('[' (fieldOptions+=FieldOption (',' fieldOptions+=FieldOption)*)? ']')? (';')+;
enum Modifier:
- required
- | optional
- | repeated;
+ required | optional | repeated;
TypeLink:
ScalarTypeLink | ComplexTypeLink;
@@ -204,6 +199,12 @@
ExtensionFieldName:
'[' target=[MessageField|QualifiedName] ']';
+QualifiedName:
+ '.'? ReducedId ('.' ReducedId)*;
+
+ReducedId:
+ ID | 'rpc';
+
SimpleValueLink:
LiteralLink | BooleanLink | NumberLink | StringLink;
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/Keywords.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/Keywords.java
new file mode 100644
index 0000000..55a74c5
--- /dev/null
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/Keywords.java
@@ -0,0 +1,42 @@
+/*
+ * 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.conversion;
+
+import static org.eclipse.xtext.GrammarUtil.getAllKeywords;
+
+import com.google.inject.*;
+
+import org.eclipse.xtext.IGrammarAccess;
+
+import java.util.Set;
+
+/**
+ * Utility methods related to grammar keywords.
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+@Singleton
+public class Keywords {
+
+ private final Set<String> keywords;
+
+ @Inject
+ public Keywords(IGrammarAccess grammarAccess) {
+ keywords = getAllKeywords(grammarAccess.getGrammar());
+ }
+
+ /**
+ * Indicates whether the given {@code String} is a keyword or not.
+ * @param s the given {@code String}.
+ * @return {@code true} if the given {@code String} is a keyword, {@code false} otherwise.
+ */
+ public boolean isKeyword(String s) {
+ return keywords.contains(s);
+ }
+}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/LONGValueConverter.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/LONGValueConverter.java
index 7205996..31b027a 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/LONGValueConverter.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/LONGValueConverter.java
@@ -15,7 +15,7 @@
import org.eclipse.xtext.nodemodel.INode;
/**
- * Converts integer numbers to {@code long}s.
+ * Converts numbers to {@code long}s.
*
* @author alruiz@google.com (Alex Ruiz)
*/
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/NameValueConverter.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/NameValueConverter.java
index e738673..1e78eb9 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/NameValueConverter.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/NameValueConverter.java
@@ -1,31 +1,33 @@
-// Copyright 2011 Google Inc. All Rights Reserved.
-
+/*
+ * 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.conversion;
-import static org.eclipse.xtext.GrammarUtil.getAllKeywords;
-
import com.google.eclipse.protobuf.protobuf.*;
import com.google.inject.Inject;
-import org.eclipse.xtext.*;
-import org.eclipse.xtext.conversion.impl.*;
+import org.eclipse.xtext.conversion.impl.AbstractLexerBasedConverter;
import org.eclipse.xtext.nodemodel.INode;
-import java.util.*;
-
/**
- * @author alruiz@google.com (Alex Ruiz)
+ * Converts names to <code>{@link Name}</code>s.
*
+ * @author alruiz@google.com (Alex Ruiz)
*/
public class NameValueConverter extends AbstractLexerBasedConverter<Name> {
- @Inject
- private IGrammarAccess grammarAccess;
-
+ private ProtobufFactory factory = ProtobufFactory.eINSTANCE;
+ @Inject private Keywords keywords;
+
@Override public Name toValue(String string, INode node) {
String value = value(string, node);
if (value == null) return null;
- Name name = ProtobufFactory.eINSTANCE.createName();
+ Name name = factory.createName();
name.setValue(value);
return name;
}
@@ -35,7 +37,6 @@
String text = node.getText();
if (text == null) return text;
text = text.trim();
- Set<String> allKeywords = getAllKeywords(grammarAccess.getGrammar());
- return allKeywords.contains(text) ? text : null;
+ return keywords.isKeyword(text) ? text : null;
}
}