/FileExtraction/Java_unseen/rubato-composer_rubato-composer/java/src/org/rubato/rubettes/denotex/DenotexParser.java
/* Generated By:JavaCC: Do not edit this line. DenotexParser.java */
package org.rubato.rubettes.denotex;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.rubato.base.Repository;
import org.rubato.math.arith.Complex;
import org.rubato.math.arith.Rational;
import org.rubato.math.module.*;
import org.rubato.math.yoneda.*;

@SuppressWarnings("all")
public final class DenotexParser implements DenotexParserConstants {

    // public constructors

    public DenotexParser(Reader input, Symboltable t) {
        this(input, t, Repository.systemRepository());
    }


    public DenotexParser(Reader input, Symboltable t, Repository rep) {
        this(input);
        this.t = t;
        this.url = null;
        this.rep = rep;
    }


    public DenotexParser(InputStream input, Symboltable t) {
        this(input, t, Repository.systemRepository());
    }


    public DenotexParser(InputStream input, Symboltable t, Repository rep) {
        this(input);
        this.t = t;
        this.url = null;
        this.rep = rep;
    }


    public DenotexParser(URL url, Symboltable t)
            throws IOException {
        this(url, t, Repository.systemRepository());
    }


    public DenotexParser(URL url, Symboltable t, Repository rep)
            throws IOException {
        this(url.openStream(), t, rep);
        this.url = url;
    }


    public void parse() throws ParseException {
        try {
            denotex();
        }
        catch(TokenMgrError e) {
            ParseException pe = parseError(e.getMessage());
            throw pe;
        }
        catch(ParseException e) {
            throw e;
        }
    }


    public void parselink() throws ParseException {
                parse();
                Linker.link(t, rep);
        }


        public void parseForm() throws ParseException {
        try {
                        formDefinition();
        }
        catch(TokenMgrError e) {
            ParseException pe = parseError(e.getMessage());
            throw pe;
        }
        catch(ParseException e) {
            throw e;
        }
        }


        public void parseDenotator() throws ParseException {
        try {
                        substanceDefinition();
        }
        catch(TokenMgrError e) {
            ParseException pe = parseError(e.getMessage());
            throw pe;
        }
        catch(ParseException e) {
            throw e;
        }
        }

    // other public constructors (do not use!)
    // automatically generated by java_cc:
    // DenotexParser(java.io.Reader reader) or
    // DenotexParser(java.io.InputStream stream)

    public static void main(String[] args) {
        Symboltable symtab = new Symboltable();
        try {
            DenotexParser parser = new DenotexParser(new URL(args[0]), symtab);
            parser.parse();
            System.out.println("input correctly parsed.");
            Linker.link(symtab);
            System.out.println("input linked.");
        } catch(Exception e) {
            e.printStackTrace();
        }
    }


    // parse exception generation method

    private ParseException parseError(String msg) {
        return new ParseException(msg);
    }


    private String stripQuotes(String s) {
        return s.substring(1, s.length()-1);
    }


    private void checkDelimiters(int real, int expected) throws ParseException {
        if (real != expected)
            throw parseError("Delimiters do not match type '" + Form.typeToString(expected) + "'");
    }


    private void includeFile(String path) throws ParseException {
        if (url != null) {
            // allow relative path name
            if (path.indexOf("://") == -1) {
                String s = url.toString();
                path = s.substring(0, s.lastIndexOf("/")+1) + path;
            }
        }
        DenotexParser p;
        try {
            p = new DenotexParser(new URL(path), t, rep);
        } catch(Exception e) {
            throw parseError("Malformed URL '" + path + "' (" + e.getMessage() + ")");
        }
        //System.out.println("Including file " + path);
        p.parse();
        //System.out.println("Done file " + path);
    }


    private Form defineForm(NameEntry name, int type, List crs) throws ParseException {
        if (t.forms().get(name) != null)
            throw parseError("Form '" + name + "' already defined.");

        Form form = null;
        NameDenotator n = NameDenotator.make(name);
        switch(type) {
            case Yoneda.SIMPLE: {
                try {
                    if (crs.size() == 1)
                        form = new SimpleForm(n, (Module)crs.get(0));
                    else
                        form = new SimpleForm(n, (Module)crs.get(0),
                                                      (ModuleElement)crs.get(1),
                                                      (ModuleElement)crs.get(2));
                } catch(Exception e) { throw parseError("Form build failed: " + e.getMessage()); }
                break;
            }
            case Yoneda.LIMIT: {
                try {
                    List l = new ArrayList(crs.size());
                    for (int i = 0; i < crs.size(); i++) l.add(null);
                    // WRONG: form = new SimpleForm(n, l);
                } catch(Exception e) { throw parseError("Form build failed: " + e.getMessage()); }
                break;
            }
            case Yoneda.COLIMIT: {
                try {
                    List l = new ArrayList(crs.size());
                    for (int i = 0; i < crs.size(); i++) l.add(null);
                    form = new ColimitForm(n, l);
                } catch(Exception e) { throw parseError("Form build failed: " + e.getMessage()); }
                break;
            }
            case Yoneda.POWER: {
                try {
                    // WRONG:
                    // form = new SimpleForm(n, null);
                } catch(Exception e) { throw parseError("Form build failed: " + e.getMessage()); }
                break;
            }
            case Yoneda.LIST: {
                try {
                    // WRONG:
                    // form = new SimpleForm(n, null);
                } catch(Exception e) { throw parseError("Form build failed: " + e.getMessage()); }
                break;
            }
            default:
                throw parseError("Illegal space type");
        }
        form.setConnector(crs);
        t.forms().put(name, form);
        return form;
    }


        private Form lookupForm(NameEntry name) {
                Form f = null;
                if (rep != null)
                        f = rep.getForm(name);
                if (f == null)
                        f = t.forms().get(name);
                return f;
        }


        private Denotator lookupDenotator(NameEntry name) {
                Denotator d = null;
                if (rep != null)
                        d = rep.getDenotator(name);
                if (d == null)
                        d = t.ndenos().get(name);
                return d;
        }


    private Denotator defineDenotator(NameEntry name, Module address, Form form, List cds)
        throws ParseException {
        try {
            if (name != null && lookupDenotator(name) != null)
                throw parseError("Denotator '" + name + "' already defined.");

            Denotator deno = null;
            if (name != null) {
                // WRONG:
                // deno = new Denotator(Naming.getNameDenotator(name), address, form, cds);
                t.ndenos().put(name, deno);
            }
            else {
                // WRONG:
                // deno = new Denotator(null, address, form, cds);
                t.adenos().add(deno);
            }
            return deno;
        }
        catch(Exception e) {
                throw parseError("Denotator construction failed: " + e.getMessage());
        }
    }

