Search in sources :

Example 36 with TerminalRule

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

the class XtextValidator method checkCurrentMustBeUnassigned.

private void checkCurrentMustBeUnassigned(final AbstractElement element) {
    final ParserRule rule = GrammarUtil.containingParserRule(element);
    if (GrammarUtil.isDatatypeRule(rule))
        return;
    XtextSwitch<Boolean> visitor = new XtextSwitch<Boolean>() {

        private boolean isNull = !rule.isFragment();

        @Override
        public Boolean caseAbstractElement(AbstractElement object) {
            return isNull;
        }

        @Override
        public Boolean caseAlternatives(Alternatives object) {
            final boolean wasIsNull = isNull;
            boolean localIsNull = wasIsNull;
            for (AbstractElement element : object.getElements()) {
                isNull = wasIsNull;
                localIsNull &= doSwitch(element);
            }
            isNull = localIsNull;
            return isNull;
        }

        @Override
        public Boolean caseUnorderedGroup(UnorderedGroup object) {
            final boolean wasIsNull = isNull;
            boolean localIsNull = wasIsNull;
            for (AbstractElement element : object.getElements()) {
                isNull = wasIsNull;
                localIsNull |= doSwitch(element);
            }
            isNull = localIsNull;
            return isNull;
        }

        @Override
        public Boolean caseAssignment(Assignment object) {
            isNull = false;
            return isNull;
        }

        @Override
        public Boolean caseGroup(Group object) {
            for (AbstractElement element : object.getElements()) doSwitch(element);
            return isNull;
        }

        @Override
        public Boolean caseAction(Action object) {
            if (object == element) {
                if (!(isNull && !isMany(object))) {
                    error("An unassigned action is not allowed, when the 'current' was already created.", object, null);
                    checkDone();
                }
            }
            isNull = false;
            return isNull;
        }

        @Override
        public Boolean caseRuleCall(RuleCall object) {
            if (object == element) {
                AbstractRule calledRule = object.getRule();
                if (calledRule instanceof ParserRule && ((ParserRule) calledRule).isFragment()) {
                    isNull = false;
                    return isNull;
                }
                if (!(isNull && !isMany(object))) {
                    error("An unassigned rule call is not allowed, when the 'current' was already created.", object, null);
                    checkDone();
                }
            }
            return doSwitch(object.getRule());
        }

        @Override
        public Boolean caseParserRule(ParserRule object) {
            isNull = GrammarUtil.isDatatypeRule(object);
            return isNull;
        }

        @Override
        public Boolean caseTerminalRule(TerminalRule object) {
            isNull = true;
            return isNull;
        }

        public boolean isMany(AbstractElement element) {
            return GrammarUtil.isMultipleCardinality(element) || ((element.eContainer() instanceof AbstractElement) && isMany((AbstractElement) element.eContainer()));
        }
    };
    visitor.doSwitch(rule.getAlternatives());
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Group(org.eclipse.xtext.Group) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) Action(org.eclipse.xtext.Action) AbstractElement(org.eclipse.xtext.AbstractElement) Alternatives(org.eclipse.xtext.Alternatives) RuleCall(org.eclipse.xtext.RuleCall) Assignment(org.eclipse.xtext.Assignment) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) XtextSwitch(org.eclipse.xtext.util.XtextSwitch) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 37 with TerminalRule

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

the class XtextValidator method checkAssignedActionAfterAssignment.

