Java Code Examples for com.intellij.psi.tree.TokenSet#contains()

The following examples show how to use com.intellij.psi.tree.TokenSet#contains() . 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: SharedParsingHelpers.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
protected static boolean expect(PsiBuilder builder, TokenSet tokenSet, String message)
{
	if(tokenSet.contains(builder.getTokenType()))
	{
		builder.advanceLexer();
		return true;
	}
	else
	{
		if(message != null)
		{
			builder.error(message);
		}
		return false;
	}
}
 
Example 2
Source File: FilteredIndentsHighlightingPass.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private boolean isComment(int offset) {
  final HighlighterIterator it = myEditor.getHighlighter().createIterator(offset);
  IElementType tokenType = it.getTokenType();
  Language language = tokenType.getLanguage();
  TokenSet comments = myComments.get(language);
  if (comments == null) {
    ParserDefinition definition = LanguageParserDefinitions.INSTANCE.forLanguage(language);
    if (definition != null) {
      comments = definition.getCommentTokens();
    }
    if (comments == null) {
      return false;
    }
    else {
      myComments.put(language, comments);
    }
  }
  return comments.contains(tokenType);
}
 
Example 3
Source File: SharedParsingHelpers.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
protected static void reportErrorUntil(CSharpBuilderWrapper builder, String error, TokenSet originalSet, TokenSet softSet)
{
	while(!builder.eof())
	{
		if(originalSet.contains(builder.getTokenType()))
		{
			break;
		}

		if(builder.getTokenType() == CSharpTokens.IDENTIFIER)
		{
			builder.enableSoftKeywords(softSet);
			IElementType tokenType = builder.getTokenType();
			builder.disableSoftKeywords(softSet);
			if(softSet.contains(tokenType))
			{
				// remap
				builder.remapBackIfSoft();
				break;
			}
		}
		PsiBuilder.Marker mark = builder.mark();
		builder.advanceLexer();
		mark.error(error);
	}
}
 
Example 4
Source File: LightTreeUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static void toBuffer(@Nonnull LighterAST tree, @Nonnull LighterASTNode node, @Nonnull StringBuilder buffer, @Nullable TokenSet skipTypes) {
  if (skipTypes != null && skipTypes.contains(node.getTokenType())) {
    return;
  }

  if (node instanceof LighterASTTokenNode) {
    buffer.append(((LighterASTTokenNode)node).getText());
    return;
  }

  if (node instanceof LighterLazyParseableNode) {
    buffer.append(((LighterLazyParseableNode)node).getText());
    return;
  }

  List<LighterASTNode> children = tree.getChildren(node);
  for (int i = 0, size = children.size(); i < size; ++i) {
    toBuffer(tree, children.get(i), buffer, skipTypes);
  }
}
 
Example 5
Source File: TypedHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static boolean isInsideStringLiteral(@Nonnull Editor editor, @Nonnull PsiFile file) {
  int offset = editor.getCaretModel().getOffset();
  PsiElement element = file.findElementAt(offset);
  if (element == null) return false;
  final ParserDefinition definition = LanguageParserDefinitions.INSTANCE.forLanguage(element.getLanguage());
  if (definition != null) {
    final TokenSet stringLiteralElements = definition.getStringLiteralElements(element.getLanguageVersion());
    final ASTNode node = element.getNode();
    if (node == null) return false;
    final IElementType elementType = node.getElementType();
    if (stringLiteralElements.contains(elementType)) {
      return true;
    }
    PsiElement parent = element.getParent();
    if (parent != null) {
      ASTNode parentNode = parent.getNode();
      if (parentNode != null && stringLiteralElements.contains(parentNode.getElementType())) {
        return true;
      }
    }
  }
  return false;
}
 
Example 6
Source File: CommandParsingUtil.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
/**
 * Reads a list of optional command params.
 *
 * @param builder
 * @param validExtraTokens
 * @return True if the list is either empty or parsed fine.
 */
public static boolean readCommandParams(final BashPsiBuilder builder, TokenSet validExtraTokens) {
    boolean ok = true;

    while (!builder.eof() && ok) {
        RedirectionParsing.RedirectParseResult result = Parsing.redirection.parseListIfValid(builder, true);
        if (result != RedirectionParsing.RedirectParseResult.NO_REDIRECT) {
            ok = result != RedirectionParsing.RedirectParseResult.PARSING_FAILED;
        } else {
            OptionalParseResult parseResult = Parsing.word.parseWordIfValid(builder, true);
            if (parseResult.isValid()) {
                ok = parseResult.isParsedSuccessfully();
            } else if (validExtraTokens.contains(builder.getTokenType())) {
                builder.advanceLexer();
                ok = true;
            } else {
                break;
            }
        }
    }

    return ok;
}
 
