/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is Protege-2000.
 *
 * The Initial Developer of the Original Code is Stanford University. Portions
 * created by Stanford University are Copyright (C) 2007.  All Rights Reserved.
 *
 * Protege was developed by Stanford Medical Informatics
 * (http://www.smi.stanford.edu) at the Stanford University School of Medicine
 * with support from the National Library of Medicine, the National Science
 * Foundation, and the Defense Advanced Research Projects Agency.  Current
 * information about Protege can be obtained at http://protege.stanford.edu.
 *
 */

/* Generated By:JavaCC: Do not edit this line. DLSyntaxParser.java */
package edu.stanford.smi.protegex.owl.model.classparser.dl;

import edu.stanford.smi.protege.model.*;
import edu.stanford.smi.protegex.owl.model.*;
import edu.stanford.smi.protegex.owl.model.classparser.*;
import java.io.*;
import java.util.*;

public class DLSyntaxParser implements DLSyntaxParserConstants {

    private static DatatypeNameChecker datatypeChecker;

    private boolean create;

    private OWLModel owlModel;

    public static DatatypeNameChecker getDatatypeNameChecker() {
        return datatypeChecker;
    }

    public OWLModel getOWLModel() {
        return owlModel;
    }

    public void setCreate(boolean b) {
        this.create = b;
    }

    public static OWLClass parseExpression(OWLModel owlModel, String expression, boolean create) throws ParseException {
        DLSyntaxParserTokenManager tokMan = new DLSyntaxParserTokenManager(new JavaCharStream(new StringReader(expression)));
        tokMan.setOWLModel(owlModel);
        DLSyntaxParser parser = new DLSyntaxParser(tokMan);
        parser.setCreate(create);
        parser.owlModel = owlModel;
        datatypeChecker = new DatatypeNameChecker(owlModel);
        return parser.parse();
    }

  final public OWLClass parse() throws ParseException {
    OWLClass result;
    result = OWLUnionClass();
    jj_consume_token(0);
        {if (true) return result;}
    throw new Error("Missing return statement in function");
  }

  final public OWLClass OWLUnionClass() throws ParseException {
    ArrayList operands = new ArrayList();
    OWLClass cls;
    cls = OWLIntersectionClass();
                                 operands.add(cls);
    label_1:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case 31:
        ;
        break;
      default:
        jj_la1[0] = jj_gen;
        break label_1;
      }
      jj_consume_token(31);
      cls = OWLIntersectionClass();
                                                                                      operands.add(cls);
    }
        if(create) {
            if(operands.size() > 1) {
                {if (true) return owlModel.createOWLUnionClass(operands);}
            }
            else {
                {if (true) return cls;}
            }

        }
        else {
            {if (true) return null;}
        }
    throw new Error("Missing return statement in function");
  }

  final public OWLClass OWLIntersectionClass() throws ParseException {
    ArrayList operands = new ArrayList();
    OWLClass cls;
    cls = IntersectionClassOperand();
                                    operands.add(cls);
    label_2:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case 32:
        ;
        break;
      default:
        jj_la1[1] = jj_gen;
        break label_2;
      }
      jj_consume_token(32);
      cls = IntersectionClassOperand();
                                        operands.add(cls);
    }
        if(create) {
            if(operands.size() > 1) {
                {if (true) return owlModel.createOWLIntersectionClass(operands);}
            }
            else {
                {if (true) return cls;}
            }
        }
        {if (true) return null;}
    throw new Error("Missing return statement in function");
  }

  final public OWLClass IntersectionClassOperand() throws ParseException {
    OWLClass cls;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case CLASS_ID:
      cls = OWLNamedClass();
      break;
    case OPENPAR:
      jj_consume_token(OPENPAR);
      cls = OWLUnionClass();
      jj_consume_token(CLOSEPAR);
      break;
    case 33:
    case 34:
    case 35:
    case 36:
    case 37:
      cls = OWLRestriction();
      break;
    case 38:
      cls = OWLComplementClass();
      break;
    case 39:
      cls = OWLEnumeratedClass();
      break;
    default:
      jj_la1[2] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
        {if (true) return cls;}
    throw new Error("Missing return statement in function");
  }

  final public OWLClass OWLNamedClass() throws ParseException {
    Token t;
    t = jj_consume_token(CLASS_ID);
      try {
        String idName = ParserUtils.dequoteIdentifier(t.image);
        {if (true) return ParserUtils.getOWLClassFromName(owlModel, idName);}
      } catch (AmbiguousNameException ex) {
        {if (true) throw new ParseException(ex.getMessage());}
      }
    throw new Error("Missing return statement in function");
  }

  final public OWLClass OWLRestriction() throws ParseException {
    OWLClass cls;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case 33:
      cls = OWLAllValuesFromRestriction();
      break;
    case 34:
      cls = OWLSomeValuesFromRestriction();
      break;
    case 36:
      cls = OWLMaxCardinalityRestriction();
      break;
    case 35:
      cls = OWLMinCardinalityRestriction();
      break;
    case 37:
      cls = OWLCardinalityRestriction();
      break;
    default:
      jj_la1[3] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
        {if (true) return cls;}
    throw new Error("Missing return statement in function");
  }

