/* Generated By:JavaCC: Do not edit this line. SqlGrammar.java */
package org.apache.beehive.controls.system.jdbc.parser;
import java.io.StringReader;

public class SqlGrammar implements SqlGrammarConstants {
    private StringBuilder buffer = new StringBuilder();
    public static void main(String[] args) throws Exception
    {
        SqlGrammar parser = new SqlGrammar(new StringReader(args[0]));
        SqlStatement statement = parser.parse();
        System.out.println(statement.toString());
    }

//
// Parse methods
//


//
// Main parse method
//
  final public SqlStatement parse() throws ParseException {
    SqlStatement statement = new SqlStatement();
    SqlFragment frag;
    Token t, sq;
    label_1:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case NON_EXPRESSION_TEXT:
      case START_EXPRESSION:
      case SQUOTE:
        ;
        break;
      default:
        jj_la1[0] = jj_gen;
        break label_1;
      }
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case NON_EXPRESSION_TEXT:
        t = jj_consume_token(NON_EXPRESSION_TEXT);
                                 statement.addChild(new LiteralFragment(t.image));
        break;
      case SQUOTE:
        sq = jj_consume_token(SQUOTE);
        t = jj_consume_token(STRING_LITERAL);
                                       statement.addChild(new LiteralFragment(sq.image + t.image));
        break;
      case START_EXPRESSION:
        frag = parseExpression();
                                statement.addChild(frag);
        break;
      default:
        jj_la1[1] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
    }
    jj_consume_token(0);
      {if (true) return statement;}
    throw new Error("Missing return statement in function");
  }

//
// Parse an expression delimited by '{}'
//
  final public SqlFragment parseExpression() throws ParseException {
    Token t, tt = null;
    SqlFragment frag = null;
    jj_consume_token(START_EXPRESSION);
    label_2:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case WHITESPACE:
        ;
        break;
      default:
        jj_la1[2] = jj_gen;
        break label_2;
      }
      jj_consume_token(WHITESPACE);
    }
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case SQL_ESCAPE:
    case SQL_SUBST:
    case SQL_FN:
      frag = parseSqlEscape();
      break;
    case JDBC_CALL:
    case JDBC_RET:
    case JDBC_DATE:
    case JDBC_TIME:
    case JDBC_TIMESTAMP:
    case JDBC_FUNCTION:
    case JDBC_ESCAPE:
    case JDBC_OUTERJOIN:
      frag = parseJdbcEscape();
      break;
    case IDENTIFIER:
      t = jj_consume_token(IDENTIFIER);
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case REFLECT_SEP:
        jj_consume_token(REFLECT_SEP);
        tt = jj_consume_token(IDENTIFIER);
        break;
      default:
        jj_la1[3] = jj_gen;
        ;
      }
      jj_consume_token(END_EXPRESSION);
                                                                         frag = new ReflectionFragment(t.image,(tt == null) ? null : tt.image);
      break;
    default:
      jj_la1[4] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    label_3:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case WHITESPACE:
        ;
        break;
      default:
        jj_la1[5] = jj_gen;
        break label_3;
      }
      jj_consume_token(WHITESPACE);
    }
     {if (true) return frag;}
    throw new Error("Missing return statement in function");
  }

