Search in sources :

Example 16 with TerminalRule

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

the class CommentOnEofBug_234135_Test method testCommentOnEof.

@Test
public void testCommentOnEof() throws Exception {
    with(ReferenceGrammarTestLanguageStandaloneSetup.class);
    String model = "//comment";
    List<ILeafNode> leafNodes = Lists.newArrayList(getRootNode(model).getLeafNodes());
    assertEquals(1, leafNodes.size());
    EObject grammarElement = leafNodes.get(0).getGrammarElement();
    assertNotNull(grammarElement);
    assertTrue(grammarElement instanceof TerminalRule);
}
Also used : ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) EObject(org.eclipse.emf.ecore.EObject) TerminalRule(org.eclipse.xtext.TerminalRule) Test(org.junit.Test)

Example 17 with TerminalRule

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

the class SyntacticSequencerPDAProvider method getType.

protected SynStateType getType(ISerState state) {
    switch(state.getType()) {
        case ELEMENT:
            AbstractElement ele = state.getGrammarElement();
            Assignment ass;
            if (ele instanceof Action) {
                if (((Action) ele).getFeature() == null)
                    return SynStateType.UNASSIGEND_ACTION_CALL;
                else
                    return SynStateType.ASSIGNED_ACTION_CALL;
            } else if (GrammarUtil.containingCrossReference(ele) != null) {
                if (ele instanceof RuleCall) {
                    RuleCall rc = (RuleCall) ele;
                    if (rc.getRule() instanceof ParserRule)
                        return SynStateType.ASSIGNED_CROSSREF_DATATYPE_RULE_CALL;
                    if (rc.getRule() instanceof TerminalRule)
                        return SynStateType.ASSIGNED_CROSSREF_TERMINAL_RULE_CALL;
                    if (rc.getRule() instanceof EnumRule)
                        return SynStateType.ASSIGNED_CROSSREF_ENUM_RULE_CALL;
                } else if (ele instanceof Keyword)
                    return SynStateType.ASSIGNED_CROSSREF_KEYWORD;
            } else if ((ass = GrammarUtil.containingAssignment(ele)) != null) {
                if (ele instanceof RuleCall) {
                    RuleCall rc = (RuleCall) ele;
                    if (rc.getRule() instanceof ParserRule) {
                        if (rc.getRule().getType().getClassifier() instanceof EClass)
                            return SynStateType.ASSIGNED_PARSER_RULE_CALL;
                        return SynStateType.ASSIGNED_DATATYPE_RULE_CALL;
                    }
                    if (rc.getRule() instanceof TerminalRule)
                        return SynStateType.ASSIGNED_TERMINAL_RULE_CALL;
                    if (rc.getRule() instanceof EnumRule)
                        return SynStateType.ASSIGNED_ENUM_RULE_CALL;
                } else if (ele instanceof Keyword) {
                    if (GrammarUtil.isBooleanAssignment(ass))
                        return SynStateType.ASSIGNED_BOOLEAN_KEYWORD;
                    else
                        return SynStateType.ASSIGNED_KEYWORD;
                }
            } else {
                if (ele instanceof RuleCall) {
                    RuleCall rc = (RuleCall) ele;
                    if (rc.getRule() instanceof ParserRule)
                        return SynStateType.UNASSIGNED_DATATYPE_RULE_CALL;
                    if (rc.getRule() instanceof TerminalRule)
                        return SynStateType.UNASSIGNED_TERMINAL_RULE_CALL;
                } else if (ele instanceof Keyword)
                    return SynStateType.UNASSIGEND_KEYWORD;
            }
            break;
        case PUSH:
            return SynStateType.UNASSIGNED_PARSER_RULE_ENTER;
        case POP:
            return SynStateType.UNASSIGNED_PARSER_RULE_EXIT;
        case START:
            return SynStateType.START;
        case STOP:
            return SynStateType.STOP;
    }
    throw new RuntimeException("no type found for " + state);
}
Also used : Assignment(org.eclipse.xtext.Assignment) ParserRule(org.eclipse.xtext.ParserRule) EnumRule(org.eclipse.xtext.EnumRule) Action(org.eclipse.xtext.Action) EClass(org.eclipse.emf.ecore.EClass) AbstractElement(org.eclipse.xtext.AbstractElement) Keyword(org.eclipse.xtext.Keyword) TerminalRule(org.eclipse.xtext.TerminalRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 18 with TerminalRule

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

the class AbstractSyntacticSequencer method acceptNode.

protected void acceptNode(INode node) {
    Object ge = node.getGrammarElement();
    if (ge instanceof Keyword)
        acceptUnassignedKeyword((Keyword) ge, node.getText(), (ILeafNode) node);
    else if (ge instanceof RuleCall) {
        RuleCall rc = (RuleCall) ge;
        if (rc.getRule() instanceof TerminalRule)
            acceptUnassignedTerminal(rc, node.getText(), (ILeafNode) node);
        else if (rc.getRule() instanceof ParserRule) {
            StringBuilder text = new StringBuilder();
            for (ILeafNode leaf : node.getLeafNodes()) if (text.length() > 0 || !leaf.isHidden())
                text.append(leaf.getText());
            acceptUnassignedDatatype(rc, text.toString(), (ICompositeNode) node);
        } else if (rc.getRule() instanceof EnumRule)
            acceptUnassignedEnum(rc, node.getText(), (ICompositeNode) node);
    } else if (ge instanceof Action)
        acceptUnassignedAction((Action) ge);
    else
        throw new RuntimeException("Unexpected grammar element: " + node.getGrammarElement());
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) EnumRule(org.eclipse.xtext.EnumRule) Action(org.eclipse.xtext.Action) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) Keyword(org.eclipse.xtext.Keyword) EObject(org.eclipse.emf.ecore.EObject) TerminalRule(org.eclipse.xtext.TerminalRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 19 with TerminalRule

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

the class NodeModelSemanticSequencer method acceptSemantic.

protected boolean acceptSemantic(EObject semanticObject, AbstractElement ele, Object value, INode node) {
    Assignment ass = GrammarUtil.containingAssignment(ele);
    int index = -2;
    if (ass != null) {
        EStructuralFeature feat = semanticObject.eClass().getEStructuralFeature(ass.getFeature());
        if (feat != null) {
            if (feat.isMany())
                index = ((List<?>) semanticObject.eGet(feat)).indexOf(value);
            else
                index = -1;
        }
    }
    String token = node.getText().trim();
    if (ele instanceof Action) {
        if (((Action) ele).getFeature() != null) {
            if (sequenceAcceptor.enterAssignedAction((Action) ele, (EObject) value, (ICompositeNode) node)) {
                createSequence(ele, (EObject) value);
                sequenceAcceptor.leaveAssignedAction((Action) ele, (EObject) value);
            }
            return true;
        }
    } else if (GrammarUtil.containingCrossReference(ele) != null) {
        if (ele instanceof RuleCall) {
            RuleCall rc = (RuleCall) ele;
            if (rc.getRule() instanceof ParserRule) {
                sequenceAcceptor.acceptAssignedCrossRefDatatype(rc, token, (EObject) value, index, (ICompositeNode) node);
                return true;
            }
            if (rc.getRule() instanceof TerminalRule) {
                sequenceAcceptor.acceptAssignedCrossRefTerminal(rc, token, (EObject) value, index, (ILeafNode) node);
                return true;
            }
            if (rc.getRule() instanceof EnumRule) {
                sequenceAcceptor.acceptAssignedCrossRefEnum(rc, token, (EObject) value, index, (ICompositeNode) node);
                return true;
            }
        // } else if (ele instanceof Keyword) {
        // acceptor.acceptAssignedCrossRefKeyword((Keyword) ele, token, (EObject) value, index,(ILeafNode) node);
        // return true;
        }
    } else if (ass != null) {
        if (ele instanceof RuleCall) {
            RuleCall rc = (RuleCall) ele;
            if (rc.getRule() instanceof ParserRule) {
                if (rc.getRule().getType().getClassifier() instanceof EClass) {
                    if (sequenceAcceptor.enterAssignedParserRuleCall(rc, (EObject) value, (ICompositeNode) node)) {
                        createSequence(rc.getRule(), (EObject) value);
                        sequenceAcceptor.leaveAssignedParserRuleCall(rc, (EObject) value);
                    }
                } else
                    sequenceAcceptor.acceptAssignedDatatype(rc, token, value, index, (ICompositeNode) node);
                return true;
            }
            if (rc.getRule() instanceof TerminalRule) {
                sequenceAcceptor.acceptAssignedTerminal(rc, token, value, index, (ILeafNode) node);
                return true;
            }
            if (rc.getRule() instanceof EnumRule) {
                sequenceAcceptor.acceptAssignedEnum(rc, token, value, index, (ICompositeNode) node);
                return true;
            }
        } else if (ele instanceof Keyword) {
            if (GrammarUtil.isBooleanAssignment(ass))
                sequenceAcceptor.acceptAssignedKeyword((Keyword) ele, token, true, index, (ILeafNode) node);
            else
                sequenceAcceptor.acceptAssignedKeyword((Keyword) ele, token, (String) value, index, (ILeafNode) node);
            return true;
        }
    }
    return false;
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Action(org.eclipse.xtext.Action) Keyword(org.eclipse.xtext.Keyword) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) RuleCall(org.eclipse.xtext.RuleCall) Assignment(org.eclipse.xtext.Assignment) EnumRule(org.eclipse.xtext.EnumRule) EClass(org.eclipse.emf.ecore.EClass) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) List(java.util.List) TerminalRule(org.eclipse.xtext.TerminalRule)

