Java Code Examples for org.eclipse.xtext.nodemodel.ICompositeNode#getLookAhead()

The following examples show how to use org.eclipse.xtext.nodemodel.ICompositeNode#getLookAhead() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ParamAwareEntryPointFinder.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected ICompositeNode findEntryPoint(ICompositeNode node, int offset) {
	ICompositeNode result = node;
	result = getApplicableNode(result);
	while (result != null) {
		int remainingLookAhead = result.getLookAhead();
		Iterator<ILeafNode> leafNodes = result.getLeafNodes().iterator();
		while (leafNodes.hasNext()) {
			ILeafNode leaf = leafNodes.next();
			if (leaf.getTotalOffset() >= offset) {
				break;
			}
			if (!leaf.isHidden()) {
				if (remainingLookAhead > 0) {
					remainingLookAhead--;
				}
				if (remainingLookAhead == 0) {
					if (!shouldUseParent(result, offset, leaf)) {
						return result;
					} else {
						break;
					}
				}
			}
		}
		result = getApplicableNode(result.getParent());
	}
	return result;
}
 
Example 2
Source File: EntryPointFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected ICompositeNode findEntryPoint(ICompositeNode node, int offset) {
	ICompositeNode result = node;
	result = getApplicableNode(result);
	while (result != null) {
		int remainingLookAhead = result.getLookAhead();
		if (remainingLookAhead != 0) {
			Iterator<ILeafNode> leafNodes = result.getLeafNodes().iterator();
			while (leafNodes.hasNext() && remainingLookAhead > 0) {
				ILeafNode leaf = leafNodes.next();
				if (leaf.getTotalOffset() >= offset) {
					break;
				}
				if (!leaf.isHidden()) {
					if (remainingLookAhead > 0) {
						remainingLookAhead--;
					}
					if (remainingLookAhead == 0) {
						if (shouldUseParent(result, offset, leaf)) {
							ICompositeNode parent = result.getParent();
							return parent;
						}
						return result;
					}
				}
			}
		}
		result = getApplicableNode(result.getParent());
	}
	return result;
}
 
Example 3
Source File: EntryPointFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected ICompositeNode normalizeToParent(ICompositeNode result) {
	if (result == null) {
		return result;
	}
	ICompositeNode parent = result.getParent();
	while (parent != null && parent.getFirstChild().equals(result)
			&& parent.getLookAhead() == result.getLookAhead()) {
		result = parent;
		parent = result.getParent();
	}
	return result;
}
 
Example 4
Source File: NodeModelTokenSource.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Filter the nodes from the iterator that do not have any impact on the parse result.
 *
 * For now we filter mostly regions that do have lookahead 1 and are closed before the requested region starts.
 */
private Iterator<INode> filterIterator(TreeIterator<AbstractNode> iterator) {
	return new AbstractIterator<>() {
		@Override
		protected INode computeNext() {
			if (iterator.hasNext()) {
				INode result = iterator.next();
				if (result instanceof ICompositeNode) {
					ICompositeNode casted = (ICompositeNode) result;
					if (casted.getTotalEndOffset() < endOffset - 1) {
						if (casted.hasChildren() && casted.getLookAhead() == 1) {
							AbstractElement grammarElement = (AbstractElement) casted.getGrammarElement();
							// Filter script elements and member declarations to the left of the cursor position.
							if (grammarElement == scriptElementCall || grammarElement == memberDeclarationCall) {
								INode sibling = casted.getNextSibling();
								while (sibling instanceof ILeafNode) {
									ILeafNode siblingLeaf = (ILeafNode) sibling;
									if (siblingLeaf.isHidden()) {
										if (siblingLeaf.getTotalEndOffset() >= endOffset) {
											return result;
										}
									} else {
										break;
									}
									sibling = siblingLeaf.getNextSibling();
								}
								iterator.prune();

								// filter statements that are completed before the cursor position and are not
								// part of the lookahead
							} else if (grammarElement == statementsCall) {
								// check if this is in the parents lookAhead to disambiguate block from object
								// literal
								ICompositeNode parent = casted.getParent();
								if (parent.getLookAhead() > 1) {
									ILeafNode firstLeaf = Iterables.get(casted.getLeafNodes(), 0);
									int remainingLA = parent.getLookAhead();
									Iterator<ILeafNode> parentLeafs = parent.getLeafNodes().iterator();
									while (parentLeafs.hasNext() && remainingLA > 0) {
										ILeafNode leafNode = parentLeafs.next();
										if (leafNode == firstLeaf) {
											break;
										}
										if (!leafNode.isHidden()) {
											remainingLA--;
											if (remainingLA == 0) {
												iterator.prune();
											}
										}
									}
								}

								// Reduce the size of object literals.
							} else if (grammarElement == propertyAssignmentCall1
									|| grammarElement == propertyAssignmentCall2) {
								iterator.prune();
								Iterator<ILeafNode> localLeafs = casted.getLeafNodes().iterator();
								while (localLeafs.hasNext()) {
									ILeafNode leaf = localLeafs.next();
									if (!leaf.isHidden()) {
										return leaf;
									}
								}
							}
						}
					}
				}
				return result;
			}
			return endOfData();
		}
	};

}
 
