/* Generated By:JavaCC: Do not edit this line. Parser.java */
package opal.javabind;
import java.util.Vector;
import java.io.InputStream;

public class Parser implements ParserConstants {

  public static Absy parse(InputStream in) throws ParseException {
    Parser parser = new Parser(in);
    return parser.spec();
  }

/***********************************************************************/
// Syntactic Rules
  final public Absy spec() throws ParseException {
  String system;
  String subsys;
  String option;
  Vector uses = new java.util.Vector();
  Vector options = new java.util.Vector();
  Vector items = new java.util.Vector();
  String[] classes;
  String struct;
  Absy.BindKind kind;
    jj_consume_token(SYSTEM);
    system = arg();
    label_1:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case OPTION:
      case USES:
      case CLUSTER:
      case BIND:
        ;
        break;
      default:
        jj_la1[0] = jj_gen;
        break label_1;
      }
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case BIND:
        jj_consume_token(BIND);
        classes = classSpec();
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case IN:
          jj_consume_token(IN);
          struct = arg();
          break;
        default:
          jj_la1[1] = jj_gen;
                             struct = null;
        }
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case AS:
          jj_consume_token(AS);
          kind = bindKind();
          break;
        default:
          jj_la1[2] = jj_gen;
                                kind = Absy.BindKind.Reference;
        }
       items.addElement(Absy.Item.newBind(classes, struct, kind));
        break;
      case USES:
        jj_consume_token(USES);
        subsys = arg();
      uses.addElement(subsys);
        break;
      case CLUSTER:
        jj_consume_token(CLUSTER);
        classes = classSpec();
        jj_consume_token(IN);
        struct = arg();
       items.addElement(Absy.Item.newCluster(classes, struct));
        break;
      case OPTION:
        jj_consume_token(OPTION);
        option = arg();
      options.addElement(option);
        break;
      default:
        jj_la1[3] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
    }
    jj_consume_token(0);
      String[] usesa = new String[uses.size()];
      uses.copyInto(usesa);
      String[] optionsa = new String[options.size()];
      options.copyInto(optionsa);
      Absy.Item[] itemsa = new Absy.Item[items.size()];
      items.copyInto(itemsa);
      {if (true) return Absy.newAbsy(system, optionsa, usesa, itemsa);}
    throw new Error("Missing return statement in function");
  }

  final public String[] classSpec() throws ParseException {
  Vector specs = new Vector();
  String spec;
    spec = arg();
                specs.addElement(spec);
    label_2:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case COMMA:
        ;
        break;
      default:
        jj_la1[4] = jj_gen;
        break label_2;
      }
      jj_consume_token(COMMA);
      spec = arg();
                        specs.addElement(spec);
    }
      String[] specsa = new String[specs.size()];
      specs.copyInto(specsa);
      {if (true) return specsa;}
    throw new Error("Missing return statement in function");
  }

  final public Absy.BindKind bindKind() throws ParseException {
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case IMMUTABLE:
      jj_consume_token(IMMUTABLE);
                {if (true) return Absy.BindKind.Immutable;}
      break;
    case REFERENCE:
      jj_consume_token(REFERENCE);
                {if (true) return Absy.BindKind.Reference;}
      break;
    case FREETYPE:
      jj_consume_token(FREETYPE);
                {if (true) return Absy.BindKind.FreeType;}
      break;
    default:
      jj_la1[5] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    throw new Error("Missing return statement in function");
  }

  final public String arg() throws ParseException {
  Token t;
    t = jj_consume_token(ARG);
      {if (true) return t.image.substring(1, t.image.length()-1);}
    throw new Error("Missing return statement in function");
  }

  public ParserTokenManager token_source;
  ASCII_UCodeESC_CharStream jj_input_stream;
  public Token token, jj_nt;
  private int jj_ntk;
  private int jj_gen;
  final private int[] jj_la1 = new int[6];
  final private int[] jj_la1_0 = {0x1e00,0x4000,0x2000,0x1e00,0x80000,0x38000,};

  public Parser(java.io.InputStream stream) {
    jj_input_stream = new ASCII_UCodeESC_CharStream(stream, 1, 1);
    token_source = new ParserTokenManager(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 6; 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 < 6; i++) jj_la1[i] = -1;
  }

  public Parser(java.io.Reader stream) {
    jj_input_stream = new ASCII_UCodeESC_CharStream(stream, 1, 1);
    token_source = new ParserTokenManager(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 6; 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 < 6; i++) jj_la1[i] = -1;
  }

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

  public void ReInit(ParserTokenManager tm) {
    token_source = tm;
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 6; 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;

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

}
