changes
[cdsspec-compiler.git] / grammer / spec_compiler.jj
index 150ad94f7345712b040f8c9c736c673d5dd31ef2..305eabf7ce927ca58f0711193f103a95d40146c4 100644 (file)
        @Begin
        @Potential_commit_point_define: ...
        @Label: ...
+       @End
+
+               OR
+
+       @Begin
+       @Potential_additional_ordering_point_define: ...
+       @Label: ...
        @End
 
        d) Commit point define construct
        @Begin
        @Commit_point_define_check: ...
        @Label: ...
+       @End
+
+               OR
+       
+       # Addition ordering point is used to order operations when there are "equal"
+       # commit point operations on the same location and that we cannot decide
+       # which operation goes first, we will use additional ordering point to order
+       # them (it's just similar to commit points). In implementation, we can just
+       # treat them as commit points with a special flag.
+
+       @Begin
+       @Additional_ordering_point_define: ...
+       @Potential_additional_ordering_point_label: ...
+       @Label: ...
        @End
        
                OR
+       
+       @Begin
+       @Additional_ordering_point_define: ...
+       @Label: ...
+       @End
 
+               OR
+
+       @Begin
+       @Additional_ordering_point_define_check: ...
+       @Label: ...
+       @End
+
+       // Commit point clear (just as a normal commit point, but it is used to
+       // clear all commit points)
        @Begin
-       @Commit_point_define: ...
-       @Potential_commit_point_label: ...
+       @Commit_point_clear: ...
        @Label: ...
        @End
 
@@ -114,6 +148,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,6 +156,7 @@ import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.CPClearConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
@@ -141,10 +177,10 @@ 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);
-                               /*
+                               
                                ArrayList<String> content = new ArrayList<String>();
                                ArrayList<Construct> constructs = new ArrayList<Construct>();
                                ArrayList<String> headers = new ArrayList<String>();
@@ -156,9 +192,9 @@ import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
                                for (int i = 0; i < constructs.size(); i++) {
                                        System.out.println(constructs.get(i));
                                }
-                               */
                                
-                               parser.Test();
+                               
+                               //parser.Test();
                                System.out.println("Parsing finished!");
                        } catch (FileNotFoundException e) {
                                e.printStackTrace();
@@ -181,6 +217,13 @@ import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
                        return null;
                }
 
+
+               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());
@@ -260,7 +303,7 @@ PARSER_END(SpecParser)
 
 
 