///////////////////////////////////////////////////////////////////////////////
//
// AllValuesFrom
  final public OWLClass OWLAllValuesFromRestriction() throws ParseException {
    OWLClass cls;
    jj_consume_token(33);
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case DATATYPE_PROPERTY_ID:
      cls = OWLAllValuesFromDatatypePart();
      break;
    case OBJECT_PROPERTY_ID:
      cls = OWLAllValuesFromObjectPart();
      break;
    default:
      jj_la1[4] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
            {if (true) return cls;}
    throw new Error("Missing return statement in function");
  }

  final public OWLClass OWLAllValuesFromDatatypePart() throws ParseException {
    OWLDatatypeProperty prop;
    RDFSDatatype datatype;
    prop = DatatypeProperty();
    datatype = Datatype();
        if(create) {
            {if (true) return owlModel.createOWLAllValuesFrom(prop, datatype);}
        }
        {if (true) return null;}
    throw new Error("Missing return statement in function");
  }

  final public OWLClass OWLAllValuesFromObjectPart() throws ParseException {
    OWLObjectProperty prop;
    OWLClass filler;
    prop = ObjectProperty();
    filler = NestedClass();
        if(create) {
            {if (true) return owlModel.createOWLAllValuesFrom(prop, filler);}
        }
        {if (true) return null;}
    throw new Error("Missing return statement in function");
  }

///////////////////////////////////////////////////////////////////////////////
//
// SomeValuesFrom
  final public OWLClass OWLSomeValuesFromRestriction() throws ParseException {
    OWLClass cls;
    jj_consume_token(34);
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case DATATYPE_PROPERTY_ID:
      cls = OWLSomeValuesFromDatatypePart();
      break;
    case OBJECT_PROPERTY_ID:
      cls = OWLSomeValuesFromObjectPart();
      break;
    default:
      jj_la1[5] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
        {if (true) return cls;}
    throw new Error("Missing return statement in function");
  }

  final public OWLClass OWLSomeValuesFromDatatypePart() throws ParseException {
    OWLDatatypeProperty prop;
    RDFSDatatype filler;
    prop = DatatypeProperty();
    filler = Datatype();
        if(create) {
            {if (true) return owlModel.createOWLSomeValuesFrom(prop, filler);}
        }
        {if (true) return null;}
    throw new Error("Missing return statement in function");
  }

  final public OWLClass OWLSomeValuesFromObjectPart() throws ParseException {
    OWLObjectProperty prop;
    OWLClass filler;
    prop = ObjectProperty();
    filler = NestedClass();
        if(create) {
            {if (true) return owlModel.createOWLSomeValuesFrom(prop, filler);}
        }
        {if (true) return null;}
    throw new Error("Missing return statement in function");
  }

///////////////////////////////////////////////////////////////////////////////
//
// MinCardi
  final public OWLClass OWLMinCardinalityRestriction() throws ParseException {
    int card;
    OWLMinCardinality cls = null;
    jj_consume_token(35);
    card = Integer();
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case DATATYPE_PROPERTY_ID:
      cls = OWLMinCardinalityDatatypePart();
      break;
    default:
      jj_la1[7] = jj_gen;
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case OBJECT_PROPERTY_ID:
        cls = OWLMinCardinalityObjectPart();
        break;
      default:
        jj_la1[6] = jj_gen;
        ;
      }
    }
        if(create && cls != null) {
            cls.setCardinality(card);
            {if (true) return cls;}
        }
        {if (true) return null;}
    throw new Error("Missing return statement in function");
  }

  final public OWLMinCardinality OWLMinCardinalityDatatypePart() throws ParseException {
    OWLDatatypeProperty prop;
    prop = DatatypeProperty();
        if(create) {
            OWLMinCardinality minCardi = owlModel.createOWLMinCardinality();
            minCardi.setOnProperty(prop);
            {if (true) return minCardi;}
        }
        {if (true) return null;}
    throw new Error("Missing return statement in function");
  }

  final public OWLMinCardinality OWLMinCardinalityObjectPart() throws ParseException {
    OWLObjectProperty prop;
    OWLClass filler = null;
    prop = ObjectProperty();
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case OPENPAR:
    case CLASS_ID:
    case 38:
    case 39:
      filler = NestedClass();
      break;
    default:
      jj_la1[8] = jj_gen;
      ;
    }
        if(create) {
            OWLMinCardinality minCardi = owlModel.createOWLMinCardinality();
            minCardi.setOnProperty(prop);
            if(filler != null) {
                minCardi.setValuesFrom(filler);
            }
            {if (true) return minCardi;}
        }
        {if (true) return null;}
    throw new Error("Missing return statement in function");
  }