Example 20 with TerminalRule

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

the class XtextAntlrGeneratorFragment2 method generateContentAssistTokenSource.

public JavaFileAccess generateContentAssistTokenSource() {
    JavaFileAccess _xblockexpression = null;
    {
        @Extension final ContentAssistGrammarNaming naming = this.contentAssistNaming;
        final JavaFileAccess file = this.fileFactory.createJavaFile(naming.getTokenSourceClass(this.getGrammar()));
        final Function1<TerminalRule, Boolean> _function = (TerminalRule it) -> {
            return Boolean.valueOf(Collections.<String>unmodifiableSet(CollectionLiterals.<String>newHashSet("BEGIN", "INDENT", "OPEN")).contains(it.getName().toUpperCase()));
        };
        final Iterable<TerminalRule> open = IterableExtensions.<TerminalRule>filter(GrammarUtil.allTerminalRules(this.getGrammar()), _function);
        final Function1<TerminalRule, Boolean> _function_1 = (TerminalRule it) -> {
            return Boolean.valueOf(Collections.<String>unmodifiableSet(CollectionLiterals.<String>newHashSet("END", "DEDENT", "CLOSE")).contains(it.getName().toUpperCase()));
        };
        final Iterable<TerminalRule> close = IterableExtensions.<TerminalRule>filter(GrammarUtil.allTerminalRules(this.getGrammar()), _function_1);
        StringConcatenationClient _client = new StringConcatenationClient() {

            @Override
            protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                _builder.append("public class ");
                String _simpleName = naming.getTokenSourceClass(XtextAntlrGeneratorFragment2.this.getGrammar()).getSimpleName();
                _builder.append(_simpleName);
                _builder.append(" extends ");
                _builder.append(AbstractIndentationTokenSource.class);
                _builder.append(" {");
                _builder.newLineIfNotEmpty();
                _builder.newLine();
                _builder.append("\t");
                _builder.append("public ");
                String _simpleName_1 = naming.getTokenSourceClass(XtextAntlrGeneratorFragment2.this.getGrammar()).getSimpleName();
                _builder.append(_simpleName_1, "\t");
                _builder.append("(");
                _builder.append(TokenSource.class, "\t");
                _builder.append(" delegate) {");
                _builder.newLineIfNotEmpty();
                _builder.append("\t\t");
                _builder.append("super(delegate);");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("}");
                _builder.newLine();
                _builder.newLine();
                _builder.append("\t");
                _builder.append("@Override");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("protected boolean shouldSplitTokenImpl(");
                _builder.append(Token.class, "\t");
                _builder.append(" token) {");
                _builder.newLineIfNotEmpty();
                {
                    final Function1<TerminalRule, TerminalRule> _function = (TerminalRule it) -> {
                        return AntlrGrammarGenUtil.<TerminalRule>getOriginalElement(it);
                    };
                    final Function1<TerminalRule, Boolean> _function_1 = (TerminalRule it) -> {
                        String _upperCase = it.getName().toUpperCase();
                        return Boolean.valueOf(Objects.equal(_upperCase, "WS"));
                    };
                    boolean _exists = IterableExtensions.<TerminalRule>exists(ListExtensions.<TerminalRule, TerminalRule>map(GrammarUtil.allTerminalRules(XtextAntlrGeneratorFragment2.this.getGrammar()), _function), _function_1);
                    if (_exists) {
                        _builder.append("\t\t");
                        _builder.append("// TODO Review assumption");
                        _builder.newLine();
                        _builder.append("\t\t");
                        _builder.append("return token.getType() == ");
                        TypeReference _internalParserClass = naming.getInternalParserClass(XtextAntlrGeneratorFragment2.this.getGrammar());
                        _builder.append(_internalParserClass, "\t\t");
                        _builder.append(".RULE_WS;");
                        _builder.newLineIfNotEmpty();
                    } else {
                        _builder.append("\t\t");
                        _builder.append("// TODO Implement me");
                        _builder.newLine();
                        _builder.append("\t\t");
                        _builder.append("throw new UnsupportedOperationException(\"Implement me\");");
                        _builder.newLine();
                    }
                }
                _builder.append("\t");
                _builder.append("}");
                _builder.newLine();
                _builder.newLine();
                _builder.append("\t");
                _builder.append("@Override");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("protected int getBeginTokenType() {");
                _builder.newLine();
                {
                    int _size = IterableExtensions.size(open);
                    boolean _equals = (_size == 1);
                    if (_equals) {
                        _builder.append("\t\t");
                        _builder.append("// TODO Review assumption");
                        _builder.newLine();
                        _builder.append("\t\t");
                        _builder.append("return ");
                        TypeReference _internalParserClass_1 = naming.getInternalParserClass(XtextAntlrGeneratorFragment2.this.getGrammar());
                        _builder.append(_internalParserClass_1, "\t\t");
                        _builder.append(".");
                        String _ruleName = XtextAntlrGeneratorFragment2.this.grammarUtil.ruleName(IterableExtensions.<TerminalRule>head(open));
                        _builder.append(_ruleName, "\t\t");
                        _builder.append(";");
                        _builder.newLineIfNotEmpty();
                    } else {
                        _builder.append("\t\t");
                        _builder.append("// TODO Implement me");
                        _builder.newLine();
                        _builder.append("\t\t");
                        _builder.append("throw new UnsupportedOperationException(\"Implement me\");");
                        _builder.newLine();
                    }
                }
                _builder.append("\t");
                _builder.append("}");
                _builder.newLine();
                _builder.newLine();
                _builder.append("\t");
                _builder.append("@Override");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("protected int getEndTokenType() {");
                _builder.newLine();
                {
                    int _size_1 = IterableExtensions.size(close);
                    boolean _equals_1 = (_size_1 == 1);
                    if (_equals_1) {
                        _builder.append("\t\t");
                        _builder.append("// TODO Review assumption");
                        _builder.newLine();
                        _builder.append("\t\t");
                        _builder.append("return ");
                        TypeReference _internalParserClass_2 = naming.getInternalParserClass(XtextAntlrGeneratorFragment2.this.getGrammar());
                        _builder.append(_internalParserClass_2, "\t\t");
                        _builder.append(".");
                        String _ruleName_1 = XtextAntlrGeneratorFragment2.this.grammarUtil.ruleName(IterableExtensions.<TerminalRule>head(close));
                        _builder.append(_ruleName_1, "\t\t");
                        _builder.append(";");
                        _builder.newLineIfNotEmpty();
                    } else {
                        _builder.append("\t\t");
                        _builder.append("// TODO Implement me");
                        _builder.newLine();
                        _builder.append("\t\t");
                        _builder.append("throw new UnsupportedOperationException(\"Implement me\");");
                        _builder.newLine();
                    }
                }
                _builder.append("\t");
                _builder.append("}");
                _builder.newLine();
                _builder.newLine();
                _builder.append("\t");
                _builder.append("@Override");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("protected boolean shouldEmitPendingEndTokens() {");
                _builder.newLine();
                _builder.append("\t\t");
                _builder.append("return false;");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("}");
                _builder.newLine();
                _builder.append("}");
                _builder.newLine();
            }
        };
        file.setContent(_client);
        _xblockexpression = file;
    }
    return _xblockexpression;
}
Also used : TokenSource(org.antlr.runtime.TokenSource) AbstractIndentationTokenSource(org.eclipse.xtext.parser.antlr.AbstractIndentationTokenSource) AbstractIndentationTokenSource(org.eclipse.xtext.parser.antlr.AbstractIndentationTokenSource) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) ContentAssistGrammarNaming(org.eclipse.xtext.xtext.generator.parser.antlr.ContentAssistGrammarNaming) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) Token(org.antlr.runtime.Token) JavaFileAccess(org.eclipse.xtext.xtext.generator.model.JavaFileAccess) GeneratedJavaFileAccess(org.eclipse.xtext.xtext.generator.model.GeneratedJavaFileAccess) TypeReference(org.eclipse.xtext.xtext.generator.model.TypeReference) 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