    /*
     * $$$RA: this should be moved somewhere else
     */
    private Module makeModule(String s, int sub, int sup) throws ParseException {
        // RA: ZASCII ist maintained here for backward compability
        if (s.equals("ZString") || s.equals("ZASCII")) {
            if (sup != -1) throw parseError("Illegal module '" + s + "^n'");
            if (sub == -1)   return ZStringRing.ring;
            else            return ZnStringRing.make(sub);
        }
        else if (s.equals("QString")) {
            if (sub != -1) throw parseError("Illegal module '" + s + "_n'");
            if (sup != -1) throw parseError("Illegal module '" + s + "^n'");
            return QStringRing.ring;
        }
        else if (s.equals("RString")) {
            if (sub != -1) throw parseError("Illegal module '" + s + "_n'");
            if (sup != -1) throw parseError("Illegal module '" + s + "^n'");
            return RStringRing.ring;
        }
        else if (s.equals("Z")) {
            if (sup == -1) {
                if (sub == -1)   return ZRing.ring;
                else            return ZnRing.make(sub);
            }
            else {
                if (sub == -1)   return ZProperFreeModule.make(sup);
                else            return ZnProperFreeModule.make(sup, sub);
            }
        }
        else if (s.equals("Q")) {
            if (sub != -1) throw parseError("Illegal module '" + s + "_n'");
            if (sup == -1)       return QRing.ring;
            else                return QProperFreeModule.make(sup);
        }
        else if (s.equals("R")) {
            if (sub != -1) throw parseError("Illegal module '" + s + "_n'");
            if (sup == -1)       return RRing.ring;
            else                return RProperFreeModule.make(sup);
        }
        else if (s.equals("C")) {
            if (sub != -1) throw parseError("Illegal module '" + s + "_n'");
            if (sup == -1)       return CRing.ring;
            else                return CProperFreeModule.make(sup);
        }
        else if (s.equals("Null")) {
            return ZProperFreeModule.nullModule;
        }
        throw parseError("Illegal module '" + s + "'");
    }

    private boolean isBasicType(String s) {
        if (s.equals("ZString") ||
           s.equals("ZASCII") ||
           s.equals("QString") ||
           s.equals("RString") ||
           s.equals("Z") ||
           s.equals("Q") ||
           s.equals("R") ||
           s.equals("Null"))
           return true;
        else
                return false;
    }

    private void beginNamespace(NameEntry name) {
        if (namespace.size() > 0) {
                NameEntry ne = (NameEntry)namespace.getFirst();
                namespace.addFirst(NameEntry.lookup(NameEntry.concat(ne, name)));
        }
        else {
                namespace.addFirst(name);
        }
    }

    private void endNamespace() {
        namespace.removeFirst();
    }

    private NameEntry getCurrentNamespace() {
        if (namespace.size() > 0)
                return (NameEntry)namespace.getFirst();
        else
                return null;
    }

        private NameEntry getNSName(NameEntry name) {
                if (name == null)
                        return null;
                else {
                        NameEntry ns = getCurrentNamespace();
                        if (ns != null)
                                return NameEntry.lookup(NameEntry.concat(ns, name));
                        else
                                return name;
                }
        }


    // private attributes

    private Symboltable t;
    private URL url;
    private Repository rep;
    private LinkedList namespace = new LinkedList();

////////////////////////////////////////////////////////////////////////////////
//////////////////// GRAMMAR DEFINITIONS ///////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  final public void denotex() throws ParseException {
    label_1:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case INCLUDE:
      case BEGIN:
        break;
      default:
        jj_la1[0] = jj_gen;
        break label_1;
      }
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case INCLUDE:
        includeCommand();
        break;
      case BEGIN:
        definitionBlock();
        break;
      default:
        jj_la1[1] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
    }
  }

////////////////////////////////////////////////////////////////////////////////
//////////////////// INCLUDE FILE //////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  final public void includeCommand() throws ParseException {
    Token t;
    jj_consume_token(INCLUDE);
    t = jj_consume_token(STRING_LITERAL);
        includeFile(stripQuotes(t.image));
  }

////////////////////////////////////////////////////////////////////////////////
//////////////////// DEFINITION BLOCK //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  final public void definitionBlock() throws ParseException {
    NameEntry name;
    if (jj_2_1(3)) {
      jj_consume_token(BEGIN);
      jj_consume_token(26);
      jj_consume_token(FORM);
      jj_consume_token(27);
      jj_consume_token(28);
      label_2:
      while (true) {
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case IDENTIFIER:
        case QIDENTIFIER:
        case 30:
          break;
        default:
          jj_la1[2] = jj_gen;
          break label_2;
        }
        formDefinition();
        jj_consume_token(28);
      }
      jj_consume_token(END);
      jj_consume_token(26);
      jj_consume_token(FORM);
      jj_consume_token(27);
      jj_consume_token(28);
    } else if (jj_2_2(3)) {
      jj_consume_token(BEGIN);
      jj_consume_token(26);
      jj_consume_token(SUBSTANCE);
      jj_consume_token(27);
      jj_consume_token(28);
      label_3:
      while (true) {
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case IDENTIFIER:
        case QIDENTIFIER:
        case 30:
        case 31:
          break;
        default:
          jj_la1[3] = jj_gen;
          break label_3;
        }
        substanceDefinition();
        jj_consume_token(28);
      }
      jj_consume_token(END);
      jj_consume_token(26);
      jj_consume_token(SUBSTANCE);
      jj_consume_token(27);
      jj_consume_token(28);
    } else if (jj_2_3(3)) {
      jj_consume_token(BEGIN);
      jj_consume_token(26);
      jj_consume_token(MODULE);
      jj_consume_token(27);
      jj_consume_token(28);
      label_4:
      while (true) {
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case IDENTIFIER:
          
          break;
        default:
          jj_la1[4] = jj_gen;
          break label_4;
        }
        moduleDefinition();
        jj_consume_token(28);
      }
      jj_consume_token(END);
      jj_consume_token(26);
      jj_consume_token(MODULE);
      jj_consume_token(27);
      jj_consume_token(28);
    } else if (jj_2_4(3)) {
      jj_consume_token(BEGIN);
      jj_consume_token(26);
      jj_consume_token(NAMESPACE);
      jj_consume_token(29);
      name = nameSpaceName();
      jj_consume_token(27);
      jj_consume_token(28);
                                     beginNamespace(name);
      label_5:
      while (true) {
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case BEGIN:
          
          break;
        default:
          jj_la1[5] = jj_gen;
          break label_5;
        }
        definitionBlock();
      }
      jj_consume_token(END);
      jj_consume_token(26);
      jj_consume_token(NAMESPACE);
      jj_consume_token(27);
      jj_consume_token(28);
                     endNamespace();
    } else {
      jj_consume_token(-1);
      throw new ParseException();
    }
  }

  final public NameEntry nameSpaceName() throws ParseException {
    NameEntry name = new NameEntry();
    String sname;
    sname = id();
      name.add(sname);
    label_6:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case 30:
        
        break;
      default:
        jj_la1[6] = jj_gen;
        break label_6;
      }
      jj_consume_token(30);
      sname = id();
                         name.add(sname);
    }
        {if (true) return name;}
    throw new Error("Missing return statement in function");
  }

