Search in sources :

Example 41 with TerminalRule

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

the class AntlrGrammarGenerator 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 _newCompositeNode = this.newCompositeNode(it);
            _builder.append(_newCompositeNode, "\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");
            _builder.append("if ($current==null) {");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("$current = ");
            CharSequence _createModelElementForParent = this.createModelElementForParent(assignment);
            _builder.append(_createModelElementForParent, "\t\t");
            _builder.append(";");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t");
            String _setOrAdd = this._grammarAccessExtensions.setOrAdd(assignment);
            _builder.append(_setOrAdd, "\t");
            _builder.append("(");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("$current,");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("\"");
            String _feature = assignment.getFeature();
            _builder.append(_feature, "\t\t");
            _builder.append("\",");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            {
                boolean _isBooleanAssignment = GrammarUtil.isBooleanAssignment(assignment);
                if (_isBooleanAssignment) {
                    _builder.append("true");
                } else {
                    String _localVar_1 = this._grammarAccessExtensions.localVar(assignment, it);
                    _builder.append(_localVar_1, "\t\t");
                }
            }
            _builder.append(",");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            CharSequence _stringLiteral = this._grammarAccessExtensions.toStringLiteral(it);
            _builder.append(_stringLiteral, "\t\t");
            _builder.append(");");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("afterParserOrEnumRuleCall();");
            _builder.newLine();
            _builder.append("}");
            _builder.newLine();
            _switchResult = _builder.toString();
        }
        if (!_matched) {
            if (_rule instanceof TerminalRule) {
                _matched = true;
                StringConcatenation _builder_1 = new StringConcatenation();
                String _localVar_2 = this._grammarAccessExtensions.localVar(assignment, it);
                _builder_1.append(_localVar_2);
                _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");
                CharSequence _newLeafNode = this.newLeafNode(it, this._grammarAccessExtensions.localVar(assignment, it));
                _builder_1.append(_newLeafNode, "\t");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("}");
                _builder_1.newLine();
                _builder_1.append("{");
                _builder_1.newLine();
                _builder_1.append("\t");
                _builder_1.append("if ($current==null) {");
                _builder_1.newLine();
                _builder_1.append("\t\t");
                _builder_1.append("$current = ");
                CharSequence _createModelElement = this.createModelElement(assignment);
                _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");
                String _setOrAdd_1 = this._grammarAccessExtensions.setOrAdd(assignment);
                _builder_1.append(_setOrAdd_1, "\t");
                _builder_1.append("WithLastConsumed(");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("\t\t");
                _builder_1.append("$current,");
                _builder_1.newLine();
                _builder_1.append("\t\t");
                _builder_1.append("\"");
                String _feature_1 = assignment.getFeature();
                _builder_1.append(_feature_1, "\t\t");
                _builder_1.append("\",");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("\t\t");
                {
                    boolean _isBooleanAssignment_1 = GrammarUtil.isBooleanAssignment(assignment);
                    if (_isBooleanAssignment_1) {
                        _builder_1.append("true");
                    } else {
                        String _localVar_3 = this._grammarAccessExtensions.localVar(assignment, it);
                        _builder_1.append(_localVar_3, "\t\t");
                    }
                }
                _builder_1.append(",");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("\t\t");
                CharSequence _stringLiteral_1 = this._grammarAccessExtensions.toStringLiteral(it);
                _builder_1.append(_stringLiteral_1, "\t\t");
                _builder_1.append(");");
                _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 42 with TerminalRule

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

the class AntlrGrammarGenerator 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 _newCompositeNode = this.newCompositeNode(it);
                _builder.append(_newCompositeNode, "\t");
                _builder.newLineIfNotEmpty();
                _builder.append("}");
                _builder.newLine();
                String _localVar = this._grammarAccessExtensions.localVar(it);
                _builder.append(_localVar);
                _builder.append("=");
                String __dataTypeEbnf2 = super._dataTypeEbnf2(it, supportActions);
                _builder.append(__dataTypeEbnf2);
                String _argumentList = AntlrGrammarGenUtil.getArgumentList(it, this.isPassCurrentIntoFragment(), (!supportActions));
                _builder.append(_argumentList);
                _builder.newLineIfNotEmpty();
                _builder.append("{");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("$current.merge(");
                String _localVar_1 = this._grammarAccessExtensions.localVar(it);
                _builder.append(_localVar_1, "\t");
                _builder.append(");");
                _builder.newLineIfNotEmpty();
                _builder.append("}");
                _builder.newLine();
                _builder.append("{");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("afterParserOrEnumRuleCall();");
                _builder.newLine();
                _builder.append("}");
                _builder.newLine();
                _switchResult = _builder.toString();
            }
        }
        if (!_matched) {
            if (_rule instanceof TerminalRule) {
                _matched = true;
                StringConcatenation _builder_1 = new StringConcatenation();
                String _localVar_2 = this._grammarAccessExtensions.localVar(it);
                _builder_1.append(_localVar_2);
                _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");
                _builder_1.append("$current.merge(");
                String _localVar_3 = this._grammarAccessExtensions.localVar(it);
                _builder_1.append(_localVar_3, "\t");
                _builder_1.append(");");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("}");
                _builder_1.newLine();
                _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._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 43 with TerminalRule

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

the class SerializerFragment2 method genGetUnassignedRuleCallToken.

private StringConcatenationClient genGetUnassignedRuleCallToken(final AbstractRule rule) {
    if ((rule instanceof TerminalRule)) {
        if ((this.detectSyntheticTerminals && this.syntheticTerminalDetector.isSyntheticTerminalRule(((TerminalRule) rule)))) {
            StringConcatenationClient _client = new StringConcatenationClient() {

                @Override
                protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                    _builder.append("/**");
                    _builder.newLine();
                    _builder.append(" ");
                    _builder.append("* Synthetic terminal rule. The concrete syntax is to be specified by clients.");
                    _builder.newLine();
                    {
                        boolean _isGenerateStub = SerializerFragment2.this.isGenerateStub();
                        boolean _not = (!_isGenerateStub);
                        if (_not) {
                            _builder.append(" * Defaults to the empty string.");
                        }
                    }
                    _builder.newLineIfNotEmpty();
                    _builder.append(" ");
                    _builder.append("*/");
                    _builder.newLine();
                    _builder.append("protected ");
                    {
                        boolean _isGenerateStub_1 = SerializerFragment2.this.isGenerateStub();
                        if (_isGenerateStub_1) {
                            _builder.append("abstract ");
                        }
                    }
                    _builder.append("String ");
                    CharSequence _unassignedCalledTokenRuleName = SerializerFragment2.this.unassignedCalledTokenRuleName(rule);
                    _builder.append(_unassignedCalledTokenRuleName);
                    _builder.append("(");
                    _builder.append(EObject.class);
                    _builder.append(" semanticObject, ");
                    _builder.append(RuleCall.class);
                    _builder.append(" ruleCall, ");
                    _builder.append(INode.class);
                    _builder.append(" node)");
                    {
                        boolean _isGenerateStub_2 = SerializerFragment2.this.isGenerateStub();
                        if (_isGenerateStub_2) {
                            _builder.append(";");
                        } else {
                            _builder.append(" { return \"\"; }");
                        }
                    }
                    _builder.newLineIfNotEmpty();
                }
            };
            return _client;
        }
    }
    StringConcatenationClient _client_1 = new StringConcatenationClient() {

        @Override
        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
            _builder.append("/**");
            _builder.newLine();
            _builder.append(" ");
            _builder.append("* ");
            String _replace = SerializerFragment2.this.textWithoutComments(NodeModelUtils.getNode(rule)).trim().replace("\n", "\n* ").replace("*/", "*/");
            _builder.append(_replace, " ");
            _builder.newLineIfNotEmpty();
            _builder.append(" ");
            _builder.append("*/");
            _builder.newLine();
            _builder.append("protected String ");
            CharSequence _unassignedCalledTokenRuleName = SerializerFragment2.this.unassignedCalledTokenRuleName(rule);
            _builder.append(_unassignedCalledTokenRuleName);
            _builder.append("(");
            _builder.append(EObject.class);
            _builder.append(" semanticObject, ");
            _builder.append(RuleCall.class);
            _builder.append(" ruleCall, ");
            _builder.append(INode.class);
            _builder.append(" node) {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("if (node != null)");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("return getTokenText(node);");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("return \"");
            String _convertToJavaString = Strings.convertToJavaString(SerializerFragment2.this.defaultValue(rule.getAlternatives(), CollectionLiterals.<AbstractElement>newHashSet()));
            _builder.append(_convertToJavaString, "\t");
            _builder.append("\";");
            _builder.newLineIfNotEmpty();
            _builder.append("}");
            _builder.newLine();
        }
    };
    return _client_1;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) TerminalRule(org.eclipse.xtext.TerminalRule)

