Java 类org.eclipse.xtext.nodemodel.BidiTreeIterator 实例源码

项目:xtext-extras    文件:XbaseValidator.java   
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);
            }
        }
    }
}
项目:xtext-extras    文件:TokenSequencePreservingPartialParsingHelper.java   
/**
 * 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;
}
项目:xtext-core    文件:NodeModelBuilder.java   
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();
        }
    }
}
项目:xtext-core    文件:AbstractPartialParserTest.java   
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());
}
项目:xtext-core    文件:CompletionPrefixProvider.java   
/**
 * 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;
}
项目:bts    文件:ParserBasedContentAssistContextFactory.java   
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;
}
项目:n4js    文件:N4JSSyntaxValidator.java   
/**
 * 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;
}
项目:xtext-extras    文件:XbaseValidator.java   
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;
}
项目:xtext-extras    文件:XbaseValidator.java   
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;
}
项目:xtext-extras    文件:Bug480686Test.java   
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());
}
项目:xtext-core    文件:AbstractInternalAntlrParser.java   
protected INode getLastLeafNode() {
    BidiTreeIterator<INode> iter = currentNode.getAsTreeIterable().iterator();
    while(iter.hasPrevious()) {
        INode previous = iter.previous();
        if (previous instanceof ILeafNode)
            return previous;
    }
    return currentNode;
}
项目:xtext-core    文件:PartialParsingHelper.java   
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;
}
项目:xtext-core    文件:PartialParsingHelper.java   
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;
}
项目:xtext-core    文件:SemanticNodeProvider.java   
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;
}
项目:xtext-core    文件:PartialParserTest.java   
private void checkGrammarAssigned(ICompositeNode rootNode) {
    BidiTreeIterator<INode> iterator = rootNode.getAsTreeIterable().iterator();
    while(iterator.hasNext()) {
        INode next = iterator.next();
        if (next != rootNode) {
            assertNotNull(next.getGrammarElement());
        }
    }
}
项目:xtext-core    文件:IndentationAwareCompletionPrefixProvider.java   
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;
}
项目:dsl-devkit    文件:FixedPartialParsingHelper.java   
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;
}
项目:dsl-devkit    文件:FixedPartialParsingHelper.java   
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;
}
项目:Gauge-Eclipse    文件:SpecHighlightingCalculator.java   
@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);
       }
}
项目:n4js    文件:N4JSSyntaxValidator.java   
/**
 * 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;
}
项目:jason-eclipse-plugin    文件:Mas2jHighlightingCalculator.java   
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());
        }
    }
}
项目:xtext-core    文件:ReversedBidiTreeIterable.java   
@Override
public BidiTreeIterator<T> iterator() {
    BidiTreeIterator<T> delegate = getDelegate().iterator(); 
    return new ReversedBidiTreeIterator<T>(delegate);
}
项目:xtext-core    文件:ReversedBidiTreeIterator.java   
public ReversedBidiTreeIterator(BidiTreeIterator<T> delegate) {
    super(delegate);
}
项目:xtext-core    文件:ReversedBidiTreeIterator.java   
@Override
protected BidiTreeIterator<T> getDelegate() {
    return (BidiTreeIterator<T>) super.getDelegate();
}
项目:xtext-core    文件:NodeModelUtils.java   
/**
 * 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;
}
项目:xtext-core    文件:AbstractNode.java   
@Override
public BidiTreeIterator<INode> iterator() {
    return new NodeTreeIterator(this);
}
项目:xtext-core    文件:AbstractNode.java   
public BidiTreeIterator<AbstractNode> basicIterator() {
    return new BasicNodeTreeIterator(this);
}
项目:xtext-core    文件:SyntheticCompositeNode.java   
@Override
public BidiTreeIterator<INode> iterator() {
    return new NodeTreeIterator(this);
}
项目:dsl-devkit    文件:LazyLoadingCompositeNode.java   
/** {@inheritDoc} */
@Override
public BidiTreeIterator<INode> iterator() {
  return new LazyNodeTreeIterator(delegateNode, this);
}