Search in sources :

Example 81 with AbstractRule

use of org.eclipse.xtext.AbstractRule in project xtext-core by eclipse.

the class GrammarUtilTest method testFindCurrentType_06.

@Test
public void testFindCurrentType_06() throws Exception {
    this.with(XtextStandaloneSetup.class);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("grammar myLang with org.eclipse.xtext.common.Terminals");
    _builder.newLine();
    _builder.append("generate g \'http://1\'");
    _builder.newLine();
    _builder.append("Rule:");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("Fragment;");
    _builder.newLine();
    _builder.append("fragment Fragment returns Rule:");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("name=ID Fragment?");
    _builder.newLine();
    _builder.append(";");
    _builder.newLine();
    String model = _builder.toString();
    final XtextResource r = this.getResourceFromString(model);
    EObject _get = r.getContents().get(0);
    final Grammar grammar = ((Grammar) _get);
    final AbstractRule rule = IterableExtensions.<AbstractRule>last(grammar.getRules());
    AbstractElement _alternatives = rule.getAlternatives();
    final AbstractElement fragmentCall = IterableExtensions.<AbstractElement>last(((Group) _alternatives).getElements());
    final EClassifier currentType = GrammarUtil.findCurrentType(fragmentCall);
    Assert.assertEquals("Rule", currentType.getName());
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) 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) Test(org.junit.Test)

Example 82 with AbstractRule

use of org.eclipse.xtext.AbstractRule in project xtext-core by eclipse.

the class GrammarUtilTest method testAllMetamodelDeclarations_02.

@Test
public void testAllMetamodelDeclarations_02() throws Exception {
    this.with(XtextStandaloneSetup.class);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("grammar foo with org.eclipse.xtext.common.Terminals");
    _builder.newLine();
    _builder.append("import \'http://www.eclipse.org/emf/2002/Ecore\' as bar");
    _builder.newLine();
    _builder.append("generate g \'http://3\' as bar");
    _builder.newLine();
    _builder.append("startrule returns bar::startrule: name=ID;");
    _builder.newLine();
    String model = _builder.toString();
    Resource r = this.getResourceFromString(model);
    EObject _get = r.getContents().get(0);
    Grammar g = ((Grammar) _get);
    List<AbstractMetamodelDeclaration> decls = GrammarUtil.allMetamodelDeclarations(g);
    Assert.assertEquals(3, decls.size());
    AbstractMetamodelDeclaration decl = decls.get(0);
    Assert.assertTrue((decl instanceof ReferencedMetamodel));
    Assert.assertNotNull(decl.getEPackage());
    Assert.assertEquals("http://www.eclipse.org/emf/2002/Ecore", decl.getEPackage().getNsURI());
    Assert.assertEquals("bar", decl.getAlias());
    decl = decls.get(1);
    Assert.assertEquals("bar", decl.getAlias());
    Assert.assertNotNull(decl.getEPackage());
    Assert.assertEquals("http://3", decl.getEPackage().getNsURI());
    decl = decls.get(2);
    Assert.assertTrue((decl instanceof ReferencedMetamodel));
    Assert.assertNotNull(decl.getEPackage());
    Assert.assertEquals("http://www.eclipse.org/emf/2002/Ecore", decl.getEPackage().getNsURI());
    Assert.assertEquals("ecore", decl.getAlias());
    AbstractRule abstractRule = g.getRules().get(0);
    Assert.assertSame(decls.get(1), abstractRule.getType().getMetamodel());
}
Also used : EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) Grammar(org.eclipse.xtext.Grammar) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) AbstractRule(org.eclipse.xtext.AbstractRule) ReferencedMetamodel(org.eclipse.xtext.ReferencedMetamodel) Test(org.junit.Test)

Example 83 with AbstractRule

use of org.eclipse.xtext.AbstractRule in project xtext-core by eclipse.

the class GrammarUtilTest method testFindCurrentType_03.

@Test
public void testFindCurrentType_03() throws Exception {
    this.with(XtextStandaloneSetup.class);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("grammar myLang with org.eclipse.xtext.common.Terminals");
    _builder.newLine();
    _builder.append("generate g \'http://1\'");
    _builder.newLine();
    _builder.append("Rule:");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("Fragment;");
    _builder.newLine();
    _builder.append("fragment Fragment: name=ID SecondFragment;");
    _builder.newLine();
    _builder.append("fragment SecondFragment: {SubRule.named=current} value=ID;");
    _builder.newLine();
    String model = _builder.toString();
    final XtextResource r = this.getResourceFromString(model);
    EObject _get = r.getContents().get(0);
    final Grammar grammar = ((Grammar) _get);
    final AbstractRule rule = IterableExtensions.<AbstractRule>head(grammar.getRules());
    final AbstractElement fragmentCall = rule.getAlternatives();
    final EClassifier currentType = GrammarUtil.findCurrentType(fragmentCall);
    Assert.assertEquals("SubRule", currentType.getName());
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) 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) Test(org.junit.Test)

