Search in sources :

Example 6 with TerminalRule

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

the class XtextLinkerTest method testQualifiedRuleCall_02.

@Test
public void testQualifiedRuleCall_02() 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();
    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(GrammarUtil.findRuleForName(IterableExtensions.<Grammar>head(grammar.getUsedGrammars()), "STRING"), 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 7 with TerminalRule

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

the class CommentTest method testSingleLineComment.

@Test
public void testSingleLineComment() throws Exception {
    String model = "// comment\n/*element foo;\nelement bar;*/";
    INode node = getRootNode(model);
    List<ILeafNode> leafNodes = Lists.newArrayList(node.getLeafNodes());
    assertEquals(2, leafNodes.size());
    assertTrue(leafNodes.get(0).getGrammarElement() instanceof TerminalRule);
    assertTrue(leafNodes.get(1).getGrammarElement() instanceof TerminalRule);
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) TerminalRule(org.eclipse.xtext.TerminalRule) Test(org.junit.Test)

Example 8 with TerminalRule

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

the class XtextValidationTest method testTerminalRuleNamingConventions.

@Test
public void testTerminalRuleNamingConventions() throws Exception {
    String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "A : someFeature=New_Line;\n" + "terminal New_Line : '\n';";
    Grammar grammar = (Grammar) getModel(grammarAsText);
    TerminalRule rule = (TerminalRule) grammar.getRules().get(1);
    XtextValidator validator = get(XtextValidator.class);
    ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(rule, false, true);
    configureValidator(validator, messageAcceptor, rule);
    validator.checkTerminalRuleNamingConventions(rule);
    messageAcceptor.validate();
}
Also used : Grammar(org.eclipse.xtext.Grammar) TerminalRule(org.eclipse.xtext.TerminalRule) Test(org.junit.Test)

Example 9 with TerminalRule

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

the class XtextValidationTest method testNegatedTokenNotEOF_2.

@Test
public void testNegatedTokenNotEOF_2() 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 | ID);";
    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(((Alternatives) token.getTerminal()).getElements().get(0));
    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) Alternatives(org.eclipse.xtext.Alternatives) Test(org.junit.Test)

Example 10 with TerminalRule

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

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