Search in sources :

Example 86 with AbstractRule

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());
}
Also used : StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) TypeReference(org.eclipse.xtext.xtext.generator.model.TypeReference) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 87 with AbstractRule

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);
}
Also used : UsedRulesFinder(org.eclipse.xtext.xtext.UsedRulesFinder) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 88 with AbstractRule

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());
        }
    }
}
Also used : EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) EClassifier(org.eclipse.emf.ecore.EClassifier) XtextResource(org.eclipse.xtext.resource.XtextResource) Grammar(org.eclipse.xtext.Grammar) AbstractRule(org.eclipse.xtext.AbstractRule) EPackage(org.eclipse.emf.ecore.EPackage) Test(org.junit.Test)

Example 89 with AbstractRule

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)));
}
Also used : StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AbstractRule(org.eclipse.xtext.AbstractRule) Test(org.junit.Test)

Example 90 with AbstractRule

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));
}
Also used : StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AbstractRule(org.eclipse.xtext.AbstractRule) Test(org.junit.Test)

Aggregations

AbstractRule (org.eclipse.xtext.AbstractRule)222 Test (org.junit.Test)128 Grammar (org.eclipse.xtext.Grammar)106 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)96 ParserRule (org.eclipse.xtext.ParserRule)94 EObject (org.eclipse.emf.ecore.EObject)59 AbstractXtextRuleInspectorTest (org.eclipse.xtext.xtext.AbstractXtextRuleInspectorTest)49 RuleCall (org.eclipse.xtext.RuleCall)36 AbstractElement (org.eclipse.xtext.AbstractElement)34 TerminalRule (org.eclipse.xtext.TerminalRule)20 XtextResource (org.eclipse.xtext.resource.XtextResource)17 EClassifier (org.eclipse.emf.ecore.EClassifier)14 EnumRule (org.eclipse.xtext.EnumRule)14 ArrayList (java.util.ArrayList)12 Assignment (org.eclipse.xtext.Assignment)12 Group (org.eclipse.xtext.Group)12 InternalEObject (org.eclipse.emf.ecore.InternalEObject)11 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)11 EClass (org.eclipse.emf.ecore.EClass)10 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)10