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

The following examples show how to use com.intellij.lang.PsiBuilder#advanceLexer() . These examples are extracted from open source projects. 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
@Nonnull
private static IElementType createElementType(String id, BiConsumer<PsiBuilder, LanguageVersion> consumer)
{
	return new IFileElementType(id, CSharpLanguage.INSTANCE)
	{
		@Override
		protected ASTNode doParseContents(@Nonnull ASTNode chameleon, @Nonnull PsiElement psi)
		{
			final Project project = psi.getProject();
			final Language languageForParser = getLanguageForParser(psi);
			final LanguageVersion tempLanguageVersion = chameleon.getUserData(LanguageVersion.KEY);
			final LanguageVersion languageVersion = tempLanguageVersion == null ? psi.getLanguageVersion() : tempLanguageVersion;
			final PsiBuilder builder = PsiBuilderFactory.getInstance().createBuilder(project, chameleon, null, languageForParser, languageVersion, chameleon.getChars());
			consumer.accept(builder, languageVersion);
			while(!builder.eof())
			{
				builder.advanceLexer();
			}
			return builder.getTreeBuilt();
		}
	};
}
 
Example 2
Source Project: netbeans-mmd-plugin   File: MMPsiParser.java    License: Apache License 2.0 6 votes vote down vote up
private void parseHeader(@Nonnull final PsiBuilder builder) {
  boolean doLoop = true;
  while (doLoop && !builder.eof()) {
    final PsiBuilder.Marker marker = builder.mark();

    if (builder.getTokenType() == null) {
      marker.drop();
    } else {
      final IElementType token = builder.getTokenType();

      if (token == MMTokens.HEADER_DELIMITER) {
        marker.done(token);
        doLoop = false;
      } else if (token == MMTokens.HEADER_LINE
          || token == MMTokens.UNKNOWN
          || token == MMTokens.WHITE_SPACE
          || token == MMTokens.ATTRIBUTES) {
        marker.done(token);
      } else {
        throw Assertions.fail("Unexpected header token : " + token);
      }
    }
    builder.advanceLexer();
  }
}
 
Example 3
Source Project: netbeans-mmd-plugin   File: MMPsiParser.java    License: Apache License 2.0 6 votes vote down vote up
private void parseTopics(@Nonnull final PsiBuilder builder) {
  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 topicLevel = ModelUtils.calcCharsOnStart('#', builder.getTokenText());
        if (topicLevel != 1) {
          marker.done(MMTokens.UNKNOWN);
        } else {
          builder.advanceLexer();
          recursiveParseTopic(builder, topicLevel);
          marker.done(MMTokens.TOPIC);
        }
      } else {
        marker.done(MMTokens.UNKNOWN);
      }
    }
    builder.advanceLexer();
  }
}
 
Example 4
public static boolean expect(PsiBuilder builder, IElementType elementType, String message)
{
	if(builder.getTokenType() == elementType)
	{
		builder.advanceLexer();
		return true;
	}
	else
	{
		if(message != null)
		{
			builder.error(message);
		}
		return false;
	}
}
 
Example 5
@Override
public void parseLight(IElementType root, PsiBuilder builder) {
    if (crashing) {
        builder.setDebugMode(true);
    }
    final PsiBuilder.Marker rootMarker = builder.mark();
    if (!builder.eof()) { //Empty file is not an error
        final GLSLParsing theRealParser = new GLSLParsing(builder);

        theRealParser.parseTranslationUnit();
        while (!builder.eof()) // exhaust the file if unable to parse everything
            builder.advanceLexer();
    }

    rootMarker.done(root);
}
 
Example 6
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 7
Source Project: consulo   File: IndentationParser.java    License: 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 8
/**
 * @param exact if true then positions should be equal;
 *              else delegate should be behind, not including exactly all foreign (skipped) or whitespace tokens
 */
private void synchronizePositions(boolean exact) {
    final PsiBuilder delegate = getDelegate();

    if (myLexPosition >= myShrunkSequenceSize || delegate.eof()) {
        myLexPosition = myShrunkSequenceSize;
        while (!delegate.eof()) {
            delegate.advanceLexer();
        }
        return;
    }

    if (delegate.getCurrentOffset() > myShrunkSequence.get(myLexPosition).realStart) {
        LOG.debug("delegate is ahead of my builder!");
        return;
    }

    final int keepUpPosition = getKeepUpPosition(exact);

    while (!delegate.eof()) {
        final int delegatePosition = delegate.getCurrentOffset();

        if (delegatePosition < keepUpPosition) {
            delegate.advanceLexer();
        } else {
            break;
        }
    }
}
 
Example 9
@Nonnull
@Override
public ASTNode parse(@Nonnull IElementType elementType, @Nonnull PsiBuilder builder, @Nonnull LanguageVersion languageVersion)
{
	PsiBuilder.Marker mark = builder.mark();
	SharedParsingHelpers.parseType(new CSharpBuilderWrapper(builder, languageVersion), SharedParsingHelpers.VAR_SUPPORT);
	while(!builder.eof())
	{
		builder.error("Unexpected token");
		builder.advanceLexer();
	}
	mark.done(elementType);
	return builder.getTreeBuilt();
}
 