Example 84 with AbstractRule

use of org.eclipse.xtext.AbstractRule in project xtext-core by eclipse.

the class ContentAssistFragment2 method generateGenJavaProposalProvider.

protected GeneratedJavaFileAccess generateGenJavaProposalProvider() {
    GeneratedJavaFileAccess _xblockexpression = null;
    {
        final Set<String> excludedFqnFeatureNames = this.getFQFeatureNamesToExclude(this.getGrammar());
        final HashSet<String> processedNames = CollectionLiterals.<String>newHashSet();
        final Function2<ArrayList<Assignment>, Assignment, ArrayList<Assignment>> _function = (ArrayList<Assignment> candidates, Assignment assignment) -> {
            ArrayList<Assignment> _xblockexpression_1 = null;
            {
                final String fqFeatureName = this.getFQFeatureName(assignment);
                if (((!processedNames.contains(fqFeatureName)) && (!excludedFqnFeatureNames.contains(fqFeatureName)))) {
                    processedNames.add(fqFeatureName);
                    candidates.add(assignment);
                }
                _xblockexpression_1 = candidates;
            }
            return _xblockexpression_1;
        };
        final ArrayList<Assignment> assignments = IterableExtensions.<Assignment, ArrayList<Assignment>>fold(GrammarUtil.containedAssignments(this.getGrammar()), CollectionLiterals.<Assignment>newArrayList(), _function);
        final Function2<ArrayList<AbstractRule>, AbstractRule, ArrayList<AbstractRule>> _function_1 = (ArrayList<AbstractRule> candidates, AbstractRule rule) -> {
            ArrayList<AbstractRule> _xblockexpression_1 = null;
            {
                final String fqnFeatureName = this.getFQFeatureName(rule);
                if (((!processedNames.contains(fqnFeatureName)) && (!excludedFqnFeatureNames.contains(fqnFeatureName)))) {
                    processedNames.add(fqnFeatureName);
                    candidates.add(rule);
                }
                _xblockexpression_1 = candidates;
            }
            return _xblockexpression_1;
        };
        final ArrayList<AbstractRule> remainingRules = IterableExtensions.<AbstractRule, ArrayList<AbstractRule>>fold(this.getGrammar().getRules(), CollectionLiterals.<AbstractRule>newArrayList(), _function_1);
        TypeReference _xifexpression = null;
        boolean _isGenerateStub = this.isGenerateStub();
        if (_isGenerateStub) {
            _xifexpression = this.getGenProposalProviderClass(this.getGrammar());
        } else {
            _xifexpression = this.getProposalProviderClass(this.getGrammar());
        }
        final TypeReference genClass = _xifexpression;
        GeneratedJavaFileAccess _createGeneratedJavaFile = this.fileAccessFactory.createGeneratedJavaFile(genClass);
        final Procedure1<GeneratedJavaFileAccess> _function_2 = (GeneratedJavaFileAccess it) -> {
            final TypeReference superClass = this.getGenProposalProviderSuperClass(this.getGrammar());
            StringConcatenationClient _client = new StringConcatenationClient() {

                @Override
                protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                    _builder.append("/**");
                    _builder.newLine();
                    _builder.append(" ");
                    _builder.append("* Represents a generated, default implementation of superclass {@link ");
                    _builder.append(superClass, " ");
                    _builder.append("}.");
                    _builder.newLineIfNotEmpty();
                    _builder.append(" ");
                    _builder.append("* Methods are dynamically dispatched on the first parameter, i.e., you can override them ");
                    _builder.newLine();
                    _builder.append(" ");
                    _builder.append("* with a more concrete subtype. ");
                    _builder.newLine();
                    _builder.append(" ");
                    _builder.append("*/");
                    _builder.newLine();
                }
            };
            it.setTypeComment(_client);
            StringConcatenationClient _client_1 = new StringConcatenationClient() {

                @Override
                protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                    _builder.append("public ");
                    {
                        boolean _isGenerateStub = ContentAssistFragment2.this.isGenerateStub();
                        if (_isGenerateStub) {
                            _builder.append("abstract ");
                        }
                    }
                    _builder.append("class ");
                    String _simpleName = genClass.getSimpleName();
                    _builder.append(_simpleName);
                    _builder.append(" extends ");
                    _builder.append(superClass);
                    _builder.append(" {");
                    _builder.newLineIfNotEmpty();
                    _builder.newLine();
                    {
                        boolean _isEmpty = assignments.isEmpty();
                        boolean _not = (!_isEmpty);
                        if (_not) {
                            {
                                for (final Assignment assignment : assignments) {
                                    _builder.append("\t");
                                    StringConcatenationClient _handleAssignment = ContentAssistFragment2.this.handleAssignment(assignment);
                                    _builder.append(_handleAssignment, "\t");
                                    _builder.newLineIfNotEmpty();
                                }
                            }
                            _builder.newLine();
                        }
                    }
                    {
                        for (final AbstractRule rule : remainingRules) {
                            _builder.append("\t");
                            _builder.append("public void complete");
                            String _fQFeatureName = ContentAssistFragment2.this.getFQFeatureName(rule);
                            _builder.append(_fQFeatureName, "\t");
                            _builder.append("(");
                            _builder.append(EObject.class, "\t");
                            _builder.append(" model, ");
                            _builder.append(RuleCall.class, "\t");
                            _builder.append(" ruleCall, ");
                            TypeReference _contentAssistContextClass = ContentAssistFragment2.this.getContentAssistContextClass();
                            _builder.append(_contentAssistContextClass, "\t");
                            _builder.append(" context, ");
                            TypeReference _iCompletionProposalAcceptorClass = ContentAssistFragment2.this.getICompletionProposalAcceptorClass();
                            _builder.append(_iCompletionProposalAcceptorClass, "\t");
                            _builder.append(" acceptor) {");
                            _builder.newLineIfNotEmpty();
                            _builder.append("\t");
                            _builder.append("\t");
                            _builder.append("// subclasses may override");
                            _builder.newLine();
                            _builder.append("\t");
                            _builder.append("}");
                            _builder.newLine();
                        }
                    }
                    _builder.append("}");
                    _builder.newLine();
                }
            };
            it.setContent(_client_1);
            it.writeTo(this.getProjectConfig().getEclipsePlugin().getSrcGen());
        };
        _xblockexpression = ObjectExtensions.<GeneratedJavaFileAccess>operator_doubleArrow(_createGeneratedJavaFile, _function_2);
    }
    return _xblockexpression;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) ArrayList(java.util.ArrayList) Function2(org.eclipse.xtext.xbase.lib.Functions.Function2) RuleCall(org.eclipse.xtext.RuleCall) Assignment(org.eclipse.xtext.Assignment) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) EObject(org.eclipse.emf.ecore.EObject) GeneratedJavaFileAccess(org.eclipse.xtext.xtext.generator.model.GeneratedJavaFileAccess) TypeReference(org.eclipse.xtext.xtext.generator.model.TypeReference) AbstractRule(org.eclipse.xtext.AbstractRule) HashSet(java.util.HashSet)

