small change
[cdsspec-compiler.git] / grammer / spec_compiler.jj
index 57956dca166ebdf1dcf5b66e3d5a18c2dcda178e..440007ad07fed8c985a509fc1daf800dba463080 100644 (file)
@@ -109,21 +109,34 @@ import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct.DefineVar;
 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.EntryPointConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.ClassBeginConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.ClassEndConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
 
        public class SpecParser {
                private static ArrayList<String> _content;
                private static File _file;
                private static ArrayList<Construct> _constructs;
 
+
                public static void main(String[] argvs)
                throws ParseException, TokenMgrError {
                        try {
+                               String line = "int* A::B<sfd, _sdf>::id(const char * ch_ptr, int a)";
+                               System.out.println(parseFuncHeader(line));
+
                                File f = new File("./grammer/spec.txt");
                                FileInputStream fis = new FileInputStream(f);
                                SpecParser parser = new SpecParser(fis);
+                               /**
                                ArrayList<String> content = new ArrayList<String>();
                                ArrayList<Construct> constructs = new ArrayList<Construct>();
                                parser.Parse(f, content, constructs);
+                               for (int i = 0; i < content.size(); i++) {
+                                       System.out.println(content.get(i));
+                               }
+                               */
+                               parser.Test();
                                System.out.println("Parsing finished!");
                        } catch (FileNotFoundException e) {
                                e.printStackTrace();
@@ -141,6 +154,21 @@ import edu.uci.eecs.specCompiler.specExtraction.EntryPointConstruct;
                        }
                }
 
+               public static ArrayList<String> getTemplateArg(String line)
+               throws ParseException {
+                       InputStream input = new ByteArrayInputStream(line.getBytes());
+                       SpecParser parser = new SpecParser(input);
+                       return parser.TemplateParamList();
+               }
+
+               public static FunctionHeader parseFuncHeader(String line)
+               throws ParseException {
+                       InputStream input = new ByteArrayInputStream(line.getBytes());
+                       SpecParser parser = new SpecParser(input);
+                       return parser.FuncDecl();
+               }
+
+
                public static String stringArray2String(ArrayList<String> content) {
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < content.size(); i++) {
@@ -223,6 +251,10 @@ SKIP : {
        <COMMIT_POINT_SET: "@Commit_point_set:">
 |
        <ENTRY_POINT: "@Entry_point">
+|
+       <CLASS_BEGIN: "@Class_begin">
+|
+       <CLASS_END: "@Class_end">
 |
        <INTERFACE_DEFINE: "@Interface_define:">
 |
@@ -264,7 +296,9 @@ SKIP : {
 |
        <STRUCT: "struct">
 |
-       <TYPENAME: "typename">
+       <CLASS: "class">
+|
+       <TEMPLATE: "template">
 |
        <#DIGIT: ["0"-"9"]>
 |
@@ -421,13 +455,22 @@ String Type() :
        ("const"
        { type = "const"; }
        )?
-       (("struct" { type = type + " struct"; })? 
-       (str = <IDENTIFIER>.image {
+       (((str = <STRUCT>.image | str = <CLASS>.image) { type = type + " " + str; })? 
+       (
+       str = QualifiedName() {
+               if (!type.equals(""))
+                       type = type + " " + str;
+               else
+                       type = str;
+       })
+       (
+       str = ParameterizedName() {
                if (!type.equals(""))
                        type = type + " " + str;
                else
                        type = str;
-       }))
+       })
+       )
        ((str = "const".image {
                if (!type.equals(""))
                        type = type + " " + str;
@@ -452,6 +495,82 @@ String Type() :
        }
 }
 
+void Test() :
+{}
+{
+       Type()
+       //FuncDecl()
+}
+
+String ParameterizedName() :
+{
+       String res = "";
+       String str;
+}
+{
+       (str = <IDENTIFIER>.image {res = str;})
+       ("<" str = <IDENTIFIER>.image { res = res + "<" + str; }
+       ("," str = <IDENTIFIER>.image { res = res + ", " + str; })* ">"
+       { res = res + ">"; }
+       )?
+       {
+               return res;
+       }
+}
+
+FunctionHeader FuncDecl() :
+{
+       String ret, qualifiedName, bareName;
+       ArrayList<String> args;
+}
+{
+       ret = Type() 
+       qualifiedName = QualifiedName() 
+       bareName = <IDENTIFIER>.image
+       args = FormalParamList() 
+       {
+               FunctionHeader res = new FunctionHeader(ret, qualifiedName, bareName, args);
+               //System.out.println(res);
+               return res;
+       }
+}
+
+String QualifiedName() :
+{
+       String qualifiedName, str;
+}
+{
+       { qualifiedName = ""; }
+       (LOOKAHEAD(2) (str = ParameterizedName() { qualifiedName = qualifiedName +
+       str + "::"; } <DOUBLECOLON> ))* 
+       {
+               return qualifiedName;
+       }
+}
+
+ArrayList<String> TemplateParamList() :
+{
+       ArrayList<String> params;
+       String str;
+}
+{
+       {
+               params = new ArrayList<String>();
+       }
+       <TEMPLATE>
+       "<"
+       (str = <IDENTIFIER>.image 
+       str = <IDENTIFIER>.image {params.add(str);})
+
+       ("," str = <IDENTIFIER>.image 
+       str = <IDENTIFIER>.image {params.add(str);})*
+       ">"
+       {
+               //System.out.println(params);
+               return params;
+       }
+}
+
 ArrayList<String> FormalParamList() :
 {
        ArrayList<String> typeParams;
@@ -460,9 +579,10 @@ ArrayList<String> FormalParamList() :
        {
                typeParams = new ArrayList<String>();
        }
+       "("
        (TypeParam(typeParams) (<COMMA> TypeParam(typeParams))*)?
+       ")"
        {
-               System.out.println(typeParams);
                return typeParams;
        }
 }
@@ -484,6 +604,7 @@ ArrayList<String> C_CPP_CODE() :
        String text;
        Token t;
        boolean newLine = false;
+       boolean inTemplate = false;
        ArrayList<String> content;
 }
 {
@@ -495,7 +616,8 @@ ArrayList<String> C_CPP_CODE() :
        (
        LOOKAHEAD(2)
        (
-       t = <CONST> | t = <STRUCT> | t = <TYPENAME> |
+       t = <CONST> | t = <STRUCT> | t = <CLASS> |
+       (t = <TEMPLATE> { inTemplate = true;  })|
        t = <IDENTIFIER> | t = <POUND> |
        (t = <OPEN_BRACE>  { newLine = true; } ) |
        (t = <CLOSE_BRACE>  { newLine = true; } ) | 
@@ -504,7 +626,9 @@ ArrayList<String> C_CPP_CODE() :
        | t = <HB_SYMBOL> | t = <COMMA> |
        t = <DOT> | t = <STAR> | t = <NEGATE> | t = <EXCLAMATION> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
        t = <PLUSPLUS> | t = <MINUS> | t = <MINUSMINUS> | t = <DIVIDE> | t = <BACKSLASH> |
-       t = <LESS_THAN> | t = <GREATER_THAN> | t = <GREATER_EQUALS>     | t = <LESS_EQUALS> |
+       t = <LESS_THAN> |
+       (t = <GREATER_THAN> { if (inTemplate) newLine = true; }) |
+       t = <GREATER_EQUALS>    | t = <LESS_EQUALS> |
        t = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR> |
        t = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> |
        (t = <SEMI_COLON> { newLine = true; } )
@@ -557,6 +681,8 @@ Construct ParseSpec() :
        LOOKAHEAD(2) res = Commit_point_define() |
        LOOKAHEAD(2) res = Commit_point_define_check() |
        LOOKAHEAD(2) res = Entry_point() |
+       LOOKAHEAD(2) res = Class_begin() |
+       LOOKAHEAD(2) res = Class_end() |
        LOOKAHEAD(2) res = Interface_define()
        )
        {
@@ -843,6 +969,30 @@ EntryPointConstruct Entry_point() :
        }
 }
 
+ClassBeginConstruct Class_begin() :
+{}
+{
+
+               <BEGIN> 
+                       <CLASS_BEGIN>
+               <END>
+       {
+               return new ClassBeginConstruct(_file, _content.size());
+       }
+}
+
+ClassEndConstruct Class_end() :
+{}
+{
+
+               <BEGIN> 
+                       <CLASS_END>
+               <END>
+       {
+               return new ClassEndConstruct(_file, _content.size());
+       }
+}
+
 InterfaceDefineConstruct Interface_define() :
 {
        String name;