protected void checkNoJavaStyleTypeCasting(INode node) { BidiTreeIterator<INode> iterator = node.getAsTreeIterable().reverse().iterator(); ILeafNode child = getFirstLeafNode(iterator); if (child != null && child.getGrammarElement() == grammarAccess.getXParenthesizedExpressionAccess().getRightParenthesisKeyword_2()) { INode expressionNode = getNode(iterator, grammarAccess.getXParenthesizedExpressionAccess().getXExpressionParserRuleCall_1()); EObject semanticObject = NodeModelUtils.findActualSemanticObjectFor(expressionNode); if (semanticObject instanceof XFeatureCall || semanticObject instanceof XMemberFeatureCall) { XAbstractFeatureCall featureCall = (XAbstractFeatureCall) semanticObject; if (featureCall.isTypeLiteral()) { ICompositeNode parenthesizedNode = child.getParent(); ITextRegion parenthesizedRegion = parenthesizedNode.getTextRegion(); addIssue("Use 'as' keyword for type casting.", featureCall, parenthesizedRegion.getOffset(), parenthesizedRegion.getLength(), JAVA_STYLE_TYPE_CAST); } } } }
/** * Returns true if the previous document state was completely broken, e.g. the parser did not recover at all. * This may happen e.g. in Xtend for documents like * <pre>import static class C {}</pre> * where the class keyword is consumed as an invalid token in the import declaration and everything thereafter * is unrecoverable. */ protected boolean isBrokenPreviousState(IParseResult previousParseResult, int offset) { if (previousParseResult.hasSyntaxErrors()) { BidiTreeIterator<AbstractNode> iterator = ((AbstractNode) previousParseResult.getRootNode()).basicIterator(); while(iterator.hasPrevious()) { AbstractNode previous = iterator.previous(); if (previous.getGrammarElement() == null) { return true; } if (previous instanceof ILeafNode && previous.getOffset() <= offset) { break; } } } return false; }
public void replaceAndTransferLookAhead(INode oldNode, INode newRootNode) { AbstractNode newNode = ((CompositeNode) newRootNode).basicGetFirstChild(); replaceWithoutChildren((AbstractNode) oldNode, newNode); if (oldNode instanceof ICompositeNode && newNode instanceof CompositeNode) { CompositeNode newCompositeNode = (CompositeNode) newNode; newCompositeNode.basicSetLookAhead(((ICompositeNode) oldNode).getLookAhead()); } ICompositeNode root = newNode.getRootNode(); BidiTreeIterator<AbstractNode> iterator = ((AbstractNode) root).basicIterator(); int offset = 0; while(iterator.hasNext()) { AbstractNode node = iterator.next(); if (node instanceof LeafNode) { ((LeafNode) node).basicSetTotalOffset(offset); offset += node.getTotalLength(); } } }
protected void assertSameStructure(ICompositeNode first, ICompositeNode second) { BidiTreeIterator<INode> firstIter = first.getAsTreeIterable().iterator(); BidiTreeIterator<INode> secondIter = second.getAsTreeIterable().iterator(); while(firstIter.hasNext()) { assertTrue(secondIter.hasNext()); INode firstNext = firstIter.next(); INode secondNext = secondIter.next(); assertEquals(firstNext.getGrammarElement(), secondNext.getGrammarElement()); assertEquals(firstNext.getClass(), secondNext.getClass()); assertEquals(firstNext.getTotalOffset(), secondNext.getTotalOffset()); assertEquals(firstNext.getTotalLength(), secondNext.getTotalLength()); assertEquals(firstNext.getText(), secondNext.getText()); } assertEquals(firstIter.hasNext(), secondIter.hasNext()); }
/** * Returns the last node that appears to be part of the prefix. This will be used to determine * the current model object that'll be the most special context instance in the proposal provider. */ public INode getLastCompleteNodeByOffset(INode node, int offset, int completionOffset) { BidiTreeIterator<INode> iterator = node.getRootNode().getAsTreeIterable().iterator(); INode result = node; while (iterator.hasNext()) { INode candidate = iterator.next(); if (candidate.getOffset() >= offset) { break; } else if ((candidate instanceof ILeafNode) && (candidate.getGrammarElement() == null || candidate.getGrammarElement() instanceof AbstractElement || candidate.getGrammarElement() instanceof ParserRule)) { result = candidate; } } return result; }
protected INode getLastCompleteNodeByOffset(INode node, int offsetPosition) { BidiTreeIterator<INode> iterator = node.getRootNode().getAsTreeIterable().iterator(); INode result = node; while (iterator.hasNext()) { INode candidate = iterator.next(); if (candidate.getOffset() >= offsetPosition ) { break; } else if ((candidate instanceof ILeafNode) && (candidate.getGrammarElement() == null || candidate.getGrammarElement() instanceof AbstractElement || candidate.getGrammarElement() instanceof ParserRule)) { result = candidate; } } return result; }
/** * Returns nodes which represent keywords and specified in keywords. * * @param keywords * keywords in natural order used in Arrays#s */ protected List<ILeafNode> filterLeafsWithKeywordInsteadOfComma(EObject semanticElement, String stopAtKeyword, ICompositeNode node, final String... keywords) { List<ILeafNode> filteredLeaves = null; 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; EObject grammarElement = leaf.getGrammarElement(); if (grammarElement instanceof Keyword) { String value = ((Keyword) grammarElement).getValue(); if (stopAtKeyword.equals(value)) { break; } if (Arrays.binarySearch(keywords, value) >= 0) { if (grammarElement.eContainer() instanceof Alternatives) { AbstractElement first = ((Alternatives) (grammarElement.eContainer())).getElements().get(0); boolean inCommaAlternative = (first instanceof Keyword && ",".equals(((Keyword) first) .getValue())); if (inCommaAlternative) { if (filteredLeaves == null) { filteredLeaves = new ArrayList<>(5); } filteredLeaves.add(leaf); } } } } } } return filteredLeaves == null ? Collections.emptyList() : filteredLeaves; }
protected INode getNode(BidiTreeIterator<INode> iterator, EObject... grammarElements) { while (iterator.hasNext()) { INode node = iterator.next(); EObject grammarElement = node.getGrammarElement(); for (EObject expectedGrammarElement : grammarElements) { if (grammarElement == expectedGrammarElement) { return node; } } } return null; }
protected ILeafNode getFirstLeafNode(BidiTreeIterator<INode> iterator) { while(iterator.hasNext()) { INode child = iterator.next(); if (isHidden(child)) { continue; } if (child instanceof ILeafNode) { return (ILeafNode) child; } } return null; }
private void assertEqual(final ICompositeNode fromScratch, final ICompositeNode reparsed) { BidiTreeIterator<INode> scratchIterator = fromScratch.getAsTreeIterable().iterator(); BidiTreeIterator<INode> reparsedIterator = reparsed.getAsTreeIterable().iterator(); while (scratchIterator.hasNext()) { { Assert.assertTrue(reparsedIterator.hasNext()); this.assertEqualNodes(scratchIterator.next(), reparsedIterator.next()); } } Assert.assertFalse(scratchIterator.hasNext()); Assert.assertFalse(reparsedIterator.hasNext()); }
protected INode getLastLeafNode() { BidiTreeIterator<INode> iter = currentNode.getAsTreeIterable().iterator(); while(iter.hasPrevious()) { INode previous = iter.previous(); if (previous instanceof ILeafNode) return previous; } return currentNode; }
private INode getLastChild(ICompositeNode parent) { BidiTreeIterator<? extends INode> iterator = parent.getAsTreeIterable().iterator(); while(iterator.hasPrevious()) { INode previous = iterator.previous(); if (previous instanceof ILeafNode) { return previous; } else if (previous instanceof ICompositeNode) { if (!((ICompositeNode) previous).hasChildren()) return previous; } } return parent; }
private INode getLastLeaf(ICompositeNode parent) { BidiTreeIterator<? extends INode> iterator = parent.getAsTreeIterable().iterator(); while(iterator.hasPrevious()) { INode previous = iterator.previous(); if (previous instanceof ILeafNode) { return previous; } } return null; }
protected EObject getSemanticChild(INode node) { EObject root = node.getSemanticElement(); if (root != null && root != this.semanticObject) return root; BidiTreeIterator<INode> iterator = node.getAsTreeIterable().iterator(); while (iterator.hasNext()) { EObject candidate = iterator.next().getSemanticElement(); if (candidate != null && candidate != this.semanticObject) return candidate; } return null; }
private void checkGrammarAssigned(ICompositeNode rootNode) { BidiTreeIterator<INode> iterator = rootNode.getAsTreeIterable().iterator(); while(iterator.hasNext()) { INode next = iterator.next(); if (next != rootNode) { assertNotNull(next.getGrammarElement()); } } }
protected INode getLastCompleteNodeByOffset(INode node, int offset) { BidiTreeIterator<INode> iterator = node.getRootNode().getAsTreeIterable().iterator(); INode result = node; ITextRegion candidateTextRegion = node.getTextRegion(); while (iterator.hasNext()) { INode candidate = iterator.next(); ITextRegion textRegion = candidate.getTextRegion(); if (textRegion.getOffset() >= offset && !(textRegion.getOffset() == offset && textRegion.getLength() == 0)) { if (!candidateTextRegion.equals(textRegion) && candidate instanceof ILeafNode && textRegion.getLength() + textRegion.getOffset() >= offset) { break; } } if ((candidate instanceof ILeafNode) && (candidate.getGrammarElement() == null || candidate.getGrammarElement() instanceof AbstractElement || candidate.getGrammarElement() instanceof ParserRule)) { if (textRegion.getLength() == 0) { if (candidateTextRegion.getOffset() + candidateTextRegion.getLength() < offset || candidateTextRegion.getLength() == 0 && candidateTextRegion.getOffset() <= offset) { result = candidate; candidateTextRegion = candidate.getTextRegion(); } } else { result = candidate; candidateTextRegion = candidate.getTextRegion(); } } } return result; }
private INode getLastChild(final ICompositeNode parent) { BidiTreeIterator<? extends INode> iterator = parent.getAsTreeIterable().iterator(); while (iterator.hasPrevious()) { INode previous = iterator.previous(); if (previous instanceof ILeafNode) { return previous; } else if (previous instanceof ICompositeNode) { if (!((ICompositeNode) previous).hasChildren()) { return previous; } } } return parent; }
private INode getLastLeaf(final ICompositeNode parent) { BidiTreeIterator<? extends INode> iterator = parent.getAsTreeIterable().iterator(); while (iterator.hasPrevious()) { INode previous = iterator.previous(); if (previous instanceof ILeafNode) { return previous; } } return null; }
@Override public void provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor) { INode root = resource.getParseResult().getRootNode(); BidiTreeIterator<INode> it = root.getAsTreeIterable().iterator(); while( it.hasNext() ) { INode node = it.next(); String nodeType=""; EObject semanticElement = node.getSemanticElement(); if( node instanceof CompositeNodeWithSemanticElement) { if(semanticElement instanceof Spec ) nodeType=SpecHighlightingConfiguration.SPEC; else if( semanticElement instanceof Scenario ) nodeType=SpecHighlightingConfiguration.SCENARIO; else if( semanticElement instanceof Tags) nodeType=SpecHighlightingConfiguration.TAGS; else if( semanticElement instanceof StaticParam ) nodeType=SpecHighlightingConfiguration.STATIC_PARAM; else if( semanticElement instanceof DynamicParam) nodeType=SpecHighlightingConfiguration.DYNAMIC_PARAM; else if( semanticElement instanceof Step ) nodeType=SpecHighlightingConfiguration.STEP; else nodeType=SpecHighlightingConfiguration.DEFAULT; } acceptor.addPosition(node.getOffset(), node.getLength(), nodeType); } }
/** * 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; }
public void provideHighlightingFor( XtextResource resource, IHighlightedPositionAcceptor acceptor ) { if ( resource == null || resource.getParseResult() == null ) return; INode root = resource.getParseResult().getRootNode(); BidiTreeIterator<INode> it = root.getAsTreeIterable().iterator(); while (it.hasNext()) { INode node = it.next(); if (node == null) continue; if (node instanceof HiddenLeafNode && node.getGrammarElement() instanceof TerminalRuleImpl) { TerminalRuleImpl ge = (TerminalRuleImpl) node.getGrammarElement(); if (ge.getName().equalsIgnoreCase("SL_COMMENT")) setStyle(acceptor, node, COMMENT_SINGLE); if (ge.getName().equalsIgnoreCase("ML_COMMENT")) setStyle(acceptor, node, COMMENT_MULTI); } else if (node.getGrammarElement() != null && node.getGrammarElement() instanceof Keyword) { Keyword k = (Keyword) node.getGrammarElement(); if (ALL_KEYWORDS.contains(k.getValue())) { setStyle( acceptor, node, KEYWORD); } else if (ALL_SYMBOLS.contains(k.getValue())) { setStyle( acceptor, node, SYMBOL); } else if (ALL_RESERVED_WORDS.contains(k.getValue())) { setStyle( acceptor, node, RESERVED_WORD); } } else if (node instanceof LeafNode && node.getSemanticElement() != null) { if (node.getSemanticElement() instanceof ClsDef) { ClsDef cls = (ClsDef) node.getSemanticElement(); if (cls.getInfrastructure() != null && ALL_PREDEFINED_PARAMS.contains(cls.getInfrastructure())) { setStyle( acceptor, node, PREDEFINED_PARAM); } } else if (node.getGrammarElement() != null) { if (node.getGrammarElement() instanceof RuleCall) { RuleCall rc = (RuleCall) node.getGrammarElement(); if (rc.getRule().getName().equals("STRING")) { setStyle( acceptor, node, STRING); } else if (rc.getRule().getName().equals("INT_LITERAL")) { setStyle( acceptor, node, NUMBER); } else { //System.out.println("" + node.getOffset() + " " + node.getLength() + node.toString() + " ## " + node.getGrammarElement().toString() + " ## " + node.getSemanticElement().toString() + " // " + rc.getRule().getName()); } } } } else if (node.getGrammarElement() != null && node.getSemanticElement() != null) { //System.out.println("" + node.getOffset() + " " + node.getLength() + node.toString() + " ## " + node.getGrammarElement().toString() + " ## " + node.getSemanticElement().toString()); } } }
@Override public BidiTreeIterator<T> iterator() { BidiTreeIterator<T> delegate = getDelegate().iterator(); return new ReversedBidiTreeIterator<T>(delegate); }
public ReversedBidiTreeIterator(BidiTreeIterator<T> delegate) { super(delegate); }
@Override protected BidiTreeIterator<T> getDelegate() { return (BidiTreeIterator<T>) super.getDelegate(); }
/** * Find the leaf node at the given offset. May return <code>null</code> if the given offset is not valid for the * node (sub-)tree. * * A node matches the <code>leafNodeOffset</code> if it fulfills the following condition: * <pre> * node.totalOffset <= leafNodeOffset && * node.totalEndOffset > leafNodeOffset * </pre> * * @param node the container node. May not be <code>null</code>. * @param leafNodeOffset the offset that is covered by the searched node. * @return the leaf node at the given offset or <code>null</code>. */ /* @Nullable */ public static ILeafNode findLeafNodeAtOffset(/* @NonNull */ INode node, int leafNodeOffset) { INode localNode = node; while(!(localNode instanceof AbstractNode)) { localNode = localNode.getParent(); } int offset = localNode.getTotalOffset(); int length = localNode.getTotalLength(); BidiTreeIterator<AbstractNode> iterator = ((AbstractNode) localNode).basicIterator(); if (leafNodeOffset > (offset + length) / 2) { while (iterator.hasPrevious()) { AbstractNode previous = iterator.previous(); int previousOffset = previous.getTotalOffset(); int previousLength = previous.getTotalLength(); if (!intersects(previousOffset, previousLength, leafNodeOffset)) { if (previousOffset + previousLength <= leafNodeOffset) { return null; } iterator.prune(); } else { if (previous instanceof ILeafNode) return (ILeafNode) previous; } } } else { while (iterator.hasNext()) { AbstractNode next = iterator.next(); int nextOffset = next.getTotalOffset(); int nextLength = next.getTotalLength(); if (!intersects(nextOffset, nextLength, leafNodeOffset)) { if (nextOffset > leafNodeOffset) { return null; } iterator.prune(); } else { if (next instanceof ILeafNode) return (ILeafNode) next; } } } return null; }
@Override public BidiTreeIterator<INode> iterator() { return new NodeTreeIterator(this); }
public BidiTreeIterator<AbstractNode> basicIterator() { return new BasicNodeTreeIterator(this); }
/** {@inheritDoc} */ @Override public BidiTreeIterator<INode> iterator() { return new LazyNodeTreeIterator(delegateNode, this); }