////////////////////////////////////////////////////////////////////////////////
//////////////////// FORM DEFINITIONS //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  final public void formDefinition() throws ParseException {
    NameEntry name; // form name
    int type;       // space type
    List clist;
    name = formName();
    jj_consume_token(31);
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case IDENTIFIER:
    case QIDENTIFIER:
      morphism();
      break;
    default:
      jj_la1[7] = jj_gen;
      
    }
    jj_consume_token(32);
    type = spaceType();
    clist = crs(type);
        defineForm(name, type, clist);
  }

  final public List crs(int type) throws ParseException {
    List clist;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case 26:
      clist = simpleCrs(type);
      break;
    case 33:
      clist = limitCrs(type);
      break;
    case 35:
      clist = colimitCrs(type);
      break;
    case 37:
      clist = powerCrs(type);
      break;
    case LIST_O:
      clist = listCrs(type);
      break;
    default:
      jj_la1[8] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
      {if (true) return clist;}
    throw new Error("Missing return statement in function");
  }

final public List simpleCrs(int type) throws ParseException {
    checkDelimiters(Yoneda.SIMPLE, type);
    List c = new ArrayList(1);
    Module m;
    ModuleElement lo = null;
    ModuleElement hi = null;
    jj_consume_token(26);
    m = module();
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case 29:
      jj_consume_token(29);
      lo = moduleElement(m);
      jj_consume_token(29);
      hi = moduleElement(m);
      break;
    default:
      jj_la1[9] = jj_gen;
      
    }
    jj_consume_token(27);
        c.add(m);
        c.add(lo);
        c.add(hi);
        {if (true) return c;}
    throw new Error("Missing return statement in function");
  }

  final public List limitCrs(int type) throws ParseException {
    checkDelimiters(Yoneda.LIMIT, type);
    List names;
    jj_consume_token(33);
    names = formNameList();
    jj_consume_token(34);
      {if (true) return names;}
    throw new Error("Missing return statement in function");
  }

  final public List colimitCrs(int type) throws ParseException {
    checkDelimiters(Yoneda.COLIMIT, type);
    List names;
    jj_consume_token(35);
    names = formNameList();
    jj_consume_token(36);
      {if (true) return names;}
    throw new Error("Missing return statement in function");
  }

  final public List powerCrs(int type) throws ParseException {
    checkDelimiters(Yoneda.POWER, type);
    List names = new ArrayList(1);
    NameEntry name;
    jj_consume_token(37);
    name = formName();
    jj_consume_token(38);
        names.add(name);
        {if (true) return names;}
    throw new Error("Missing return statement in function");
  }

  final public List listCrs(int type) throws ParseException {
    checkDelimiters(Yoneda.LIST, type);
    List names = new ArrayList(1);
    NameEntry name;
    jj_consume_token(LIST_O);
    name = formName();
    jj_consume_token(LIST_C);
        names.add(name);
        {if (true) return names;}
    throw new Error("Missing return statement in function");
  }

  final public List formNameList() throws ParseException {
    List names = new ArrayList();
    NameEntry name;
    name = formName();
                        names.add(name);
    label_7:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case 29:
        
        break;
      default:
        jj_la1[10] = jj_gen;
        break label_7;
      }
      jj_consume_token(29);
      name = formName();
                                                                      names.add(name);
    }
        {if (true) return names;}
    throw new Error("Missing return statement in function");
  }

  final public NameEntry formName() throws ParseException {
    NameEntry name = new NameEntry();
    String sname;
    boolean root = false;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case 30:
      jj_consume_token(30);
                root = true;
      break;
    default:
      jj_la1[11] = jj_gen;
      
    }
    sname = id();
      name.add(sname);
    label_8:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case 30:
        
        break;
      default:
        jj_la1[12] = jj_gen;
        break label_8;
      }
      jj_consume_token(30);
      sname = id();
                         name.add(sname);
    }
        if (root) {
                    {if (true) return NameEntry.lookup(name);}
                }
                else {
                        {if (true) return getNSName(name);}
                }
    throw new Error("Missing return statement in function");
  }

  final public int spaceType() throws ParseException {
    String type;
    type = id();
        try {
            {if (true) return Form.stringToType(type);}
        }
        catch(Exception e) {
            {if (true) throw parseError("Illegal space type '" + type + "'");}
        }
    throw new Error("Missing return statement in function");
  }

  final public void morphism() throws ParseException {
    id();
        {if (true) throw parseError("Morphisms not supported yet");}
  }

  final public void diagram() throws ParseException {
    id();
        {if (true) throw parseError("Diagrams not supported yet");}
  }

