Search in sources :

Example 1 with NamedArgument

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

the class XtextValidator method checkOrderOfArguments.

@Check
public void checkOrderOfArguments(RuleCall call) {
    AbstractRule rule = call.getRule();
    if (rule instanceof ParserRule) {
        Set<Parameter> usedParameters = Sets.newHashSet();
        boolean hasError = false;
        boolean hasPositionalArgument = false;
        boolean hasNamedArgument = false;
        for (NamedArgument argument : call.getArguments()) {
            Parameter parameter = argument.getParameter();
            if (parameter == null || parameter.eIsProxy()) {
                hasError = true;
            } else if (!usedParameters.add(parameter)) {
                hasError = true;
                error("Duplicate value for parameter " + parameter.getName(), argument, XtextPackage.Literals.NAMED_ARGUMENT__PARAMETER);
            }
            if (!argument.isCalledByName()) {
                hasPositionalArgument = true;
            } else {
                hasNamedArgument = true;
            }
        }
        if (hasError) {
            return;
        }
        List<Parameter> parameters = ((ParserRule) rule).getParameters();
        if (!hasPositionalArgument) {
            if (usedParameters.size() != parameters.size()) {
                StringBuilder missing = new StringBuilder();
                int count = 0;
                for (Parameter parameter : parameters) {
                    if (!usedParameters.contains(parameter)) {
                        if (count > 0) {
                            missing.append(", ");
                        }
                        missing.append(parameter.getName());
                        count++;
                    }
                }
                if (count == 1) {
                    error("Missing argument for parameter " + missing, call, XtextPackage.Literals.RULE_CALL__RULE);
                } else {
                    error(count + " missing arguments for the following parameters: " + missing, call, XtextPackage.Literals.RULE_CALL__RULE);
                }
            }
        } else {
            if (usedParameters.size() != parameters.size()) {
                error(String.format("Expected %d arguments but got %d", parameters.size(), usedParameters.size()), call, XtextPackage.Literals.RULE_CALL__RULE);
            } else if (hasNamedArgument) {
                for (int i = 0, max = usedParameters.size(); i < max; i++) {
                    NamedArgument argument = call.getArguments().get(i);
                    Parameter param = parameters.get(i);
                    if (argument.isCalledByName() && argument.getParameter() != param) {
                        error("Out of sequence named argument. Expected value for " + param.getName(), argument, XtextPackage.Literals.NAMED_ARGUMENT__PARAMETER);
                    }
                }
            }
        }
    }
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Parameter(org.eclipse.xtext.Parameter) NamedArgument(org.eclipse.xtext.NamedArgument) AbstractRule(org.eclipse.xtext.AbstractRule) Check(org.eclipse.xtext.validation.Check)

Example 2 with NamedArgument

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

the class XtextLinkerTest method testNamedParameterLinking.

@Test
public void testNamedParameterLinking() throws Exception {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("grammar test.Lang with org.eclipse.xtext.common.Terminals");
    _builder.newLine();
    _builder.append("generate test \'http://test\'");
    _builder.newLine();
    _builder.append("Root<MyArg>: rule=Rule<MyArg>;");
    _builder.newLine();
    _builder.append("Rule<MyParam>: name=ID child=Root<MyArg=MyParam>?;");
    _builder.newLine();
    final String grammarAsString = _builder.toString();
    EObject _model = this.getModel(grammarAsString);
    final Grammar grammar = ((Grammar) _model);
    AbstractRule _head = IterableExtensions.<AbstractRule>head(grammar.getRules());
    final ParserRule rootRule = ((ParserRule) _head);
    AbstractRule _last = IterableExtensions.<AbstractRule>last(grammar.getRules());
    final ParserRule lastRule = ((ParserRule) _last);
    AbstractElement _alternatives = lastRule.getAlternatives();
    AbstractElement _last_1 = IterableExtensions.<AbstractElement>last(((Group) _alternatives).getElements());
    final Assignment lastAssignment = ((Assignment) _last_1);
    AbstractElement _terminal = lastAssignment.getTerminal();
    final RuleCall ruleCall = ((RuleCall) _terminal);
    final NamedArgument argument = IterableExtensions.<NamedArgument>head(ruleCall.getArguments());
    Assert.assertEquals(IterableExtensions.<Parameter>head(rootRule.getParameters()), argument.getParameter());
    Condition _value = argument.getValue();
    Assert.assertEquals(IterableExtensions.<Parameter>head(lastRule.getParameters()), ((ParameterReference) _value).getParameter());
}
Also used : Assignment(org.eclipse.xtext.Assignment) LiteralCondition(org.eclipse.xtext.LiteralCondition) Condition(org.eclipse.xtext.Condition) ParserRule(org.eclipse.xtext.ParserRule) AbstractElement(org.eclipse.xtext.AbstractElement) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Grammar(org.eclipse.xtext.Grammar) NamedArgument(org.eclipse.xtext.NamedArgument) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall) Test(org.junit.Test)

