private ASTNode createAST(String text) { ParserDefinition definition = new BuildParserDefinition(); PsiParser parser = definition.createParser(getProject()); Lexer lexer = definition.createLexer(getProject()); PsiBuilderImpl psiBuilder = new PsiBuilderImpl( getProject(), null, definition, lexer, new CharTableImpl(), text, null, null); PsiBuilderAdapter adapter = new PsiBuilderAdapter(psiBuilder) { @Override public void error(String messageText) { super.error(messageText); errors.add(messageText); } }; return parser.parse(definition.getFileNodeType(), adapter); }
public static PsiBuilder stoppingBuilder(final PsiBuilder builder, final int stopAt) { return new PsiBuilderAdapter(builder) { @Override public IElementType getTokenType() { return getCurrentOffset() < stopAt ? super.getTokenType() : null; } @Override public boolean eof() { return getCurrentOffset() >= stopAt || super.eof(); } }; }