parser checked
[cdsspec-compiler.git] / grammer / spec-compiler.jj
index a006e7b..2238268 100644 (file)
@@ -31,6 +31,8 @@
        @Check: (Optional)
                ...
        @Action: (Optional)
+               @DefineVar: Type var1 = SomeExpression (Optional)
+               @Code (Optional)
                ...
        @Post_action: (Optional)
        @Post_check: (Optional)
@@ -69,6 +71,11 @@ package edu.uci.eecs.specCompiler.grammerParser;
 
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
+import java.io.InputStream;
+import java.io.ByteArrayInputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
 
 import edu.uci.eecs.specCompiler.specExtraction.Construct;
 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
@@ -77,6 +84,8 @@ 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.ConditionalInterface;
+import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct.DefineVar;
 
        public class SpecParser {
                public static void main(String[] argvs)
@@ -84,12 +93,21 @@ import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
                        try {
                                FileInputStream fis = new FileInputStream("./grammer/spec.txt");
                                SpecParser parser = new SpecParser(fis);
-                               parser.Start();
+                               parser.Parse();
                                System.out.println("Parsing finished!");
                        } catch (FileNotFoundException e) {
                                e.printStackTrace();
                        }
                }
+       
+               public static Construct parseSpec(String text)
+               throws ParseException, TokenMgrError {
+                       InputStream input = new ByteArrayInputStream(text.getBytes());
+                       SpecParser parser = new SpecParser(input);
+                       return parser.Parse();
+               }
+
+
        }
 PARSER_END(SpecParser)
 
@@ -142,6 +160,10 @@ TOKEN :
        <CHECK: "@Check:">
 |
        <ACTION: "@Action:">
+|
+       <DEFINEVAR: "@DefineVar:">
+|
+       <CODE: "@Code:">
 |
        <POST_ACTION: "@Post_action:">
 |
@@ -187,6 +209,8 @@ TOKEN :
        <STAR: "*">
 |
        <NEGATE: "~">
+|
+       <EXCLAMATION: "!">
 |
        <AND: "&">
 |
@@ -288,7 +312,7 @@ TOKEN :
        < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
 }
 
-Construct Start() :
+Construct Parse() :
 {
        Construct res;  
 }
@@ -301,7 +325,10 @@ Construct Start() :
        LOOKAHEAD(3) res = Commit_point_define_check()
        )
        <EOF>
-       { return res; }
+       {
+               //System.out.println(res);
+               return res;
+       }
 }
 
 GlobalConstruct Global_construct() :
@@ -321,7 +348,6 @@ GlobalConstruct Global_construct() :
        <TAIL>
        {
                res.unfoldInterfaceCluster();
-               System.out.println(res);
                return res;
        }
 }
@@ -336,13 +362,15 @@ String C_CPP_CODE() :
                text = new StringBuilder();
                t = new Token();
        }