///////////////////////////////////////////////////////////////////////////////
//
// MaxCardi
  final public OWLClass OWLMaxCardinalityRestriction() throws ParseException {
    int card;
    OWLMaxCardinality cls = null;
    jj_consume_token(36);
    card = Integer();
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case DATATYPE_PROPERTY_ID:
      cls = OWLMaxCardinalityDatatypePart();
      break;
    default:
      jj_la1[10] = jj_gen;
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case OBJECT_PROPERTY_ID:
        cls = OWLMaxCardinalityObjectPart();
        break;
      default:
        jj_la1[9] = jj_gen;
        ;
      }
    }
        if(create && cls != null) {
            cls.setCardinality(card);
            {if (true) return cls;}
        }
        {if (true) return null;}
    throw new Error("Missing return statement in function");
  }

  final public OWLMaxCardinality OWLMaxCardinalityDatatypePart() throws ParseException {
    OWLDatatypeProperty prop;
    prop = DatatypeProperty();
        if(create) {
            OWLMaxCardinality maxCardi = owlModel.createOWLMaxCardinality();
            maxCardi.setOnProperty(prop);
            {if (true) return maxCardi;}
        }
        {if (true) return null;}
    throw new Error("Missing return statement in function");
  }

  final public OWLMaxCardinality OWLMaxCardinalityObjectPart() throws ParseException {
    OWLObjectProperty prop;
    OWLClass filler = null;
    prop = ObjectProperty();
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case OPENPAR:
    case CLASS_ID:
    case 38:
    case 39:
      filler = NestedClass();
      break;
    default:
      jj_la1[11] = jj_gen;
      ;
    }
        if(create) {
            OWLMaxCardinality maxCardi = owlModel.createOWLMaxCardinality();
            maxCardi.setOnProperty(prop);
            if(filler != null) {
                maxCardi.setValuesFrom(filler);
            }
            {if (true) return maxCardi;}
        }
        {if (true) return null;}
    throw new Error("Missing return statement in function");
  }

///////////////////////////////////////////////////////////////////////////////
//
// Cardi
  final public OWLClass OWLCardinalityRestriction() throws ParseException {
    int card;
    OWLCardinality cls = null;
    jj_consume_token(37);
    card = Integer();
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case DATATYPE_PROPERTY_ID:
      cls = OWLCardinalityDatatypePart();
      break;
    default:
      jj_la1[13] = jj_gen;
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case OBJECT_PROPERTY_ID:
        cls = OWLCardinalityObjectPart();
        break;
      default:
        jj_la1[12] = jj_gen;
        ;
      }
    }
        if(create && cls != null) {
            cls.setCardinality(card);
            {if (true) return cls;}
        }
        {if (true) return null;}
    throw new Error("Missing return statement in function");
  }

  final public OWLCardinality OWLCardinalityDatatypePart() throws ParseException {
    OWLDatatypeProperty prop;
    prop = DatatypeProperty();
        if(create) {
            OWLCardinality cardi = owlModel.createOWLCardinality();
            cardi.setOnProperty(prop);
            {if (true) return cardi;}
        }
        {if (true) return null;}
    throw new Error("Missing return statement in function");
  }

  final public OWLCardinality OWLCardinalityObjectPart() throws ParseException {
    OWLObjectProperty prop;
    OWLClass filler = null;
    prop = ObjectProperty();
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case OPENPAR:
    case CLASS_ID:
    case 38:
    case 39:
      filler = NestedClass();
      break;
    default:
      jj_la1[14] = jj_gen;
      ;
    }
        if(create) {
            OWLCardinality cardi = owlModel.createOWLCardinality();
            cardi.setOnProperty(prop);
            if(filler != null) {
                cardi.setValuesFrom(filler);
            }
            {if (true) return cardi;}
        }
        {if (true) return null;}
    throw new Error("Missing return statement in function");
  }

///////////////////////////////////////////////////////////////////////////////
//
// Complement class
  final public OWLClass OWLComplementClass() throws ParseException {
    OWLClass cls;
    jj_consume_token(38);
    cls = NestedClass();
        if(create) {
            {if (true) return owlModel.createOWLComplementClass(cls);}
        }
        {if (true) return null;}
    throw new Error("Missing return statement in function");
  }

