Search in sources :

Example 26 with Keyword

use of org.eclipse.xtext.Keyword 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 27 with Keyword

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

the class AbstractElementFinder method findKeywords.

public List<Keyword> findKeywords(String... keywords) {
    Set<String> kwds = new HashSet<String>(Arrays.asList(keywords));
    ArrayList<Keyword> r = new ArrayList<Keyword>();
    for (AbstractRule ar : getRules()) {
        TreeIterator<EObject> i = ar.eAllContents();
        while (i.hasNext()) {
            EObject o = i.next();
            if (o instanceof Keyword) {
                Keyword k = (Keyword) o;
                if (kwds.contains(k.getValue()))
                    r.add(k);
            }
        }
    }
    return r;
}
Also used : Keyword(org.eclipse.xtext.Keyword) EObject(org.eclipse.emf.ecore.EObject) ArrayList(java.util.ArrayList) AbstractRule(org.eclipse.xtext.AbstractRule) HashSet(java.util.HashSet)

Example 28 with Keyword

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

the class LazyLinkerTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    with(new AbstractModule() {

        @Override
        protected void configure() {
            bind(EPackage.Registry.class).toInstance(EPackage.Registry.INSTANCE);
            bind(IGrammarAccess.class).toInstance(new IGrammarAccess() {

                @Override
                public List<Pair<Keyword, Keyword>> findKeywordPairs(String leftKw, String rightKw) {
                    return Collections.emptyList();
                }

                @Override
                public List<Keyword> findKeywords(String... keywords) {
                    return Collections.emptyList();
                }

                @Override
                public List<RuleCall> findRuleCalls(AbstractRule... rules) {
                    return Collections.emptyList();
                }

                @Override
                public Grammar getGrammar() {
                    return XtextFactory.eINSTANCE.createGrammar();
                }
            });
        }
    });
    linker = get(LazyLinker.class);
    resourceSet = get(XtextResourceSet.class);
    resourceSet.setClasspathURIContext(getClass());
    lazyLinkingPackage = (EPackage) resourceSet.getResource(URI.createURI("classpath:/org/eclipse/xtext/linking/lazy/LazyLinking.ecore"), true).getContents().get(0);
}
Also used : IGrammarAccess(org.eclipse.xtext.IGrammarAccess) Keyword(org.eclipse.xtext.Keyword) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall) AbstractModule(com.google.inject.AbstractModule) EPackage(org.eclipse.emf.ecore.EPackage) Pair(org.eclipse.xtext.util.Pair)

Example 29 with Keyword

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

the class NodeModelTest method testKeywordInAlternative.

@Test
public void testKeywordInAlternative() throws Exception {
    with(SimpleExpressionsTestLanguageStandaloneSetup.class);
    EObject object = getModel("d / e");
    ICompositeNode root = NodeModelUtils.getNode(object).getRootNode();
    List<ILeafNode> nodes = Lists.newArrayList(root.getLeafNodes());
    assertTrue(nodes.get(2).getGrammarElement() instanceof Keyword);
}
Also used : Keyword(org.eclipse.xtext.Keyword) EObject(org.eclipse.emf.ecore.EObject) Test(org.junit.Test)

Example 30 with Keyword

use of org.eclipse.xtext.Keyword in project n4js by eclipse.

the class N4JSSyntaxValidator method doFindLeafWithKeyword.

/**
 * Returns the first keyword with the given value, or null if no such keyword is found.
 */
private ILeafNode doFindLeafWithKeyword(EObject semanticElement, String stopAtKeyword, ICompositeNode node, String keyWord, boolean commaAlternative, int hitNumber) {
    EObject grammarElement;
    int foundHits = 0;
    for (BidiTreeIterator<INode> iter = node.getAsTreeIterable().iterator(); iter.hasNext(); ) {
        INode child = iter.next();
        EObject childSemElement = child.getSemanticElement();
        if (child != node && childSemElement != null && childSemElement != semanticElement) {
            iter.prune();
        } else if (child instanceof ILeafNode) {
            ILeafNode leaf = (ILeafNode) child;
            grammarElement = leaf.getGrammarElement();
            if (grammarElement instanceof Keyword) {
                String value = ((Keyword) grammarElement).getValue();
                if (stopAtKeyword.equals(value)) {
                    return null;
                }
                if (keyWord.equals(value)) {
                    if (grammarElement.eContainer() instanceof Alternatives) {
                        AbstractElement first = ((Alternatives) (grammarElement.eContainer())).getElements().get(0);
                        boolean inCommaAlternative = (first instanceof Keyword && ",".equals(((Keyword) first).getValue()));
                        if (inCommaAlternative == commaAlternative) {
                            foundHits++;
                            if (foundHits >= hitNumber) {
                                return leaf;
                            }
                        }
                    } else {
                        if (!commaAlternative) {
                            foundHits++;
                            if (foundHits >= hitNumber) {
                                return leaf;
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) Keyword(org.eclipse.xtext.Keyword) AbstractElement(org.eclipse.xtext.AbstractElement) EObject(org.eclipse.emf.ecore.EObject) Alternatives(org.eclipse.xtext.Alternatives)

Aggregations

Keyword (org.eclipse.xtext.Keyword)67 EObject (org.eclipse.emf.ecore.EObject)15 RuleCall (org.eclipse.xtext.RuleCall)15 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)14 INode (org.eclipse.xtext.nodemodel.INode)13 AbstractElement (org.eclipse.xtext.AbstractElement)9 AbstractRule (org.eclipse.xtext.AbstractRule)9 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)9 Test (org.junit.Test)9 Grammar (org.eclipse.xtext.Grammar)8 ParserRule (org.eclipse.xtext.ParserRule)8 Action (org.eclipse.xtext.Action)7 CrossReference (org.eclipse.xtext.CrossReference)7 Assignment (org.eclipse.xtext.Assignment)6 EnumLiteralDeclaration (org.eclipse.xtext.EnumLiteralDeclaration)6 EnumRule (org.eclipse.xtext.EnumRule)6 Alternatives (org.eclipse.xtext.Alternatives)5 ArrayList (java.util.ArrayList)4 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)4 TerminalRule (org.eclipse.xtext.TerminalRule)4