/* Generated By:JavaCC: Do not edit this line. FormatParser.java */
package org.j_paine.formatter;

class FormatParser implements FormatParserConstants {

  static final public int Integer() throws ParseException {
  Token t;
    t = jj_consume_token(INTEGER);
    {if (true) return (Integer.valueOf(t.image)).intValue();}
    throw new Error("Missing return statement in function");
  }

/* I split FormatIOElement into FormatIOElementFloat and
 * FormatIOElementNonFloat because a floating point edit
 * descriptor (F, E, D, or G) may follow a P edit descriptor
 * without a comma. --kgs
 */
  static final public FormatElement FormatIOElementFloat() throws ParseException {
  FormatElement fe;
  int w, d, m;
  w = d = m = -1;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case F_DESC:
      jj_consume_token(F_DESC);
      w = Integer();
      jj_consume_token(13);
      d = Integer();
                                              fe=new FormatF(w,d);
      break;
    case D_DESC:
      jj_consume_token(D_DESC);
      w = Integer();
      jj_consume_token(13);
      d = Integer();
                                              fe=new FormatE(w,d);
      break;
    case E_DESC:
      jj_consume_token(E_DESC);
      w = Integer();
      jj_consume_token(13);
      d = Integer();
                                              fe=new FormatE(w,d);
      break;
    case G_DESC:
      jj_consume_token(G_DESC);
      w = Integer();
      jj_consume_token(13);
      d = Integer();
                                              fe=new FormatE(w,d);
      break;
    default:
      jj_la1[0] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    {if (true) return fe;}
    throw new Error("Missing return statement in function");
  }

  static final public FormatElement FormatIOElementNonFloat() throws ParseException {
  FormatElement fe;
  int w, d, m;
  w = d = m = -1;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case A_DESC:
      jj_consume_token(A_DESC);
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case INTEGER:
        w = Integer();
        break;
      default:
        jj_la1[1] = jj_gen;
        ;
      }
                                              fe=new FormatA(w);
      break;
    case I_DESC:
      jj_consume_token(I_DESC);
      w = Integer();
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case 13:
        jj_consume_token(13);
        m = Integer();
        break;
      default:
        jj_la1[2] = jj_gen;
        ;
      }
                                              fe=new FormatI(w);
      break;
    case L_DESC:
      jj_consume_token(L_DESC);
      w = Integer();
                                              fe=new FormatL(w);
      break;
    default:
      jj_la1[3] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    {if (true) return fe;}
    throw new Error("Missing return statement in function");
  }

// This represents a format element that transfers one
// data item.
  static final public FormatElement FormatNonIOElement() throws ParseException {
    jj_consume_token(X_DESC);
                {if (true) return new FormatX();}
    throw new Error("Missing return statement in function");
  }

// This represents a format element that doesn't transfer
// any data items.
  static final public FormatElement FormatElement() throws ParseException {
  FormatElement fe;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case F_DESC:
    case D_DESC:
    case E_DESC:
    case G_DESC:
      fe = FormatIOElementFloat();
      break;
    case A_DESC:
    case I_DESC:
    case L_DESC:
      fe = FormatIOElementNonFloat();
      break;
    case X_DESC:
      fe = FormatNonIOElement();
      break;
    case P_DESC:
      fe = FormatScale();
      break;
    default:
      jj_la1[4] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    {if (true) return fe;}
    throw new Error("Missing return statement in function");
  }

  static final public FormatElement FormatScale() throws ParseException {
  FormatElement fe = null;
  int r=1;
    jj_consume_token(P_DESC);
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case INTEGER:
    case F_DESC:
    case D_DESC:
    case E_DESC:
    case G_DESC:
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case INTEGER:
        r = Integer();
        break;
      default:
        jj_la1[5] = jj_gen;
        ;
      }
      fe = FormatIOElementFloat();
      break;
    default:
      jj_la1[6] = jj_gen;
      ;
    }
    {if (true) return new FormatP(r, fe);}
    throw new Error("Missing return statement in function");
  }

  static final public FormatSlash FormatSlash() throws ParseException {
    jj_consume_token(14);
        {if (true) return new FormatSlash();}
    throw new Error("Missing return statement in function");
  }

// These are a special case. Unlike other format elements,
// Fortran permits several slashes to be concatenated without
// commas to separate them, and you can't use a repetition
// factor on them.
  static final public FormatString FormatString() throws ParseException {
  Token t;
  String s;
    t = jj_consume_token(STRING);
    s = t.image;
    s = s.substring(1,s.length()-1); // Remove the quotes.
    {if (true) return new FormatString(s);}
    throw new Error("Missing return statement in function");
  }