///////////////////////////////////////////////////////////////////////////////
//
// Enumerated class
  final public OWLClass OWLEnumeratedClass() throws ParseException {
    OWLIndividual ind;
    ArrayList individuals = new ArrayList();
    jj_consume_token(39);
    ind = Individual();
         individuals.add(ind);
    label_3:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case INDIVIDUAL_ID:
        ;
        break;
      default:
        jj_la1[15] = jj_gen;
        break label_3;
      }
      ind = Individual();
               individuals.add(ind);
    }
    jj_consume_token(40);
        {if (true) return owlModel.createOWLEnumeratedClass(individuals);}
    throw new Error("Missing return statement in function");
  }

  final public RDFSDatatype Datatype() throws ParseException {
    Token token;
    token = jj_consume_token(DATATYPE_ID);
        String idName = ParserUtils.dequoteIdentifier(token.image);
        {if (true) return owlModel.getRDFSDatatypeByName(datatypeChecker.getDatatypeQName(idName));}
    throw new Error("Missing return statement in function");
  }

  final public OWLDatatypeProperty DatatypeProperty() throws ParseException {
    Token t;
    OWLDatatypeProperty prop;
    t = jj_consume_token(DATATYPE_PROPERTY_ID);
      try {
        String idName = ParserUtils.dequoteIdentifier(t.image);
        prop = ParserUtils.getOWLDatatypePropertyFromName(owlModel, idName);
      } catch (AmbiguousNameException ex) {
        {if (true) throw new ParseException(ex.getMessage());}
      }
      {if (true) return prop;}
    throw new Error("Missing return statement in function");
  }

  final public OWLObjectProperty ObjectProperty() throws ParseException {
    Token t;
    OWLObjectProperty prop;
    t = jj_consume_token(OBJECT_PROPERTY_ID);
       try {
           String idName = ParserUtils.dequoteIdentifier(t.image);
           prop = ParserUtils.getOWLObjectPropertyFromName(owlModel, idName);
       } catch (AmbiguousNameException ex) {
                  {if (true) throw new ParseException(ex.getMessage());}
       }
       {if (true) return prop;}
    throw new Error("Missing return statement in function");
  }

  final public OWLIndividual Individual() throws ParseException {
    Token t;
    t = jj_consume_token(INDIVIDUAL_ID);
       try {
        String idName = ParserUtils.dequoteIdentifier(t.image);
        {if (true) return ParserUtils.getOWLIndividualFromName(owlModel, idName);}
       } catch (AmbiguousNameException ex) {
          {if (true) throw new ParseException(ex.getMessage());}
       }
    throw new Error("Missing return statement in function");
  }

  final public OWLClass NestedClass() throws ParseException {
    OWLClass cls;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case CLASS_ID:
      cls = OWLNamedClass();
      break;
    case 39:
      cls = OWLEnumeratedClass();
      break;
    case 38:
      cls = OWLComplementClass();
      break;
    case OPENPAR:
      jj_consume_token(OPENPAR);
      cls = OWLUnionClass();
      jj_consume_token(CLOSEPAR);
      break;
    default:
      jj_la1[16] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
        {if (true) return cls;}
    throw new Error("Missing return statement in function");
  }

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

  public DLSyntaxParserTokenManager token_source;
  JavaCharStream jj_input_stream;
  public Token token, jj_nt;
  private int jj_ntk;
  private int jj_gen;
  final private int[] jj_la1 = new int[17];
  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[] {0x80000000,0x0,0x2000040,0x0,0xc000000,0xc000000,0x4000000,0x8000000,0x2000040,0x4000000,0x8000000,0x2000040,0x4000000,0x8000000,0x2000040,0x10000000,0x2000040,};
   }
   private static void jj_la1_1() {
      jj_la1_1 = new int[] {0x0,0x1,0xfe,0x3e,0x0,0x0,0x0,0x0,0xc0,0x0,0x0,0xc0,0x0,0x0,0xc0,0x0,0xc0,};
   }

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

  public void ReInit(java.io.InputStream stream) {
     ReInit(stream, null);
  }
  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 < 17; i++) jj_la1[i] = -1;
  }

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

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

  public void ReInit(DLSyntaxParserTokenManager tm) {
    token_source = tm;
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 17; 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[41];
    for (int i = 0; i < 41; i++) {
      la1tokens[i] = false;
    }
    if (jj_kind >= 0) {
      la1tokens[jj_kind] = true;
      jj_kind = -1;
    }
    for (int i = 0; i < 17; 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 < 41; 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() {
  }

}