-<*> SKIP :
+<IN_POTENTIAL_SPEC, IN_SPEC> SKIP :
 {
        " "
 |
@@ -289,6 +332,10 @@ SKIP : {
        "/*": IN_COMMENT
 }
 
+<DEFAULT> TOKEN: {
+       <ANY: ~[]>
+}
+
 <*> SKIP : {
        // "//" comment for the specification
        <"//" (~["\n", "\r"])* (["\n", "\r"])>
@@ -319,6 +366,8 @@ SKIP : {
        <DECLARE_VAR: "@DeclareVar:">
 |
        <INIT_VAR: "@InitVar:">
+|
+       <CLEANUP: "@Cleanup:">
 |
        <DEFINE_FUNC: "@DefineFunc:">
 |
@@ -355,18 +404,28 @@ SKIP : {
        <POST_CHECK: "@Post_check:">
 |
        <POTENTIAL_COMMIT_POINT_DEFINE: "@Potential_commit_point_define:">
+|
+       <POTENTIAL_ADDITIONAL_ORDERING_POINT_DEFINE: "@Potential_additional_ordering_point_define:">
 |
        <LABEL: "@Label:">
 |
        <COMMIT_POINT_DEFINE_CHECK: "@Commit_point_define_check:">
+|
+       <ADDITIONAL_ORDERING_POINT_DEFINE_CHECK: "@Additional_ordering_point_define_check:">
 |
        <COMMIT_POINT_DEFINE: "@Commit_point_define:">
+|
+       <ADDITIONAL_ORDERING_POINT_DEFINE: "@Additional_ordering_point_define:">
+|
+       <COMMIT_POINT_CLEAR: "@Commit_point_clear:">
 |
        <POTENTIAL_COMMIT_POINT_LABEL: "@Potential_commit_point_label:">
+|
+       <POTENTIAL_ADDITIONAL_ORDERING_POINT_LABEL: "@Potential_additional_ordering_point_label:">
 }
 
 
-<IN_SPEC, DEFAULT> TOKEN :
+<IN_SPEC> TOKEN :
 {
 /*   Specification & C/C++ shared tokens   */
 // Reserved keywords
@@ -414,6 +473,8 @@ SKIP : {
 |
 /*   C/C++ only token*/
        <DOT: ".">
+|
+       <DOLLAR: "$">
 |
        <STAR: "*">
 |
@@ -560,7 +621,7 @@ String Type() :
 }
 {
        { type = ""; }
-       ("const"
+       (<CONST>
        { type = "const"; }
        )?
        (((str = <STRUCT>.image | str = <CLASS>.image | str = <UNSIGNED>.image) { type = type + " " + str; })? 
@@ -572,7 +633,7 @@ String Type() :
                        type = name.fullName;
        })
        )
-       ((str = "const".image {
+       ((str = <CONST>.image {
                if (!type.equals(""))
                        type = type + " " + str;
                else
@@ -596,24 +657,6 @@ String Type() :
        }
 }
 
-void Test() :
-{
-       String str;     
-       FunctionHeader func;
-}
-{
-       /*
-       str = Type()
-       {
-               System.out.println(str);
-       }
-       */
-       func = FuncDecl() 
-       {
-               System.out.println(func);
-       }
-       
-}
 
 String ParameterizedName() :
 {
@@ -622,8 +665,8 @@ String ParameterizedName() :
 }
 {
        (str = <IDENTIFIER>.image {res = str;})
-       ("<" str = Type() { res = res + "<" + str; }
-       ("," str = Type() { res = res + ", " + str; })* ">"
+       (<OPEN_BRACKET> str = Type() { res = res + "<" + str; }
+       (<COMMA> str = Type() { res = res + ", " + str; })* <CLOSE_BRACKET>
        { res = res + ">"; }
        )?
        {
@@ -676,7 +719,7 @@ ArrayList<VariableDeclaration> TemplateParamList() :
                params = new ArrayList<VariableDeclaration>();
        }
        <TEMPLATE>
-       "<"
+       <OPEN_BRACKET>
        (type = <IDENTIFIER>.image 
        name = <IDENTIFIER>.image
        {
@@ -684,13 +727,13 @@ ArrayList<VariableDeclaration> TemplateParamList() :
        }
        )
 
-       ("," type = <IDENTIFIER>.image 
+       (<COMMA> type = <IDENTIFIER>.image 
        name = <IDENTIFIER>.image
        {
                params.add(new VariableDeclaration(type, name));
        }
        )*
-       ">"
+       <CLOSE_BRACKET>
        {
                //System.out.println(params);
                return params;
@@ -706,10 +749,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;
        }
@@ -766,7 +809,7 @@ ArrayList<String> C_CPP_CODE(ArrayList<String> headers) :
        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; }) |
@@ -827,17 +870,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() :
@@ -851,6 +932,10 @@ Construct ParseSpec() :
        LOOKAHEAD(2) res = Potential_commit_point_define() |
        LOOKAHEAD(2) res = Commit_point_define() |
        LOOKAHEAD(2) res = Commit_point_define_check() |
+       LOOKAHEAD(2) res = Potential_additional_ordering_point_define() |
+       LOOKAHEAD(2) res = Additional_ordering_point_define() |
+       LOOKAHEAD(2) res = Additional_ordering_point_define_check() |
+       LOOKAHEAD(2) res = Commit_point_clear() |
        LOOKAHEAD(2) res = Entry_point() |
        LOOKAHEAD(2) res = Class_begin() |
        LOOKAHEAD(2) res = Class_end() |
@@ -901,7 +986,7 @@ GlobalConstruct Global_construct() :
 
 SequentialDefineSubConstruct Global_define() :
 {
-       ArrayList<String> initVar, defineFunc, code, declareStruct;
+       ArrayList<String> initVar, cleanup, defineFunc, code, declareStruct;
        ArrayList<ArrayList<String>> defineFuncs;
        ArrayList<VariableDeclaration> declareVars;
        ArrayList<ArrayList<String>> declareStructs;
@@ -911,20 +996,22 @@ SequentialDefineSubConstruct Global_define() :
 {
        {
                declareVars = new ArrayList<VariableDeclaration>();
-               initVar = null;
+               initVar = new ArrayList<String>();
+               cleanup = new ArrayList<String>();
                defineFuncs = new ArrayList<ArrayList<String>>();
                declareStructs = new ArrayList<ArrayList<String>>();
        }
        <GLOBAL_DEFINE>
        (<DECLARE_STRUCT> (declareStruct = C_CPP_CODE(null) {
                declareStructs.add(declareStruct); }))*
-               (<DECLARE_VAR> ((declareVar = TypeParam() ";" {
+               (<DECLARE_VAR> ((declareVar = TypeParam() <SEMI_COLON> {
                        declareVars.add(declareVar); } )*))?
        (<INIT_VAR> (code = C_CPP_CODE(null) { initVar = code; } ))?
+       (<CLEANUP> (code = C_CPP_CODE(null) { cleanup = code; } ))?
        (<DEFINE_FUNC> (defineFunc = C_CPP_CODE(null) { defineFuncs.add(defineFunc); }))*
        {
                SequentialDefineSubConstruct res = new
-                       SequentialDefineSubConstruct(declareStructs, declareVars, initVar, defineFuncs);
+                       SequentialDefineSubConstruct(declareStructs, declareVars, initVar, cleanup, defineFuncs);
                //System.out.println(res);
                return res;
        }
@@ -1065,6 +1152,26 @@ PotentialCPDefineConstruct Potential_commit_point_define() :
        }
 }
 
+PotentialCPDefineConstruct Potential_additional_ordering_point_define() :
+{
+       PotentialCPDefineConstruct res;
+       String label, condition;
+       ArrayList<String> content;
+}
+{
+
+       { res = null; }
+               <BEGIN>
+                       <POTENTIAL_ADDITIONAL_ORDERING_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
+                       <LABEL> (label = <IDENTIFIER>.image)
+               <END>
+       {
+               // Set the boolean flag isAdditionalOrderingPoint to be true
+               res = new PotentialCPDefineConstruct(_file, _content.size(), true, label, condition); 
+               return res;
+       }
+}
+
 
 CPDefineConstruct Commit_point_define() :
 {
@@ -1081,7 +1188,47 @@ CPDefineConstruct Commit_point_define() :
                        <LABEL> (label = <IDENTIFIER>.image)
                <END>
        {
-               res = new CPDefineConstruct(_file, _content.size(), label, potentialCPLabel, condition);
+               res = new CPDefineConstruct(_file, _content.size(), false, label, potentialCPLabel, condition);
+               return res;
+       }
+}
+
+CPDefineConstruct Additional_ordering_point_define() :
+{
+       CPDefineConstruct res;
+       String label, potentialCPLabel, condition;
+       ArrayList<String> content;
+}
+{
+
+       { res = null; }
+               <BEGIN>
+                       <ADDITIONAL_ORDERING_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
+                       <POTENTIAL_ADDITIONAL_ORDERING_POINT_LABEL> (potentialCPLabel = <IDENTIFIER>.image)
+                       <LABEL> (label = <IDENTIFIER>.image)
+               <END>
+       {
+               // Set the boolean flag isAdditionalOrderingPoint to be true
+               res = new CPDefineConstruct(_file, _content.size(), true, label, potentialCPLabel, condition);
+               return res;
+       }
+}
+
+CPClearConstruct Commit_point_clear() :
+{
+       CPClearConstruct res;   
+       String label, condition;
+       ArrayList<String> content;
+}
+{
+
+       { res = null; }
+               <BEGIN> 
+                       <COMMIT_POINT_CLEAR> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
+                       <LABEL> (label = <IDENTIFIER>.image)
+               <END>
+       {
+               res = new CPClearConstruct(_file, _content.size(), label, condition);
                return res;
        }
 }
@@ -1101,7 +1248,27 @@ CPDefineCheckConstruct Commit_point_define_check() :
                        <LABEL> (label = <IDENTIFIER>.image)
                <END>
        {
-               res = new CPDefineCheckConstruct(_file, _content.size(), label, condition);
+               res = new CPDefineCheckConstruct(_file, _content.size(), false, label, condition);
+               return res;
+       }
+}
+
+CPDefineCheckConstruct Additional_ordering_point_define_check() :
+{
+       CPDefineCheckConstruct res;     
+       String label, condition;
+       ArrayList<String> content;
+}
+{
+
+       { res = null; }
+               <BEGIN> 
+                       <ADDITIONAL_ORDERING_POINT_DEFINE_CHECK> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
+                       <LABEL> (label = <IDENTIFIER>.image)
+               <END>
+       {
+               // Set the boolean flag isAdditionalOrderingPoint to be true
+               res = new CPDefineCheckConstruct(_file, _content.size(), true, label, condition);
                return res;
        }
 }