Example 7
Source File: AlignmentInColumnsHelper.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static List<IElementType> findSubNodeTypes(ASTNode node, TokenSet types) {
  List<IElementType> foundTypes = new SmartList<IElementType>();
  for (ASTNode child = node.getFirstChildNode(); child != null && child.getTreeParent() == node; child = child.getTreeNext()) {
    IElementType type = child.getElementType();
    if (types.contains(type)) {
      foundTypes.add(type);
    }
  }
  return foundTypes;
}
 
Example 8
Source File: LightTreeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static List<LighterASTNode> getChildrenOfType(@Nonnull LighterAST tree, @Nonnull LighterASTNode node, @Nonnull TokenSet types) {
  List<LighterASTNode> children = tree.getChildren(node);
  List<LighterASTNode> result = null;

  for (int i = 0, size = children.size(); i < size; ++i) {
    LighterASTNode child = children.get(i);
    if (types.contains(child.getTokenType())) {
      if (result == null) result = new SmartList<>();
      result.add(child);
    }
  }

  return result != null ? result: Collections.emptyList();
}
 
Example 9
Source File: PsiBuilderUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * Advances lexer if current token is of expected type, does nothing otherwise.
 *
 * @param builder       PSI builder to operate on.
 * @param expectedTypes expected token types.
 * @return true if token matches, false otherwise.
 */
public static boolean expect(final PsiBuilder builder, final TokenSet expectedTypes) {
  if (expectedTypes.contains(builder.getTokenType())) {
    builder.advanceLexer();
    return true;
  }
  return false;
}
 
Example 10
Source File: FormatterUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean isPrecededBy(@Nullable ASTNode node, TokenSet expectedTypes, IElementType... skipTypes) {
  ASTNode prevNode = node == null ? null : node.getTreePrev();
  while (prevNode != null && (isWhitespaceOrEmpty(prevNode) || isOneOf(prevNode, skipTypes))) {
    prevNode = prevNode.getTreePrev();
  }
  if (prevNode == null) return false;
  return expectedTypes.contains(prevNode.getElementType());
}
 
Example 11
Source File: MemberWithBodyParsing.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
private static void parseAccessor(CSharpBuilderWrapper builder, IElementType to, TokenSet tokenSet)
{
	PsiBuilder.Marker marker = builder.mark();

	Pair<PsiBuilder.Marker, ModifierSet> pairModifierList = parseModifierListWithAttributes(builder, STUB_SUPPORT);

	builder.enableSoftKeywords(tokenSet);
	boolean contains = tokenSet.contains(builder.getTokenType());
	builder.disableSoftKeywords(tokenSet);

	if(contains)
	{
		builder.advanceLexer();

		MethodParsing.parseMethodBodyOrSemicolon(builder, pairModifierList.getSecond());

		marker.done(to);
	}
	else
	{
		// non empty
		if(!pairModifierList.getSecond().isEmpty())
		{
			marker.drop();
			builder.error("Expected accessor name");
		}
		else
		{
			marker.rollbackTo();
			PsiBuilder.Marker errorMarker = builder.mark();
			builder.advanceLexer(); // advance one element
			errorMarker.error("Expected accessor name");
		}
	}
}
 
Example 12
Source File: WhitespacesBinders.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static WhitespacesAndCommentsBinder trailingCommentsBinder(@Nonnull final TokenSet commentTypes) {
  return new WhitespacesAndCommentsBinder() {
    @Override
    public int getEdgePosition(List<IElementType> tokens, boolean atStreamEdge, TokenTextGetter getter) {
      int i = tokens.size() - 1;
      while (i >= 0 && !commentTypes.contains(tokens.get(i))) {
        i--;
      }
      return i + 1;
    }
  };
}
 
Example 13
Source File: TreeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static ASTNode findSibling(ASTNode start, TokenSet types) {
  ASTNode child = start;
  while (true) {
    if (child == null) return null;
    if (types.contains(child.getElementType())) return child;
    child = child.getTreeNext();
  }
}
 
Example 14
Source File: LightTreeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static LighterASTNode firstChildOfType(@Nonnull LighterAST tree, @Nullable LighterASTNode node, @Nonnull TokenSet types) {
  if (node == null) return null;

  List<LighterASTNode> children = tree.getChildren(node);
  for (int i = 0; i < children.size(); ++i) {
    LighterASTNode child = children.get(i);
    if (types.contains(child.getTokenType())) return child;
  }

  return null;
}
 