////////////////////////////////////////////////////////////////////////////////
//////////////////// DENOTATOR DEFINITIONS /////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  final public void substanceDefinition() throws ParseException {
    NameEntry name = null;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case IDENTIFIER:
    case QIDENTIFIER:
    case 30:
      name = denotatorName();
      break;
    default:
      jj_la1[13] = jj_gen;
      
    }
    jj_consume_token(31);
    denotatorDefinition(name, null, null);
  }

  final public Denotator denotatorDefinition(NameEntry name, Module expectedAddress, Form expectedForm) throws ParseException {
    Module a;          // address
    NameEntry fname;   // form name
    Form f;            // form
    List c;
    a = address();
    jj_consume_token(39);
    fname = formName();
        if (expectedAddress != null && expectedAddress.compareTo(a) != 0)
            {if (true) throw parseError("Expected address " + expectedAddress +
                                     ", got address " + a);}

        f = lookupForm(fname);
        if (f == null)
            {if (true) throw parseError("Form '" + fname + "' not defined.");}
        if (expectedForm != null && !expectedForm.equals(f))
            {if (true) throw parseError("DenotexParser.denotatorDefinition: Expected form '"+
                                        expectedForm.getNameString()+
                                        "', got form '" + f.getNameString() + "'");}

        try {
            Linker.link(f, t, rep);
        }
        catch(Exception e) {
            {if (true) throw parseError("Form '" + f + "' could not be linked: " + e.getMessage());}
        }
    c = cds(a, f);
        {if (true) return defineDenotator(name, a, f, c);}
    throw new Error("Missing return statement in function");
  }

  final public Module address() throws ParseException {
    Module a = null;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case IDENTIFIER:
    case 37:
      a = module();
      break;
    default:
      jj_la1[14] = jj_gen;
      
    }
        if (a == null)
            a = ZProperFreeModule.nullModule;
        {if (true) return a;}
    throw new Error("Missing return statement in function");
  }

  final public List cds(Module address, Form form) throws ParseException {
    List l;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case 26:
      l = simpleCds(address, form);
      break;
    case 33:
      l = limitCds(address, form);
      break;
    case 35:
      l = colimitCds(address, form);
      break;
    case 37:
      l = powerCds(address, form);
      break;
    case LIST_O:
      l = listCds(address, form);
      break;
    default:
      jj_la1[15] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
        {if (true) return l;}
    throw new Error("Missing return statement in function");
  }

  final public List simpleCds(Module address, Form form) throws ParseException {
    checkDelimiters(Yoneda.SIMPLE, form.getType());
    Module m = form.getIdentifier().getCodomainModule();
    List c = new ArrayList(1);
    ModuleElement e;
    jj_consume_token(26);
    e = moduleElement(m);
    jj_consume_token(27);
        c.add(e);
        {if (true) return c;}
    throw new Error("Missing return statement in function");
  }

  final public List limitCds(Module address, Form form) throws ParseException {
    checkDelimiters(Yoneda.LIMIT, form.getType());
    FormDiagram diag = (FormDiagram)form.getIdentifier().getCodomainDiagram();
    List c = new ArrayList();
    Denotator d;
    int index = 0;
    jj_consume_token(33);
        if (index >= diag.getFormCount())
            {if (true) throw parseError("Too many elements in limit: " + index);}
    d = denotator(address, diag.getForm(index++));
      c.add(d);
    label_9:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case 29:
        
        break;
      default:
        jj_la1[16] = jj_gen;
        break label_9;
      }
      jj_consume_token(29);
            if (index >= diag.getFormCount())
                {if (true) throw parseError("Too many elements in limit: " + index);}
      d = denotator(address, diag.getForm(index++));
          c.add(d);
    }
    jj_consume_token(34);
        if (index < diag.getFormCount())
                {if (true) throw parseError("Not enough elements in limit: " + index);}
        {if (true) return c;}
    throw new Error("Missing return statement in function");
  }

  final public List colimitCds(Module address, Form form) throws ParseException {
    checkDelimiters(Yoneda.COLIMIT, form.getType());
    FormDiagram diag = (FormDiagram)form.getIdentifier().getCodomainDiagram();
    List c = new ArrayList(2);
    Token p;
    Denotator d;
    jj_consume_token(35);
    p = jj_consume_token(INTEGER_LITERAL);
        int index = Integer.parseInt(p.image);
        if (index < 0 || index >= diag.getFormCount())
            {if (true) throw parseError("Colimit index out of range: " + index);}
    jj_consume_token(29);
    d = denotator(address, diag.getForm(index));
    jj_consume_token(36);
        c.add(new Integer(index));
        c.add(d);
        {if (true) return c;}
    throw new Error("Missing return statement in function");
  }

  final public List powerCds(Module address, Form form) throws ParseException {
    checkDelimiters(Yoneda.POWER, form.getType());
    FormDiagram diag = (FormDiagram)form.getIdentifier().getCodomainDiagram();
    Form ff = diag.getForm(0);
    List c = new ArrayList();
    Denotator d;
    jj_consume_token(37);
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case INTEGER_LITERAL:
    case FLOATING_POINT_LITERAL:
    case STRING_LITERAL:
    case LIST_O:
    case IDENTIFIER:
    case QIDENTIFIER:
    case 26:
    case 30:
    case 33:
    case 35:
    case 37:
    case 39:
    case 42:
    case 43:
      d = denotator(address, ff);
                                       c.add(d);
      label_10:
      while (true) {
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case 29:
          
          break;
        default:
          jj_la1[17] = jj_gen;
          break label_10;
        }
        jj_consume_token(29);
        d = denotator(address, ff);
                                            c.add(d);
      }
      break;
    default:
      jj_la1[18] = jj_gen;
      
    }
    jj_consume_token(38);
        {if (true) return c;}
    throw new Error("Missing return statement in function");
  }

  final public List listCds(Module address, Form form) throws ParseException {
    checkDelimiters(Yoneda.LIST, form.getType());
    FormDiagram diag = (FormDiagram)form.getIdentifier().getCodomainDiagram();
    Form ff = diag.getForm(0);
    List c = new ArrayList();
    Denotator d;
    jj_consume_token(LIST_O);
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case INTEGER_LITERAL:
    case FLOATING_POINT_LITERAL:
    case STRING_LITERAL:
    case LIST_O:
    case IDENTIFIER:
    case QIDENTIFIER:
    case 26:
    case 30:
    case 33:
    case 35:
    case 37:
    case 39:
    case 42:
    case 43:
      d = denotator(address, ff);
                                       c.add(d);
      label_11:
      while (true) {
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case 29:
          
          break;
        default:
          jj_la1[19] = jj_gen;
          break label_11;
        }
        jj_consume_token(29);
        d = denotator(address, ff);
                                            c.add(d);
      }
      break;
    default:
      jj_la1[20] = jj_gen;
      
    }
    jj_consume_token(LIST_C);
        {if (true) return c;}
    throw new Error("Missing return statement in function");
  }

  final public Denotator denotator(Module address, Form form) throws ParseException {
    Denotator d = null;
    NameEntry name;
    if (jj_2_5(2)) {
      name = denotatorName();
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case 31:
        jj_consume_token(31);
        d = denotatorDefinition(name, address, form);
        break;
      default:
        jj_la1[21] = jj_gen;
        
      }
        if (d == null) {
            d = lookupDenotator(name);
            // $$$RA 030129: significant change: allow undefined denos
            if (d == null) {
                // WRONG:
                // d = new Denotator(NameDenotator.make(name), form);
                d.setConnector(name);
            }
            else {
                if (address.compareTo(d.getAddress()) != 0)
                    {if (true) throw parseError("Expected address " + address +
                                     ", got address " + d.getAddress());}
            }
        }
        {if (true) return d;}
    } else if (jj_2_6(2)) {
      d = denotatorDefinition(null, address, form);
                {if (true) return d;}
    } else if (jj_2_7(2)) {
            Module m = form.getIdentifier().getCodomainModule();
        List c = new ArrayList(1);
                LinkedList elements;
      elements = basicElement(m);
        c.add(m.createElement(elements));
        {if (true) return defineDenotator(null, address, form, c);}
    } else {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case LIST_O:
      case 26:
      case 33:
      case 35:
      case 37:
                List c;
        c = cds(address, form);
        {if (true) return defineDenotator(null, address, form, c);}
        break;
      default:
        jj_la1[22] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
    }
    throw new Error("Missing return statement in function");
  }

  final public NameEntry denotatorName() throws ParseException {
    NameEntry name = new NameEntry();
    String sname;
    boolean root = false;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case 30:
      jj_consume_token(30);
                root = true;
      break;
    default:
      jj_la1[23] = jj_gen;
      
    }
    sname = id();
      name.add(sname);
    label_12:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case 30:
        
        break;
      default:
        jj_la1[24] = jj_gen;
        break label_12;
      }
      jj_consume_token(30);
      sname = id();
                         name.add(sname);
    }
        if (root) {
                    {if (true) return NameEntry.lookup(name);}
                }
                else {
                        {if (true) return getNSName(name);}
                }
    throw new Error("Missing return statement in function");
  }

