/*
 * Copyright 2000-2009 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.lexer;

import com.intellij.psi.tree.IElementType;

import java.io.IOException;

/**
 * @author max
 */
public class FlexAdapter extends LexerBase {
  private FlexLexer myFlex = null;
  private IElementType myTokenType = null;
  private CharSequence myText;

  private int myEnd;
  private int myState;

  public FlexAdapter(final FlexLexer flex) {
    myFlex = flex;
  }

  public FlexLexer getFlex() {
    return myFlex;
  }

  public void start(final CharSequence buffer, int startOffset, int endOffset, final int initialState) {
    myText = buffer;
    myEnd = endOffset;
    try {
      myFlex.reset(myText, startOffset, endOffset, initialState);
    } catch(AbstractMethodError ame) {
      // Demetra compatibility
      myFlex.reset(myText.subSequence(startOffset, endOffset), initialState);
    }
    myTokenType = null;
  }

  public int getState() {
    locateToken();
    return myState;
  }

  public IElementType getTokenType() {
    locateToken();
    return myTokenType;
  }

  public int getTokenStart() {
    locateToken();
    return myFlex.getTokenStart();
  }

  public int getTokenEnd() {
    locateToken();
    return myFlex.getTokenEnd();
  }

  public void advance() {
    locateToken();
    myTokenType = null;
  }

  public CharSequence getBufferSequence() {
    return myText;
  }

  public int getBufferEnd() {
    return myEnd;
  }

  private void locateToken() {
    if (myTokenType != null) return;
    try {
      myState = myFlex.yystate();
      myTokenType = myFlex.advance();
    }
    catch (IOException e) { /*Can't happen*/ }
  }
}

@@ -623,16 +623,76 @@
 lexer;%0A%0A
+import com.intellij.openapi.application.ApplicationManager;%0A
 import c
@@ -2242,14 +2242,240 @@
 pen*/ %7D%0A
+    catch (Error e) %7B%0A      // add lexer class name to the error%0A      final Error error = new Error(myFlex.getClass().getName() + %22: %22 + e.getMessage());%0A      error.setStackTrace(e.getStackTrace());%0A      throw error;%0A    %7D%0A
   %7D%0A%7D%0A

add lexer class name to the error