/*
 * Copyright 2000-2015 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.psi.tree;

import com.intellij.lang.*;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * A token type which represents a fragment of text (possibly in a different language)
 * which is not parsed during the current lexer or parser pass and can be parsed later when
 * its contents is requested.
 *
 * @author max
 */

public class ILazyParseableElementType extends IElementType {

  public static final Key<Language> LANGUAGE_KEY = Key.create("LANGUAGE_KEY");

  public ILazyParseableElementType(@NotNull @NonNls final String debugName) {
    this(debugName, null);
  }

  public ILazyParseableElementType(@NotNull @NonNls final String debugName, @Nullable final Language language) {
    super(debugName, language);
  }

  public ILazyParseableElementType(@NotNull @NonNls final String debugName, @Nullable final Language language, final boolean register) {
    super(debugName, language, register);
  }

  /**
   * Parses the contents of the specified chameleon node and returns the AST tree
   * representing the parsed contents.
   *
   * @param chameleon the node to parse.
   * @return the parsed contents of the node.
   */
  public ASTNode parseContents(final ASTNode chameleon) {
    final PsiElement parentElement = chameleon.getTreeParent().getPsi();
    assert parentElement != null : "Bad chameleon: " + chameleon;
    return doParseContents(chameleon, parentElement);
  }

  protected ASTNode doParseContents(@NotNull final ASTNode chameleon, @NotNull final PsiElement psi) {
    Project project = psi.getProject();
    Language languageForParser = getLanguageForParser(psi);
    PsiBuilder builder = PsiBuilderFactory.getInstance().createBuilder(project, chameleon, null, languageForParser, chameleon.getChars());
    PsiParser parser = LanguageParserDefinitions.INSTANCE.forLanguage(languageForParser).createParser(project);
    ASTNode node = parser.parse(this, builder);
    return node.getFirstChildNode();
  }

  protected Language getLanguageForParser(PsiElement psi) {
    return getLanguage();
  }

  @Nullable
  public ASTNode createNode(CharSequence text) {
    return null;
  }
}

@@ -1723,16 +1723,770 @@
 );%0A  %7D%0A%0A
+  /**%0A   * Parses the contents of the specified chameleon node and returns PsiBuilder.%0A   * In future this method should deprecate all other parsing methods: parseContents(), doParseContents(), etc.%0A   * It provides more flexible and CPU/memory efficient access to parser algorithms for all needs:%0A   * editing, indexing and analysis.%0A   * %3Cp/%3E%0A   *%0A   * The parseContent() implementation in terms of parseLight() is just the following:%0A   * %7B@code%7DparseLight().getTreeBuilt().getFirstChildNode()%7B@code%7D%0A   *%0A   * @param chameleon the node to parse.%0A   * @return the parsed contents of the node in the form PsiBuilder.%0A   */%0A  public PsiBuilder parseLight(ASTNode chameleon) %7B%0A    throw new UnsupportedOperationException(String.valueOf(chameleon));%0A  %7D%0A%0A
   /**%0A  
@@ -2733,22 +2733,16 @@
 ontents(
-final 
 ASTNode 
@@ -2758,22 +2758,16 @@
 ) %7B%0A    
-final 
 PsiEleme
@@ -2861,21 +2861,26 @@
  : %22
-Bad chameleon
+parent psi is null
 : %22 
@@ -2996,22 +2996,16 @@
 NotNull 
-final 
 ASTNode 
@@ -3024,22 +3024,16 @@
 NotNull 
-final 
 PsiEleme

introduce ILazyParsable#parseLight()