Example 10
Source Project: BashSupport   File: ParserUtil.java    License: 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 11
@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 12
@Nullable
private static PsiBuilder.Marker parsePrimaryExpressionStart(final PsiBuilder builder)
{
	IElementType tokenType = builder.getTokenType();


	if(tokenType == IDENTIFIER)
	{
		PsiBuilder.Marker refExpr = builder.mark();

		builder.advanceLexer();
		refExpr.done(REFERENCE_EXPRESSION);
		return refExpr;
	}

	if(tokenType == LPAR)
	{
		final PsiBuilder.Marker parenth = builder.mark();
		builder.advanceLexer();

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

		if(!SharedParsingHelpers.expect(builder, RPAR, null))
		{
			if(inner != null)
			{
				builder.error("')' expected");
			}
		}

		parenth.done(PARENTHESES_EXPRESSION);
		return parenth;
	}
	return null;
}
 
Example 13
@NotNull
public ASTNode parse(IElementType iElementType, PsiBuilder psiBuilder) {
  final PsiBuilder.Marker marker = psiBuilder.mark();
  PsiBuilder.Marker statement = psiBuilder.mark();
  IElementType statementType = null;

  while(!psiBuilder.eof()) {
    final IElementType tokenType = psiBuilder.getTokenType();

    if (tokenType == MakefileTokenTypes.SEMANTIC_WHITESPACE) {
      psiBuilder.advanceLexer();
      statement.done(statementType != null ? statementType:MakefileTokenTypes.STATEMENT);
      statementType = null;
      statement = psiBuilder.mark();
      continue;
    } else if (shouldProduceComposite(tokenType)) {
      PsiBuilder.Marker identifier = psiBuilder.mark();
      psiBuilder.advanceLexer();
      identifier.done(tokenType);
      continue;
    } else if (tokenType == MakefileTokenTypes.VAR_DEFINITION) {
      statementType = tokenType;
    } else if (tokenType == MakefileTokenTypes.IDENTIFIER_START) {
      readIdentifier(psiBuilder, MakefileTokenTypes.IDENTIFIER_END, MakefileTokenTypes.IDENTIFIER);
    }
    else if (tokenType == MakefileTokenTypes.TARGET_IDENTIFIER_START) {
      readIdentifier(psiBuilder, MakefileTokenTypes.TARGET_IDENTIFIER_END, MakefileTokenTypes.TARGET_IDENTIFIER);
      if (!psiBuilder.eof()) psiBuilder.advanceLexer();

      statementType = MakefileTokenTypes.TARGET_DECLARATION;
      continue;
    }
    psiBuilder.advanceLexer();
  }

  statement.done(MakefileTokenTypes.STATEMENT);
  marker.done(iElementType);
  return psiBuilder.getTreeBuilt();
}
 
Example 14
Source Project: intellij-latte   File: LatteParserUtil.java    License: 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 15
Source Project: intellij-latte   File: LatteParserUtil.java    License: 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 16
Source Project: reasonml-idea-plugin   File: DuneParser.java    License: MIT License 4 votes vote down vote up
@Override
protected void parseFile(@NotNull PsiBuilder builder, @NotNull ParserState state) {
    IElementType tokenType = null;

    //long parseStart = System.currentTimeMillis();

    int c = current_position_(builder);
    while (true) {
        //long parseTime = System.currentTimeMillis();
        //if (5 < parseTime - parseStart) {
        // Protection: abort the parsing if too much time spent
        //break;
        //}

        state.previousElementType1 = tokenType;
        tokenType = builder.getTokenType();
        if (tokenType == null) {
            break;
        }

        // ( .. )
        if (tokenType == m_types.LPAREN) {
            parseLParen(builder, state);
        } else if (tokenType == m_types.RPAREN) {
            if (state.isCurrentResolution(stanzaNamedFields)) {
                state.popEnd();
            }

            if (state.isInScopeExpression()) {
                state.complete();
                state.advance();
                state.popEnd();
            } else {
                builder.error("Unbalanced parenthesis");
            }
        }

        // %{ .. }
        else if (tokenType == m_types.VAR_START) {
            parseVarStart(builder, state);
        } else if (tokenType == m_types.VAR_END) {
            parseVarEnd(builder, state);
        } else if (tokenType == m_types.ATOM) {
            parseAtom(builder, state);
        }

        if (state.dontMove) {
            state.dontMove = false;
        } else {
            builder.advanceLexer();
        }

        if (!empty_element_parsed_guard_(builder, "duneFile", c)) {
            break;
        }

        c = builder.rawTokenIndex();
    }
}
 
Example 17
Source Project: BashSupport   File: ParserUtil.java    License: Apache License 2.0 4 votes vote down vote up
public static void errorToken(PsiBuilder builder, @PropertyKey(resourceBundle = BUNDLE) String message) {
    PsiBuilder.Marker marker = builder.mark();
    builder.advanceLexer();
    marker.error(BashStrings.message(message));
}
 
Example 18
Source Project: consulo   File: IndentationParser.java    License: Apache License 2.0 4 votes vote down vote up
protected void advanceLexer(@Nonnull PsiBuilder builder) {
  builder.advanceLexer();
}
 
Example 19
Source Project: consulo-unity3d   File: ShaderLabParser.java    License: 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 20
Source Project: consulo-unity3d   File: ShaderLabParser.java    License: Apache License 2.0 4 votes vote down vote up
public static void doneError(PsiBuilder builder, String message)
{
	PsiBuilder.Marker mark = builder.mark();
	builder.advanceLexer();
	mark.error(message);
}