//
// parse an sql: escape sequence
//
  final public SqlSubstitutionFragment parseSqlEscape() throws ParseException {
    Token id1, id2;
    Token t = null;
    SqlSubstitutionFragment frag;
    String func = null;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case SQL_ESCAPE:
    case SQL_SUBST:
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case SQL_ESCAPE:
        jj_consume_token(SQL_ESCAPE);
        break;
      case SQL_SUBST:
        jj_consume_token(SQL_SUBST);
        break;
      default:
        jj_la1[6] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
      t = jj_consume_token(IDENTIFIER);
           if (t != null) {
             frag = new SqlSubstitutionFragment(new ReflectionFragment(t.image));
           } else {
             frag = new SqlSubstitutionFragment(new LiteralFragment(func));
           }
      break;
    case SQL_FN:
      jj_consume_token(SQL_FN);
      label_4:
      while (true) {
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case SQL_FN_WHITESPACE:
          ;
          break;
        default:
          jj_la1[7] = jj_gen;
          break label_4;
        }
        jj_consume_token(SQL_FN_WHITESPACE);
      }
      jj_consume_token(SQL_FN_NM);
      label_5:
      while (true) {
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case SQL_FN_WHITESPACE:
          ;
          break;
        default:
          jj_la1[8] = jj_gen;
          break label_5;
        }
        jj_consume_token(SQL_FN_WHITESPACE);
      }
      jj_consume_token(SQL_FN_PAREN);
      label_6:
      while (true) {
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case SQL_FN_WHITESPACE:
          ;
          break;
        default:
          jj_la1[9] = jj_gen;
          break label_6;
        }
        jj_consume_token(SQL_FN_WHITESPACE);
      }
      id1 = jj_consume_token(SQL_FN_IDENTIFIER);
      label_7:
      while (true) {
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case SQL_FN_WHITESPACE:
          ;
          break;
        default:
          jj_la1[10] = jj_gen;
          break label_7;
        }
        jj_consume_token(SQL_FN_WHITESPACE);
      }
      jj_consume_token(SQL_FN_COMMA);
      label_8:
      while (true) {
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case SQL_FN_WHITESPACE:
          ;
          break;
        default:
          jj_la1[11] = jj_gen;
          break label_8;
        }
        jj_consume_token(SQL_FN_WHITESPACE);
      }
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case SQL_FN_PSTART:
        t = jj_consume_token(SQL_FN_PSTART);
        break;
      default:
        jj_la1[12] = jj_gen;
        ;
      }
      id2 = jj_consume_token(SQL_FN_IDENTIFIER);
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case SQL_FN_PEND:
        jj_consume_token(SQL_FN_PEND);
        break;
      default:
        jj_la1[13] = jj_gen;
        ;
      }
      label_9:
      while (true) {
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case SQL_FN_WHITESPACE:
          ;
          break;
        default:
          jj_la1[14] = jj_gen;
          break label_9;
        }
        jj_consume_token(SQL_FN_WHITESPACE);
      }
      jj_consume_token(SQL_FN_END);
         if (t == null) {
           frag = new SqlSubstitutionFragment(new LiteralFragment("(" + id1.image + " IN (" + id2.image + ")"));
         } else {
           frag = new SqlSubstitutionFragment(new LiteralFragment("(" + id1.image + " IN ("),
                                        new ReflectionFragment(id2.image),
                                        new LiteralFragment("))"));
         }
      break;
    default:
      jj_la1[15] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    jj_consume_token(END_EXPRESSION);
      {if (true) return frag;}
    throw new Error("Missing return statement in function");
  }

//
// parse a param sub inside of a jdbc escape
//
  final public SqlFragment parseReflect() throws ParseException {
   Token t, tt = null;
   SqlFragment frag;
    jj_consume_token(JDBC_PARAM);
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case PARAM_IDENTIFIER:
      t = jj_consume_token(PARAM_IDENTIFIER);
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case PARAM_REFLECT_SEP:
        jj_consume_token(PARAM_REFLECT_SEP);
        tt = jj_consume_token(IDENTIFIER);
        break;
      default:
        jj_la1[16] = jj_gen;
        ;
      }
            frag = new ReflectionFragment(t.image, (tt == null) ? null : tt.image);
      break;
    case PARAM_LITERAL:
      t = jj_consume_token(PARAM_LITERAL);
              frag = new LiteralFragment(t.image);
      break;
    default:
      jj_la1[17] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    jj_consume_token(PARAM_END);
      {if (true) return frag;}
    throw new Error("Missing return statement in function");
  }