Example 5
Source File: PartialParsingHelper.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Investigates the composite nodes containing the changed region and collects a list of nodes which could possibly
 * replaced by a partial parse. Such a node has a parent that consumes all his current lookahead tokens and all of
 * these tokens are located before the changed region.
 */
private List<ICompositeNode> internalFindValidReplaceRootNodeForChangeRegion(
		List<ICompositeNode> nodesEnclosingRegion, Range range) {
	List<ICompositeNode> result = new ArrayList<ICompositeNode>();
	boolean mustSkipNext = false;
	ICompositeNode previous = null;
	/*
	 * set to 'true' as soon as the lookahead of an enclosing
	 * exceeds the given range
	 */
	boolean done = false;  
	for (int i = 0; i < nodesEnclosingRegion.size() && !done; i++) {
		ICompositeNode node = nodesEnclosingRegion.get(i);
		if (node.getGrammarElement() != null) {
			if (!mustSkipNext) {
				boolean process = true;
				if (previous != null && !node.hasNextSibling()) {
					if (previous.getLookAhead() == node.getLookAhead() && previous.getLookAhead() == 0) {
						process = false;
					}
				}
				EObject semanticElement = NodeModelUtils.findActualSemanticObjectFor(node);
				if (semanticElement != null) {
					ICompositeNode actualNode = NodeModelUtils.findActualNodeFor(semanticElement);
					if (actualNode != null && (actualNode.getTotalOffset() < node.getTotalOffset() || actualNode.getTotalEndOffset() > node.getTotalEndOffset()))
						process = false;
				}
				if (process) {
					int remainingLookAhead = node.getLookAhead();
					if (remainingLookAhead != 0) {
						Iterator<ILeafNode> iterator = node.getLeafNodes().iterator();
						while(iterator.hasNext() && remainingLookAhead > 0) {
							ILeafNode leaf = iterator.next();
							if (!leaf.isHidden()) {
								if (remainingLookAhead > 0)
									remainingLookAhead--;
								if (remainingLookAhead == 0) {
									if (leaf.getTotalEndOffset() <= range.getOffset()) {
										result.add(node);
										previous = node;
										if (isActionNode(node)) {
											mustSkipNext = true;
										}
										break;
									} else {
										// lookahead ends left of the range, don't dive into child nodes
										done = true;
									}
								}
							}
						}
						if (remainingLookAhead != 0) {
							done = true;
						}
					} else {
						result.add(node);
						previous = node;
						if (isActionNode(node)) {
							mustSkipNext = true;
						}
					}
				}
			} else { // !mustSkipNext
				mustSkipNext = isActionNode(node);
			}
		}
	}
	return result;
}
 
Example 6
Source File: FixedPartialParsingHelper.java    From dsl-devkit with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Investigates the composite nodes containing the changed region and collects a list of nodes which could possibly
 * replaced by a partial parse. Such a node has a parent that consumes all his current lookahead tokens and all of
 * these tokens are located before the changed region.
 */
private List<ICompositeNode> internalFindValidReplaceRootNodeForChangeRegion(final List<ICompositeNode> nodesEnclosingRegion, final Range range) {
  List<ICompositeNode> result = new ArrayList<ICompositeNode>();
  boolean mustSkipNext = false;
  ICompositeNode previous = null;
  /*
   * set to 'true' as soon as the lookahead of an enclosing
   * exceeds the given range
   */
  boolean done = false;
  for (int i = 0; i < nodesEnclosingRegion.size() && !done; i++) {
    ICompositeNode node = nodesEnclosingRegion.get(i);
    if (node.getGrammarElement() != null) {
      if (!mustSkipNext) {
        boolean process = true;
        if (previous != null && !node.hasNextSibling()) {
          if (previous.getLookAhead() == node.getLookAhead() && previous.getLookAhead() == 0) {
            process = false;
          }
        }
        EObject semanticElement = NodeModelUtils.findActualSemanticObjectFor(node);
        if (semanticElement != null) {
          ICompositeNode actualNode = NodeModelUtils.findActualNodeFor(semanticElement);
          if (actualNode != null && (actualNode.getTotalOffset() < node.getTotalOffset() || actualNode.getTotalEndOffset() > node.getTotalEndOffset())) {
            mustSkipNext = isActionNode(node);
            process = false;
          }
        }
        if (process) {
          int remainingLookAhead = node.getLookAhead();
          if (remainingLookAhead != 0) {
            Iterator<ILeafNode> iterator = node.getLeafNodes().iterator();
            while (iterator.hasNext() && remainingLookAhead > 0) {
              ILeafNode leaf = iterator.next();
              if (!leaf.isHidden()) {
                if (remainingLookAhead > 0) {
                  remainingLookAhead--;
                }
                if (remainingLookAhead == 0) {
                  if (leaf.getTotalEndOffset() <= range.getOffset()) {
                    result.add(node);
                    previous = node;
                    if (isActionNode(node)) {
                      mustSkipNext = true;
                    }
                    break;
                  } else {
                    // lookahead ends left of the range, don't dive into child nodes
                    done = true;
                  }
                }
              }
            }
            if (remainingLookAhead != 0) {
              done = true;
            }
          } else {
            result.add(node);
            previous = node;
            if (isActionNode(node)) {
              mustSkipNext = true;
            }
          }
        }
      } else { // !mustSkipNext
        mustSkipNext = isActionNode(node);
      }
    }
  }
  return result;
}