@Check
public void checkAssignedActionAfterAssignment(final Action action) {
    if (action.getFeature() != null) {
        ParserRule rule = GrammarUtil.containingParserRule(action);
        if (rule.isFragment() && !rule.isWildcard()) {
            error("An action is not allowed in fragments.", action, null);
            return;
        }
        XtextSwitch<Boolean> visitor = new XtextSwitch<Boolean>() {

            private boolean assignedActionAllowed = false;

            @Override
            public Boolean caseAbstractElement(AbstractElement object) {
                return assignedActionAllowed;
            }

            @Override
            public Boolean caseAlternatives(Alternatives object) {
                boolean wasActionAllowed = assignedActionAllowed;
                boolean localActionAllowed = true;
                for (AbstractElement element : object.getElements()) {
                    assignedActionAllowed = wasActionAllowed;
                    localActionAllowed &= doSwitch(element);
                }
                assignedActionAllowed = wasActionAllowed || (localActionAllowed && !GrammarUtil.isOptionalCardinality(object));
                return assignedActionAllowed;
            }

            @Override
            public Boolean caseUnorderedGroup(UnorderedGroup object) {
                boolean wasActionAllowed = assignedActionAllowed;
                boolean localActionAllowed = false;
                for (AbstractElement element : object.getElements()) {
                    assignedActionAllowed = wasActionAllowed;
                    localActionAllowed |= doSwitch(element);
                }
                assignedActionAllowed = wasActionAllowed || (localActionAllowed && !GrammarUtil.isOptionalCardinality(object));
                return assignedActionAllowed;
            }

            @Override
            public Boolean caseAssignment(Assignment object) {
                assignedActionAllowed = assignedActionAllowed || !GrammarUtil.isOptionalCardinality(object);
                return assignedActionAllowed;
            }

            @Override
            public Boolean caseGroup(Group object) {
                boolean wasAssignedActionAllowed = assignedActionAllowed;
                for (AbstractElement element : object.getElements()) doSwitch(element);
                assignedActionAllowed = wasAssignedActionAllowed || (assignedActionAllowed && !GrammarUtil.isOptionalCardinality(object));
                return assignedActionAllowed;
            }

            @Override
            public Boolean caseAction(Action object) {
                if (object == action) {
                    if (!assignedActionAllowed) {
                        error("An action is not allowed in fragments and when the current may still be unassigned.", null);
                        checkDone();
                    }
                }
                assignedActionAllowed = true;
                return assignedActionAllowed;
            }

            @Override
            public Boolean caseRuleCall(RuleCall object) {
                if (object.getRule() == null)
                    return assignedActionAllowed;
                assignedActionAllowed = assignedActionAllowed || doSwitch(object.getRule()) && !GrammarUtil.isOptionalCardinality(object);
                return assignedActionAllowed;
            }

            @Override
            public Boolean caseParserRule(ParserRule object) {
                assignedActionAllowed = !GrammarUtil.isDatatypeRule(object) && !object.isFragment();
                return assignedActionAllowed;
            }

            @Override
            public Boolean caseTerminalRule(TerminalRule object) {
                return assignedActionAllowed;
            }
        };
        visitor.doSwitch(rule.getAlternatives());
    }
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Group(org.eclipse.xtext.Group) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) Action(org.eclipse.xtext.Action) AbstractElement(org.eclipse.xtext.AbstractElement) Alternatives(org.eclipse.xtext.Alternatives) RuleCall(org.eclipse.xtext.RuleCall) Assignment(org.eclipse.xtext.Assignment) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) XtextSwitch(org.eclipse.xtext.util.XtextSwitch) TerminalRule(org.eclipse.xtext.TerminalRule) Check(org.eclipse.xtext.validation.Check)

Example 38 with TerminalRule

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

the class FlattenedGrammarAccess method copyRuleStubs.

