Search in sources :

Example 91 with EClass

use of org.eclipse.emf.ecore.EClass in project xtext-core by eclipse.

the class SequencerDiagnosticProvider method createInvalidContextOrTypeDiagnostic.

@Override
public ISerializationDiagnostic createInvalidContextOrTypeDiagnostic(EObject sem, ISerializationContext context) {
    Set<ISerializationContext> contexts = Sets.newHashSet(contextFinder.findByContentsAndContainer(sem, null));
    Set<EClass> validTypes = getValidTypes(context);
    List<ISerializationContext> recommendedCtxs = Lists.newArrayList();
    List<ISerializationContext> otherValidCtxs = Lists.newArrayList();
    for (ISerializationContext ctx : getValidContexts(sem.eClass())) {
        if (contexts.contains(ctx))
            recommendedCtxs.add(ctx);
        else
            otherValidCtxs.add(ctx);
    }
    String semanticType = sem.eClass().getName();
    String validTypeNames = Joiner.on(", ").join(Iterables.transform(validTypes, new NamedElement2Name()));
    StringBuilder msg = new StringBuilder();
    msg.append("The context '" + context + "' is not valid for type '" + semanticType + "'\n");
    msg.append("Recommended contexts for type '" + semanticType + "': " + recommendedCtxs + "\n");
    if (!otherValidCtxs.isEmpty())
        msg.append("Other valid contexts for type '" + semanticType + "': " + otherValidCtxs);
    msg.append("The context '" + context + "' is valid for types: " + validTypeNames + "\n");
    return new SerializationDiagnostic(INVALID_CONTEXT_OR_TYPE, sem, grammarAccess.getGrammar(), msg.toString());
}
Also used : EClass(org.eclipse.emf.ecore.EClass) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext)

Example 92 with EClass

use of org.eclipse.emf.ecore.EClass in project xtext-core by eclipse.

the class ContextFinder method getConstraints.

protected Multimap<IConstraint, ISerializationContext> getConstraints(EObject sem, Iterable<ISerializationContext> contextCandidates) {
    EClass type = sem == null ? null : sem.eClass();
    Multimap<IConstraint, ISerializationContext> result = ArrayListMultimap.create();
    for (ISerializationContext ctx : contextCandidates) {
        IConstraint constraint = constraints.get(ctx);
        if (constraint != null && constraint.getType() == type) {
            result.put(constraint, ctx);
        }
    }
    return result;
}
Also used : EClass(org.eclipse.emf.ecore.EClass) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint)

Example 93 with EClass

use of org.eclipse.emf.ecore.EClass in project xtext-core by eclipse.

the class NodeModelSemanticSequencer method acceptSemantic.

