hashtable still buggy
[cdsspec-compiler.git] / grammer / spec_compiler.jj
index 22fada84bf025aaee801daa7da9d01569a3f9729..37749aab497b2b2438959e1c2b9b918ef508234e 100644 (file)
@@ -16,6 +16,7 @@
                # entry point.
                LANG = C;
        @Global_define:
+               @DeclareStruct:
                @DeclareVar:
                @InitVar:
                @DefineFunc:
@@ -29,7 +30,7 @@
        
        b) Interface construct
        @Begin
-       @Interface: ...
+       @Interface_decl: ...
        @Commit_point_set:
                IDENTIFIER | IDENTIFIER ...
        @Condition: ... (Optional)
        @Begin
        @Interface_define: <Interface_Name>
        @End
+
+       g) Interface declare & define construct
+       @Begin
+       @Interface_decl_define: <Interface_Name>
+       @Commit_point_set:
+               IDENTIFIER | IDENTIFIER ...
+       @Condition: ... (Optional)
+       @HB_Condition:
+               IDENTIFIER :: <C_CPP_Condition>
+       @HB_Condition: ...
+       @ID: ... (Optional, use default ID)
+       @Check: (Optional)
+               ...
+       @Action: (Optional)
+               ...
+       @Post_action: (Optional)
+       @Post_check: (Optional)
+       @End
+
 */
 
 
@@ -94,6 +114,7 @@ import java.io.File;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Arrays;
 
 import edu.uci.eecs.specCompiler.specExtraction.Construct;
 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
@@ -121,7 +142,7 @@ import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
                public static void main(String[] argvs)
                throws ParseException, TokenMgrError {
                        try {
-                               File f = new File("./grammer/spec.txt");
+                               File f = new File("./grammer/spec1.txt");
                                FileInputStream fis = new FileInputStream(f);
                                SpecParser parser = new SpecParser(fis);
                                
@@ -137,6 +158,7 @@ import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
                                        System.out.println(constructs.get(i));
                                }
                                
+                               
                                //parser.Test();
                                System.out.println("Parsing finished!");
                        } catch (FileNotFoundException e) {
@@ -160,7 +182,14 @@ import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
                        return null;
                }
 
-               public static ArrayList<String> getTemplateArg(String line)
+
+               private static ArrayList<String> breakLines(String all) {
+                       String lines[] = all.split("[\\r\\n]+");
+                       return new ArrayList<String>(Arrays.asList(lines));
+               }
+
+
+               public static ArrayList<VariableDeclaration> getTemplateArg(String line)
                throws ParseException {
                        InputStream input = new ByteArrayInputStream(line.getBytes());
                        SpecParser parser = new SpecParser(input);
@@ -177,18 +206,69 @@ import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
 
                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();
                }
 
+               /**
+               boolean spaceSeparator(Token t) {
+                       switch (t.image) {
+                               case "[":
+                               case "]":
+                               case "=":
+                               case "(":
+                               case ")":
+                               case ",":
+                               case ".":
+                               case "*":
+                               case "~":
+                               case "!":
+                               case "&":
+                               case "|":
+                               case "%":
+                               case "+":
+                               case "-":
+                               case "/":
+                               case "<":
+                               case ">":
+                               case "<=":
+                               case ">=":
+                               case "==":
+                               case "!=":
+                               case "&&":
+                               case "||":
+                               case "^":
+                               case "?":
+                               case ":":
+                               case "::":
+                               case "<<":
+                               case ">>":
+                               case ">>>":
+                               case "+=":
+                               case "-=":
+                               case "*=":
+                               case "/=":
+                               case "%=":
+                               case "^=":
+                               case "&=":
+                               case ";":
+                                       return false;
+                               default:
+                                       return true;
+                       }
+               }
+               */
+
        }
 PARSER_END(SpecParser)
 
 
 
