Java Code Examples for com.intellij.lang.PsiBuilder#getTokenType()

The following examples show how to use com.intellij.lang.PsiBuilder#getTokenType() . 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: OclParser.java    From reasonml-idea-plugin with MIT License 6 votes vote down vote up
private void parseGt(@NotNull PsiBuilder builder, @NotNull ParserState state) {
    if (state.isCurrentContext(object)) {
        // < ... |> > <| ..
        if (state.isCurrentResolution(objectFieldNamed)) {
            state.popEnd();
        }
        state.advance();
        if ("Js".equals(builder.getTokenText())) {
            // it might be a Js object (same with Js.t at the end)
            state.advance();
            if (builder.getTokenType() == m_types.DOT) {
                state.advance();
                if ("t".equals(builder.getTokenText())) {
                    state.updateCurrentCompositeElementType(m_types.C_JS_OBJECT).advance().complete();
                }
            }
        } else {
            state.complete();
        }
        state.popEnd();
    }
}
 
Example 2
Source File: LatteParserUtil.java    From intellij-latte with MIT License 6 votes vote down vote up
public static boolean checkEmptyMacro(PsiBuilder builder, int level)
{
	PsiBuilder.Marker marker = builder.mark();
	boolean result = false;
	while (true) {
		IElementType token = builder.getTokenType();
		if (token == null) {
			break;
		} else if (token == LatteTypes.T_MACRO_TAG_CLOSE) {
			break;
		} else if (token == LatteTypes.T_MACRO_TAG_CLOSE_EMPTY) {
			result = true;
			break;
		}
		builder.advanceLexer();
	}
	marker.rollbackTo();

	return result;
}
 
Example 3
Source File: LatteParserUtil.java    From intellij-latte with MIT License 6 votes vote down vote up
/**
 * Looks for a classic macro a returns true if it finds the macro a and it is pair or unpaired (based on pair parameter).
 */
public static boolean checkPairMacro(PsiBuilder builder, int level, Parser parser) {
	boolean pair = parser == LatteParser.TRUE_parser_;
	if (builder.getTokenType() != T_MACRO_OPEN_TAG_OPEN) return false;

	PsiBuilder.Marker marker = builder.mark();
	String macroName = getMacroName(builder);

	boolean result;

	LatteTagSettings macro = getTag(builder);
	if (macro != null && macro.getType() == LatteTagSettings.Type.AUTO_EMPTY) {
		result = pair == isAutoEmptyPair(macroName, builder);
	} else {
		result = (macro != null ? (macro.getType() == (pair ? LatteTagSettings.Type.PAIR : LatteTagSettings.Type.UNPAIRED)) : !pair);
	}

	marker.rollbackTo();
	return result;
}
 
Example 4
Source File: PreprocessorExpressionParsing.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Nullable
private static PsiBuilder.Marker parseUnary(final PsiBuilder builder)
{
	final IElementType tokenType = builder.getTokenType();

	if(PREFIX_OPS.contains(tokenType))
	{
		final PsiBuilder.Marker unary = builder.mark();
		builder.advanceLexer();

		final PsiBuilder.Marker operand = parseUnary(builder);
		if(operand == null)
		{
			builder.error("Expression expected");
		}

		unary.done(PREFIX_EXPRESSION);
		return unary;
	}
	else
	{
		return parsePrimary(builder);
	}
}
 
Example 5
Source File: HaxeExpressionCodeFragmentImpl.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
@Override
protected ASTNode doParseContents(@NotNull ASTNode chameleon, @NotNull PsiElement psi) {
  final PsiBuilderFactory factory = PsiBuilderFactory.getInstance();
  final PsiBuilder psiBuilder = factory.createBuilder(getProject(), chameleon);
  final PsiBuilder builder = adapt_builder_(HaxeTokenTypes.EXPRESSION, psiBuilder, new HaxeParser());

  final PsiBuilder.Marker marker = enter_section_(builder, 0, _NONE_, "<code fragment>");
  HaxeParser.expression(builder, 1);
  while (builder.getTokenType() != null) {
    builder.advanceLexer();
  }
  marker.done(HaxeTokenTypes.EXPRESSION);
  return builder.getTreeBuilt();
}
 