////////////////////////////////////////////////////////////////////////////////
//////////////////// MODULE DEFINITIONS ////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  final public void moduleDefinition() throws ParseException {
    String n;
    Module m;
    n = moduleName();
    jj_consume_token(31);
    m = module();
        if (t.modules().get(n) != null)
            {if (true) throw parseError("Module '" + n + "' already defined.");}
        t.modules().put(n, m);
  }

  final public String moduleName() throws ParseException {
    Token t;
    t = jj_consume_token(IDENTIFIER);
        {if (true) return t.image;}
    throw new Error("Missing return statement in function");
  }

  final public Module module() throws ParseException {
    Module m = null;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case IDENTIFIER:
      m = basicModule();
        {if (true) return m;}
      break;
    case 37:
                List l = new LinkedList();
      jj_consume_token(37);
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case IDENTIFIER:
      case 37:
        m = module();
                         l.add(m);
        label_13:
        while (true) {
          switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
          case 29:
            
            break;
          default:
            jj_la1[25] = jj_gen;
            break label_13;
          }
          jj_consume_token(29);
          m = module();
                                                          l.add(m);
        }
        break;
      default:
        jj_la1[26] = jj_gen;
        
      }
      jj_consume_token(38);
        // WRONG:
        // return new CompositeModule((Module[])l.toArray(new Module[0]));
        {if (true) return null;}
      break;
    default:
      jj_la1[27] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    throw new Error("Missing return statement in function");
  }

  final public Module basicModule() throws ParseException {
    String n;
    int sub = -1;
    int sup = -1;
    n = basicType();
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case 40:
      jj_consume_token(40);
      sub = nLiteral();
      break;
    default:
      jj_la1[28] = jj_gen;
      
    }
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case 41:
      jj_consume_token(41);
      sup = nLiteral();
      break;
    default:
      jj_la1[29] = jj_gen;
      
    }
        if (isBasicType(n))
           {if (true) return makeModule(n, sub, sup);}
        else {
            if (sub != -1 || sup != -1)
                {if (true) throw parseError("Sub- or superscript not allowed for module name.");}
            Module m = t.modules().get(n);
            if (m == null)
                {if (true) throw parseError("Module '" + n + "' not defined.");}
            {if (true) return m;}
        }
    throw new Error("Missing return statement in function");
  }

  final public String basicType() throws ParseException {
    Token t;
    t = jj_consume_token(IDENTIFIER);
        {if (true) return t.image;}
    throw new Error("Missing return statement in function");
  }

  final public ModuleElement moduleElement(Module m) throws ParseException {
    LinkedList elements;
    elements = compositeElement(m);
        {if (true) return m.createElement(elements);}
    throw new Error("Missing return statement in function");
  }

  final public LinkedList compositeElement(Module m) throws ParseException {
        LinkedList elements = new LinkedList();
        LinkedList newElements;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case INTEGER_LITERAL:
    case FLOATING_POINT_LITERAL:
    case STRING_LITERAL:
    case 42:
    case 43:
      elements = basicElement(m);
        {if (true) return elements;}
      break;
    case 37:
      int i = 0;
      jj_consume_token(37);
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case INTEGER_LITERAL:
      case FLOATING_POINT_LITERAL:
      case STRING_LITERAL:
      case 37:
      case 42:
      case 43:
            if (i < 0 || i >= m.getDimension())
                {if (true) throw parseError("Composite module index out of range: " + i);}
        elements = compositeElement(m.getComponentModule(i++));
        label_14:
        while (true) {
          switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
          case 29:
            
            break;
          default:
            jj_la1[30] = jj_gen;
            break label_14;
          }
          jj_consume_token(29);
                if (i < 0 || i >= m.getDimension())
                    {if (true) throw parseError("Composite module index out of range: " + i);}
          newElements = compositeElement(m.getComponentModule(i++));
                elements.addAll(newElements);
        }
        break;
      default:
        jj_la1[31] = jj_gen;
        
      }
      jj_consume_token(38);
        if (i != m.getDimension())
                {if (true) throw parseError("Expected " + m.getDimension() +
                                 " elements in composite element");}
        {if (true) return elements;}
      break;
    default:
      jj_la1[32] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    throw new Error("Missing return statement in function");
  }

  final public LinkedList basicElement(Module m) throws ParseException {
        LinkedList elements = new LinkedList();
    if (jj_2_8(2)) {
                   Rational q;
      q = qLiteral();
        try {
            int n = q.getNumerator();
            int d = q.getDenominator();
            if (m instanceof ZRing) {
                if (d != 1) {if (true) throw parseError("");}
                elements.add(new ZElement(n));
            }
            else if (m instanceof ZnRing) {
                if (d != 1) {if (true) throw parseError("");}
                elements.add(new ZnElement(n, ((ZnRing)m).getModulus()));
            }
            else if (m instanceof QRing)
                elements.add(new QElement(q));
            else if (m instanceof RRing)
                elements.add(new RElement((double)n / (double)d));

            // we also allow to convert numbers into strings without quotes
            else if (m instanceof ZStringRing)
                elements.add(new ZStringElement("" + (double)n / (double)d));
            else if (m instanceof ZnStringRing)
                elements.add(new ZnStringElement("" + (double)n / (double)d,
                                            ((ZnStringRing)m).getModulus()));
            else if (m instanceof QStringRing)
                elements.add(new QStringElement("" + (double)n / (double)d));
            else if (m instanceof RStringRing)
                elements.add(new RStringElement("" + (double)n / (double)d));

            // error
            else {if (true) throw parseError("");}
        }
        catch(Exception e) {
            {if (true) throw parseError("Expected element from " + m.toString());}
        }
        {if (true) return elements;}
    } else if (jj_2_9(2)) {
                   double r;
      r = rLiteral();
        try {
            if (m instanceof RRing)
                elements.add(new RElement(r));

            // we also allow to convert numbers into strings without quotes
            else if (m instanceof ZStringRing)
                elements.add(new ZStringElement("" + r));
            else if (m instanceof ZnStringRing)
                elements.add(new ZnStringElement("" + r, ((ZnStringRing)m).getModulus()));
            else if (m instanceof QStringRing)
                elements.add(new QStringElement("" + r));
            else if (m instanceof RStringRing)
                elements.add(new RStringElement("" + r));

            // error
            else {if (true) throw parseError("");}
        }
        catch(Exception e) {
            {if (true) throw parseError("Expected element from " + m.toString());}
        }
        {if (true) return elements;}
    } else if (jj_2_10(2)) {
                   Complex c;
      c = cLiteral();
        try {
                if (m instanceof CRing)
                        elements.add(new CElement(c));
                else
                        {if (true) throw parseError("");}
        }
        catch (Exception e) {
                {if (true) throw parseError("Expected element from " + m.toString());}
        }
        {if (true) return elements;}
    } else {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case STRING_LITERAL:
      String s;
        s = sLiteral();
        try {
            if (m instanceof ZStringRing)
                elements.add(new ZStringElement(s));
            else if (m instanceof ZnStringRing)
                elements.add(new ZnStringElement(s, ((ZnStringRing)m).getModulus()));
            else if (m instanceof QStringRing)
                elements.add(new QStringElement(s));
            else if (m instanceof RStringRing)
                elements.add(new RStringElement(s));

            // error
            else {if (true) throw parseError("Expected StringElement, got " + s);}
        } catch(Exception e) {
            e.printStackTrace();
            {if (true) throw parseError("Expected element from " + m.toString());}
        }
        {if (true) return elements;}
        break;
      default:
        jj_la1[33] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
    }
    throw new Error("Missing return statement in function");
  }

