add more, GlobalConstruct parsed
[cdsspec-compiler.git] / grammer / spec-compiler.jj
index 110dcb9..a006e7b 100644 (file)
        @Begin
        @Interface: ...
        @Commit_point_set:
-               ...
+               IDENTIFIER | IDENTIFIER ...
        @Condition: ... (Optional)
+       @HB_Condition:
+               IDENTIFIER :: <C_CPP_Condition>
+       @HB_Condition: ...
        @ID: ... (Optional, use default ID)
        @Check: (Optional)
                ...
@@ -67,7 +70,15 @@ package edu.uci.eecs.specCompiler.grammerParser;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 
-       class SpecParser {
+import edu.uci.eecs.specCompiler.specExtraction.Construct;
+import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
+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.ConditionalInterface;
+
+       public class SpecParser {
                public static void main(String[] argvs)
                throws ParseException, TokenMgrError {
                        try {
@@ -93,18 +104,17 @@ SKIP :
        "\r\n"
 |
        "\t"
-       /*
 |
        // "#" comment for the specification
        <"#" (~["\n", "\r"])* (["\n", "\r"])>
 |
        // "//" comment for the specification
        <"//" (~["\n", "\r"])* (["\n", "\r"])>
-       */
 }
 
 TOKEN :
 {
+/*   Above are specification-only tokens   */
        <HEAD: "/**">
 |
        <TAIL: "*/">
@@ -125,7 +135,7 @@ TOKEN :
 |
        <CONDITION: "@Condition:">
 |
-       <HB_CONDITIONS: "@HB_condition:">
+       <HB_CONDITION: "@HB_condition:">
 |
        <ID: "@ID:">
 |
@@ -146,112 +156,342 @@ TOKEN :
        <COMMIT_POINT_DEFINE: "@Commit_point_define:">
 |
        <POTENTIAL_COMMIT_POINT_LABEL: "@Potential_commit_point_label:">
+
+
+/*   Specification & C/C++ shared tokens   */
 |
        <#DIGIT: ["0"-"9"]>
 |
        <#LETTER: ["a"-"z", "A"-"Z"]>
 |
-       <IDENTIFIER: <LETTER> (<LETTER> | <DIGIT> | "_")*>
+       <IDENTIFIER: (<LETTER> | "_") (<LETTER> | <DIGIT> | "_")*>
 |
        <EQUALS: "=">
 |
-       <LEFT_PARAN: "{">
+       <OPEN_PAREN: "{">
+|
+       <CLOSE_PAREN: "}">
+|
+       <OPEN_BRACKET: "(">
 |
-       <RIGHT_PARAN: "}">
+       <CLOSE_BRACKET: ")">
 |
        <HB_SYMBOL: "->">
 |
        <COMMA: ",">
+
+|
+/*   C/C++ only token*/
+       <DOT: ".">
+|
+       <STAR: "*">
+|
+       <NEGATE: "~">
+|
+       <AND: "&">
+|
+       <OR: "|">
+|
+       <MOD: "%">
+|
+       <PLUS: "+">
+|
+       <PLUSPLUS: "++">
+|
+       <MINUS: "-">
+|
+       <MINUSMINUS: "--">
+|
+       <DIVIDE: "/">
+|
+       <BACKSLASH: "\\">
+|
+       <LESS_THAN: "<">
+|
+       <GREATER_THAN: ">">
+|
+       <GREATER_EQUALS: ">=">
+|
+       <LESS_EQUALS: "<=">
+|
+       <LOGICAL_EQUALS: "==">
+|
+       <NOT_EQUALS: "!=">
+|
+       <LOGICAL_AND: "&&">
+|
+       <LOGICAL_OR: "||">
+|
+       <XOR: "^">
+|
+       <QUESTION_MARK: "?">
+|
+       <COLON: ":">
+|
+       <DOUBLECOLON: "::">
+|
+       <SEMI_COLON: ";">
+|
+       <STRING_LITERAL:
+       "\""
+       ((~["\"","\\","\n","\r"])
+       | ("\\"
+               ( ["n","t","b","r","f","\\","'","\""]
+               | ["0"-"7"] ( ["0"-"7"] )?
+               | ["0"-"3"] ["0"-"7"]
+                       ["0"-"7"]
+               )
+               )
+       )*
+       "\"">
+|
+       <CHARACTER_LITERAL:
+       "'"
+       ((~["'","\\","\n","\r"])
+       | ("\\"
+               (["n","t","b","r","f","\\","'","\""]
+               | ["0"-"7"] ( ["0"-"7"] )?
+               | ["0"-"3"] ["0"-"7"]
+               ["0"-"7"]
+               )
+               )
+       )
+       "'">
+|
+       < INTEGER_LITERAL:
+        <DECIMAL_LITERAL> (["l","L"])?
+      | <HEX_LITERAL> (["l","L"])?
+      | <OCTAL_LITERAL> (["l","L"])?>
+|
+       < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
+|
+       < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
+|
+       < #OCTAL_LITERAL: "0" (["0"-"7"])* >
+|
+       < FLOATING_POINT_LITERAL:
+        <DECIMAL_FLOATING_POINT_LITERAL>
+      | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
+|
+       < #DECIMAL_FLOATING_POINT_LITERAL:
+        (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
+      | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
+      | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
+      | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
+|
+       < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
+|
+       < #HEXADECIMAL_FLOATING_POINT_LITERAL:
+        "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
+      | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
+|
+       < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
 }
 
-void Start() :
-{}
+Construct Start() :
 {
-       //Global_construct() <EOF>
-       <IDENTIFIER> <EOF>
-       //<IDENTIFIER> C_CPP_CODE() <EOF>
+       Construct res;  
+}
+{
+       (
+       LOOKAHEAD(3) res = Global_construct() |
+       LOOKAHEAD(3) res = Interface() |
+       LOOKAHEAD(3) res = Potential_commit_point_define() |
+       LOOKAHEAD(3) res = Commit_point_define() |
+       LOOKAHEAD(3) res = Commit_point_define_check()
+       )
+       <EOF>
+       { return res; }
 }
 
-void Global_construct() :
-{}
+GlobalConstruct Global_construct() :
 {
+       GlobalConstruct res;
+       String code;
+}
+{
+       { res = null; }
        <HEAD>
                <BEGIN> 
-                       Global_define() (Interface_cluster())? (Happens_before())?
+                       (code = Global_define())
+                       { res = new GlobalConstruct(code); }
+                       (Interface_clusters(res))?
+                       (Happens_before(res))?
                <END>
        <TAIL>
+       {
+               res.unfoldInterfaceCluster();
+               System.out.println(res);
+               return res;
+       }
 }
 
-/*
-void C_CPP_CODE() :
-{}
+String C_CPP_CODE() :
 {
-       <(~["@"])+>
+       StringBuilder text;
+       Token t;
 }
-*/
-void Global_define() :
-{}
 {
-       <GLOBAL_DEFINE> //C_CPP_CODE()
+       {
+               text = new StringBuilder();
+               t = new Token();
+       }
+       ((
+       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 = <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 = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> |
+       t = <SEMI_COLON> | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
+       t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL>
+       )
+       {
+               text.append(t.image);
+               if (t.image.equals(";") || t.image.equals("\\")
+                       || t.image.equals("{") || t.image.equals("}"))
+                       text.append("\n");
+               else
+                       text.append(" ");
+       }
+       )+
+       {
+               //System.out.println(text);
+               return text.toString();
+       }
 }
 
-void Conditional_interface() :
-{}
+String Global_define() :
 {
-       <IDENTIFIER> (<"(" <IDENTIFIER> ")">)*
+       String code;    
+}
+{
+       <GLOBAL_DEFINE> (code = C_CPP_CODE())
+       {
+               return code;
+       }
 }
 
-void Interface_cluster() :
-{}
+ConditionalInterface Conditional_interface() :
 {
-       <IDENTIFIER> <EQUALS> <LEFT_PARAN>
-               Conditional_interface() (<COMMA> Conditional_interface())*
-       <RIGHT_PARAN>
+       String interfaceName, hbConditionLabel;
+}
+{
+       {
+               hbConditionLabel = "";
+       }
+       interfaceName = <IDENTIFIER>.image (<OPEN_BRACKET> hbConditionLabel =
+       <IDENTIFIER>.image <CLOSE_BRACKET>)?
+       {
+               return new ConditionalInterface(interfaceName, hbConditionLabel);
+       }
 }
 
-void Interface_clusters() :
-{}
+void Interface_cluster(GlobalConstruct inst) :
 {
-       <INTERFACE_CLUSTER> (Interface_cluster())+
+       String clusterName;
+       ConditionalInterface condInterface;
+}
+{
+       (clusterName= <IDENTIFIER>.image)
+       <EQUALS> <OPEN_PAREN>
+               (condInterface = Conditional_interface()
+               { inst.addInterface2Cluster(clusterName, condInterface); } 
+               )
+               (<COMMA> condInterface = Conditional_interface()
+               { inst.addInterface2Cluster(clusterName, condInterface); } 
+               )*
+       <CLOSE_PAREN>
 }
 
-void Happens_before() :
+void Interface_clusters(GlobalConstruct inst) :
 {}
 {
-       <HAPPENS_BEFORE> (Conditional_interface() <HB_SYMBOL> Conditional_interface())+
+       <INTERFACE_CLUSTER> (Interface_cluster(inst))+
 }
 
-void Interface() :
-{}
+void Happens_before(GlobalConstruct inst) :
 {
+       ConditionalInterface left, right;       
+}
+{
+       <HAPPENS_BEFORE> 
+       (
+       left = Conditional_interface() <HB_SYMBOL> right = Conditional_interface()
+       { inst.addHBCondition(left, right); }
+       )+
+}
+
+InterfaceConstruct Interface() :
+{
+       InterfaceConstruct res; 
+}
+{
+       { res = null; }
+       <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())?
+               <END>
        <TAIL>
+       { return res; }
 }
 
-void Potential_commit_point_define() :
-{}
+PotentialCPDefineConstruct Potential_commit_point_define() :
+{
+       PotentialCPDefineConstruct res; 
+}
 {
+
+       { res = null; }
        <HEAD> 
-               <BEGIN> 
+               <BEGIN>
+                       <POTENTIAL_COMMIT_POINT_DEFINE> C_CPP_CODE()
+                       <LABEL> <IDENTIFIER>
                <END>
        <TAIL>
+       { return res; }
 }
 
 
-void Commit_point_define() :
-{}
+CPDefineConstruct Commit_point_define() :
 {
+       CPDefineConstruct res;  
+}
+{
+
+       { res = null; }
        <HEAD> 
-               <BEGIN> 
+               <BEGIN>
+                       <COMMIT_POINT_DEFINE> C_CPP_CODE()
+                       <POTENTIAL_COMMIT_POINT_LABEL> <IDENTIFIER>
+                       <LABEL> <IDENTIFIER>
                <END>
        <TAIL>
+       { return res; }
 }
 
 
-void Commit_point_define_check() :
-{}
+CPDefineCheckConstruct Commit_point_define_check() :
 {
+       CPDefineCheckConstruct res;     
+}
+{
+
+       { res = null; }
        <HEAD> 
                <BEGIN> 
+                       <COMMIT_POINT_DEFINE_CHECK> C_CPP_CODE()
+                       <LABEL> <IDENTIFIER>
                <END>
        <TAIL>
+       { return res; }
 }