//
// parse a jdbc escape sequence
//
  final public JdbcFragment parseJdbcEscape() throws ParseException {
  Token lit, c;
  JdbcFragment jfrag = new JdbcFragment();
  jfrag.addChild(new LiteralFragment("{"));
  SqlFragment frag = null;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case JDBC_CALL:
      c = jj_consume_token(JDBC_CALL);
      break;
    case JDBC_RET:
      c = jj_consume_token(JDBC_RET);
      break;
    case JDBC_DATE:
      c = jj_consume_token(JDBC_DATE);
      break;
    case JDBC_ESCAPE:
      c = jj_consume_token(JDBC_ESCAPE);
      break;
    case JDBC_FUNCTION:
      c = jj_consume_token(JDBC_FUNCTION);
      break;
    case JDBC_TIME:
      c = jj_consume_token(JDBC_TIME);
      break;
    case JDBC_TIMESTAMP:
      c = jj_consume_token(JDBC_TIMESTAMP);
      break;
    case JDBC_OUTERJOIN:
      c = jj_consume_token(JDBC_OUTERJOIN);
      break;
    default:
      jj_la1[18] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
         jfrag.addChild(new LiteralFragment(c.image));
    label_10:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case JDBC_LIT:
        lit = jj_consume_token(JDBC_LIT);
                           jfrag.addChild(new LiteralFragment(lit.image));
        break;
      case JDBC_PARAM:
        frag = parseReflect();
                                  jfrag.addChild(frag);
        break;
      default:
        jj_la1[19] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case JDBC_LIT:
      case JDBC_PARAM:
        ;
        break;
      default:
        jj_la1[20] = jj_gen;
        break label_10;
      }
    }
    jj_consume_token(JDBC_END);
      jfrag.addChild(new LiteralFragment("}"));
      {if (true) return jfrag;}
    throw new Error("Missing return statement in function");
  }

  public SqlGrammarTokenManager token_source;
  SimpleCharStream jj_input_stream;
  public Token token, jj_nt;
  private int jj_ntk;
  private int jj_gen;
  final private int[] jj_la1 = new int[21];
  static private int[] jj_la1_0;
  static private int[] jj_la1_1;
  static {
      jj_la1_0();
      jj_la1_1();
   }
   private static void jj_la1_0() {
      jj_la1_0 = new int[] {0xe,0xe,0x100000,0x100,0x2ffe00,0x100000,0x600,0x40000000,0x40000000,0x40000000,0x40000000,0x40000000,0x10000000,0x20000000,0x40000000,0xe00,0x0,0x0,0xff000,0x0,0x0,};
   }
   private static void jj_la1_1() {
      jj_la1_1 = new int[] {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x10,0x28,0x0,0x6,0x6,};
   }

  public SqlGrammar(java.io.InputStream stream) {
    jj_input_stream = new SimpleCharStream(stream, 1, 1);
    token_source = new SqlGrammarTokenManager(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 21; i++) jj_la1[i] = -1;
  }

  public void ReInit(java.io.InputStream stream) {
    jj_input_stream.ReInit(stream, 1, 1);
    token_source.ReInit(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 21; i++) jj_la1[i] = -1;
  }

  public SqlGrammar(java.io.Reader stream) {
    jj_input_stream = new SimpleCharStream(stream, 1, 1);
    token_source = new SqlGrammarTokenManager(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 21; i++) jj_la1[i] = -1;
  }

  public void ReInit(java.io.Reader stream) {
    jj_input_stream.ReInit(stream, 1, 1);
    token_source.ReInit(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 21; i++) jj_la1[i] = -1;
  }

  public SqlGrammar(SqlGrammarTokenManager tm) {
    token_source = tm;
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 21; i++) jj_la1[i] = -1;
  }

  public void ReInit(SqlGrammarTokenManager tm) {
    token_source = tm;
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 21; i++) jj_la1[i] = -1;
  }

  final private Token jj_consume_token(int kind) throws ParseException {
    Token oldToken;
    if ((oldToken = token).next != null) token = token.next;
    else token = token.next = token_source.getNextToken();
    jj_ntk = -1;
    if (token.kind == kind) {
      jj_gen++;
      return token;
    }
    token = oldToken;
    jj_kind = kind;
    throw generateParseException();
  }

  final public Token getNextToken() {
    if (token.next != null) token = token.next;
    else token = token.next = token_source.getNextToken();
    jj_ntk = -1;
    jj_gen++;
    return token;
  }

  final public Token getToken(int index) {
    Token t = token;
    for (int i = 0; i < index; i++) {
      if (t.next != null) t = t.next;
      else t = t.next = token_source.getNextToken();
    }
    return t;
  }

  final private int jj_ntk() {
    if ((jj_nt=token.next) == null)
      return (jj_ntk = (token.next=token_source.getNextToken()).kind);
    else
      return (jj_ntk = jj_nt.kind);
  }

  private java.util.Vector jj_expentries = new java.util.Vector();
  private int[] jj_expentry;
  private int jj_kind = -1;

  public ParseException generateParseException() {
    jj_expentries.removeAllElements();
    boolean[] la1tokens = new boolean[39];
    for (int i = 0; i < 39; i++) {
      la1tokens[i] = false;
    }
    if (jj_kind >= 0) {
      la1tokens[jj_kind] = true;
      jj_kind = -1;
    }
    for (int i = 0; i < 21; i++) {
      if (jj_la1[i] == jj_gen) {
        for (int j = 0; j < 32; j++) {
          if ((jj_la1_0[i] & (1<<j)) != 0) {
            la1tokens[j] = true;
          }
          if ((jj_la1_1[i] & (1<<j)) != 0) {
            la1tokens[32+j] = true;
          }
        }
      }
    }
    for (int i = 0; i < 39; i++) {
      if (la1tokens[i]) {
        jj_expentry = new int[1];
        jj_expentry[0] = i;
        jj_expentries.addElement(jj_expentry);
      }
    }
    int[][] exptokseq = new int[jj_expentries.size()][];
    for (int i = 0; i < jj_expentries.size(); i++) {
      exptokseq[i] = (int[])jj_expentries.elementAt(i);
    }
    return new ParseException(token, exptokseq, tokenImage);
  }

  final public void enable_tracing() {
  }

  final public void disable_tracing() {
  }

}