Example 85 with AbstractRule

use of org.eclipse.xtext.AbstractRule in project xtext-core by eclipse.

the class ValidatorFragment2 method generateValidationToDeprecateRules.

protected StringConcatenationClient generateValidationToDeprecateRules() {
    StringConcatenationClient _client = new StringConcatenationClient() {

        @Override
        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
            {
                if (ValidatorFragment2.this.generateDeprecationValidation) {
                    {
                        List<AbstractRule> _deprecatedRulesFromGrammar = ValidatorFragment2.this.getDeprecatedRulesFromGrammar();
                        for (final AbstractRule deprecatedRule : _deprecatedRulesFromGrammar) {
                            EClassifier _classifier = deprecatedRule.getType().getClassifier();
                            ResourceSet _resourceSet = ValidatorFragment2.this.getGrammar().eResource().getResourceSet();
                            final TypeReference elementType = new TypeReference(((EClass) _classifier), _resourceSet);
                            _builder.newLineIfNotEmpty();
                            _builder.newLine();
                            _builder.append("@");
                            _builder.append(Check.class);
                            _builder.newLineIfNotEmpty();
                            _builder.append("public void checkDeprecated");
                            String _simpleName = elementType.getSimpleName();
                            _builder.append(_simpleName);
                            _builder.append("(");
                            _builder.append(elementType);
                            _builder.append(" element) {");
                            _builder.newLineIfNotEmpty();
                            _builder.append("\t");
                            _builder.append("warning(\"This part of the language is marked as deprecated and might get removed in the future!\", element, null);");
                            _builder.newLine();
                            _builder.append("}");
                            _builder.newLine();
                        }
                    }
                }
            }
        }
    };
    return _client;
}
Also used : EClass(org.eclipse.emf.ecore.EClass) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) EClassifier(org.eclipse.emf.ecore.EClassifier) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) TypeReference(org.eclipse.xtext.xtext.generator.model.TypeReference) AbstractRule(org.eclipse.xtext.AbstractRule)

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