Example 44 with TerminalRule

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

the class XtextLinkerTest method testQualifiedRuleCall_03.

@Test
public void testQualifiedRuleCall_03() throws Exception {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("grammar test with org.eclipse.xtext.common.Terminals");
    _builder.newLine();
    _builder.append("generate test \'http://test\'");
    _builder.newLine();
    _builder.append("Rule: name=ID;");
    _builder.newLine();
    _builder.append("terminal STRING: super;");
    _builder.newLine();
    _builder.append("terminal super: \'super\';");
    _builder.newLine();
    final String grammarAsString = _builder.toString();
    final XtextResource resource = this.getResourceFromString(grammarAsString);
    EObject _get = resource.getContents().get(0);
    Grammar grammar = ((Grammar) _get);
    AbstractRule _get_1 = grammar.getRules().get(1);
    final TerminalRule string = ((TerminalRule) _get_1);
    AbstractElement _alternatives = string.getAlternatives();
    final RuleCall callToSuper = ((RuleCall) _alternatives);
    Assert.assertEquals(IterableExtensions.<AbstractRule>last(grammar.getRules()), callToSuper.getRule());
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtextResource(org.eclipse.xtext.resource.XtextResource) Grammar(org.eclipse.xtext.Grammar) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall) Test(org.junit.Test)

Example 45 with TerminalRule

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

the class XtextValidationTest method testCheckRuleCallInUnorderedGroup_04.

@Test
public void testCheckRuleCallInUnorderedGroup_04() throws Exception {
    XtextValidator validator = get(XtextValidator.class);
    UnorderedGroup unorderedGroup = XtextFactory.eINSTANCE.createUnorderedGroup();
    RuleCall ruleCall = XtextFactory.eINSTANCE.createRuleCall();
    TerminalRule terminalRule = XtextFactory.eINSTANCE.createTerminalRule();
    ruleCall.setRule(terminalRule);
    unorderedGroup.getElements().add(ruleCall);
    ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, false, false);
    validator.setMessageAcceptor(messageAcceptor);
    validator.checkRuleCallInUnorderedGroup(ruleCall);
    messageAcceptor.validate();
}
Also used : UnorderedGroup(org.eclipse.xtext.UnorderedGroup) TerminalRule(org.eclipse.xtext.TerminalRule) RuleCall(org.eclipse.xtext.RuleCall) 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