-       ((
+       (
+       //LOOKAHEAD(2)
+       (
        t = <IDENTIFIER> | 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 = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
+       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 = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR>
+       t = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR> |
        t = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> |
        t = <SEMI_COLON> | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
        t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL>
@@ -425,73 +453,160 @@ void Happens_before(GlobalConstruct inst) :
 
 InterfaceConstruct Interface() :
 {
-       InterfaceConstruct res; 
+       InterfaceConstruct res;
+       String interfaceName, condition, idCode, check, postAction,
+               postCheck, commitPoint, hbLabel, hbCondition;
+       ActionSubConstruct action;
+       ArrayList<String> commitPointSet;
+       HashMap<String, String> hbConditions;
 }
 {
-       { res = null; }
+       {
+               res = null;
+               action = null;
+               condition = "";
+               idCode = "";
+               check = "";
+               postAction = "";
+               postCheck = "";
+               commitPointSet = new ArrayList<String>();
+               hbConditions = new HashMap<String, String>();
+       }
        <HEAD> 
                <BEGIN>
-                       <INTERFACE> <IDENTIFIER>
-                       <COMMIT_POINT_SET> <IDENTIFIER> (<OR> <IDENTIFIER>)*
-                       (<CONDITION> C_CPP_CODE())?
-                       (<HB_CONDITION> <IDENTIFIER> C_CPP_CODE())*
-                       (<ID> C_CPP_CODE())?
-                       (<CHECK> C_CPP_CODE())?
-                       (<ACTION> C_CPP_CODE())?
-                       (<POST_ACTION> C_CPP_CODE())?
-                       (<POST_CHECK> C_CPP_CODE())?
+                       <INTERFACE> (interfaceName = <IDENTIFIER>.image)
+                       <COMMIT_POINT_SET>
+                               (commitPoint = <IDENTIFIER>.image
+                               { commitPointSet.add(commitPoint); }
+                               )
+                               (<OR>
+                                       (commitPoint = <IDENTIFIER>.image)
+                                       {
+                                               if (commitPointSet.contains(commitPoint)) {
+                                                       throw new ParseException(interfaceName + " has" +
+                                                               "duplicate commit point labels");
+                                               }
+                                               commitPointSet.add(commitPoint);
+                                       }
+                               )*
+
+                       (<CONDITION> (condition = C_CPP_CODE()))?
+                       (
+                               <HB_CONDITION>
+                               (hbLabel = <IDENTIFIER>.image)
+                               (hbCondition = C_CPP_CODE())
+                               {
+                                       if (hbConditions.containsKey(hbLabel)) {
+                                               throw new ParseException(interfaceName + " has" +
+                                                       "duplicate happens-before condtion labels");
+                                       }
+                                       hbConditions.put(hbLabel, hbCondition);
+                               }
+                       )*
+                       (<ID> (idCode = C_CPP_CODE()))?
+                       (<CHECK> (check = C_CPP_CODE()))?
+                       (action = Action())?
+                       (<POST_ACTION> (postAction = C_CPP_CODE()))?
+                       (<POST_CHECK> (postCheck = C_CPP_CODE()))?
                <END>
        <TAIL>
-       { return res; }
+       {
+               res = new InterfaceConstruct(interfaceName, commitPointSet, condition,
+                       hbConditions, idCode, check, action, postAction, postCheck);
+               return res;
+       }
+}
+
+ActionSubConstruct Action() :
+{
+       String type, name, expr, defineVarStr, code;
+       ArrayList<DefineVar> defineVars;
+}
+{
+       {
+               defineVars = new ArrayList<DefineVar>();
+               code = "";
+       }
+       <ACTION>
+       (
+               (
+               (<DEFINEVAR> (defineVarStr = C_CPP_CODE()) 
+               {
+                       int eqIdx = defineVarStr.indexOf('=');
+                       int typeEnd = defineVarStr.lastIndexOf(' ', eqIdx - 2);
+                       type = defineVarStr.substring(0, typeEnd);
+                       name = defineVarStr.substring(typeEnd + 1, eqIdx - 1);
+                       expr = defineVarStr.substring(eqIdx + 2);
+                       DefineVar defineVar = new DefineVar(type, name, expr);
+                       defineVars.add(defineVar);
+               })*  (<CODE> (code = C_CPP_CODE()))? ) 
+       )
+       
+       {
+               ActionSubConstruct res = new ActionSubConstruct(defineVars, code);
+               return res;
+       }
 }
 
 PotentialCPDefineConstruct Potential_commit_point_define() :
 {
-       PotentialCPDefineConstruct res; 
+       PotentialCPDefineConstruct res;
+       String label, condition;
 }
 {
 
        { res = null; }
        <HEAD> 
                <BEGIN>
-                       <POTENTIAL_COMMIT_POINT_DEFINE> C_CPP_CODE()
-                       <LABEL> <IDENTIFIER>
+                       <POTENTIAL_COMMIT_POINT_DEFINE> (condition = C_CPP_CODE())
+                       <LABEL> (label = <IDENTIFIER>.image)
                <END>
        <TAIL>
-       { return res; }
+       {
+               res = new PotentialCPDefineConstruct(label, condition); 
+               return res;
+       }
 }
 
 
 CPDefineConstruct Commit_point_define() :
 {
-       CPDefineConstruct res;  
+       CPDefineConstruct res;
+       String label, potentialCPLabel, condition;
 }
 {
 
        { res = null; }
        <HEAD> 
                <BEGIN>
-                       <COMMIT_POINT_DEFINE> C_CPP_CODE()
-                       <POTENTIAL_COMMIT_POINT_LABEL> <IDENTIFIER>
-                       <LABEL> <IDENTIFIER>
+                       <COMMIT_POINT_DEFINE> (condition = C_CPP_CODE())
+                       <POTENTIAL_COMMIT_POINT_LABEL> (potentialCPLabel = <IDENTIFIER>.image)
+                       <LABEL> (label = <IDENTIFIER>.image)
                <END>
        <TAIL>
-       { return res; }
+       {
+               res = new CPDefineConstruct(label, potentialCPLabel, condition);
+               return res;
+       }
 }
 
 
 CPDefineCheckConstruct Commit_point_define_check() :
 {
        CPDefineCheckConstruct res;     
+       String label, condition;
 }
 {
 
        { res = null; }
        <HEAD> 
                <BEGIN> 
-                       <COMMIT_POINT_DEFINE_CHECK> C_CPP_CODE()
-                       <LABEL> <IDENTIFIER>
+                       <COMMIT_POINT_DEFINE_CHECK> (condition = C_CPP_CODE())
+                       <LABEL> (label = <IDENTIFIER>.image)
                <END>
        <TAIL>
-       { return res; }
+       {
+               res = new CPDefineCheckConstruct(label, condition);
+               return res;
+       }
 }