Example 6
Source File: ShaderLabParser.java    From consulo-unity3d with Apache License 2.0 5 votes vote down vote up
public static boolean parseReference(PsiBuilder builder)
{
	if(builder.getTokenType() == ShaderLabTokens.IDENTIFIER)
	{
		PsiBuilder.Marker mark = builder.mark();
		builder.advanceLexer();
		mark.done(ShaderLabElements.REFERENCE);
		return true;
	}
	return false;
}
 
Example 7
Source File: LatteParserUtil.java    From intellij-latte with MIT License 5 votes vote down vote up
private static boolean isAutoEmptyPair(String macroName, PsiBuilder builder)
{
	builder.advanceLexer();
	if (isEmptyPair(builder)) {
		return true;
	}
	int pairMacrosLevel = 0;
	IElementType type = builder.getTokenType();
	while (type != null) {
		if(type == T_HTML_TAG_NATTR_NAME && ("n:" + macroName).equals(builder.getTokenText())) {
			return false;
		}
		if (nextTokenIsFast(builder, T_MACRO_CLOSE_TAG_OPEN, T_MACRO_OPEN_TAG_OPEN)) {
			boolean closing = type == T_MACRO_CLOSE_TAG_OPEN;
			String macroName2 = getMacroName(builder);
			if (macroName2.equals(macroName)) {
				return closing;
			} else if (closing) {
				if (pairMacrosLevel == 0) {
					return true;
				} else {
					pairMacrosLevel--;
				}
			} else if (isClosingTagExpected(builder, macroName2)) {
				pairMacrosLevel++;
			}
		}
		builder.advanceLexer();
		type = builder.getTokenType();
	}
	return false;
}
 
Example 8
Source File: ParserUtil.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
public static boolean smartRemapAndAdvance(PsiBuilder builder, String expectedTokenText, IElementType expectedTokenType, IElementType newTokenType) {
    IElementType current = builder.getTokenType();
    if (current == newTokenType) {
        // already remapped, probably due to reverting an earlier parse result
        builder.advanceLexer();
    } else if (expectedTokenText.equals(builder.getTokenText()) && current == expectedTokenType) {
        builder.remapCurrentToken(newTokenType);
        builder.advanceLexer();
    } else {
        builder.error("unexpected token");
        return false;
    }

    return true;
}
 
Example 9
Source File: IndentationParser.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void passEOLsAndIndents(@Nonnull final PsiBuilder builder) {
  IElementType tokenType = builder.getTokenType();
  while (tokenType == myEolTokenType || tokenType == myIndentTokenType) {
    builder.advanceLexer();
    tokenType = builder.getTokenType();
  }
}
 
Example 10
Source File: ShaderLabBlendRole.java    From consulo-unity3d with Apache License 2.0 5 votes vote down vote up
private static void parseValue(PsiBuilder builder)
{
	if(builder.getTokenType() == ShaderLabTokens.LBRACKET)
	{
		ShaderLabParser.parseBracketReference(builder);
	}
	else if(builder.getTokenType() == ShaderLabTokens.IDENTIFIER)
	{
		ShaderLabParser.validateIdentifier(builder, ourFactors);
	}
	else
	{
		builder.error("Unexpected token");
	}
}
 
Example 11
Source File: LatteParserUtil.java    From intellij-latte with MIT License 5 votes vote down vote up
public static boolean isNamespace(PsiBuilder builder, int level) {
	PsiBuilder.Marker marker = builder.mark();

	boolean result = false;

	IElementType type = builder.getTokenType();
	IElementType nextToken = builder.lookAhead(1);
	if (type == LatteTypes.T_PHP_NAMESPACE_REFERENCE && nextToken == LatteTypes.T_PHP_NAMESPACE_RESOLUTION) {
		result = true;
	}

	marker.rollbackTo();

	return result;
}
 
