use of org.eclipse.emf.ecore.EEnumLiteral in project xtext-core by eclipse.
the class GrammarParserTest method testEnum_05.
@Test
public void testEnum_05() throws Exception {
String modelAsString = "grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" + "import 'classpath:/org/eclipse/xtext/enumrules/enums.ecore'\n" + "generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/5'\n" + "Model: enumValue=ExistingEnum;\n" + "enum ExistingEnum: SameName = 'value';";
Grammar grammar = (Grammar) getModel(modelAsString);
assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty());
checkEnums(grammar);
EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
assertEquals("http://www.eclipse.org/2009/tmf/xtext/EnumRulesTestLanguage/imported", pack.getNsURI());
EEnum eEnum = (EEnum) pack.getEClassifier("ExistingEnum");
assertNotNull(eEnum);
assertEquals(3, eEnum.getELiterals().size());
EEnumLiteral value = eEnum.getELiterals().get(0);
assertEquals(ExistingEnum.SAME_NAME.getName(), value.getName());
assertEquals(ExistingEnum.SAME_NAME.getValue(), value.getValue());
assertEquals(ExistingEnum.SAME_NAME.getLiteral(), value.getLiteral());
EnumRule rule = (EnumRule) grammar.getRules().get(1);
assertEquals(eEnum, rule.getType().getClassifier());
EnumLiteralDeclaration decl = (EnumLiteralDeclaration) rule.getAlternatives();
assertEquals(value, decl.getEnumLiteral());
assertNotNull(decl.getLiteral());
assertEquals("value", decl.getLiteral().getValue());
}
use of org.eclipse.emf.ecore.EEnumLiteral in project xtext-core by eclipse.
the class GrammarParserTest method testEnum_03.
@Test
public void testEnum_03() throws Exception {
String modelAsString = "grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" + "generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/3'\n" + "Model: enumValue=MyEnumRule;\n" + "enum MyEnumRule returns MyEnum: Value1 = 'value';";
Grammar grammar = (Grammar) getModel(modelAsString);
assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty());
checkEnums(grammar);
EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
assertEquals("http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/3", pack.getNsURI());
EEnum eEnum = (EEnum) pack.getEClassifier("MyEnum");
assertNotNull(eEnum);
assertEquals(1, eEnum.getELiterals().size());
EEnumLiteral value = eEnum.getELiterals().get(0);
assertEquals("Value1", value.getName());
assertEquals(0, value.getValue());
assertEquals("value", value.getLiteral());
}
use of org.eclipse.emf.ecore.EEnumLiteral in project xtext-core by eclipse.
the class GrammarParserTest method testEnum_07.
@Test
public void testEnum_07() throws Exception {
String modelAsString = "grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" + "generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/7'\n" + "Model: enumValue=MyEnum;\n" + "enum MyEnum: Value | Value;";
Grammar grammar = (Grammar) getModel(modelAsString);
assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty());
checkEnums(grammar);
EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
assertEquals("http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/7", pack.getNsURI());
EEnum eEnum = (EEnum) pack.getEClassifier("MyEnum");
assertNotNull(eEnum);
assertEquals(1, eEnum.getELiterals().size());
EEnumLiteral value = eEnum.getELiterals().get(0);
assertEquals("Value", value.getName());
assertEquals(0, value.getValue());
assertEquals("Value", value.getLiteral());
}
use of org.eclipse.emf.ecore.EEnumLiteral in project xtext-core by eclipse.
the class Xtext2EcoreTransformer method deriveEnums.
private void deriveEnums(EnumRule rule) {
EEnum returnType = (EEnum) rule.getType().getClassifier();
if (returnType != null) {
List<EnumLiteralDeclaration> decls = EcoreUtil2.getAllContentsOfType(rule, EnumLiteralDeclaration.class);
for (EnumLiteralDeclaration decl : decls) {
if (decl.getEnumLiteral() == null) {
List<INode> nodes = NodeModelUtils.findNodesForFeature(decl, XtextPackage.Literals.ENUM_LITERAL_DECLARATION__ENUM_LITERAL);
if (!nodes.isEmpty()) {
if (nodes.size() > 1)
throw new IllegalStateException("Unexpected nodes found: " + nodes);
INode node = nodes.get(0);
String text = node.getText();
EEnumLiteral literal = null;
if (rule.getType().getMetamodel() instanceof ReferencedMetamodel) {
literal = returnType.getEEnumLiteral(text);
} else {
EEnumLiteral existing = returnType.getEEnumLiteral(text);
if (existing == null) {
literal = EcoreFactory.eINSTANCE.createEEnumLiteral();
int index = returnType.getELiterals().size();
returnType.getELiterals().add(literal);
literal.setName(text);
literal.setValue(index);
if (decl.getLiteral() != null) {
literal.setLiteral(decl.getLiteral().getValue());
} else {
literal.setLiteral(text);
}
} else {
literal = existing;
}
SourceAdapter.adapt(literal, decl);
}
if (literal == null) {
reportError(new TransformationException(TransformationErrorCode.InvalidFeature, "Enum literal '" + text + "' does not exist.", decl));
} else {
decl.setEnumLiteral(literal);
}
}
}
if (decl.getLiteral() == null && decl.getEnumLiteral() != null) {
Keyword kw = XtextFactory.eINSTANCE.createKeyword();
kw.setValue(decl.getEnumLiteral().getLiteral());
decl.setLiteral(kw);
}
}
}
}
use of org.eclipse.emf.ecore.EEnumLiteral in project xtext-core by eclipse.
the class Ecore2XtextGrammarCreator method rule.
public CharSequence rule(final EClassifier it) {
CharSequence _switchResult = null;
boolean _matched = false;
if (it instanceof EClass) {
_matched = true;
StringConcatenation _builder = new StringConcatenation();
String _concreteRuleName = Ecore2XtextExtensions.concreteRuleName(((EClass) it));
_builder.append(_concreteRuleName);
_builder.append(" returns ");
String _fqn = Ecore2XtextExtensions.fqn(it);
_builder.append(_fqn);
_builder.append(":");
_builder.newLineIfNotEmpty();
{
boolean _onlyOptionalFeatures = Ecore2XtextExtensions.onlyOptionalFeatures(((EClass) it));
if (_onlyOptionalFeatures) {
_builder.append("\t");
_builder.append("{");
String _fqn_1 = Ecore2XtextExtensions.fqn(it);
_builder.append(_fqn_1, "\t");
_builder.append("}");
_builder.newLineIfNotEmpty();
}
}
{
Iterable<EStructuralFeature> _prefixFeatures = Ecore2XtextExtensions.prefixFeatures(((EClass) it));
for (final EStructuralFeature strF : _prefixFeatures) {
_builder.append("\t");
CharSequence _assigment = this.assigment(strF);
_builder.append(_assigment, "\t");
_builder.newLineIfNotEmpty();
}
}
_builder.append("\t");
_builder.append("\'");
String _name = ((EClass) it).getName();
_builder.append(_name, "\t");
_builder.append("\'");
_builder.newLineIfNotEmpty();
_builder.append("\t");
CharSequence _idAssignment = this.idAssignment(((EClass) it));
_builder.append(_idAssignment, "\t");
{
boolean _isEmpty = IterableExtensions.isEmpty(Ecore2XtextExtensions.inlinedFeatures(((EClass) it)));
boolean _not = (!_isEmpty);
if (_not) {
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\'{\'");
_builder.newLine();
{
Iterable<EAttribute> _allAttributes = Ecore2XtextExtensions.allAttributes(((EClass) it));
for (final EAttribute attr : _allAttributes) {
_builder.append("\t");
_builder.append("\t");
CharSequence _assigment_1 = this.assigment(attr);
_builder.append(_assigment_1, "\t\t");
_builder.newLineIfNotEmpty();
}
}
{
Iterable<EReference> _allCrossReferences = Ecore2XtextExtensions.allCrossReferences(((EClass) it));
for (final EReference ref : _allCrossReferences) {
_builder.append("\t");
_builder.append("\t");
CharSequence _assigment_2 = this.assigment(ref);
_builder.append(_assigment_2, "\t\t");
_builder.newLineIfNotEmpty();
}
}
{
Iterable<EReference> _allContainmentReferences = Ecore2XtextExtensions.allContainmentReferences(((EClass) it));
for (final EReference conti : _allContainmentReferences) {
_builder.append("\t");
_builder.append("\t");
CharSequence _assigment_3 = this.assigment(conti);
_builder.append(_assigment_3, "\t\t");
_builder.newLineIfNotEmpty();
}
}
_builder.append("\t");
_builder.append("\'}\'");
}
}
_builder.append(";");
_builder.newLineIfNotEmpty();
_switchResult = _builder;
}
if (!_matched) {
if (it instanceof EEnum) {
_matched = true;
StringConcatenation _builder = new StringConcatenation();
_builder.append("enum ");
String _quoteIfNeccesary = Ecore2XtextExtensions.quoteIfNeccesary(((EEnum) it).getName());
_builder.append(_quoteIfNeccesary);
_builder.append(" returns ");
String _fqn = Ecore2XtextExtensions.fqn(it);
_builder.append(_fqn);
_builder.append(":");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t\t");
final Function1<EEnumLiteral, String> _function = (EEnumLiteral it_1) -> {
String _name = it_1.getName();
String _plus = (_name + " = \'");
String _name_1 = it_1.getName();
String _plus_1 = (_plus + _name_1);
return (_plus_1 + "\'");
};
String _join = IterableExtensions.join(ListExtensions.<EEnumLiteral, String>map(((EEnum) it).getELiterals(), _function), " | ");
_builder.append(_join, "\t\t\t\t");
_builder.append(";");
_switchResult = _builder;
}
}
if (!_matched) {
if (it instanceof EDataType) {
_matched = true;
CharSequence _xifexpression = null;
boolean _isSerializable = ((EDataType) it).isSerializable();
if (_isSerializable) {
StringConcatenation _builder = new StringConcatenation();
String _uniqueName = UniqueNameUtil.uniqueName(it);
_builder.append(_uniqueName);
_builder.append(" returns ");
String _fqn = Ecore2XtextExtensions.fqn(it);
_builder.append(_fqn);
_builder.append(":");
_builder.newLineIfNotEmpty();
_builder.append("\t");
String _dataTypeRuleBody = Ecore2XtextExtensions.dataTypeRuleBody(((EDataType) it));
_builder.append(_dataTypeRuleBody, "\t");
_builder.append(";");
_builder.newLineIfNotEmpty();
_xifexpression = _builder;
}
_switchResult = _xifexpression;
}
}
if (!_matched) {
throw new IllegalStateException(("No rule template for " + it));
}
return _switchResult;
}
Aggregations