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

The following examples show how to use com.intellij.lang.PsiBuilder#mark() . 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
@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 2
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 3
private static boolean parseOperator(PsiBuilder builder_, IElementType operator, IElementType... tokens) {
  final PsiBuilder.Marker marker_ = builder_.mark();

  whiteSpaceSkipped = false;

  builder_.setWhitespaceSkippedCallback(new WhitespaceSkippedCallback() {
    @Override
    public void onSkip(IElementType type, int i, int i1) {
      whiteSpaceSkipped = true;
    }
  });

  for (IElementType token : tokens) {
    if (!consumeTokenFast(builder_, token) || whiteSpaceSkipped) {
      marker_.rollbackTo();
      builder_.setWhitespaceSkippedCallback(null);
      return false;
    }
  }

  builder_.setWhitespaceSkippedCallback(null);
  marker_.collapse(operator);
  return true;
}
 
Example 4
Source Project: intellij-latte   File: LatteParserUtil.java    License: 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 5
Source Project: intellij-latte   File: LatteParserUtil.java    License: 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 6
Source Project: intellij   File: BuildParserDefinition.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ASTNode parse(IElementType root, PsiBuilder builder) {
  if (debug.getValue()) {
    System.err.println(builder.getUserDataUnprotected(FileContextUtil.CONTAINING_FILE_KEY));
  }
  PsiBuilder.Marker rootMarker = builder.mark();
  ParsingContext context = new ParsingContext(builder);
  context.statementParser.parseFileInput();
  rootMarker.done(root);
  return builder.getTreeBuilt();
}
 
Example 7
@Nullable
private static PsiBuilder.Marker parsePrimary(final PsiBuilder builder)
{
	PsiBuilder.Marker startMarker = builder.mark();

	PsiBuilder.Marker expr = parsePrimaryExpressionStart(builder);
	if(expr == null)
	{
		startMarker.drop();
		return null;
	}
	startMarker.drop();
	return expr;
}
 
Example 8
@Nonnull
@Override
public ASTNode parse(@Nonnull IElementType elementType, @Nonnull PsiBuilder builder, @Nonnull LanguageVersion languageVersion)
{
	PsiBuilder.Marker mark = builder.mark();
	ExpressionParsing.parse(new CSharpBuilderWrapper(builder, languageVersion), ModifierSet.EMPTY);
	while(!builder.eof())
	{
		builder.error("Unexpected token");
		builder.advanceLexer();
	}
	mark.done(elementType);
	return builder.getTreeBuilt();
}
 
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: react-templates-plugin   File: RTJSParser.java    License: MIT License 5 votes vote down vote up
public RTJSParser(PsiBuilder builder) {
    super(JavaScriptSupportLoader.JAVASCRIPT_1_5, builder);
    myExpressionParser = new RTExpressionParser();
    myStatementParser = new StatementParser<RTJSParser>(this) {
        @Override
        protected void doParseStatement(boolean canHaveClasses) {
            final IElementType firstToken = builder.getTokenType();
            if (firstToken == JSTokenTypes.LBRACE) {
                parseExpressionStatement();
                checkForSemicolon();
                return;
            }
            if (isIdentifierToken(firstToken)) {
                final IElementType nextToken = builder.lookAhead(1);
                if (nextToken == JSTokenTypes.IN_KEYWORD) {
                    parseInStatement();
                    return;
                }
            }
            if (builder.getTokenType() == JSTokenTypes.LPAR) {
                if (parseInStatement()) {
                    return;
                }
            }
            super.doParseStatement(canHaveClasses);
        }

        private boolean parseInStatement() {
            PsiBuilder.Marker statement = builder.mark();
            if (!getExpressionParser().parseInExpression()) {
                statement.drop();
                return false;
            }
            statement.done(JSElementTypes.EXPRESSION_STATEMENT);
            return true;
        }
    };
}
 
Example 11
Source Project: consulo-unity3d   File: ShaderLabParser.java    License: 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 12
@NotNull
@Override
public ASTNode parse(IElementType root, PsiBuilder builder) {
    PsiBuilder.Marker marker = builder.mark();
    while (!builder.eof()) {
        builder.advanceLexer();
    }
    marker.done(root);
    return builder.getTreeBuilt();
}
 
Example 13
Source Project: intellij-latte   File: LatteParserUtil.java    License: 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 14
Source Project: intellij-latte   File: LatteParserUtil.java    License: MIT License 5 votes vote down vote up
public static boolean checkAutoClosedMacro(PsiBuilder builder, int level) {
	PsiBuilder.Marker marker = builder.mark();

	LatteTagSettings macro = getTag(builder);

	marker.rollbackTo();

	if (macro != null && macro.getType() == LatteTagSettings.Type.AUTO_EMPTY) {
		lastMacro = macro;
		return true;
	}
	return false;
}
 
Example 15
protected static boolean doneOneElement(PsiBuilder builder, IElementType elementType, IElementType to, String message)
{
	PsiBuilder.Marker mark = builder.mark();
	if(expect(builder, elementType, message))
	{
		mark.done(to);
		return true;
	}
	else
	{
		mark.drop();
		return false;
	}
}
 