Example 12
Source File: LatteParserUtil.java    From intellij-latte with MIT License 5 votes vote down vote up
private static boolean isEmptyPair(PsiBuilder builder)
{
	IElementType type = builder.getTokenType();
	while (type != null) {
		if (type == T_MACRO_TAG_CLOSE_EMPTY) {
			return true;
		} else if (type == T_MACRO_TAG_CLOSE) {
			return false;
		}
		builder.advanceLexer();
		type = builder.getTokenType();
	}
	return false;
}
 
Example 13
Source File: LatteParserUtil.java    From intellij-latte with MIT License 5 votes vote down vote up
private static boolean isClosingTagExpected(PsiBuilder builder, String macroName)
{
	IElementType type = builder.getTokenType();
	Builder.Marker marker = builder.mark();
	if (type == T_MACRO_CLOSE_TAG_OPEN || isEmptyPair(builder)) {
		return false;
	}
	marker.rollbackTo();
	LatteTagSettings macro = LatteConfiguration.getInstance(builder.getProject()).getTag(macroName);

	if (macro != null && macro.getType() == LatteTagSettings.Type.AUTO_EMPTY) {
		return isAutoEmptyPair(macroName, builder);
	}
	return macro != null && macro.getType() == LatteTagSettings.Type.PAIR;
}
 
Example 14
Source File: LatteParserUtil.java    From intellij-latte with MIT License 5 votes vote down vote up
public static boolean checkPairHtmlTag(PsiBuilder builder, int level, Parser parser) {
	boolean pair = parser == LatteParser.TRUE_parser_;
	if (builder.getTokenType() != T_HTML_OPEN_TAG_OPEN) return false;

	PsiBuilder.Marker marker = builder.mark();
	String tagName = getHtmlTagName(builder);

	boolean isVoidTag = LatteHtmlUtil.isVoidTag(tagName);
	boolean result = (!isVoidTag && pair) || (isVoidTag && !pair);

	marker.rollbackTo();
	return result;
}
 
Example 15
Source File: HaskellParserUtilBase.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
public static boolean toplevel_recover_debug(PsiBuilder builder, int level) {
    IElementType typ = builder.getTokenType();
    final boolean res = !(TOPLEVEL_RECOVER_TYPES.contains(typ) || builder.eof());
    System.out.println(
        "toplevel_recover_debug: res: " + res
          + "; offset: " + builder.getCurrentOffset()
          + "; type: " + typ
          + "; text: '" + builder.getTokenText() + "'");
    return res;
}
 
Example 16
Source File: MMPsiParser.java    From netbeans-mmd-plugin with Apache License 2.0 4 votes vote down vote up
private int recursiveParseTopic(@Nonnull final PsiBuilder builder, final int level) {

    while (!builder.eof()) {
      final PsiBuilder.Marker marker = builder.mark();

      final IElementType token = builder.getTokenType();

      if (token == null) {
        marker.drop();
      } else {
        if (token == MMTokens.TOPIC_LEVEL) {
          final PsiBuilder.Marker levelMarker = builder.mark();
          levelMarker.done(token);

          final int theTopicLevel = ModelUtils.calcCharsOnStart('#', builder.getTokenText());
          if (theTopicLevel <= 1) {
            marker.done(MMTokens.UNKNOWN);
          } else {
            if (theTopicLevel <= level) {
              marker.rollbackTo();
              return theTopicLevel;
            } else {
              builder.advanceLexer();
              final int parsedTopicLevel = recursiveParseTopic(builder, theTopicLevel);
              marker.done(MMTokens.TOPIC);
              if (parsedTopicLevel < theTopicLevel) {
                return parsedTopicLevel;
              }
              if (parsedTopicLevel == theTopicLevel) {
                continue;
              }
            }
          }
        } else if (token == MMTokens.TOPIC_TITLE || token == MMTokens.CODE_SNIPPET_BODY || token == MMTokens.CODE_SNIPPET_END || token == MMTokens.CODE_SNIPPET_START || token == MMTokens.ATTRIBUTES) {
          marker.done(token);
        } else if (token == MMTokens.EXTRA_TYPE) {
          try {
            if (parseExtraBlock(builder)) {
              continue;
            }
          } finally {
            marker.done(MMTokens.EXTRA_DATA);
          }
        } else if (token == MMTokens.WHITE_SPACE) {
          marker.done(token);
        } else {
          marker.done(MMTokens.UNKNOWN);
        }
      }
      builder.advanceLexer();
    }


    return level;
  }
 