////////////////////////////////////////////////////////////////////////////////
//////////////////// LITERALS AND VALUES  //////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  final public String id() throws ParseException {
    Token n;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case IDENTIFIER:
      n = jj_consume_token(IDENTIFIER);
        {if (true) return n.image;}
      break;
    case QIDENTIFIER:
      n = jj_consume_token(QIDENTIFIER);
        {if (true) return stripQuotes(n.image);}
      break;
    default:
      jj_la1[34] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    throw new Error("Missing return statement in function");
  }

  final public int nLiteral() 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");
  }

  final public int zLiteral() throws ParseException {
    int n;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case INTEGER_LITERAL:
    case 42:
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case 42:
        jj_consume_token(42);
        break;
      default:
        jj_la1[35] = jj_gen;
        
      }
      n = nLiteral();
        {if (true) return n;}
      break;
    case 43:
      jj_consume_token(43);
      n = nLiteral();
        {if (true) return -n;}
      break;
    default:
      jj_la1[36] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    throw new Error("Missing return statement in function");
  }

  final public Rational qLiteral() throws ParseException {
    int n;
    int d = 1;
    n = zLiteral();
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case 30:
      jj_consume_token(30);
      d = zLiteral();
      break;
    default:
      jj_la1[37] = jj_gen;
      
    }
        {if (true) return new Rational(n, d);}
    throw new Error("Missing return statement in function");
  }

  final public double rLiteral() throws ParseException {
    Token t;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case FLOATING_POINT_LITERAL:
    case 42:
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case 42:
        jj_consume_token(42);
        break;
      default:
        jj_la1[38] = jj_gen;
        
      }
      t = jj_consume_token(FLOATING_POINT_LITERAL);
        {if (true) return Double.parseDouble(t.image);}
      break;
    case 43:
      jj_consume_token(43);
      t = jj_consume_token(FLOATING_POINT_LITERAL);
        {if (true) return -Double.parseDouble(t.image);}
      break;
    default:
      jj_la1[39] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    throw new Error("Missing return statement in function");
  }

  final public Complex cLiteral() throws ParseException {
    double r = 0.0;
    double i = 0.0;
    r = rLiteral();
    jj_consume_token(42);
    jj_consume_token(44);
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case 45:
      jj_consume_token(45);
      break;
    default:
      jj_la1[40] = jj_gen;
      
    }
    i = rLiteral();
                {if (true) return new Complex(r, i);}
    throw new Error("Missing return statement in function");
  }

  final public String sLiteral() throws ParseException {
    Token t;
    t = jj_consume_token(STRING_LITERAL);
        {if (true) return stripQuotes(t.image);}
    throw new Error("Missing return statement in function");
  }

  final private boolean jj_2_1(int xla) {
    jj_la = xla; jj_lastpos = jj_scanpos = token;
    try { return !jj_3_1(); }
    catch(LookaheadSuccess ls) { return true; }
    finally { jj_save(0, xla); }
  }

  final private boolean jj_2_2(int xla) {
    jj_la = xla; jj_lastpos = jj_scanpos = token;
    try { return !jj_3_2(); }
    catch(LookaheadSuccess ls) { return true; }
    finally { jj_save(1, xla); }
  }

  final private boolean jj_2_3(int xla) {
    jj_la = xla; jj_lastpos = jj_scanpos = token;
    try { return !jj_3_3(); }
    catch(LookaheadSuccess ls) { return true; }
    finally { jj_save(2, xla); }
  }

  final private boolean jj_2_4(int xla) {
    jj_la = xla; jj_lastpos = jj_scanpos = token;
    try { return !jj_3_4(); }
    catch(LookaheadSuccess ls) { return true; }
    finally { jj_save(3, xla); }
  }

  final private boolean jj_2_5(int xla) {
    jj_la = xla; jj_lastpos = jj_scanpos = token;
    try { return !jj_3_5(); }
    catch(LookaheadSuccess ls) { return true; }
    finally { jj_save(4, xla); }
  }

  final private boolean jj_2_6(int xla) {
    jj_la = xla; jj_lastpos = jj_scanpos = token;
    try { return !jj_3_6(); }
    catch(LookaheadSuccess ls) { return true; }
    finally { jj_save(5, xla); }
  }

  final private boolean jj_2_7(int xla) {
    jj_la = xla; jj_lastpos = jj_scanpos = token;
    try { return !jj_3_7(); }
    catch(LookaheadSuccess ls) { return true; }
    finally { jj_save(6, xla); }
  }

  final private boolean jj_2_8(int xla) {
    jj_la = xla; jj_lastpos = jj_scanpos = token;
    try { return !jj_3_8(); }
    catch(LookaheadSuccess ls) { return true; }
    finally { jj_save(7, xla); }
  }

  final private boolean jj_2_9(int xla) {
    jj_la = xla; jj_lastpos = jj_scanpos = token;
    try { return !jj_3_9(); }
    catch(LookaheadSuccess ls) { return true; }
    finally { jj_save(8, xla); }
  }

  final private boolean jj_2_10(int xla) {
    jj_la = xla; jj_lastpos = jj_scanpos = token;
    try { return !jj_3_10(); }
    catch(LookaheadSuccess ls) { return true; }
    finally { jj_save(9, xla); }
  }

  final private boolean jj_3R_22() {
    if (jj_scan_token(30)) return true;
    return false;
  }

  final private boolean jj_3R_24() {
    if (jj_scan_token(30)) return true;
    return false;
  }

  final private boolean jj_3R_15() {
    Token xsp;
    xsp = jj_scanpos;
    if (jj_3R_22()) jj_scanpos = xsp;
    if (jj_3R_23()) return true;
    while (true) {
      xsp = jj_scanpos;
      if (jj_3R_24()) { jj_scanpos = xsp; break; }
    }
    return false;
  }

  final private boolean jj_3R_45() {
    if (jj_scan_token(IDENTIFIER)) return true;
    return false;
  }

  final private boolean jj_3R_46() {
    if (jj_scan_token(40)) return true;
    return false;
  }

  final private boolean jj_3R_33() {
    if (jj_scan_token(QIDENTIFIER)) return true;
    return false;
  }

  final private boolean jj_3R_34() {
    if (jj_3R_39()) return true;
    return false;
  }

  final private boolean jj_3R_23() {
    Token xsp;
    xsp = jj_scanpos;
    if (jj_3R_32()) {
    jj_scanpos = xsp;
    if (jj_3R_33()) return true;
    }
    return false;
  }

  final private boolean jj_3R_32() {
    if (jj_scan_token(IDENTIFIER)) return true;
    return false;
  }

  final private boolean jj_3R_25() {
    Token xsp;
    xsp = jj_scanpos;
    if (jj_3R_34()) jj_scanpos = xsp;
    return false;
  }

  final private boolean jj_3R_35() {
    if (jj_scan_token(30)) return true;
    return false;
  }

  final private boolean jj_3R_26() {
    Token xsp;
    xsp = jj_scanpos;
    if (jj_3R_35()) jj_scanpos = xsp;
    if (jj_3R_23()) return true;
    return false;
  }

  final private boolean jj_3R_36() {
    if (jj_scan_token(STRING_LITERAL)) return true;
    return false;
  }

  final private boolean jj_3R_43() {
    if (jj_3R_45()) return true;
    Token xsp;
    xsp = jj_scanpos;
    if (jj_3R_46()) jj_scanpos = xsp;
    xsp = jj_scanpos;
    if (jj_3R_47()) jj_scanpos = xsp;
    return false;
  }

  final private boolean jj_3R_21() {
    if (jj_3R_20()) return true;
    if (jj_scan_token(42)) return true;
    return false;
  }

  final private boolean jj_3R_16() {
    if (jj_scan_token(31)) return true;
    return false;
  }

  final private boolean jj_3R_18() {
    Token xsp;
    xsp = jj_scanpos;
    if (jj_3_8()) {
    jj_scanpos = xsp;
    if (jj_3_9()) {
    jj_scanpos = xsp;
    if (jj_3_10()) {
    jj_scanpos = xsp;
    if (jj_3R_27()) return true;
    }
    }
    }
    return false;
  }

  final private boolean jj_3_8() {
    if (jj_3R_19()) return true;
    return false;
  }

  final private boolean jj_3_7() {
    if (jj_3R_18()) return true;
    return false;
  }

  final private boolean jj_3R_44() {
    if (jj_3R_39()) return true;
    return false;
  }

  final private boolean jj_3_6() {
    if (jj_3R_17()) return true;
    return false;
  }

  final private boolean jj_3R_29() {
    if (jj_scan_token(30)) return true;
    return false;
  }

  final private boolean jj_3R_17() {
    if (jj_3R_25()) return true;
    if (jj_scan_token(39)) return true;
    if (jj_3R_26()) return true;
    return false;
  }

  final private boolean jj_3R_27() {
    if (jj_3R_36()) return true;
    return false;
  }

  final private boolean jj_3R_31() {
    if (jj_scan_token(43)) return true;
    if (jj_scan_token(FLOATING_POINT_LITERAL)) return true;
    return false;
  }

  final private boolean jj_3R_42() {
    if (jj_scan_token(37)) return true;
    Token xsp;
    xsp = jj_scanpos;
    if (jj_3R_44()) jj_scanpos = xsp;
    if (jj_scan_token(38)) return true;
    return false;
  }

  final private boolean jj_3R_20() {
    Token xsp;
    xsp = jj_scanpos;
    if (jj_3R_30()) {
    jj_scanpos = xsp;
    if (jj_3R_31()) return true;
    }
    return false;
  }

  final private boolean jj_3R_30() {
    Token xsp;
    xsp = jj_scanpos;
    if (jj_scan_token(42)) jj_scanpos = xsp;
    if (jj_scan_token(FLOATING_POINT_LITERAL)) return true;
    return false;
  }

  final private boolean jj_3R_41() {
    if (jj_3R_43()) return true;
    return false;
  }

  final private boolean jj_3R_39() {
    Token xsp;
    xsp = jj_scanpos;
    if (jj_3R_41()) {
    jj_scanpos = xsp;
    if (jj_3R_42()) return true;
    }
    return false;
  }

  final private boolean jj_3_5() {
    if (jj_3R_15()) return true;
    Token xsp;
    xsp = jj_scanpos;
    if (jj_3R_16()) jj_scanpos = xsp;
    return false;
  }

  final private boolean jj_3_10() {
    if (jj_3R_21()) return true;
    return false;
  }

  final private boolean jj_3R_19() {
    if (jj_3R_28()) return true;
    Token xsp;
    xsp = jj_scanpos;
    if (jj_3R_29()) jj_scanpos = xsp;
    return false;
  }

  final private boolean jj_3R_38() {
    if (jj_scan_token(43)) return true;
    if (jj_3R_40()) return true;
    return false;
  }

  final private boolean jj_3_4() {
    if (jj_scan_token(BEGIN)) return true;
    if (jj_scan_token(26)) return true;
    if (jj_scan_token(NAMESPACE)) return true;
    return false;
  }

  final private boolean jj_3R_47() {
    if (jj_scan_token(41)) return true;
    return false;
  }

  final private boolean jj_3_3() {
    if (jj_scan_token(BEGIN)) return true;
    if (jj_scan_token(26)) return true;
    if (jj_scan_token(MODULE)) return true;
    return false;
  }

  final private boolean jj_3R_28() {
    Token xsp;
    xsp = jj_scanpos;
    if (jj_3R_37()) {
    jj_scanpos = xsp;
    if (jj_3R_38()) return true;
    }
    return false;
  }

  final private boolean jj_3R_37() {
    Token xsp;
    xsp = jj_scanpos;
    if (jj_scan_token(42)) jj_scanpos = xsp;
    if (jj_3R_40()) return true;
    return false;
  }

  final private boolean jj_3_2() {
    if (jj_scan_token(BEGIN)) return true;
    if (jj_scan_token(26)) return true;
    if (jj_scan_token(SUBSTANCE)) return true;
    return false;
  }

  final private boolean jj_3_1() {
    if (jj_scan_token(BEGIN)) return true;
    if (jj_scan_token(26)) return true;
    if (jj_scan_token(FORM)) return true;
    return false;
  }

  final private boolean jj_3_9() {
    if (jj_3R_20()) return true;
    return false;
  }

  final private boolean jj_3R_40() {
    if (jj_scan_token(INTEGER_LITERAL)) return true;
    return false;
  }

  public DenotexParserTokenManager token_source;
  SimpleCharStream jj_input_stream;
  public Token token, jj_nt;
  private int jj_ntk;
  private Token jj_scanpos, jj_lastpos;
  private int jj_la;
  public boolean lookingAhead = false;
  private boolean jj_semLA;
  private int jj_gen;
  final private int[] jj_la1 = new int[41];
  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[] {0x1800,0x1800,0x40c00000,0xc0c00000,0x400000,0x1000,0x40000000,0xc00000,0x4100000,0x20000000,0x20000000,0x40000000,0x40000000,0x40c00000,0x400000,0x4100000,0x20000000,0x20000000,0x44d00580,0x20000000,0x44d00580,0x80000000,0x4100000,0x40000000,0x40000000,0x20000000,0x400000,0x400000,0x0,0x0,0x20000000,0x580,0x580,0x400,0xc00000,0x0,0x80,0x40000000,0x0,0x100,0x0,};
   }
   private static void jj_la1_1() {
      jj_la1_1 = new int[] {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x2a,0x0,0x0,0x0,0x0,0x0,0x20,0x2a,0x0,0x0,0xcaa,0x0,0xcaa,0x0,0x2a,0x0,0x0,0x0,0x20,0x20,0x100,0x200,0x0,0xc20,0xc20,0x0,0x0,0x400,0xc00,0x0,0x400,0xc00,0x2000,};
   }
  final private JJCalls[] jj_2_rtns = new JJCalls[10];
  private boolean jj_rescan = false;
  private int jj_gc = 0;

  public DenotexParser(java.io.InputStream stream) {
    jj_input_stream = new SimpleCharStream(stream, 1, 1);
    token_source = new DenotexParserTokenManager(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 41; i++) jj_la1[i] = -1;
    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  }

  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 < 41; i++) jj_la1[i] = -1;
    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  }

  public DenotexParser(java.io.Reader stream) {
    jj_input_stream = new SimpleCharStream(stream, 1, 1);
    token_source = new DenotexParserTokenManager(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 41; i++) jj_la1[i] = -1;
    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  }

  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 < 41; i++) jj_la1[i] = -1;
    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  }

  public DenotexParser(DenotexParserTokenManager tm) {
    token_source = tm;
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 41; i++) jj_la1[i] = -1;
    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  }

  public void ReInit(DenotexParserTokenManager tm) {
    token_source = tm;
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 41; i++) jj_la1[i] = -1;
    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  }

  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++;
      if (++jj_gc > 100) {
        jj_gc = 0;
        for (int i = 0; i < jj_2_rtns.length; i++) {
          JJCalls c = jj_2_rtns[i];
          while (c != null) {
            if (c.gen < jj_gen) c.first = null;
            c = c.next;
          }
        }
      }
      return token;
    }
    token = oldToken;
    jj_kind = kind;
    throw generateParseException();
  }

  static private final class LookaheadSuccess extends java.lang.Error { }
  final private LookaheadSuccess jj_ls = new LookaheadSuccess();
  final private boolean jj_scan_token(int kind) {
    if (jj_scanpos == jj_lastpos) {
      jj_la--;
      if (jj_scanpos.next == null) {
        jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.getNextToken();
      } else {
        jj_lastpos = jj_scanpos = jj_scanpos.next;
      }
    } else {
      jj_scanpos = jj_scanpos.next;
    }
    if (jj_rescan) {
      int i = 0; Token tok = token;
      while (tok != null && tok != jj_scanpos) { i++; tok = tok.next; }
      if (tok != null) jj_add_error_token(kind, i);
    }
    if (jj_scanpos.kind != kind) return true;
    if (jj_la == 0 && jj_scanpos == jj_lastpos) throw jj_ls;
    return false;
  }

  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 = lookingAhead ? jj_scanpos : 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;
  private int[] jj_lasttokens = new int[100];
  private int jj_endpos;

  private void jj_add_error_token(int kind, int pos) {
    if (pos >= 100) return;
    if (pos == jj_endpos + 1) {
      jj_lasttokens[jj_endpos++] = kind;
    } else if (jj_endpos != 0) {
      jj_expentry = new int[jj_endpos];
      for (int i = 0; i < jj_endpos; i++) {
        jj_expentry[i] = jj_lasttokens[i];
      }
      boolean exists = false;
      for (java.util.Enumeration e = jj_expentries.elements(); e.hasMoreElements();) {
        int[] oldentry = (int[])(e.nextElement());
        if (oldentry.length == jj_expentry.length) {
          exists = true;
          for (int i = 0; i < jj_expentry.length; i++) {
            if (oldentry[i] != jj_expentry[i]) {
              exists = false;
              break;
            }
          }
          if (exists) break;
        }
      }
      if (!exists) jj_expentries.addElement(jj_expentry);
      if (pos != 0) jj_lasttokens[(jj_endpos = pos) - 1] = kind;
    }
  }

  public ParseException generateParseException() {
    jj_expentries.removeAllElements();
    boolean[] la1tokens = new boolean[46];
    for (int i = 0; i < 46; i++) {
      la1tokens[i] = false;
    }
    if (jj_kind >= 0) {
      la1tokens[jj_kind] = true;
      jj_kind = -1;
    }
    for (int i = 0; i < 41; 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 < 46; i++) {
      if (la1tokens[i]) {
        jj_expentry = new int[1];
        jj_expentry[0] = i;
        jj_expentries.addElement(jj_expentry);
      }
    }
    jj_endpos = 0;
    jj_rescan_token();
    jj_add_error_token(0, 0);
    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() {
  }

  final private void jj_rescan_token() {
    jj_rescan = true;
    for (int i = 0; i < 10; i++) {
      JJCalls p = jj_2_rtns[i];
      do {
        if (p.gen > jj_gen) {
          jj_la = p.arg; jj_lastpos = jj_scanpos = p.first;
          switch (i) {
            case 0: jj_3_1(); break;
            case 1: jj_3_2(); break;
            case 2: jj_3_3(); break;
            case 3: jj_3_4(); break;
            case 4: jj_3_5(); break;
            case 5: jj_3_6(); break;
            case 6: jj_3_7(); break;
            case 7: jj_3_8(); break;
            case 8: jj_3_9(); break;
            case 9: jj_3_10(); break;
          }
        }
        p = p.next;
      } while (p != null);
    }
    jj_rescan = false;
  }

  final private void jj_save(int index, int xla) {
    JJCalls p = jj_2_rtns[index];
    while (p.gen > jj_gen) {
      if (p.next == null) { p = p.next = new JJCalls(); break; }
      p = p.next;
    }
    p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla;
  }

  static final class JJCalls {
    int gen;
    Token first;
    int arg;
    JJCalls next;
  }

}