Example 3 with NamedArgument

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

the class AntlrGrammarGenUtil method getArgumentList.

/**
 * @since 2.9
 */
public static String getArgumentList(final RuleCall ruleCall, boolean passCurrentIntoFragment, boolean isPredicate) {
    final List<NamedArgument> arguments = ruleCall.getArguments();
    AbstractRule abstractRule = ruleCall.getRule();
    boolean needsCurrent = passCurrentIntoFragment && GrammarUtil.isEObjectFragmentRule(abstractRule) && !GrammarUtil.isDatatypeRule((ParserRule) getOriginalElement(abstractRule));
    if (arguments.isEmpty()) {
        if (needsCurrent) {
            return isPredicate ? "[null]" : "[$current]";
        }
        return "";
    }
    ParserRule rule = (ParserRule) abstractRule;
    StringBuilder result = new StringBuilder();
    result.append("[");
    if (needsCurrent) {
        if (isPredicate) {
            result.append("null, ");
        } else {
            result.append("$current, ");
        }
    }
    Joiner.on(", ").appendTo(result, Iterables.transform(rule.getParameters(), new Function<Parameter, String>() {

        @Override
        public String apply(Parameter input) {
            for (NamedArgument argument : arguments) {
                if (argument.getParameter() == input) {
                    return conditionToAntlr(argument.getValue(), true);
                }
            }
            throw new IllegalStateException("Cannot find argument for parameter: " + input.getName());
        }
    }));
    result.append("]");
    return result.toString();
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Function(com.google.common.base.Function) Parameter(org.eclipse.xtext.Parameter) NamedArgument(org.eclipse.xtext.NamedArgument) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 4 with NamedArgument

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

the class SerializationContext method forChild.

public static ISerializationContext forChild(ISerializationContext container, RuleCall ruleCall, EObject sem) {
    EClass type = sem == null ? null : sem.eClass();
    ISerializationContext result = new TypeContext(new RuleContext(null, (ParserRule) ruleCall.getRule()), type);
    EList<NamedArgument> arguments = ruleCall.getArguments();
    if (!arguments.isEmpty() && container != null) {
        Set<Parameter> params = Sets.newLinkedHashSet();
        ConditionEvaluator evaluator = new ConditionEvaluator(container.getEnabledBooleanParameters());
        for (NamedArgument argument : arguments) {
            if (evaluator.evaluate(argument.getValue())) {
                params.add(argument.getParameter());
            }
        }
        result = new SerializationContext.ParameterValueContext(result, params);
    }
    return result;
}
Also used : ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) ParserRule(org.eclipse.xtext.ParserRule) EClass(org.eclipse.emf.ecore.EClass) ConditionEvaluator(org.eclipse.xtext.xtext.ConditionEvaluator) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) Parameter(org.eclipse.xtext.Parameter) NamedArgument(org.eclipse.xtext.NamedArgument)

Example 5 with NamedArgument

use of org.eclipse.xtext.NamedArgument 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

NamedArgument (org.eclipse.xtext.NamedArgument)13 ParserRule (org.eclipse.xtext.ParserRule)11 AbstractRule (org.eclipse.xtext.AbstractRule)10 RuleCall (org.eclipse.xtext.RuleCall)9 EObject (org.eclipse.emf.ecore.EObject)7 Parameter (org.eclipse.xtext.Parameter)7 AbstractElement (org.eclipse.xtext.AbstractElement)6 Assignment (org.eclipse.xtext.Assignment)6 Condition (org.eclipse.xtext.Condition)5 Grammar (org.eclipse.xtext.Grammar)5 LiteralCondition (org.eclipse.xtext.LiteralCondition)5 InternalEObject (org.eclipse.emf.ecore.InternalEObject)4 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)4 Test (org.junit.Test)4 Group (org.eclipse.xtext.Group)3 TerminalRule (org.eclipse.xtext.TerminalRule)3 TypeRef (org.eclipse.xtext.TypeRef)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 EClass (org.eclipse.emf.ecore.EClass)2 AbstractMetamodelDeclaration (org.eclipse.xtext.AbstractMetamodelDeclaration)2