private ArrayList<AbstractRule> copyRuleStubs(final RuleNames names, final Map<RuleWithParameterValues, AbstractRule> origToCopy, final List<AbstractRule> rulesToCopy, final boolean discardTypeRef) {
    final ArrayList<AbstractRule> result = CollectionLiterals.<AbstractRule>newArrayList();
    for (final AbstractRule rule : rulesToCopy) {
        {
            String ruleName = names.getAntlrRuleName(rule);
            boolean _matched = false;
            if (rule instanceof ParserRule) {
                _matched = true;
                List<Parameter> params = ((ParserRule) rule).getParameters();
                boolean _isEmpty = params.isEmpty();
                if (_isEmpty) {
                    ParserRule copy = this.<ParserRule>copy(((ParserRule) rule));
                    copy.setName(ruleName);
                    copy.setFragment(((ParserRule) rule).isFragment());
                    copy.setWildcard(((ParserRule) rule).isWildcard());
                    if ((!discardTypeRef)) {
                        copy.setType(this.copyTypeRef(((ParserRule) rule).getType()));
                    }
                    this.attachTo(copy, rule, origToCopy);
                    result.add(copy);
                } else {
                    final Procedure2<Set<Parameter>, Integer> _function = (Set<Parameter> parameterConfig, Integer i) -> {
                        RuleWithParameterValues parameterValues = new RuleWithParameterValues(rule, parameterConfig);
                        ParserRule copy_1 = this.<ParserRule>copy(((ParserRule) rule));
                        copy_1.setName(names.getAntlrRuleName(rule, (i).intValue()));
                        copy_1.setFragment(((ParserRule) rule).isFragment());
                        copy_1.setWildcard(((ParserRule) rule).isWildcard());
                        if ((!discardTypeRef)) {
                            copy_1.setType(this.copyTypeRef(((ParserRule) rule).getType()));
                        }
                        origToCopy.put(parameterValues, copy_1);
                        parameterValues.attachToEmfObject(copy_1);
                        result.add(copy_1);
                    };
                    IterableExtensions.<Set<Parameter>>forEach(Sets.<Parameter>powerSet(ImmutableSet.<Parameter>copyOf(params)), _function);
                }
            }
            if (!_matched) {
                if (rule instanceof TerminalRule) {
                    _matched = true;
                    TerminalRule orig = ((TerminalRule) rule);
                    TerminalRule copy = this.<TerminalRule>copy(orig);
                    copy.setName(ruleName);
                    copy.setFragment(orig.isFragment());
                    this.attachTo(copy, orig, origToCopy);
                    result.add(copy);
                }
            }
            if (!_matched) {
                if (rule instanceof EnumRule) {
                    _matched = true;
                    EnumRule copy = this.<EnumRule>copy(((EnumRule) rule));
                    copy.setName(ruleName);
                    this.attachTo(copy, rule, origToCopy);
                    result.add(copy);
                }
            }
        }
    }
    return result;
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) EnumRule(org.eclipse.xtext.EnumRule) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Procedure2(org.eclipse.xtext.xbase.lib.Procedures.Procedure2) RuleWithParameterValues(org.eclipse.xtext.xtext.RuleWithParameterValues) Parameter(org.eclipse.xtext.Parameter) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 39 with TerminalRule

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

the class AntlrGrammarGenerator method _ebnf2.

