Search in sources :

Example 66 with AbstractRule

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

the class XtextValidationTest method testBug_282852_03.

@Test
public void testBug_282852_03() throws Exception {
    Grammar base = XtextFactory.eINSTANCE.createGrammar();
    Grammar child = XtextFactory.eINSTANCE.createGrammar();
    child.getUsedGrammars().add(base);
    AbstractRule ruleFoo = XtextFactory.eINSTANCE.createParserRule();
    ruleFoo.setName("Foo");
    base.getRules().add(ruleFoo);
    AbstractRule subRuleFoo = XtextFactory.eINSTANCE.createParserRule();
    subRuleFoo.setName("Foo");
    child.getRules().add(subRuleFoo);
    XtextValidator validator = get(XtextValidator.class);
    validator.setMessageAcceptor(this);
    validator.checkRuleName(subRuleFoo);
    assertNull(lastMessage);
}
Also used : Grammar(org.eclipse.xtext.Grammar) AbstractRule(org.eclipse.xtext.AbstractRule) Test(org.junit.Test)

Example 67 with AbstractRule

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

the class AbstractParseTreeConstructor method assignNodesByMatching.

protected void assignNodesByMatching(Map<EObject, AbstractToken> eObject2Token, ICompositeNode rootNode, Map<ILeafNode, EObject> comments) throws IOException {
    NodeIterator contents = new NodeIterator(rootNode);
    while (contents.hasNext()) {
        INode containedNode = contents.next();
        AbstractRule rule = containedNode.getGrammarElement() instanceof AbstractRule ? (AbstractRule) containedNode.getGrammarElement() : null;
        if (hiddenTokenHelper.isWhitespace(rule))
            continue;
        else if (containedNode instanceof ILeafNode && hiddenTokenHelper.isComment(rule))
            assignComment((ILeafNode) containedNode, eObject2Token, comments);
        else if (tokenUtil.isToken(containedNode)) {
            Pair<List<ILeafNode>, List<ILeafNode>> leadingAndTrailingHiddenTokens = tokenUtil.getLeadingAndTrailingHiddenTokens(containedNode);
            for (ILeafNode leadingHiddenNode : leadingAndTrailingHiddenTokens.getFirst()) {
                if (tokenUtil.isCommentNode(leadingHiddenNode)) {
                    assignComment(leadingHiddenNode, eObject2Token, comments);
                }
            }
            assignTokenByMatcher(containedNode, eObject2Token);
            for (ILeafNode trailingHiddenNode : leadingAndTrailingHiddenTokens.getSecond()) {
                if (tokenUtil.isCommentNode(trailingHiddenNode)) {
                    assignComment(trailingHiddenNode, eObject2Token, comments);
                }
            }
            contents.prune();
            ICompositeNode parentNode = containedNode.getParent();
            while (parentNode != null && assignTokenDirect(parentNode, eObject2Token)) parentNode = parentNode.getParent();
            if (containedNode.getOffset() > rootNode.getEndOffset()) {
                break;
            }
        }
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) ArrayList(java.util.ArrayList) List(java.util.List) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 68 with AbstractRule

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

the class PsiAntlrGrammarGenerator method _dataTypeEbnf2.

@Override
protected String _dataTypeEbnf2(final RuleCall it, final boolean supportActions) {
    String _xifexpression = null;
    if (supportActions) {
        String _switchResult = null;
        AbstractRule _rule = it.getRule();
        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._dataTypeEbnf2(it, supportActions);
        }
        if (!_matched) {
            if (_rule instanceof EnumRule) {
                _matched = true;
            }
            if (!_matched) {
                if (_rule instanceof ParserRule) {
                    _matched = true;
                }
            }
            if (_matched) {
                StringConcatenation _builder = new StringConcatenation();
                _builder.append("{");
                _builder.newLine();
                _builder.append("\t");
                CharSequence _markComposite = this.markComposite(it);
                _builder.append(_markComposite, "\t");
                _builder.newLineIfNotEmpty();
                _builder.append("}");
                _builder.newLine();
                String __dataTypeEbnf2 = super._dataTypeEbnf2(it, supportActions);
                _builder.append(__dataTypeEbnf2);
                _builder.newLineIfNotEmpty();
                _builder.append("{");
                _builder.newLine();
                _builder.append("\t");
                CharSequence _doneComposite = this.doneComposite(it);
                _builder.append(_doneComposite, "\t");
                _builder.newLineIfNotEmpty();
                _builder.append("}");
                _builder.newLine();
                _switchResult = _builder.toString();
            }
        }
        if (!_matched) {
            if (_rule instanceof TerminalRule) {
                _matched = true;
                StringConcatenation _builder_1 = new StringConcatenation();
                _builder_1.append("{");
                _builder_1.newLine();
                _builder_1.append("\t");
                CharSequence _markLeaf = this.markLeaf(it);
                _builder_1.append(_markLeaf, "\t");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("}");
                _builder_1.newLine();
                String _localVar = this._grammarAccessExtensions.localVar(it);
                _builder_1.append(_localVar);
                _builder_1.append("=");
                String __dataTypeEbnf2_1 = super._dataTypeEbnf2(it, supportActions);
                _builder_1.append(__dataTypeEbnf2_1);
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("{");
                _builder_1.newLine();
                _builder_1.append("\t");
                CharSequence _doneLeaf = this.doneLeaf(it, this._grammarAccessExtensions.localVar(it));
                _builder_1.append(_doneLeaf, "\t");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("}");
                _builder_1.newLine();
                _switchResult = _builder_1.toString();
            }
        }
        if (!_matched) {
            _switchResult = super._dataTypeEbnf2(it, supportActions);
        }
        _xifexpression = _switchResult;
    } else {
        _xifexpression = super._dataTypeEbnf2(it, 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) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 69 with AbstractRule

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

the class PsiAntlrGrammarGenerator method _ebnf2.

@Override
protected String _ebnf2(final RuleCall it, final AntlrOptions options, final boolean supportActions) {
    String _xifexpression = null;
    if (supportActions) {
        String _switchResult = null;
        AbstractRule _rule = it.getRule();
        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.<AbstractRule>getOriginalElement(it.getRule()));
                    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 _markComposite = this.markComposite(it);
                _builder.append(_markComposite, "\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");
                CharSequence _doneComposite = this.doneComposite(it);
                _builder.append(_doneComposite, "\t");
                _builder.newLineIfNotEmpty();
                _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) {");
                        _builder_1.newLine();
                        _builder_1.append("\t");
                        _builder_1.append("\t");
                        CharSequence _associateWithSemanticElement = this.associateWithSemanticElement();
                        _builder_1.append(_associateWithSemanticElement, "\t\t");
                        _builder_1.newLineIfNotEmpty();
                        _builder_1.append("\t");
                        _builder_1.append("\t");
                        _builder_1.append("$current = true;");
                        _builder_1.newLine();
                        _builder_1.append("\t");
                        _builder_1.append("}");
                        _builder_1.newLine();
                    }
                }
                _builder_1.append("\t");
                CharSequence _markComposite_1 = this.markComposite(it);
                _builder_1.append(_markComposite_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");
                CharSequence _doneComposite_1 = this.doneComposite(it);
                _builder_1.append(_doneComposite_1, "\t");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("}");
                _builder_1.newLine();
                _switchResult = _builder_1.toString();
            }
        }
        if (!_matched) {
            if (_rule instanceof TerminalRule) {
                _matched = true;
                StringConcatenation _builder_1 = new StringConcatenation();
                _builder_1.append("{");
                _builder_1.newLine();
                _builder_1.append("\t");
                CharSequence _markLeaf = this.markLeaf(it);
                _builder_1.append(_markLeaf, "\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");
                CharSequence _doneLeaf = this.doneLeaf(it, this._grammarAccessExtensions.localVar(it));
                _builder_1.append(_doneLeaf, "\t");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("}");
                _builder_1.newLine();
                _switchResult = _builder_1.toString();
            }
        }
        if (!_matched) {
            _switchResult = super._ebnf2(it, options, supportActions);
        }
        _xifexpression = _switchResult;
    } else {
        _xifexpression = super._ebnf2(it, options, 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) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 70 with AbstractRule

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

the class PsiAntlrGrammarGenerator method _assignmentEbnf.

@Override
protected String _assignmentEbnf(final RuleCall it, final Assignment assignment, final AntlrOptions options, final boolean supportActions) {
    String _xifexpression = null;
    if (supportActions) {
        String _switchResult = null;
        AbstractRule _rule = it.getRule();
        boolean _matched = false;
        if (_rule instanceof EnumRule) {
            _matched = true;
        }
        if (!_matched) {
            if (_rule instanceof ParserRule) {
                _matched = true;
            }
        }
        if (_matched) {
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("{");
            _builder.newLine();
            _builder.append("\t");
            CharSequence _markComposite = this.markComposite(it);
            _builder.append(_markComposite, "\t");
            _builder.newLineIfNotEmpty();
            _builder.append("}");
            _builder.newLine();
            String _localVar = this._grammarAccessExtensions.localVar(assignment, it);
            _builder.append(_localVar);
            _builder.append("=");
            String __assignmentEbnf = super._assignmentEbnf(it, assignment, options, supportActions);
            _builder.append(__assignmentEbnf);
            _builder.newLineIfNotEmpty();
            _builder.append("{");
            _builder.newLine();
            _builder.append("\t");
            CharSequence _doneComposite = this.doneComposite(it);
            _builder.append(_doneComposite, "\t");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("if(!$current) {");
            _builder.newLine();
            _builder.append("\t\t");
            CharSequence _associateWithSemanticElement = this.associateWithSemanticElement();
            _builder.append(_associateWithSemanticElement, "\t\t");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("$current = true;");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("}");
            _builder.newLine();
            _switchResult = _builder.toString();
        }
        if (!_matched) {
            if (_rule instanceof TerminalRule) {
                _matched = true;
                StringConcatenation _builder_1 = new StringConcatenation();
                _builder_1.append("{");
                _builder_1.newLine();
                _builder_1.append("\t");
                CharSequence _markLeaf = this.markLeaf(it);
                _builder_1.append(_markLeaf, "\t");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("}");
                _builder_1.newLine();
                String _localVar_1 = this._grammarAccessExtensions.localVar(assignment, it);
                _builder_1.append(_localVar_1);
                _builder_1.append("=");
                String __assignmentEbnf_1 = super._assignmentEbnf(it, assignment, options, supportActions);
                _builder_1.append(__assignmentEbnf_1);
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("{");
                _builder_1.newLine();
                _builder_1.append("\t");
                _builder_1.append("if(!$current) {");
                _builder_1.newLine();
                _builder_1.append("\t\t");
                CharSequence _associateWithSemanticElement_1 = this.associateWithSemanticElement();
                _builder_1.append(_associateWithSemanticElement_1, "\t\t");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("\t\t");
                _builder_1.append("$current = true;");
                _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();
                _builder_1.append("\t");
                CharSequence _doneLeaf = this.doneLeaf(it, this._grammarAccessExtensions.localVar(assignment, it));
                _builder_1.append(_doneLeaf, "\t");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("}");
                _builder_1.newLine();
                _switchResult = _builder_1.toString();
            }
        }
        if (!_matched) {
            throw new IllegalStateException(("assignmentEbnf is not supported for " + it));
        }
        _xifexpression = _switchResult;
    } else {
        _xifexpression = super._assignmentEbnf(it, assignment, options, 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) 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