edits
[cdsspec-compiler.git] / grammer / util.jj
index 2be56d6f9f6fc7b211ff1a0cd0e510ff19bdccba..5b5446bb9c102c3882efd813cab07652ea4687be 100644 (file)
@@ -6,10 +6,11 @@ options {
 }
 
 PARSER_BEGIN(UtilParser)
-package edu.uci.eecs.specCompiler.grammerParser.utilParser;
-import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
-import edu.uci.eecs.specCompiler.specExtraction.QualifiedName;
-import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
+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;
@@ -17,47 +18,54 @@ import java.io.InputStream;
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.util.ArrayList;
+
        
-       public class UtilParser {
-               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 class UtilParser {
+       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 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 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) + "\n");
-                       }
-                       return sb.toString();
+       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) + "\n");
                }
+               return sb.toString();
        }
+}
 PARSER_END(UtilParser)
 
 SKIP :
@@ -272,7 +280,10 @@ String Type() :
        (<CONST>
        { type = "const"; }
        )?
-       (((str = <STRUCT>.image | str = <CLASS>.image | str = <UNSIGNED>.image) { type = type + " " + str; })? 
+       (
+               ((str = <STRUCT>.image | str = <CLASS>.image | str = <UNSIGNED>.image) {
+               type = type.equals("") ? type + str : type + " " + str;
+               })? 
        (
        name = ParseQualifiedName() {
                if (!type.equals(""))
@@ -331,8 +342,8 @@ String ParameterizedName() :
 }
 {
        (str = <IDENTIFIER>.image {res = str;})
-       (<OPEN_BRACKET> str = Type() { res = res + "<" + str; }
-       (<COMMA> str = Type() { res = res + ", " + str; })* <CLOSE_BRACKET>
+       (<LESS_THAN> str = Type() { res = res + "<" + str; }
+       (<COMMA> str = Type() { res = res + ", " + str; })* <GREATER_THAN>
        { res = res + ">"; }
        )?
        {
@@ -385,7 +396,7 @@ ArrayList<VariableDeclaration> TemplateParamList() :
                params = new ArrayList<VariableDeclaration>();
        }
        <TEMPLATE>
-       <OPEN_BRACKET>
+       <LESS_THAN>
        (type = <IDENTIFIER>.image 
        name = <IDENTIFIER>.image
        {
@@ -399,7 +410,7 @@ ArrayList<VariableDeclaration> TemplateParamList() :
                params.add(new VariableDeclaration(type, name));
        }
        )*
-       <CLOSE_BRACKET>
+       <GREATER_THAN>
        {
                //System.out.println(params);
                return params;
@@ -424,6 +435,17 @@ ArrayList<VariableDeclaration > FormalParamList() :
        }
 }
 
+VariableDeclaration Declaration() :
+{
+       String type, param;
+}
+{
+       (type = Type()) (param = <IDENTIFIER>.image)  <SEMI_COLON>
+       {
+               return new VariableDeclaration(type, param);
+       }
+}
+
 VariableDeclaration TypeParam() :
 {
        String type, param;