@Override
protected String _ebnf2(final RuleCall it, final AntlrOptions options, final boolean supportActions) {
    String _xifexpression = null;
    if ((!supportActions)) {
        _xifexpression = super._ebnf2(it, options, supportActions);
    } else {
        String _switchResult = null;
        AbstractRule _rule = it.getRule();
        final AbstractRule rule = _rule;
        boolean _matched = false;
        if (rule instanceof EnumRule) {
            boolean _isAssigned = GrammarUtil.isAssigned(it);
            if (_isAssigned) {
                _matched = true;
            }
        }
        if (!_matched) {
            if (rule instanceof ParserRule) {
                boolean _isAssigned = GrammarUtil.isAssigned(it);
                if (_isAssigned) {
                    _matched = true;
                }
            }
        }
        if (_matched) {
            _switchResult = super._ebnf2(it, options, supportActions);
        }
        if (!_matched) {
            if (rule instanceof EnumRule) {
                _matched = true;
            }
            if (!_matched) {
                if (rule instanceof ParserRule) {
                    boolean _isDatatypeRule = GrammarUtil.isDatatypeRule(AntlrGrammarGenUtil.<ParserRule>getOriginalElement(((ParserRule) rule)));
                    if (_isDatatypeRule) {
                        _matched = true;
                    }
                }
            }
            if (_matched) {
                StringConcatenation _builder = new StringConcatenation();
                {
                    boolean _isBacktrack = options.isBacktrack();
                    if (_isBacktrack) {
                        _builder.append("{");
                        _builder.newLine();
                        _builder.append("\t");
                        _builder.append("/* */");
                        _builder.newLine();
                        _builder.append("}");
                        _builder.newLine();
                    }
                }
                _builder.append("{");
                _builder.newLine();
                _builder.append("\t");
                CharSequence _newCompositeNode = this.newCompositeNode(it);
                _builder.append(_newCompositeNode, "\t");
                _builder.newLineIfNotEmpty();
                _builder.append("}");
                _builder.newLine();
                String __ebnf2 = super._ebnf2(it, options, supportActions);
                _builder.append(__ebnf2);
                _builder.newLineIfNotEmpty();
                _builder.append("{");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("afterParserOrEnumRuleCall();");
                _builder.newLine();
                _builder.append("}");
                _builder.newLine();
                _switchResult = _builder.toString();
            }
        }
        if (!_matched) {
            if (rule instanceof ParserRule) {
                _matched = true;
                StringConcatenation _builder_1 = new StringConcatenation();
                {
                    boolean _isBacktrack_1 = options.isBacktrack();
                    if (_isBacktrack_1) {
                        _builder_1.append("{");
                        _builder_1.newLine();
                        _builder_1.append("\t");
                        _builder_1.append("/* */");
                        _builder_1.newLine();
                        _builder_1.append("}");
                        _builder_1.newLine();
                    }
                }
                _builder_1.append("{");
                _builder_1.newLine();
                {
                    boolean _isEObjectFragmentRuleCall = GrammarUtil.isEObjectFragmentRuleCall(it);
                    if (_isEObjectFragmentRuleCall) {
                        _builder_1.append("\t");
                        _builder_1.append("if ($current==null) {");
                        _builder_1.newLine();
                        _builder_1.append("\t");
                        _builder_1.append("\t");
                        _builder_1.append("$current = ");
                        CharSequence _createModelElement = this.createModelElement(it);
                        _builder_1.append(_createModelElement, "\t\t");
                        _builder_1.append(";");
                        _builder_1.newLineIfNotEmpty();
                        _builder_1.append("\t");
                        _builder_1.append("}");
                        _builder_1.newLine();
                    }
                }
                _builder_1.append("\t");
                CharSequence _newCompositeNode_1 = this.newCompositeNode(it);
                _builder_1.append(_newCompositeNode_1, "\t");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("}");
                _builder_1.newLine();
                String _localVar = this._grammarAccessExtensions.localVar(it);
                _builder_1.append(_localVar);
                _builder_1.append("=");
                String __ebnf2_1 = super._ebnf2(it, options, supportActions);
                _builder_1.append(__ebnf2_1);
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("{");
                _builder_1.newLine();
                _builder_1.append("\t");
                _builder_1.append("$current = $");
                String _localVar_1 = this._grammarAccessExtensions.localVar(it);
                _builder_1.append(_localVar_1, "\t");
                _builder_1.append(".current;");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("\t");
                _builder_1.append("afterParserOrEnumRuleCall();");
                _builder_1.newLine();
                _builder_1.append("}");
                _builder_1.newLine();
                _switchResult = _builder_1.toString();
            }
        }
        if (!_matched) {
            if (rule instanceof TerminalRule) {
                _matched = true;
                StringConcatenation _builder_1 = new StringConcatenation();
                String _localVar = this._grammarAccessExtensions.localVar(it);
                _builder_1.append(_localVar);
                _builder_1.append("=");
                String __ebnf2_1 = super._ebnf2(it, options, supportActions);
                _builder_1.append(__ebnf2_1);
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("{");
                _builder_1.newLine();
                _builder_1.append("\t");
                CharSequence _newLeafNode = this.newLeafNode(it, this._grammarAccessExtensions.localVar(it));
                _builder_1.append(_newLeafNode, "\t");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("}");
                _builder_1.newLine();
                _switchResult = _builder_1.toString();
            }
        }
        if (!_matched) {
            _switchResult = super._ebnf2(it, options, supportActions);
        }
        _xifexpression = _switchResult;
    }
    return _xifexpression;
}
Also used : EnumRule(org.eclipse.xtext.EnumRule) ParserRule(org.eclipse.xtext.ParserRule) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 40 with TerminalRule

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