// Another special case that can't be repeated, and can be
// concatenated to other elements without commas.
  static final public void OptionalFormatSlashesOrStrings(Format f) throws ParseException {
  FormatUniv fs;
    label_1:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case STRING:
      case 14:
        ;
        break;
      default:
        jj_la1[7] = jj_gen;
        break label_1;
      }
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case 14:
        fs = FormatSlash();
        break;
      case STRING:
        fs = FormatString();
        break;
      default:
        jj_la1[8] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
                                             f.addElement(fs);
    }
  }

  static final public FormatRepeatedItem FormatRepeatedItem() throws ParseException {
  int r=1;
  FormatUniv fu;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case INTEGER:
      r = Integer();
      break;
    default:
      jj_la1[9] = jj_gen;
      ;
    }
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case 15:
      jj_consume_token(15);
      fu = Format();
      jj_consume_token(16);
      break;
    case A_DESC:
    case P_DESC:
    case X_DESC:
    case I_DESC:
    case F_DESC:
    case D_DESC:
    case E_DESC:
    case G_DESC:
    case L_DESC:
      fu = FormatElement();
      break;
    default:
      jj_la1[10] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    /* here we check whether the parsed format element is a P edit
     * descriptor.  in that case, it may have parsed a floating point
     * edit descriptor along with it (if it followed without a comma)
     * so return that element here.  --kgs
     */

    if(fu instanceof FormatP) {
      FormatRepeatedItem ritem;

      ritem = ((FormatP)fu).getRepeatedItem();

      if(ritem != null)
        {if (true) return ritem;}
      else
        {if (true) return new FormatRepeatedItem( r, fu );}
    }
    else
      {if (true) return new FormatRepeatedItem( r, fu );}
    throw new Error("Missing return statement in function");
  }

  static final public void FormatGroup(Format f) throws ParseException {
  FormatRepeatedItem fri;
    OptionalFormatSlashesOrStrings(f);
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case INTEGER:
    case A_DESC:
    case P_DESC:
    case X_DESC:
    case I_DESC:
    case F_DESC:
    case D_DESC:
    case E_DESC:
    case G_DESC:
    case L_DESC:
    case 15:
      fri = FormatRepeatedItem();
                                    if(fri != null) f.addElement(fri);
      OptionalFormatSlashesOrStrings(f);
      break;
    default:
      jj_la1[11] = jj_gen;
      ;
    }
  }

// This rather messy syntax allows us to have slashes and/or
// strings either side of a format element or repeated group
// without needing to separate them from each other or the element
// with commas.
// It also means that we can have empty format groups and format
// groups that don't transfer any data elements. So for example,
// the format ,/, is valid under this grammar.
  static final public Format Format() throws ParseException {
  FormatRepeatedItem fri;
  Format f = new Format();
    FormatGroup(f);
    label_2:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case 17:
        ;
        break;
      default:
        jj_la1[12] = jj_gen;
        break label_2;
      }
      jj_consume_token(17);
      FormatGroup(f);
    }
    {if (true) return f;}
    throw new Error("Missing return statement in function");
  }

  static private boolean jj_initialized_once = false;
  static public FormatParserTokenManager token_source;
  static SimpleCharStream jj_input_stream;
  static public Token token, jj_nt;
  static private int jj_ntk;
  static private int jj_gen;
  static final private int[] jj_la1 = new int[13];
  static private int[] jj_la1_0;
  static {
      jj_la1_0();
   }
   private static void jj_la1_0() {
      jj_la1_0 = new int[] {0xf00,0x4,0x2000,0x1090,0x1ff0,0x4,0xf04,0x4008,0x4008,0x4,0x9ff0,0x9ff4,0x20000,};
   }

  public FormatParser(java.io.InputStream stream) {
     this(stream, null);
  }
  public FormatParser(java.io.InputStream stream, String encoding) {
    if (jj_initialized_once) {
      System.out.println("ERROR: Second call to constructor of static parser.  You must");
      System.out.println("       either use ReInit() or set the JavaCC option STATIC to false");
      System.out.println("       during parser generation.");
      throw new Error();
    }
    jj_initialized_once = true;
    try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
    token_source = new FormatParserTokenManager(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 13; i++) jj_la1[i] = -1;
  }

  static public void ReInit(java.io.InputStream stream) {
     ReInit(stream, null);
  }
  static public void ReInit(java.io.InputStream stream, String encoding) {
    try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
    token_source.ReInit(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 13; i++) jj_la1[i] = -1;
  }

  public FormatParser(java.io.Reader stream) {
    if (jj_initialized_once) {
      System.out.println("ERROR: Second call to constructor of static parser.  You must");
      System.out.println("       either use ReInit() or set the JavaCC option STATIC to false");
      System.out.println("       during parser generation.");
      throw new Error();
    }
    jj_initialized_once = true;
    jj_input_stream = new SimpleCharStream(stream, 1, 1);
    token_source = new FormatParserTokenManager(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 13; i++) jj_la1[i] = -1;
  }

  static 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 < 13; i++) jj_la1[i] = -1;
  }

  public FormatParser(FormatParserTokenManager tm) {
    if (jj_initialized_once) {
      System.out.println("ERROR: Second call to constructor of static parser.  You must");
      System.out.println("       either use ReInit() or set the JavaCC option STATIC to false");
      System.out.println("       during parser generation.");
      throw new Error();
    }
    jj_initialized_once = true;
    token_source = tm;
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 13; i++) jj_la1[i] = -1;
  }

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

  static 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();
  }

  static 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;
  }

  static 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;
  }

  static 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);
  }

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

  static public ParseException generateParseException() {
    jj_expentries.removeAllElements();
    boolean[] la1tokens = new boolean[18];
    for (int i = 0; i < 18; i++) {
      la1tokens[i] = false;
    }
    if (jj_kind >= 0) {
      la1tokens[jj_kind] = true;
      jj_kind = -1;
    }
    for (int i = 0; i < 13; 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;
          }
        }
      }
    }
    for (int i = 0; i < 18; 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);
  }

  static final public void enable_tracing() {
  }

  static final public void disable_tracing() {
  }

}
