ASTNode (final ASTNode chameleon, final BasicJavaParserUtil.ParserWrapper wrapper, final boolean eatAll, final LanguageLevel level, Function<LanguageLevel, JavaDocLexer> javaDocLexer, Function<LanguageLevel, BasicJavaLexer> javaLexer) { final PsiElement psi = chameleon.getTreeParent() != null ? chameleon.getTreeParent().getPsi() : chameleon.getPsi(); assert psi != null : chameleon; final Project project = psi.getProject(); final PsiBuilderFactory factory = PsiBuilderFactory.getInstance(); final Lexer lexer = BasicJavaAstTreeUtil.is(chameleon, BASIC_DOC_COMMENT) ? javaDocLexer.apply(level) : javaLexer.apply(level); final PsiBuilder builder = factory.createBuilder(project, chameleon, lexer, chameleon.getElementType().getLanguage(), chameleon.getChars()); setLanguageLevel(builder, level); final PsiBuilder.Marker root = builder.mark(); wrapper.parse(builder); if (!builder.eof()) { if (!eatAll) throw new AssertionError("Unexpected token: '" + builder.getTokenText() + "'"); final PsiBuilder.Marker extras = builder.mark(); while (!builder.eof()) builder.advanceLexer(); extras.error(JavaPsiBundle.message("unexpected.tokens")); } root.done(chameleon.getElementType()); return builder.getTreeBuilt().getFirstChildNode(); }