the class AntlrGrammarGenerator method crossrefEbnf.

@Override
protected String crossrefEbnf(final AbstractRule it, final RuleCall call, final CrossReference ref, final boolean supportActions) {
    String _xifexpression = null;
    if (supportActions) {
        String _switchResult = null;
        boolean _matched = false;
        if (it instanceof EnumRule) {
            _matched = true;
        }
        if (!_matched) {
            if (it instanceof ParserRule) {
                _matched = true;
            }
        }
        if (_matched) {
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("{");
            _builder.newLine();
            _builder.append("\t");
            CharSequence _newCompositeNode = this.newCompositeNode(ref);
            _builder.append(_newCompositeNode, "\t");
            _builder.newLineIfNotEmpty();
            _builder.append("}");
            _builder.newLine();
            String _ruleName = this._grammarAccessExtensions.ruleName(it);
            _builder.append(_ruleName);
            String _argumentList = AntlrGrammarGenUtil.getArgumentList(call, this.isPassCurrentIntoFragment(), (!supportActions));
            _builder.append(_argumentList);
            _builder.newLineIfNotEmpty();
            _builder.append("{");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("afterParserOrEnumRuleCall();");
            _builder.newLine();
            _builder.append("}");
            _builder.newLine();
            _switchResult = _builder.toString();
        }
        if (!_matched) {
            if (it instanceof TerminalRule) {
                _matched = true;
                StringConcatenation _builder_1 = new StringConcatenation();
                String _localVar = this._grammarAccessExtensions.localVar(GrammarUtil.containingAssignment(ref));
                _builder_1.append(_localVar);
                _builder_1.append("=");
                String _ruleName_1 = this._grammarAccessExtensions.ruleName(it);
                _builder_1.append(_ruleName_1);
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("{");
                _builder_1.newLine();
                _builder_1.append("\t");
                CharSequence _newLeafNode = this.newLeafNode(ref, this._grammarAccessExtensions.localVar(GrammarUtil.containingAssignment(ref)));
                _builder_1.append(_newLeafNode, "\t");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("}");
                _builder_1.newLine();
                _switchResult = _builder_1.toString();
            }
        }
        if (!_matched) {
            throw new IllegalStateException(("crossrefEbnf is not supported for " + it));
        }
        _xifexpression = _switchResult;
    } else {
        _xifexpression = super.crossrefEbnf(it, call, ref, supportActions);
    }
    return _xifexpression;
}
Also used : EnumRule(org.eclipse.xtext.EnumRule) ParserRule(org.eclipse.xtext.ParserRule) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TerminalRule(org.eclipse.xtext.TerminalRule)

Aggregations

TerminalRule (org.eclipse.xtext.TerminalRule)51 ParserRule (org.eclipse.xtext.ParserRule)21 AbstractRule (org.eclipse.xtext.AbstractRule)20 RuleCall (org.eclipse.xtext.RuleCall)19 EnumRule (org.eclipse.xtext.EnumRule)17 Grammar (org.eclipse.xtext.Grammar)17 Test (org.junit.Test)16 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)12 AbstractElement (org.eclipse.xtext.AbstractElement)10 EObject (org.eclipse.emf.ecore.EObject)6 Action (org.eclipse.xtext.Action)6 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)5 Assignment (org.eclipse.xtext.Assignment)5 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 EClass (org.eclipse.emf.ecore.EClass)4 Alternatives (org.eclipse.xtext.Alternatives)4 Group (org.eclipse.xtext.Group)4 Keyword (org.eclipse.xtext.Keyword)4