Example 17
Source File: ShaderLabParser.java    From consulo-unity3d with Apache License 2.0 4 votes vote down vote up
public static void parseElementsInBraces(@Nonnull PsiBuilder builder, IElementType open, IElementType close, IElementType valid)
{
	if(builder.getTokenType() == open)
	{
		builder.advanceLexer();

		while(!builder.eof())
		{
			IElementType tokenType = builder.getTokenType();
			if(tokenType == close)
			{
				break;
			}

			if(valid != null)
			{
				if(tokenType == valid)
				{
					builder.advanceLexer();
				}
				else
				{
					doneError(builder, "Unexpected token");
				}
			}
			else
			{
				builder.advanceLexer();
			}

			if(builder.getTokenType() == ShaderLabTokens.COMMA)
			{
				builder.advanceLexer();
			}
			else if(builder.getTokenType() != close)
			{
				doneError(builder, "Unexpected token");
			}
		}
		expectWithError(builder, close, "Unexpected token");
	}
}
 
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: RmlParser.java    From reasonml-idea-plugin with MIT License 4 votes vote down vote up
private void parseComma(@NotNull PsiBuilder builder, @NotNull ParserState state) {
    if (state.isCurrentResolution(functionBody)) {
        // a function is part of something else, close it first
        state.popEnd().popEnd();
    }

    if (state.isCurrentResolution(functionParameterNamedSignatureItem)) {
        state.complete().
                popEndUntilResolution(functionParameterNamed).updateCurrentResolution(functionParameterNamedSignature);
    }

    if ((state.isCurrentResolution(signatureItem) || state.isCurrentResolution(signatureItemEq)) && !state.isCurrentContext(recordSignature)) {
        state.popEnd();
        state.advance();
        state.add(mark(builder, state.currentContext(), signatureItem, m_types.C_SIG_ITEM).complete());
    } else if (state.isCurrentContext(signature)) {
        state.popEnd();
    }

    if (state.isCurrentContext(recordSignature)) {
        state.complete().
                popEndUntilResolution(recordField).popEnd().
                advance().
                add(mark(builder, state.currentContext(), recordField, m_types.C_RECORD_FIELD));
    } else if (state.isCurrentResolution(recordField)) {
        state.popEnd().advance().add(mark(builder, state.currentContext(), recordField, m_types.C_RECORD_FIELD));
    } else if (state.isCurrentResolution(objectFieldNamed)) {
        state.popEnd().advance().add(mark(builder, state.currentContext(), objectField, m_types.C_OBJECT_FIELD));
    } else if (state.isCurrentResolution(mixin)) {
        state.popEnd();
    } else if (state.isCurrentResolution(functionParameter) || state.isCurrentResolution(functionParameterNamed) || state
            .isCurrentResolution(functionParameterNamedSignature) || state.isCurrentResolution(functionParameterNamedBinding)) {
        state.complete();
        if (state.isCurrentResolution(functionParameterNamedBinding)) {
            state.popEndUntilResolution(functionParameters);
        } else {
            state.popEnd();
        }
        state.advance().
                add(mark(builder, state.currentContext(), functionParameter,
                         state.currentContext() == functionCall ? m_types.C_FUN_CALL_PARAM : m_types.C_FUN_PARAM));
        IElementType nextTokenType = builder.getTokenType();
        if (nextTokenType != m_types.RPAREN) {
            // not at the end of a list: ie not => (p1, p2<,> )
            state.complete();
        }
    } else if (state.isCurrentCompositeElementType(m_types.C_UNKNOWN_EXPR)) {
        // We don't know yet but we need to complete the marker
        state.complete();
        state.popEnd();
    } else if (state.isCurrentContext(let) && state.isCurrentResolution(genericExpression)) {
        // It must be a deconstruction
        // let ( a |>,<| b ) = ..
        state.updateCurrentResolution(deconstruction).updateCurrentCompositeElementType(m_types.C_DECONSTRUCTION);
    }
}
 
