edits
[cdsspec-compiler.git] / src / edu / uci / eecs / utilParser / UtilParser.java
diff --git a/src/edu/uci/eecs/utilParser/UtilParser.java b/src/edu/uci/eecs/utilParser/UtilParser.java
new file mode 100644 (file)
index 0000000..eb363d0
--- /dev/null
@@ -0,0 +1,540 @@
+/* UtilParser.java */
+/* Generated By:JavaCC: Do not edit this line. UtilParser.java */
+package edu.uci.eecs.utilParser;
+import edu.uci.eecs.specExtraction.FunctionHeader;
+import edu.uci.eecs.specExtraction.QualifiedName;
+import edu.uci.eecs.specExtraction.VariableDeclaration;
+//import edu.uci.eecs.specExtraction.WrongAnnotationException;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.util.ArrayList;
+
+
+public class UtilParser implements UtilParserConstants {
+        public static void main(String[] argvs)
+        throws ParseException, TokenMgrError {
+                try {
+                        File f = new File("./grammer/spec1.txt");
+                        FileInputStream fis = new FileInputStream(f);
+                        UtilParser parser = new UtilParser(fis);
+
+                        //parser.Test();
+                        System.out.println("Parsing finished!");
+                } catch (FileNotFoundException e) {
+                        e.printStackTrace();
+                }
+        }
+
+        public static ArrayList<VariableDeclaration> getTemplateArg(String line)
+        throws ParseException {
+                InputStream input = new ByteArrayInputStream(line.getBytes());
+                UtilParser parser = new UtilParser(input);
+                return parser.TemplateParamList();
+        }
+
+        public static FunctionHeader parseFuncHeader(String line)
+        throws ParseException {
+                InputStream input = new ByteArrayInputStream(line.getBytes());
+                UtilParser parser = new UtilParser(input);
+                return parser.FuncDecl();
+        }
+
+        public static VariableDeclaration parseDeclaration(String line)
+        throws ParseException {
+                InputStream input = new ByteArrayInputStream(line.getBytes());
+                UtilParser parser = new UtilParser(input);
+                return parser.Declaration();
+        }
+
+        public static String stringArray2String(ArrayList<String> content) {
+                StringBuilder sb = new StringBuilder();
+                if (content.size() == 1)
+                        return content.get(0);
+                for (int i = 0; i < content.size(); i++) {
+                        sb.append(content.get(i) + "\u005cn");
+                }
+                return sb.toString();
+        }
+
+  final public String Type() throws ParseException {String type;
+        String str;
+        QualifiedName name;
+type = "";
+    switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
+    case CONST:{
+      jj_consume_token(CONST);
+type = "const";
+      break;
+      }
+    default:
+      jj_la1[0] = jj_gen;
+      ;
+    }
+    switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
+    case STRUCT:
+    case CLASS:
+    case UNSIGNED:{
+      switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
+      case STRUCT:{
+        str = jj_consume_token(STRUCT).image;
+        break;
+        }
+      case CLASS:{
+        str = jj_consume_token(CLASS).image;
+        break;
+        }
+      case UNSIGNED:{
+        str = jj_consume_token(UNSIGNED).image;
+        break;
+        }
+      default:
+        jj_la1[1] = jj_gen;
+        jj_consume_token(-1);
+        throw new ParseException();
+      }
+type = type.equals("") ? type + str : type + " " + str;
+      break;
+      }
+    default:
+      jj_la1[2] = jj_gen;
+      ;
+    }
+    name = ParseQualifiedName();
+if (!type.equals(""))
+                        type = type + " " + name.fullName;
+                else
+                        type = name.fullName;
+    label_1:
+    while (true) {
+      switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
+      case CONST:
+      case STAR:
+      case AND:{
+        ;
+        break;
+        }
+      default:
+        jj_la1[3] = jj_gen;
+        break label_1;
+      }
+      switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
+      case CONST:{
+        str = jj_consume_token(CONST).image;
+if (!type.equals(""))
+                        type = type + " " + str;
+                else
+                        type = str;
+        break;
+        }
+      case STAR:{
+        str = jj_consume_token(STAR).image;
+if (!type.equals(""))
+                        type = type + " " + str;
+                else
+                        type = str;
+        break;
+        }
+      case AND:{
+        str = jj_consume_token(AND).image;
+if (!type.equals(""))
+                        type = type + " " + str;
+                else
+                        type = str;
+        break;
+        }
+      default:
+        jj_la1[4] = jj_gen;
+        jj_consume_token(-1);
+        throw new ParseException();
+      }
+    }
+{if ("" != null) return type;}
+    throw new Error("Missing return statement in function");
+  }
+
+  final public void Test() throws ParseException {String str;
+        FunctionHeader func;
+    /*
+       str = Type()
+       {
+               System.out.println(str);
+       }
+       */
+            func = FuncDecl();
+System.out.println(func);
+  }
+
+  final public String ParameterizedName() throws ParseException {String res = "";
+        String str;
+    str = jj_consume_token(IDENTIFIER).image;
+res = str;
+    switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
+    case LESS_THAN:{
+      jj_consume_token(LESS_THAN);
+      str = Type();
+res = res + "<" + str;
+      label_2:
+      while (true) {
+        switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
+        case COMMA:{
+          ;
+          break;
+          }
+        default:
+          jj_la1[5] = jj_gen;
+          break label_2;
+        }
+        jj_consume_token(COMMA);
+        str = Type();
+res = res + ", " + str;
+      }
+      jj_consume_token(GREATER_THAN);
+res = res + ">";
+      break;
+      }
+    default:
+      jj_la1[6] = jj_gen;
+      ;
+    }
+{if ("" != null) return res;}
+    throw new Error("Missing return statement in function");
+  }
+
+  final public FunctionHeader FuncDecl() throws ParseException {String ret;
+        QualifiedName funcName;
+        ArrayList<VariableDeclaration> args;
+    label_3:
+    while (true) {
+      switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
+      case INLINE:
+      case STATIC:{
+        ;
+        break;
+        }
+      default:
+        jj_la1[7] = jj_gen;
+        break label_3;
+      }
+      switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
+      case STATIC:{
+        jj_consume_token(STATIC);
+        break;
+        }
+      case INLINE:{
+        jj_consume_token(INLINE);
+        break;
+        }
+      default:
+        jj_la1[8] = jj_gen;
+        jj_consume_token(-1);
+        throw new ParseException();
+      }
+    }
+    ret = Type();
+    funcName = ParseQualifiedName();
+    args = FormalParamList();
+FunctionHeader res = new FunctionHeader(ret, funcName, args);
+                //System.out.println(res);
+                {if ("" != null) return res;}
+    throw new Error("Missing return statement in function");
+  }
+
+  final public QualifiedName ParseQualifiedName() throws ParseException {String qualifiedName, str;
+qualifiedName = "";
+    str = ParameterizedName();
+qualifiedName = qualifiedName + str;
+    label_4:
+    while (true) {
+      switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
+      case DOUBLECOLON:{
+        ;
+        break;
+        }
+      default:
+        jj_la1[9] = jj_gen;
+        break label_4;
+      }
+      jj_consume_token(DOUBLECOLON);
+      str = ParameterizedName();
+qualifiedName = qualifiedName +
+        "::" + str;
+    }
+QualifiedName res = new QualifiedName(qualifiedName);
+                //System.out.println(res);
+                {if ("" != null) return res;}
+    throw new Error("Missing return statement in function");
+  }
+
+  final public ArrayList<VariableDeclaration> TemplateParamList() throws ParseException {ArrayList<VariableDeclaration> params;
+        String type;
+        String name;
+params = new ArrayList<VariableDeclaration>();
+    jj_consume_token(TEMPLATE);
+    jj_consume_token(LESS_THAN);
+    type = jj_consume_token(IDENTIFIER).image;
+    name = jj_consume_token(IDENTIFIER).image;
+params.add(new VariableDeclaration(type, name));
+    label_5:
+    while (true) {
+      switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
+      case COMMA:{
+        ;
+        break;
+        }
+      default:
+        jj_la1[10] = jj_gen;
+        break label_5;
+      }
+      jj_consume_token(COMMA);
+      type = jj_consume_token(IDENTIFIER).image;
+      name = jj_consume_token(IDENTIFIER).image;
+params.add(new VariableDeclaration(type, name));
+    }
+    jj_consume_token(GREATER_THAN);
+//System.out.println(params);
+                {if ("" != null) return params;}
+    throw new Error("Missing return statement in function");
+  }
+
+  final public ArrayList<VariableDeclaration > FormalParamList() throws ParseException {ArrayList<VariableDeclaration > typeParams;
+        VariableDeclaration varDecl;
+typeParams = new ArrayList<VariableDeclaration >();
+    jj_consume_token(OPEN_PAREN);
+    switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
+    case CONST:
+    case STRUCT:
+    case CLASS:
+    case UNSIGNED:
+    case IDENTIFIER:{
+      varDecl = TypeParam();
+typeParams.add(varDecl);
+      label_6:
+      while (true) {
+        switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
+        case COMMA:{
+          ;
+          break;
+          }
+        default:
+          jj_la1[11] = jj_gen;
+          break label_6;
+        }
+        jj_consume_token(COMMA);
+        varDecl = TypeParam();
+typeParams.add(varDecl);
+      }
+      break;
+      }
+    default:
+      jj_la1[12] = jj_gen;
+      ;
+    }
+    jj_consume_token(CLOSE_PAREN);
+{if ("" != null) return typeParams;}
+    throw new Error("Missing return statement in function");
+  }
+
+  final public VariableDeclaration Declaration() throws ParseException {String type, param;
+    type = Type();
+    param = jj_consume_token(IDENTIFIER).image;
+    jj_consume_token(SEMI_COLON);
+{if ("" != null) return new VariableDeclaration(type, param);}
+    throw new Error("Missing return statement in function");
+  }
+
+  final public VariableDeclaration TypeParam() throws ParseException {String type, param;
+    type = Type();
+    param = jj_consume_token(IDENTIFIER).image;
+{if ("" != null) return new VariableDeclaration(type, param);}
+    throw new Error("Missing return statement in function");
+  }
+
+  /** Generated Token Manager. */
+  public UtilParserTokenManager token_source;
+  JavaCharStream jj_input_stream;
+  /** Current token. */
+  public Token token;
+  /** Next token. */
+  public Token jj_nt;
+  private int jj_ntk;
+  private int jj_gen;
+  final private int[] jj_la1 = new int[13];
+  static private int[] jj_la1_0;
+  static private int[] jj_la1_1;
+  static private int[] jj_la1_2;
+  static {
+      jj_la1_init_0();
+      jj_la1_init_1();
+      jj_la1_init_2();
+   }
+   private static void jj_la1_init_0() {
+      jj_la1_0 = new int[] {0x40,0x380,0x380,0x20000040,0x20000040,0x4000000,0x0,0x1800,0x1800,0x0,0x4000000,0x4000000,0x103c0,};
+   }
+   private static void jj_la1_init_1() {
+      jj_la1_1 = new int[] {0x0,0x0,0x0,0x1,0x1,0x0,0x200,0x0,0x0,0x100000,0x0,0x0,0x0,};
+   }
+   private static void jj_la1_init_2() {
+      jj_la1_2 = new int[] {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,};
+   }
+
+  /** Constructor with InputStream. */
+  public UtilParser(java.io.InputStream stream) {
+     this(stream, null);
+  }
+  /** Constructor with InputStream and supplied encoding */
+  public UtilParser(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 UtilParserTokenManager(jj_input_stream);
+    token = new Token();
+    jj_ntk = -1;
+    jj_gen = 0;
+    for (int i = 0; i < 13; i++) jj_la1[i] = -1;
+  }
+
+  /** Reinitialise. */
+  public void ReInit(java.io.InputStream stream) {
+     ReInit(stream, null);
+  }
+  /** Reinitialise. */
+  public void ReInit(java.io.InputStream stream, String encoding) {
+    try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
+    token_source.ReInit(jj_input_stream);
+    token = new Token();
+    jj_ntk = -1;
+    jj_gen = 0;
+    for (int i = 0; i < 13; i++) jj_la1[i] = -1;
+  }
+
+  /** Constructor. */
+  public UtilParser(java.io.Reader stream) {
+    jj_input_stream = new JavaCharStream(stream, 1, 1);
+    token_source = new UtilParserTokenManager(jj_input_stream);
+    token = new Token();
+    jj_ntk = -1;
+    jj_gen = 0;
+    for (int i = 0; i < 13; i++) jj_la1[i] = -1;
+  }
+
+  /** Reinitialise. */
+  public void ReInit(java.io.Reader stream) {
+    jj_input_stream.ReInit(stream, 1, 1);
+    token_source.ReInit(jj_input_stream);
+    token = new Token();
+    jj_ntk = -1;
+    jj_gen = 0;
+    for (int i = 0; i < 13; i++) jj_la1[i] = -1;
+  }
+
+  /** Constructor with generated Token Manager. */
+  public UtilParser(UtilParserTokenManager tm) {
+    token_source = tm;
+    token = new Token();
+    jj_ntk = -1;
+    jj_gen = 0;
+    for (int i = 0; i < 13; i++) jj_la1[i] = -1;
+  }
+
+  /** Reinitialise. */
+  public void ReInit(UtilParserTokenManager tm) {
+    token_source = tm;
+    token = new Token();
+    jj_ntk = -1;
+    jj_gen = 0;
+    for (int i = 0; i < 13; i++) jj_la1[i] = -1;
+  }
+
+  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();
+  }
+
+
+/** Get the next Token. */
+  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;
+  }
+
+/** Get the specific 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;
+  }
+
+  private int jj_ntk_f() {
+    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.List<int[]> jj_expentries = new java.util.ArrayList<int[]>();
+  private int[] jj_expentry;
+  private int jj_kind = -1;
+
+  /** Generate ParseException. */
+  public ParseException generateParseException() {
+    jj_expentries.clear();
+    boolean[] la1tokens = new boolean[80];
+    if (jj_kind >= 0) {
+      la1tokens[jj_kind] = true;
+      jj_kind = -1;
+    }
+    for (int i = 0; i < 13; i++) {
+      if (jj_la1[i] == jj_gen) {
+        for (int j = 0; j < 32; j++) {
+          if ((jj_la1_0[i] & (1<<j)) != 0) {
+            la1tokens[j] = true;
+          }
+          if ((jj_la1_1[i] & (1<<j)) != 0) {
+            la1tokens[32+j] = true;
+          }
+          if ((jj_la1_2[i] & (1<<j)) != 0) {
+            la1tokens[64+j] = true;
+          }
+        }
+      }
+    }
+    for (int i = 0; i < 80; i++) {
+      if (la1tokens[i]) {
+        jj_expentry = new int[1];
+        jj_expentry[0] = i;
+        jj_expentries.add(jj_expentry);
+      }
+    }
+    int[][] exptokseq = new int[jj_expentries.size()][];
+    for (int i = 0; i < jj_expentries.size(); i++) {
+      exptokseq[i] = jj_expentries.get(i);
+    }
+    return new ParseException(token, exptokseq, tokenImage);
+  }
+
+  /** Enable tracing. */
+  final public void enable_tracing() {
+  }
+
+  /** Disable tracing. */
+  final public void disable_tracing() {
+  }
+
+}