Example 15
Source File: CompositeElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public ASTNode findChildByType(@Nonnull TokenSet types) {
  if (DebugUtil.CHECK_INSIDE_ATOMIC_ACTION_ENABLED) {
    assertReadAccessAllowed();
  }
  for (ASTNode element = getFirstChildNode(); element != null; element = element.getTreeNext()) {
    if (types.contains(element.getElementType())) return element;
  }
  return null;
}
 
Example 16
Source File: TreeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static ASTNode findSiblingBackward(ASTNode start, TokenSet types) {
  ASTNode child = start;
  while (true) {
    if (child == null) return null;
    if (types.contains(child.getElementType())) return child;
    child = child.getTreePrev();
  }
}
 
Example 17
Source File: BuckFormatUtil.java    From buck with Apache License 2.0 4 votes vote down vote up
public static boolean hasElementType(ASTNode node, TokenSet set) {
  return set.contains(node.getElementType());
}
 
Example 18
Source File: PreprocessorExpressionParsing.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@Nullable
private static PsiBuilder.Marker parseBinary(final PsiBuilder builder, final ExprType type, final TokenSet ops)
{
	PsiBuilder.Marker result = parseExpression(builder, type);
	if(result == null)
	{
		return null;
	}
	int operandCount = 1;

	IElementType tokenType = builder.getTokenType();
	IElementType currentExprTokenType = tokenType;
	while(true)
	{
		if(tokenType == null || !ops.contains(tokenType))
		{
			break;
		}

		builder.advanceLexer();

		final PsiBuilder.Marker right = parseExpression(builder, type);
		operandCount++;
		tokenType = builder.getTokenType();
		if(tokenType == null || !ops.contains(tokenType) || tokenType != currentExprTokenType || right == null)
		{
			// save
			result = result.precede();
			if(right == null)
			{
				builder.error("Expression expected");
			}
			result.done(operandCount > 2 ? POLYADIC_EXPRESSION : BINARY_EXPRESSION);
			if(right == null)
			{
				break;
			}
			currentExprTokenType = tokenType;
			operandCount = 1;
		}
	}

	return result;
}
 
Example 19
Source File: CSharpDocElements.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
private void parseArguments(CSharpBuilderWrapper builder, IElementType stopElement)
{
	TokenSet stoppers = TokenSet.create(stopElement, CSharpTokens.RBRACE, CSharpTokens.SEMICOLON);

	boolean commaEntered = false;
	while(!builder.eof())
	{
		if(stoppers.contains(builder.getTokenType()))
		{
			if(commaEntered)
			{
				PsiBuilder.Marker mark = builder.mark();
				SharedParsingHelpers.emptyElement(builder, CSharpElements.ERROR_EXPRESSION);
				// call(test,)
				builder.error("Type expected");
				mark.done(CSharpElements.DOC_CALL_ARGUMENT);
			}
			break;
		}
		commaEntered = false;

		PsiBuilder.Marker argumentMarker = builder.mark();
		SharedParsingHelpers.TypeInfo marker = SharedParsingHelpers.parseType(builder, SharedParsingHelpers.VAR_SUPPORT | SharedParsingHelpers.INSIDE_DOC);
		if(marker == null)
		{
			PsiBuilder.Marker errorMarker = builder.mark();
			builder.advanceLexer();
			builder.error("Type expected");
			errorMarker.done(CSharpElements.ERROR_EXPRESSION);
		}
		argumentMarker.done(CSharpElements.DOC_CALL_ARGUMENT);


		if(builder.getTokenType() == CSharpTokens.COMMA)
		{
			builder.advanceLexer();
			commaEntered = true;
		}
		else if(!stoppers.contains(builder.getTokenType()))
		{
			builder.error("',' expected");
		}
	}
}
 
Example 20
Source File: BuckPsiUtils.java    From Buck-IntelliJ-Plugin with Apache License 2.0 2 votes vote down vote up
/**
 * Check that element type of the given AST node belongs to the token set.
 * <p/>
 * It slightly less verbose than {@code set.contains(node.getElementType())}
 * and overloaded methods with the same name allow check ASTNode/PsiElement against both concrete
 * element types and token sets in uniform way.
 */
public static boolean hasElementType(@NotNull ASTNode node, @NotNull TokenSet set) {
  return set.contains(node.getElementType());
}