Example 16
@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 17
@NotNull
@Override
public ASTNode parse(IElementType root, PsiBuilder builder) {
	ProgressIndicatorProvider.checkCanceled();

	TokenSource source = new PSITokenSource(builder);
	TokenStream tokens = new CommonTokenStream(source);
	parser.setTokenStream(tokens);
	parser.setErrorHandler(new ErrorStrategyAdaptor()); // tweaks missing tokens
	parser.removeErrorListeners();
	parser.addErrorListener(new SyntaxErrorListener()); // trap errors
	ParseTree parseTree = null;
	PsiBuilder.Marker rollbackMarker = builder.mark();
	try {
		parseTree = parse(parser, root);
	}
	finally {
		rollbackMarker.rollbackTo();
	}

	// Now convert ANTLR parser tree to PSI tree by mimicking subtree
	// enter/exit with mark/done calls. I *think* this creates their parse
	// tree (AST as they call it) when you call {@link PsiBuilder#getTreeBuilt}
	ANTLRParseTreeToPSIConverter listener = createListener(parser, root, builder);
	PsiBuilder.Marker rootMarker = builder.mark();
	ParseTreeWalker.DEFAULT.walk(listener, parseTree);
	while (!builder.eof()) {
		ProgressIndicatorProvider.checkCanceled();
		builder.advanceLexer();
	}
	// NOTE: parse tree returned from parse will be the
	// usual ANTLR tree ANTLRParseTreeToPSIConverter will
	// convert that to the analogous jetbrains AST nodes
	// When parsing an entire file, the root IElementType
	// will be a IFileElementType.
	//
	// When trying to rename IDs and so on, you get a
	// dummy root and a type arg identifier IElementType.
	// This results in a weird tree that has for example
	// (ID (expr (primary ID))) with the ID IElementType
	// as a subtree root as well as the appropriate leaf
	// all the way at the bottom.  The dummy ID root is a
	// CompositeElement and created by
	// ParserDefinition.createElement() despite having
	// being TokenIElementType.
	rootMarker.done(root);
	return builder.getTreeBuilt(); // calls the ASTFactory.createComposite() etc...
}
 
Example 18
Source Project: netbeans-mmd-plugin   File: MMPsiParser.java    License: 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 19
Source Project: CppTools   File: CppParserDefinition.java    License: Apache License 2.0 4 votes vote down vote up
@NotNull
public ASTNode parse(IElementType iElementType, PsiBuilder psiBuilder) {
  final PsiBuilder.Marker rootMarker = psiBuilder.mark();
  final LinkedList<BlockInfo> blocks = new LinkedList<BlockInfo>();
  boolean openBlock = true;

  while (psiBuilder.getTokenType() != null) {
    if (openBlock) {
      final IElementType type = psiBuilder.getTokenType();
      if (type != CppTokenTypes.RBRACE && type != CppTokenTypes.SEMICOLON && type != CppTokenTypes.COMMA &&
        type != CppTokenTypes.LBRACE && (type != CppTokenTypes.PRE_KEYWORD || !psiBuilder.getTokenText().equals("\\"))) {
        openBlock = false;
        blocks.add(new BlockInfo(psiBuilder.mark(), blocks.size() > 0 ? BlockInfo.BlockType.STATEMENT : BlockInfo.BlockType.FUNC));
      }
    }

    final IElementType tokenType = psiBuilder.getTokenType();

    if (tokenType == CppTokenTypes.LBRACE) {
      blocks.add(new BlockInfo(psiBuilder.mark(), BlockInfo.BlockType.BLOCK));
    } else if (tokenType == CppTokenTypes.LPAR || tokenType == CppTokenTypes.LBRACKET) {
      if (blocks.size() > 0) blocks.getLast().addParens(psiBuilder.mark());
    }

    final PsiBuilder.Marker tokenMarker = requiresComposite(tokenType) ? psiBuilder.mark():null;
    psiBuilder.advanceLexer();
    if (tokenMarker != null) tokenMarker.done(tokenType);

    if (tokenType == CppTokenTypes.LBRACE) {
      openBlock = true;
    } else if (tokenType == CppTokenTypes.RPAR || tokenType == CppTokenTypes.RBRACKET) {
      if (blocks.size() > 0) blocks.getLast().doneParens();
    } else if (tokenType == CppTokenTypes.RBRACE && blocks.size() > 0) {
      blocks.removeLast().done();
      if (blocks.size() > 0) blocks.removeLast().done();
      openBlock = true;
    }
    else if (tokenType == CppTokenTypes.SEMICOLON && blocks.size() > 0 && !blocks.getLast().hasParens() && !openBlock) {
      blocks.removeLast().done();
      openBlock = true;
    }
  }

  while(blocks.size() > 0) {
    blocks.removeLast().done();
  }
  rootMarker.done(iElementType);
  return psiBuilder.getTreeBuilt();
}
 
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);
}