protected boolean acceptSemantic(EObject semanticObject, AbstractElement ele, Object value, INode node) {
    Assignment ass = GrammarUtil.containingAssignment(ele);
    int index = -2;
    if (ass != null) {
        EStructuralFeature feat = semanticObject.eClass().getEStructuralFeature(ass.getFeature());
        if (feat != null) {
            if (feat.isMany())
                index = ((List<?>) semanticObject.eGet(feat)).indexOf(value);
            else
                index = -1;
        }
    }
    String token = node.getText().trim();
    if (ele instanceof Action) {
        if (((Action) ele).getFeature() != null) {
            if (sequenceAcceptor.enterAssignedAction((Action) ele, (EObject) value, (ICompositeNode) node)) {
                createSequence(ele, (EObject) value);
                sequenceAcceptor.leaveAssignedAction((Action) ele, (EObject) value);
            }
            return true;
        }
    } else if (GrammarUtil.containingCrossReference(ele) != null) {
        if (ele instanceof RuleCall) {
            RuleCall rc = (RuleCall) ele;
            if (rc.getRule() instanceof ParserRule) {
                sequenceAcceptor.acceptAssignedCrossRefDatatype(rc, token, (EObject) value, index, (ICompositeNode) node);
                return true;
            }
            if (rc.getRule() instanceof TerminalRule) {
                sequenceAcceptor.acceptAssignedCrossRefTerminal(rc, token, (EObject) value, index, (ILeafNode) node);
                return true;
            }
            if (rc.getRule() instanceof EnumRule) {
                sequenceAcceptor.acceptAssignedCrossRefEnum(rc, token, (EObject) value, index, (ICompositeNode) node);
                return true;
            }
        // } else if (ele instanceof Keyword) {
        // acceptor.acceptAssignedCrossRefKeyword((Keyword) ele, token, (EObject) value, index,(ILeafNode) node);
        // return true;
        }
    } else if (ass != null) {
        if (ele instanceof RuleCall) {
            RuleCall rc = (RuleCall) ele;
            if (rc.getRule() instanceof ParserRule) {
                if (rc.getRule().getType().getClassifier() instanceof EClass) {
                    if (sequenceAcceptor.enterAssignedParserRuleCall(rc, (EObject) value, (ICompositeNode) node)) {
                        createSequence(rc.getRule(), (EObject) value);
                        sequenceAcceptor.leaveAssignedParserRuleCall(rc, (EObject) value);
                    }
                } else
                    sequenceAcceptor.acceptAssignedDatatype(rc, token, value, index, (ICompositeNode) node);
                return true;
            }
            if (rc.getRule() instanceof TerminalRule) {
                sequenceAcceptor.acceptAssignedTerminal(rc, token, value, index, (ILeafNode) node);
                return true;
            }
            if (rc.getRule() instanceof EnumRule) {
                sequenceAcceptor.acceptAssignedEnum(rc, token, value, index, (ICompositeNode) node);
                return true;
            }
        } else if (ele instanceof Keyword) {
            if (GrammarUtil.isBooleanAssignment(ass))
                sequenceAcceptor.acceptAssignedKeyword((Keyword) ele, token, true, index, (ILeafNode) node);
            else
                sequenceAcceptor.acceptAssignedKeyword((Keyword) ele, token, (String) value, index, (ILeafNode) node);
            return true;
        }
    }
    return false;
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Action(org.eclipse.xtext.Action) Keyword(org.eclipse.xtext.Keyword) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) RuleCall(org.eclipse.xtext.RuleCall) Assignment(org.eclipse.xtext.Assignment) EnumRule(org.eclipse.xtext.EnumRule) EClass(org.eclipse.emf.ecore.EClass) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) List(java.util.List) TerminalRule(org.eclipse.xtext.TerminalRule)

Example 94 with EClass

use of org.eclipse.emf.ecore.EClass in project xtext-core by eclipse.

the class SerializerFragment2 method genMethodSequenceComment.

private StringConcatenationClient genMethodSequenceComment(final IGrammarConstraintProvider.IConstraint c) {
    StringConcatenationClient _client = new StringConcatenationClient() {

        @Override
        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
            _builder.append("// This method is commented out because it has the same signature as another method in this class.");
            _builder.newLine();
            _builder.append("// This is probably a bug in Xtext\'s serializer, please report it here: ");
            _builder.newLine();
            _builder.append("// https://bugs.eclipse.org/bugs/enter_bug.cgi?product=TMF");
            _builder.newLine();
            _builder.append("//");
            _builder.newLine();
            _builder.append("// Contexts:");
            _builder.newLine();
            _builder.append("//     ");
            String _replaceAll = IterableExtensions.join(IterableExtensions.<ISerializationContext>sort(c.getContexts()), "\n").replaceAll("\\n", "\n//     ");
            _builder.append(_replaceAll);
            _builder.newLineIfNotEmpty();
            _builder.append("//");
            _builder.newLine();
            _builder.append("// Constraint:");
            _builder.newLine();
            _builder.append("//     ");
            {
                IGrammarConstraintProvider.IConstraintElement _body = c.getBody();
                boolean _tripleEquals = (_body == null);
                if (_tripleEquals) {
                    _builder.append("{");
                    String _name = c.getType().getName();
                    _builder.append(_name);
                    _builder.append("}");
                } else {
                    String _replaceAll_1 = c.getBody().toString().replaceAll("\\n", "\n//     ");
                    _builder.append(_replaceAll_1);
                }
            }
            _builder.newLineIfNotEmpty();
            _builder.append("//");
            _builder.newLine();
            _builder.append("// protected void sequence_");
            String _simpleName = c.getSimpleName();
            _builder.append(_simpleName);
            _builder.append("(");
            _builder.append(ISerializationContext.class);
            _builder.append(" context, ");
            EClass _type = c.getType();
            _builder.append(_type);
            _builder.append(" semanticObject) { }");
            _builder.newLineIfNotEmpty();
        }
    };
    return _client;
}
Also used : EClass(org.eclipse.emf.ecore.EClass) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient)

