Search in sources :

Example 11 with TerminalRule

use of org.eclipse.xtext.TerminalRule 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 12 with TerminalRule

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

Example 13 with TerminalRule

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

the class AbstractAntlrGrammarGenerator method _assignmentEbnf.

protected String _assignmentEbnf(final RuleCall it, final Assignment assignment, final AntlrOptions options, final boolean supportActions) {
    String _switchResult = null;
    AbstractRule _rule = it.getRule();
    final AbstractRule rule = _rule;
    boolean _matched = false;
    if (rule instanceof EnumRule) {
        _matched = true;
    }
    if (!_matched) {
        if (rule instanceof ParserRule) {
            _matched = true;
        }
    }
    if (!_matched) {
        if (rule instanceof TerminalRule) {
            _matched = true;
        }
    }
    if (_matched) {
        _switchResult = this._grammarAccessExtensions.ruleName(rule);
    }
    if (!_matched) {
        throw new IllegalStateException(("assignmentEbnf is not supported for " + rule));
    }
    return _switchResult;
}
Also used : EnumRule(org.eclipse.xtext.EnumRule) ParserRule(org.eclipse.xtext.ParserRule) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 14 with TerminalRule

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

the class AbstractAntlrGrammarGenerator method compileTokens.

protected CharSequence compileTokens(final Grammar it, final AntlrOptions options) {
    StringConcatenation _builder = new StringConcatenation();
    {
        boolean _isBacktrackLexer = options.isBacktrackLexer();
        if (_isBacktrackLexer) {
            _builder.append("tokens {");
            _builder.newLine();
            {
                Set<String> _allKeywords = this.keywordHelper.getAllKeywords();
                for (final String kw : _allKeywords) {
                    _builder.append("\t");
                    String _ruleName = this.keywordHelper.getRuleName(kw);
                    _builder.append(_ruleName, "\t");
                    _builder.append(";");
                    _builder.newLineIfNotEmpty();
                }
            }
            {
                List<TerminalRule> _allTerminalRules = GrammarUtil.allTerminalRules(it);
                for (final TerminalRule rule : _allTerminalRules) {
                    _builder.append("\t");
                    String _ruleName_1 = this._grammarAccessExtensions.ruleName(rule);
                    _builder.append(_ruleName_1, "\t");
                    _builder.append(";");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append("}");
            _builder.newLine();
        }
    }
    return _builder;
}
Also used : StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TerminalRule(org.eclipse.xtext.TerminalRule)

Example 15 with TerminalRule

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

the class XtextValidationTest method testNegatedTokenNotEOF.

@Test
public void testNegatedTokenNotEOF() throws Exception {
    String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "A: foo=DUMMY;\n" + "terminal DUMMY: !EOF;";
    Grammar grammar = (Grammar) getModel(grammarAsText);
    XtextValidator validator = get(XtextValidator.class);
    ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, true, false);
    TerminalRule terminal = (TerminalRule) grammar.getRules().get(1);
    NegatedToken token = (NegatedToken) terminal.getAlternatives();
    messageAcceptor.expectedContext(token.getTerminal());
    configureValidator(validator, messageAcceptor, token);
    validator.checkNegatedTokenNotEOF(token);
    messageAcceptor.validate();
}
Also used : Grammar(org.eclipse.xtext.Grammar) TerminalRule(org.eclipse.xtext.TerminalRule) NegatedToken(org.eclipse.xtext.NegatedToken) Test(org.junit.Test)

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