Example 20
Source File: RmlParser.java    From reasonml-idea-plugin with MIT License 4 votes vote down vote up
private void parseColon(@NotNull PsiBuilder builder, @NotNull ParserState state) {
    if (state.isCurrentContext(maybeRecord) && state.isCurrentResolution(genericExpression)) {
        // yes it is a record, remove the maybe
        ParserScope fieldState = state.pop();

        state.updateCurrentContext(recordUsage).updateCurrentCompositeElementType(m_types.C_RECORD_EXPR);
        if (state.isCurrentResolution(maybeRecordUsage)) {
            state.updateCurrentResolution(record);
        }

        if (fieldState != null) {
            fieldState.context(recordUsage);
            fieldState.resolution(recordField);
            fieldState.updateCompositeElementType(m_types.C_RECORD_FIELD);
            state.add(fieldState);
        }
    }

    if (state.isCurrentResolution(externalNamed)) {
        state.updateCurrentResolution(externalNamedSignature).
                advance().
                add(mark(builder, signature, m_types.C_SIG_EXPR).complete()).
                add(mark(builder, signature, signatureItem, m_types.C_SIG_ITEM).complete());
    } else if (state.isCurrentResolution(letNamed)) {
        state.advance();
        state.add(mark(builder, signature, letNamedSignature, m_types.C_SIG_EXPR).complete());
        IElementType nextTokenType = builder.getTokenType();
        if (nextTokenType != m_types.LPAREN) {
            state.add(mark(builder, signature, signatureItem, m_types.C_SIG_ITEM).complete());
        }
    } else if (state.isCurrentResolution(moduleNamed)) {
        // module M |> :<| ...
        state.updateCurrentResolution(moduleNamedSignature).
                complete();
    } else if (state.isCurrentResolution(functorNamedEq)) {
        // module M = (X:Y) |> :<| ...
        state.updateCurrentResolution(functorNamedEqColon).advance();
        IElementType tokenType = builder.getTokenType();
        if (tokenType == m_types.LPAREN) {
            // module M = (X:Y) : |>(<| S ... ) = ...
            state.add(markScope(builder, state.currentContext(), scope, m_types.C_SCOPED_EXPR, m_types.LPAREN)).advance();
        }
        state.add(mark(builder, state.currentContext(), functorNamedColonResult, m_types.C_FUNCTOR_RESULT));
    } else if (state.isCurrentResolution(functorParam)) {
        state.updateCurrentResolution(functorParamColon);
    } else if (state.isCurrentResolution(recordField)) {
        state.complete().advance();
        if (!state.isCurrentContext(recordUsage)) {
            state.add(mark(builder, recordSignature, signature, m_types.C_SIG_EXPR).complete());
            state.add(mark(builder, recordSignature, signatureItem, m_types.C_SIG_ITEM).complete());
        }
    } else if (state.isCurrentResolution(objectField)) {
        state.complete();
        state.updateCurrentResolution(objectFieldNamed);
    } else if (state.isCurrentResolution(functionParameter)) {
        state.updateCurrentResolution(functionParameterNamed).
                advance().
                add(mark(builder, signature, functionParameterNamedSignature, m_types.C_SIG_EXPR).complete()).
                add(mark(builder, signature, functionParameterNamedSignatureItem, m_types.C_SIG_ITEM).complete());
    } else if (state.isCurrentResolution(paren) && state.isCurrentCompositeElementType(m_types.C_UNKNOWN_EXPR)) {
        state.complete().
                updateCurrentContext(functionParameter).updateCurrentResolution(functionParameterNamed).
                advance().
                add(mark(builder, signature, functionParameterNamedSignature, m_types.C_SIG_EXPR).complete()).
                add(mark(builder, signatureItem, functionParameterNamedSignatureItem, m_types.C_SIG_ITEM).complete());
    }
}