Example 95 with EClass

use of org.eclipse.emf.ecore.EClass in project xtext-core by eclipse.

the class FlattenedGrammarAccess method copyRuleBodies.

private Multimap<TerminalRule, AbstractRule> copyRuleBodies(final List<AbstractRule> copies, final Map<RuleWithParameterValues, AbstractRule> origToCopy) {
    abstract class __FlattenedGrammarAccess_1 extends EcoreUtil.Copier {

        final __FlattenedGrammarAccess_1 _this__FlattenedGrammarAccess_1 = this;

        abstract Set<Parameter> getParameterConfig(final RuleCall origRuleCall, final RuleCall copyRuleCall);

        abstract void mergePredicates(final AbstractElement into, final AbstractElement from);

        abstract void mergeCardinalities(final AbstractElement into, final AbstractElement from);

        abstract boolean evaluate(final Condition condition);
    }
    final HashMultimap<TerminalRule, AbstractRule> calledFrom = HashMultimap.<TerminalRule, AbstractRule>create();
    for (final AbstractRule copy : copies) {
        {
            AbstractRule orig = RuleWithParameterValues.getOriginalRule(copy);
            final Set<Parameter> paramValues = RuleWithParameterValues.getParamValues(copy);
            EcoreUtil.Copier copier = new __FlattenedGrammarAccess_1() {

                @Override
                protected void copyReference(final EReference eReference, final EObject eObject, final EObject copyEObject) {
                    if ((eReference == XtextPackage.Literals.RULE_CALL__RULE)) {
                        RuleCall origRuleCall = ((RuleCall) eObject);
                        RuleCall copyRuleCall = ((RuleCall) copyEObject);
                        AbstractRule _rule = origRuleCall.getRule();
                        Set<Parameter> _parameterConfig = this.getParameterConfig(origRuleCall, copyRuleCall);
                        RuleWithParameterValues _ruleWithParameterValues = new RuleWithParameterValues(_rule, _parameterConfig);
                        AbstractRule calledCopy = origToCopy.get(_ruleWithParameterValues);
                        copyRuleCall.setRule(calledCopy);
                        if ((calledCopy instanceof TerminalRule)) {
                            calledFrom.put(((TerminalRule) calledCopy), copy);
                        }
                    } else {
                        super.copyReference(eReference, eObject, copyEObject);
                    }
                }

                Set<Parameter> getParameterConfig(final RuleCall origRuleCall, final RuleCall copyRuleCall) {
                    boolean _isEmpty = origRuleCall.getArguments().isEmpty();
                    if (_isEmpty) {
                        return Collections.<Parameter>emptySet();
                    }
                    final Function1<NamedArgument, Boolean> _function = (NamedArgument it) -> {
                        return Boolean.valueOf(this.evaluate(it.getValue()));
                    };
                    final Function1<NamedArgument, Parameter> _function_1 = (NamedArgument it) -> {
                        return it.getParameter();
                    };
                    Set<Parameter> result = IterableExtensions.<Parameter>toSet(IterableExtensions.<NamedArgument, Parameter>map(IterableExtensions.<NamedArgument>filter(origRuleCall.getArguments(), _function), _function_1));
                    return result;
                }

                @Override
                protected void copyContainment(final EReference eReference, final EObject eObject, final EObject copyEObject) {
                    boolean _matched = false;
                    if (Objects.equal(eReference, XtextPackage.Literals.RULE_CALL__ARGUMENTS)) {
                        _matched = true;
                    }
                    if (!_matched) {
                        if (Objects.equal(eReference, XtextPackage.Literals.GROUP__GUARD_CONDITION)) {
                            _matched = true;
                        }
                    }
                    if (_matched) {
                        return;
                    }
                    super.copyContainment(eReference, eObject, copyEObject);
                }

                @Override
                public EObject copy(final EObject eObject) {
                    if ((eObject instanceof Group)) {
                        Group group = ((Group) eObject);
                        Condition _guardCondition = group.getGuardCondition();
                        boolean _tripleNotEquals = (_guardCondition != null);
                        if (_tripleNotEquals) {
                            boolean _evaluate = this.evaluate(group.getGuardCondition());
                            boolean _not = (!_evaluate);
                            if (_not) {
                                return null;
                            }
                        }
                    }
                    EObject result = super.copy(eObject);
                    if ((result instanceof CompoundElement)) {
                        List<AbstractElement> elements = ((CompoundElement) result).getElements();
                        int _size = elements.size();
                        boolean _tripleEquals = (_size == 1);
                        if (_tripleEquals) {
                            if (((!((CompoundElement) result).isFirstSetPredicated()) && (!((CompoundElement) result).isPredicated()))) {
                                AbstractElement element = elements.get(0);
                                this.mergeCardinalities(element, ((AbstractElement) result));
                                this.mergePredicates(element, ((AbstractElement) result));
                                return element;
                            } else {
                                AbstractElement element_1 = elements.get(0);
                                this.mergePredicates(((AbstractElement) result), element_1);
                                element_1.setFirstSetPredicated(false);
                                element_1.setPredicated(false);
                            }
                        }
                    }
                    if ((eObject instanceof AbstractElement)) {
                        OriginalElement original = new OriginalElement(((AbstractElement) eObject));
                        EClass _eClass = ((AbstractElement) eObject).eClass();
                        EClass _eClass_1 = result.eClass();
                        boolean _notEquals = (!Objects.equal(_eClass, _eClass_1));
                        if (_notEquals) {
                            String _name = result.eClass().getName();
                            String _plus = ("copy is: \'" + _name);
                            String _plus_1 = (_plus + "\' but original was: \'");
                            String _name_1 = ((AbstractElement) eObject).eClass().getName();
                            String _plus_2 = (_plus_1 + _name_1);
                            String _plus_3 = (_plus_2 + "\'");
                            throw new IllegalStateException(_plus_3);
                        }
                        original.attachToEmfObject(result);
                    }
                    return result;
                }

                void mergePredicates(final AbstractElement into, final AbstractElement from) {
                    boolean _isPredicated = from.isPredicated();
                    if (_isPredicated) {
                        into.setPredicated(true);
                        into.setFirstSetPredicated(false);
                    } else {
                        if (((!into.isPredicated()) && from.isFirstSetPredicated())) {
                            into.setFirstSetPredicated(true);
                        }
                    }
                }

                void mergeCardinalities(final AbstractElement into, final AbstractElement from) {
                    String c1 = into.getCardinality();
                    String c2 = from.getCardinality();
                    String _switchResult = null;
                    boolean _matched = false;
                    if ((Objects.equal(c1, "*") || Objects.equal(c2, "*"))) {
                        _matched = true;
                    }
                    if (!_matched) {
                        if ((Objects.equal(c1, "+") && Objects.equal(c2, "?"))) {
                            _matched = true;
                        }
                    }
                    if (!_matched) {
                        if ((Objects.equal(c1, "?") && Objects.equal(c2, "+"))) {
                            _matched = true;
                        }
                    }
                    if (_matched) {
                        _switchResult = "*";
                    }
                    if (!_matched) {
                        if (Objects.equal(c1, null)) {
                            _matched = true;
                            _switchResult = c2;
                        }
                    }
                    if (!_matched) {
                        _switchResult = c1;
                    }
                    into.setCardinality(_switchResult);
                }

                boolean evaluate(final Condition condition) {
                    boolean result = new ConditionEvaluator(paramValues).evaluate(condition);
                    return result;
                }
            };
            EObject _copy = copier.copy(orig.getAlternatives());
            AbstractElement copiedBody = ((AbstractElement) _copy);
            copier.copyReferences();
            copy.setAlternatives(copiedBody);
            if ((orig instanceof ParserRule)) {
                ParserRule castedCopy = ((ParserRule) copy);
                boolean _isDefinesHiddenTokens = ((ParserRule) orig).isDefinesHiddenTokens();
                if (_isDefinesHiddenTokens) {
                    castedCopy.setDefinesHiddenTokens(true);
                    EList<AbstractRule> _hiddenTokens = ((ParserRule) orig).getHiddenTokens();
                    for (final AbstractRule rule : _hiddenTokens) {
                        {
                            RuleWithParameterValues _ruleWithParameterValues = new RuleWithParameterValues(rule);
                            final AbstractRule copiedTerminalRule = origToCopy.get(_ruleWithParameterValues);
                            EList<AbstractRule> _hiddenTokens_1 = castedCopy.getHiddenTokens();
                            _hiddenTokens_1.add(copiedTerminalRule);
                            calledFrom.put(((TerminalRule) copiedTerminalRule), castedCopy);
                        }
                    }
                }
            }
        }
    }
    return calledFrom;
}
Also used : Group(org.eclipse.xtext.Group) ParserRule(org.eclipse.xtext.ParserRule) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) RuleCall(org.eclipse.xtext.RuleCall) EClass(org.eclipse.emf.ecore.EClass) EObject(org.eclipse.emf.ecore.EObject) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) NamedArgument(org.eclipse.xtext.NamedArgument) EReference(org.eclipse.emf.ecore.EReference) OriginalElement(org.eclipse.xtext.xtext.OriginalElement) Condition(org.eclipse.xtext.Condition) AbstractElement(org.eclipse.xtext.AbstractElement) RuleWithParameterValues(org.eclipse.xtext.xtext.RuleWithParameterValues) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) ConditionEvaluator(org.eclipse.xtext.xtext.ConditionEvaluator) EList(org.eclipse.emf.common.util.EList) Parameter(org.eclipse.xtext.Parameter) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule) CompoundElement(org.eclipse.xtext.CompoundElement)

Aggregations

EClass (org.eclipse.emf.ecore.EClass)205 Test (org.junit.Test)99 EPackage (org.eclipse.emf.ecore.EPackage)70 EClassifier (org.eclipse.emf.ecore.EClassifier)67 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)43 EObject (org.eclipse.emf.ecore.EObject)35 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)27 Resource (org.eclipse.emf.ecore.resource.Resource)23 EReference (org.eclipse.emf.ecore.EReference)22 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)16 ISerializationContext (org.eclipse.xtext.serializer.ISerializationContext)13 ParserRule (org.eclipse.xtext.ParserRule)12 QualifiedName (org.eclipse.xtext.naming.QualifiedName)12 InternalEObject (org.eclipse.emf.ecore.InternalEObject)11 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)11 IScope (org.eclipse.xtext.scoping.IScope)11 ArrayList (java.util.ArrayList)10 AbstractRule (org.eclipse.xtext.AbstractRule)10 List (java.util.List)9 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)9