use of org.eclipse.xtext.AbstractRule in project xtext-core by eclipse.
the class XbaseGeneratorFragment2 method doGenerateXtendInferrer.
protected void doGenerateXtendInferrer() {
final TypeReference firstRuleType = TypeReference.typeRef(GenModelUtil2.getJavaTypeName(IterableExtensions.<AbstractRule>head(this.getLanguage().getGrammar().getRules()).getType().getClassifier(), this.getLanguage().getGrammar().eResource().getResourceSet()));
TypeReference _jvmModelInferrer = this.getJvmModelInferrer();
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
_builder.append("/**");
_builder.newLine();
_builder.append(" ");
_builder.append("* <p>Infers a JVM model from the source model.</p> ");
_builder.newLine();
_builder.append(" ");
_builder.append("*");
_builder.newLine();
_builder.append(" ");
_builder.append("* <p>The JVM model should contain all elements that would appear in the Java code ");
_builder.newLine();
_builder.append(" ");
_builder.append("* which is generated from the source model. Other models link against the JVM model rather than the source model.</p> ");
_builder.newLine();
_builder.append(" ");
_builder.append("*/");
_builder.newLine();
_builder.append("class ");
String _simpleName = XbaseGeneratorFragment2.this.getJvmModelInferrer().getSimpleName();
_builder.append(_simpleName);
_builder.append(" extends ");
TypeReference _typeRef = TypeReference.typeRef("org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer");
_builder.append(_typeRef);
_builder.append(" {");
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("\t");
_builder.append("/**");
_builder.newLine();
_builder.append("\t ");
_builder.append("* convenience API to build and initialize JVM types and their members.");
_builder.newLine();
_builder.append("\t ");
_builder.append("*/");
_builder.newLine();
_builder.append("\t");
_builder.append("@");
_builder.append(Inject.class, "\t");
_builder.append(" extension ");
TypeReference _typeRef_1 = TypeReference.typeRef("org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder");
_builder.append(_typeRef_1, "\t");
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("\t");
_builder.append("/**");
_builder.newLine();
_builder.append("\t ");
_builder.append("* The dispatch method {@code infer} is called for each instance of the");
_builder.newLine();
_builder.append("\t ");
_builder.append("* given element\'s type that is contained in a resource.");
_builder.newLine();
_builder.append("\t ");
_builder.append("* ");
_builder.newLine();
_builder.append("\t ");
_builder.append("* @param element");
_builder.newLine();
_builder.append("\t ");
_builder.append("* the model to create one or more");
_builder.newLine();
_builder.append("\t ");
_builder.append("* {@link org.eclipse.xtext.common.types.JvmDeclaredType declared");
_builder.newLine();
_builder.append("\t ");
_builder.append("* types} from.");
_builder.newLine();
_builder.append("\t ");
_builder.append("* @param acceptor");
_builder.newLine();
_builder.append("\t ");
_builder.append("* each created");
_builder.newLine();
_builder.append("\t ");
_builder.append("* {@link org.eclipse.xtext.common.types.JvmDeclaredType type}");
_builder.newLine();
_builder.append("\t ");
_builder.append("* without a container should be passed to the acceptor in order");
_builder.newLine();
_builder.append("\t ");
_builder.append("* get attached to the current resource. The acceptor\'s");
_builder.newLine();
_builder.append("\t ");
_builder.append("* {@link IJvmDeclaredTypeAcceptor#accept(org.eclipse.xtext.common.types.JvmDeclaredType)");
_builder.newLine();
_builder.append("\t ");
_builder.append("* accept(..)} method takes the constructed empty type for the");
_builder.newLine();
_builder.append("\t ");
_builder.append("* pre-indexing phase. This one is further initialized in the");
_builder.newLine();
_builder.append("\t ");
_builder.append("* indexing phase using the lambda you pass as the last argument.");
_builder.newLine();
_builder.append("\t ");
_builder.append("* @param isPreIndexingPhase");
_builder.newLine();
_builder.append("\t ");
_builder.append("* whether the method is called in a pre-indexing phase, i.e.");
_builder.newLine();
_builder.append("\t ");
_builder.append("* when the global index is not yet fully updated. You must not");
_builder.newLine();
_builder.append("\t ");
_builder.append("* rely on linking using the index if isPreIndexingPhase is");
_builder.newLine();
_builder.append("\t ");
_builder.append("* <code>true</code>.");
_builder.newLine();
_builder.append("\t ");
_builder.append("*/");
_builder.newLine();
_builder.append("\t");
_builder.append("def dispatch void infer(");
_builder.append(firstRuleType, "\t");
_builder.append(" element, ");
TypeReference _typeRef_2 = TypeReference.typeRef("org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor");
_builder.append(_typeRef_2, "\t");
_builder.append(" acceptor, boolean isPreIndexingPhase) {");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("// Here you explain how your model is mapped to Java elements, by writing the actual translation code.");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("// An implementation for the initial hello world example could look like this:");
_builder.newLine();
_builder.append("// \t\tacceptor.accept(element.toClass(\"my.company.greeting.MyGreetings\")) [");
_builder.newLine();
_builder.append("// \t\t\tfor (greeting : element.greetings) {");
_builder.newLine();
_builder.append("// \t\t\t\tmembers += greeting.toMethod(\"hello\" + greeting.name, typeRef(String)) [");
_builder.newLine();
_builder.append("// \t\t\t\t\tbody = ");
_builder.append("\'\'\'");
_builder.newLineIfNotEmpty();
_builder.append("//\t\t\t\t\t\treturn \"Hello ");
_builder.append("�");
_builder.append("greeting.name");
_builder.append("�");
_builder.append("\";");
_builder.newLineIfNotEmpty();
_builder.append("//\t\t\t\t\t");
_builder.append("\'\'\'");
_builder.newLineIfNotEmpty();
_builder.append("//\t\t\t\t]");
_builder.newLine();
_builder.append("//\t\t\t}");
_builder.newLine();
_builder.append("//\t\t]");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
}
};
this.fileAccessFactory.createXtendFile(_jvmModelInferrer, _client).writeTo(this.getProjectConfig().getRuntime().getSrc());
}
use of org.eclipse.xtext.AbstractRule in project xtext-core by eclipse.
the class XbaseUsageDetector method usesXImportSection.
public boolean usesXImportSection(final Grammar grammar) {
final Set<AbstractRule> usedRules = CollectionLiterals.<AbstractRule>newHashSet();
new UsedRulesFinder(usedRules).compute(grammar);
final Function1<AbstractRule, Boolean> _function = (AbstractRule it) -> {
return Boolean.valueOf((Objects.equal(it.getName(), "XImportSection") && Objects.equal(GrammarUtil.getGrammar(it).getName(), "org.eclipse.xtext.xbase.Xtype")));
};
return IterableExtensions.<AbstractRule>exists(usedRules, _function);
}
use of org.eclipse.xtext.AbstractRule in project xtext-core by eclipse.
the class Xtext2EcoreTransformerTest method testBug_266440.
@Test
public void testBug_266440() throws Exception {
StringConcatenation _builder = new StringConcatenation();
_builder.append("grammar test with org.eclipse.xtext.common.Terminals import \'http://www.eclipse.org/emf/2002/Ecore\' as ecore generate bugreport \'http://bugreport/266440\'");
_builder.newLine();
_builder.append("CompositeModel: (type+=EClassifier)+;");
_builder.newLine();
_builder.append("EClassifier: EDataType | EClass;");
_builder.newLine();
_builder.append("EClass: \'class\' name=ID;");
_builder.newLine();
_builder.append("EDataType: \'dt\' name=ID;");
String grammar = _builder.toString();
final XtextResource resource = this.getResourceFromString(grammar);
Assert.assertTrue(resource.getErrors().isEmpty());
EObject _head = IterableExtensions.<EObject>head(resource.getContents());
final Grammar parsedGrammar = ((Grammar) _head);
EList<AbstractRule> _rules = parsedGrammar.getRules();
for (final AbstractRule rule : _rules) {
{
final EClassifier classifier = rule.getType().getClassifier();
EPackage pack = classifier.getEPackage();
Assert.assertEquals("bugreport", pack.getName());
}
}
}
use of org.eclipse.xtext.AbstractRule in project xtext-core by eclipse.
the class ResourceDescriptionStrategyFragmentTests method testGenerate_NoStubs.
@Test
public void testGenerate_NoStubs() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("grammar org.xtext.Foo with org.eclipse.xtext.common.Terminals");
_builder.newLine();
_builder.append("generate foo \"http://org.xtext/foo\"");
_builder.newLine();
_builder.append("Model: rules+=Rule;");
_builder.newLine();
_builder.append("@Exported");
_builder.newLine();
_builder.append("Rule: name=ID;");
_builder.newLine();
_builder.append("@Exported");
_builder.newLine();
_builder.append("Foo returns Rule: name=ID;");
_builder.newLine();
final ResourceDescriptionStrategyFragmentTests.TestableResourceDescriptionStrategyFragment fragment = this.<ResourceDescriptionStrategyFragmentTests.TestableResourceDescriptionStrategyFragment>initializeFragmentWithGrammarFromString(ResourceDescriptionStrategyFragmentTests.TestableResourceDescriptionStrategyFragment.class, _builder.toString());
fragment.setGenerateStub(false);
fragment.setGenerateXtendStub(false);
final List<AbstractRule> exportedRules = fragment.getExportedRulesFromGrammar();
Assert.assertFalse(exportedRules.isEmpty());
Assert.assertEquals(1, exportedRules.size());
Assert.assertTrue(fragment.shouldGenerateArtefacts(exportedRules));
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package org.xtext.resource;");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("import org.eclipse.emf.ecore.EObject;");
_builder_1.newLine();
_builder_1.append("import org.eclipse.xtext.resource.IEObjectDescription;");
_builder_1.newLine();
_builder_1.append("import org.eclipse.xtext.resource.impl.DefaultResourceDescriptionStrategy;");
_builder_1.newLine();
_builder_1.append("import org.eclipse.xtext.util.IAcceptor;");
_builder_1.newLine();
_builder_1.append("import org.xtext.foo.Rule;");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("public class FooDefaultResourceDescriptionStrategy extends DefaultResourceDescriptionStrategy {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("public boolean createEObjectDescriptions(EObject eObject, IAcceptor<IEObjectDescription> acceptor) {");
_builder_1.newLine();
_builder_1.append("\t\t");
_builder_1.append("if(eObject instanceof Rule) {");
_builder_1.newLine();
_builder_1.append("\t\t\t");
_builder_1.append("return createEObjectDescriptionsForRule(eObject, acceptor);");
_builder_1.newLine();
_builder_1.append("\t\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.append("\t\t");
_builder_1.append("return true;");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("protected boolean createEObjectDescriptionsForRule(EObject eObject, IAcceptor<IEObjectDescription> acceptor) {");
_builder_1.newLine();
_builder_1.append("\t\t");
_builder_1.append("return super.createEObjectDescriptions(eObject, acceptor);");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
Assert.assertEquals(_builder_1.toString(), this.concatenationClientToString(fragment.generateResourceDescriptionStrategy(exportedRules)));
}
use of org.eclipse.xtext.AbstractRule in project xtext-core by eclipse.
the class ResourceDescriptionStrategyFragmentTests method testGenerateNothing.
@Test
public void testGenerateNothing() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("grammar org.xtext.Foo with org.eclipse.xtext.common.Terminals");
_builder.newLine();
_builder.append("generate foo \"http://org.xtext/foo\"");
_builder.newLine();
_builder.append("Model: rules+=Rule;");
_builder.newLine();
_builder.append("Rule: name=ID;");
_builder.newLine();
final ResourceDescriptionStrategyFragmentTests.TestableResourceDescriptionStrategyFragment fragment = this.<ResourceDescriptionStrategyFragmentTests.TestableResourceDescriptionStrategyFragment>initializeFragmentWithGrammarFromString(ResourceDescriptionStrategyFragmentTests.TestableResourceDescriptionStrategyFragment.class, _builder.toString());
final List<AbstractRule> exportedRules = fragment.getExportedRulesFromGrammar();
Assert.assertTrue(exportedRules.isEmpty());
Assert.assertFalse(fragment.shouldGenerateArtefacts(exportedRules));
}
Aggregations