-<*> SKIP :
+<IN_POTENTIAL_SPEC, IN_SPEC> SKIP :
 {
        " "
 |
@@ -217,6 +297,10 @@ SKIP : {
        "/*": IN_COMMENT
 }
 
+<DEFAULT> TOKEN: {
+       <ANY: ~[]>
+}
+
 <*> SKIP : {
        // "//" comment for the specification
        <"//" (~["\n", "\r"])* (["\n", "\r"])>
@@ -241,6 +325,8 @@ SKIP : {
        <OPTIONS: "@Options:">
 |
        <GLOBAL_DEFINE: "@Global_define:">
+|
+       <DECLARE_STRUCT: "@DeclareStruct:">
 |
        <DECLARE_VAR: "@DeclareVar:">
 |
@@ -292,7 +378,7 @@ SKIP : {
 }
 
 
-<IN_SPEC, DEFAULT> TOKEN :
+<IN_SPEC> TOKEN :
 {
 /*   Specification & C/C++ shared tokens   */
 // Reserved keywords
@@ -301,8 +387,16 @@ SKIP : {
        <STRUCT: "struct">
 |
        <CLASS: "class">
+|
+       <UNSIGNED: "unsigned">
 |
        <TEMPLATE: "template">
+|
+       <INLINE: "inline">
+|
+       <STATIC: "static">
+|
+       <FOR: "for">
 |
        <#DIGIT: ["0"-"9"]>
 |
@@ -332,6 +426,8 @@ SKIP : {
 |
 /*   C/C++ only token*/
        <DOT: ".">
+|
+       <DOLLAR: "$">
 |
        <STAR: "*">
 |
@@ -380,6 +476,28 @@ SKIP : {
        <COLON: ":">
 |
        <DOUBLECOLON: "::">
+|
+       <DOUBLELESSTHAN: "<<">
+|
+       <DOUBLEGREATERTHAN: ">>">
+|
+       <TRIPLEGREATERTHAN: ">>>">
+|
+       <PLUS_EQUALS: "+=">
+|
+       <MINUS_EQUALS: "-=">
+|
+       <TIMES_EQUALS: "*=">
+|
+       <DIVIDE_EQUALS: "/=">
+|
+       <MOD_EQUALS: "%=">
+|
+       <XOR_EQUALS: "^=">
+|
+       <OR_EQUALS: "|=">
+|
+       <AND_EQUALS: "&=">
 |
        <SEMI_COLON: ";">
 |
@@ -456,10 +574,10 @@ String Type() :
 }
 {
        { type = ""; }
-       ("const"
+       (<CONST>
        { type = "const"; }
        )?
-       (((str = <STRUCT>.image | str = <CLASS>.image) { type = type + " " + str; })? 
+       (((str = <STRUCT>.image | str = <CLASS>.image | str = <UNSIGNED>.image) { type = type + " " + str; })? 
        (
        name = ParseQualifiedName() {
                if (!type.equals(""))
@@ -468,7 +586,7 @@ String Type() :
                        type = name.fullName;
        })
        )
-       ((str = "const".image {
+       ((str = <CONST>.image {
                if (!type.equals(""))
                        type = type + " " + str;
                else
@@ -492,23 +610,6 @@ String Type() :
        }
 }
 
-void Test() :
-{
-       String str;     
-       FunctionHeader func;
-}
-{
-       /*
-       str = Type()
-       {
-               System.out.println(str);
-       }
-       */
-       func = FuncDecl() 
-       {
-               System.out.println(func);
-       }
-}
 
 String ParameterizedName() :
 {
@@ -517,8 +618,8 @@ String ParameterizedName() :
 }
 {
        (str = <IDENTIFIER>.image {res = str;})
-       ("<" str = <IDENTIFIER>.image { res = res + "<" + str; }
-       ("," str = <IDENTIFIER>.image { res = res + ", " + str; })* ">"
+       (<OPEN_BRACKET> str = Type() { res = res + "<" + str; }
+       (<COMMA> str = Type() { res = res + ", " + str; })* <CLOSE_BRACKET>
        { res = res + ">"; }
        )?
        {
@@ -533,6 +634,7 @@ FunctionHeader FuncDecl() :
        ArrayList<VariableDeclaration> args;
 }
 {
+       (<STATIC> | <INLINE>)*
        ret = Type() 
        funcName = ParseQualifiedName() 
        args = FormalParamList() 
@@ -559,23 +661,32 @@ QualifiedName ParseQualifiedName() :
        }
 }
 
-ArrayList<String> TemplateParamList() :
+ArrayList<VariableDeclaration> TemplateParamList() :
 {
-       ArrayList<String> params;
-       String str;
+       ArrayList<VariableDeclaration> params;
+       String type;
+       String name;
 }
 {
        {
-               params = new ArrayList<String>();
+               params = new ArrayList<VariableDeclaration>();
        }
        <TEMPLATE>
-       "<"
-       (str = <IDENTIFIER>.image 
-       str = <IDENTIFIER>.image {params.add(str);})
+       <OPEN_BRACKET>
+       (type = <IDENTIFIER>.image 
+       name = <IDENTIFIER>.image
+       {
+               params.add(new VariableDeclaration(type, name));
+       }
+       )
 
-       ("," str = <IDENTIFIER>.image 
-       str = <IDENTIFIER>.image {params.add(str);})*
-       ">"
+       (<COMMA> type = <IDENTIFIER>.image 
+       name = <IDENTIFIER>.image
+       {
+               params.add(new VariableDeclaration(type, name));
+       }
+       )*
+       <CLOSE_BRACKET>
        {
                //System.out.println(params);
                return params;
@@ -591,10 +702,10 @@ ArrayList<VariableDeclaration > FormalParamList() :
        {
                typeParams = new ArrayList<VariableDeclaration >();
        }
-       "("
+       <OPEN_PAREN>
        ((varDecl = TypeParam() {typeParams.add(varDecl);})
        ((<COMMA> varDecl = TypeParam() {typeParams.add(varDecl);}))*)?
-       ")"
+       <CLOSE_PAREN>
        {
                return typeParams;
        }
@@ -611,12 +722,16 @@ VariableDeclaration TypeParam() :
        }
 }
 
+
+
 ArrayList<String> C_CPP_CODE(ArrayList<String> headers) :
 {
        String text;
        Token t;
        boolean newLine = false;
+       boolean newSpace = true;
        boolean inTemplate = false;
+       boolean inForLoop = false;
        ArrayList<String> content;
        String header;
 }
@@ -629,42 +744,77 @@ ArrayList<String> C_CPP_CODE(ArrayList<String> headers) :
        (
        LOOKAHEAD(2)
        (
-       t = <CONST> | t = <STRUCT> | t = <CLASS> |
+       t = <CONST> | t = <STRUCT> | t = <CLASS> | t = <UNSIGNED> |
        (t = <TEMPLATE> { inTemplate = true;  })|
-       (header = <INCLUDE>.image {
+       t = <STATIC> | t = <INLINE> |
+       (t = <FOR> { inForLoop = true; })|
+       (t = <INCLUDE>
+       {
+               header = t.image;
+               newLine = true;
                if (headers != null) {
                        headers.add(header.substring(header.lastIndexOf(' ') + 1));
                }
        })
        | t = <IDENTIFIER> | t = <POUND> |
        (t = <OPEN_BRACE>  { newLine = true; } ) |
-       (t = <CLOSE_BRACE>  { newLine = true; } ) | 
+       (t = <CLOSE_BRACE>  { newLine = true; inForLoop = false;} ) | 
        t = <EQUALS> | t = <OPEN_PAREN> | t = <CLOSE_PAREN> | 
        t = <OPEN_BRACKET> | t = <CLOSE_BRACKET>
        | t = <HB_SYMBOL> | t = <COMMA> |
-       t = <DOT> | t = <STAR> | t = <NEGATE> | t = <EXCLAMATION> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
+       t = <DOT> | t = <STAR> | t = <DOLLAR> | 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> { 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; } )
+       t = <DOUBLELESSTHAN> | 
+       t = <DOUBLEGREATERTHAN> |
+       t = <TRIPLEGREATERTHAN> | 
+
+       t = <PLUS_EQUALS> |
+       t = <MINUS_EQUALS> |
+       t = <TIMES_EQUALS> |
+       t = <DIVIDE_EQUALS> |
+       t = <MOD_EQUALS> |
+       t = <XOR_EQUALS> |
+       t = <OR_EQUALS> |
+       t = <AND_EQUALS> |
+
+       (t = <SEMI_COLON> { if (!inForLoop) newLine = true; } )
        | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
        t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL> |
        (t = <DEFINE> { newLine = true; } )
        )
        {
-               text = text + " " + t.image;
+               if (text.equals("")) {
+                       text = t.image;
+                       newSpace = true;
+               } else {
+                       text = text + " " + t.image;
+                       /*
+                       if (newSpace && spaceSeparator(t)) {
+                               text = text + " " + t.image;
+                       } else {
+                               text = text + t.image;
+                               if (spaceSeparator(t))
+                                       newSpace = true;
+                       }*/
+               }
                if (newLine) {
                        content.add(text);
                        text = "";
                        newLine = false;
+                       inTemplate = false;
                }
        }
        )+
 
        {
+               if (content.size() == 0) {
+                       content.add(text);
+               }
                return content;
        }
 }
@@ -673,17 +823,55 @@ ArrayList<String> C_CPP_CODE(ArrayList<String> headers) :
 void Parse(File f, ArrayList<String> content, ArrayList<Construct> constructs, ArrayList<String> headers) :
 {
        Construct inst;
-       ArrayList<String> code;
+       StringBuilder sb;
+       boolean flushSB;
 }
 {
        {
                _file = f;
                _content = content;
                _constructs = constructs;
+               sb = new StringBuilder();
+       }
+       (
+       (inst = ParseSpec()
+       {
+               _constructs.add(inst);
+       }
+       ) |
+       //((code = C_CPP_CODE(headers)) { _content.addAll(code); })
+       (
+       flushSB = OriginalCode(sb)
+       {
+               if (flushSB) {
+                       sb = new StringBuilder();
+               }
+       }
+       )
+       )* 
+       // For the last piece of code
+       {
+               _content.add(sb.toString());
+       }
+       <EOF>
+}
+
+// If true, there's a new line and sb should be flushed
+boolean OriginalCode(StringBuilder sb) :
+{
+       String str;
+}
+{
+       str = <ANY>.image
+       {
+               if (!str.equals("\n")) {
+                       sb.append(str);
+                       return false;
+               } else {
+                       _content.add(sb.toString());
+                       return true;
+               }
        }
-       ((inst = ParseSpec() { _constructs.add(inst); }) |
-       ((code = C_CPP_CODE(headers)) { _content.addAll(code); })
-       )* <EOF>
 }
 
 Construct ParseSpec() :
@@ -747,26 +935,30 @@ GlobalConstruct Global_construct() :
 
 SequentialDefineSubConstruct Global_define() :
 {
-       ArrayList<String> initVar, defineFunc, code;
+       ArrayList<String> initVar, defineFunc, code, declareStruct;
        ArrayList<ArrayList<String>> defineFuncs;
        ArrayList<VariableDeclaration> declareVars;
+       ArrayList<ArrayList<String>> declareStructs;
        VariableDeclaration declareVar;
 
 }
 {
        {
-               declareVars = null;
+               declareVars = new ArrayList<VariableDeclaration>();
                initVar = null;
                defineFuncs = new ArrayList<ArrayList<String>>();
+               declareStructs = new ArrayList<ArrayList<String>>();
        }
        <GLOBAL_DEFINE>
-               (<DECLARE_VAR> ((declareVar = TypeParam() ";" {
+       (<DECLARE_STRUCT> (declareStruct = C_CPP_CODE(null) {
+               declareStructs.add(declareStruct); }))*
+               (<DECLARE_VAR> ((declareVar = TypeParam() <SEMI_COLON> {
                        declareVars.add(declareVar); } )*))?
        (<INIT_VAR> (code = C_CPP_CODE(null) { initVar = code; } ))?
        (<DEFINE_FUNC> (defineFunc = C_CPP_CODE(null) { defineFuncs.add(defineFunc); }))*
        {
                SequentialDefineSubConstruct res = new
-                       SequentialDefineSubConstruct(declareVars, initVar, defineFuncs);
+                       SequentialDefineSubConstruct(declareStructs, declareVars, initVar, defineFuncs);
                //System.out.println(res);
                return res;
        }
@@ -780,8 +972,8 @@ ConditionalInterface Conditional_interface() :
        {
                hbConditionLabel = "";
        }
-       interfaceName = <IDENTIFIER>.image (<OPEN_BRACKET> hbConditionLabel =
-       <IDENTIFIER>.image <CLOSE_BRACKET>)?
+       interfaceName = <IDENTIFIER>.image (<OPEN_PAREN> hbConditionLabel =
+       <IDENTIFIER>.image <CLOSE_PAREN>)?
        {
                return new ConditionalInterface(interfaceName, hbConditionLabel);
        }
@@ -794,14 +986,14 @@ void Interface_cluster(GlobalConstruct inst) :
 }
 {
        (clusterName= <IDENTIFIER>.image)
-       <EQUALS> <OPEN_PAREN>
+       <EQUALS> <OPEN_BRACE>
                (condInterface = Conditional_interface()
                { inst.addInterface2Cluster(clusterName, condInterface); } 
                )
                (<COMMA> condInterface = Conditional_interface()
                { inst.addInterface2Cluster(clusterName, condInterface); } 
                )*
-       <CLOSE_PAREN>
+       <CLOSE_BRACE>
 }
 
 void Interface_clusters(GlobalConstruct inst) :
@@ -835,16 +1027,14 @@ InterfaceConstruct Interface() :
 {
        {
                res = null;
-               action = null;
+               action = new ArrayList<String>();
                condition = "";
                idCode = "";
                check = "";
                postCheck = "";
                commitPointSet = new ArrayList<String>();
                hbConditions = new HashMap<String, String>();
-
-               action = null;
-               postAction = null;
+               postAction = new ArrayList<String>();
        }
                <BEGIN>
                        <INTERFACE> (interfaceName = <IDENTIFIER>.image)
@@ -866,7 +1056,7 @@ InterfaceConstruct Interface() :
                        (<CONDITION> (content = C_CPP_CODE(null) { condition = stringArray2String(content); }))?
                        (
                                <HB_CONDITION>
-                               (hbLabel = <IDENTIFIER>.image)
+                               (hbLabel = <IDENTIFIER>.image) <DOUBLECOLON>
                                (content = C_CPP_CODE(null) { hbCondition = stringArray2String(content); })
                                {
                                        if (hbConditions.containsKey(hbLabel)) {