more changes
[cdsspec-compiler.git] / grammer / spec-compiler.jj
index 85aabff176d0ab19da6572b2fac30649eb15f218..e4f723f64f99d828bc615ef9a08e45bca4e3ce48 100644 (file)
        
        a) Global construct
        @Begin
+       @Options:
+               # If LANG is not define, it's C++ by default. C does not support class
+               # and template, so if it's defined as C, we should also have a explicit
+               # entry point.
+               LANG = C;
        @Global_define:
+               @DeclareVar:
+               @InitVar:
+               @DefineFunc:
                ...
        @Interface_cluster:
                ...
        @Potential_commit_point_label: ...
        @Label: ...
        @End
+
+       e) Entry point construct
+       @Begin
+       @Entry_point
+       @End
+
+       f) Interface define construct
+       @Begin
+       @Interface_define: <Interface_Name>
+       @End
 */
 
 
@@ -71,6 +89,8 @@ 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;
@@ -84,6 +104,9 @@ 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;
+import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.EntryPointConstruct;
 
        public class SpecParser {
                public static void main(String[] argvs)
@@ -97,6 +120,15 @@ import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct.DefineVar;
                                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)
 
@@ -129,8 +161,16 @@ TOKEN :
        <BEGIN: "@Begin">
 |
        <END: "@End">
+|
+       <OPTIONS: "@Options:">
 |
        <GLOBAL_DEFINE: "@Global_define:">
+|
+       <DECLARE_VAR: "@DeclareVar:">
+|
+       <INIT_VAR: "@InitVar:">
+|
+       <DEFINE_FUNC: "@DefineFunc:">
 |
        <INTERFACE_CLUSTER: "@Interface_cluster:">
 |
@@ -139,6 +179,10 @@ TOKEN :
        <INTERFACE: "@Interface:">
 |
        <COMMIT_POINT_SET: "@Commit_point_set:">
+|
+       <ENTRY_POINT: "@Entry_point">
+|
+       <INTERFACE_DEFINE: "@Interface_define:">
 |
        <CONDITION: "@Condition:">
 |
@@ -198,6 +242,8 @@ TOKEN :
        <STAR: "*">
 |
        <NEGATE: "~">
+|
+       <EXCLAMATION: "!">
 |
        <AND: "&">
 |
@@ -309,11 +355,13 @@ Construct Parse() :
        LOOKAHEAD(3) res = Interface() |
        LOOKAHEAD(3) res = Potential_commit_point_define() |
        LOOKAHEAD(3) res = Commit_point_define() |
-       LOOKAHEAD(3) res = Commit_point_define_check()
+       LOOKAHEAD(3) res = Commit_point_define_check() |
+       LOOKAHEAD(3) res = Entry_point() |
+       LOOKAHEAD(3) res = Interface_define()
        )
        <EOF>
        {
-               System.out.println(res);
+               //System.out.println(res);
                return res;
        }
 }
@@ -321,14 +369,32 @@ Construct Parse() :
 GlobalConstruct Global_construct() :
 {
        GlobalConstruct res;
-       String code;
+       SequentialDefineSubConstruct code;
+       HashMap<String, String> options;
+       String key, value;
 }
 {
-       { res = null; }
+       {
+               res = null;
+               options = new HashMap<String, String>();
+       }
        <HEAD>
                <BEGIN> 
+                       (<OPTIONS>
+                               ((key = <IDENTIFIER>.image)
+                               <EQUALS>
+                               (value = <IDENTIFIER>.image)
+                               {
+                                       if (options.containsKey(key)) {
+                                               throw new ParseException("Duplicate options!");
+                                       }
+                                       options.put(key, value);
+                               }
+                               <SEMI_COLON>
+                               )*
+                       )?
                        (code = Global_define())
-                       { res = new GlobalConstruct(code); }
+                       { res = new GlobalConstruct(code, options); }
                        (Interface_clusters(res))?
                        (Happens_before(res))?
                <END>
@@ -354,7 +420,7 @@ String C_CPP_CODE() :
        (
        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> |
@@ -377,14 +443,24 @@ String C_CPP_CODE() :
        }
 }
 
-String Global_define() :
+SequentialDefineSubConstruct Global_define() :
 {
-       String code;    
+       String declareVar, initVar, defineFunc;
 }
 {
-       <GLOBAL_DEFINE> (code = C_CPP_CODE())
        {
-               return code;
+               declareVar = "";
+               initVar = "";
+               defineFunc = "";
+       }
+       <GLOBAL_DEFINE>
+               (<DECLARE_VAR> (declareVar = C_CPP_CODE()))?
+       (<INIT_VAR> (initVar = C_CPP_CODE()))?
+       (<DEFINE_FUNC> (defineFunc = C_CPP_CODE()))?
+       {
+               SequentialDefineSubConstruct res = new SequentialDefineSubConstruct(declareVar, initVar, defineFunc);
+               //System.out.println(res);
+               return res;
        }
 }
 
@@ -512,8 +588,11 @@ ActionSubConstruct Action() :
 {
        {
                defineVars = new ArrayList<DefineVar>();
+               code = "";
        }
        <ACTION>
+       (
+               (
                (<DEFINEVAR> (defineVarStr = C_CPP_CODE()) 
                {
                        int eqIdx = defineVarStr.indexOf('=');
@@ -523,13 +602,13 @@ ActionSubConstruct Action() :
                        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;
-               }
+               })*  (<CODE> (code = C_CPP_CODE()))? ) 
+       )
+       
+       {
+               ActionSubConstruct res = new ActionSubConstruct(defineVars, code);
+               return res;
+       }
 }
 
 PotentialCPDefineConstruct Potential_commit_point_define() :
@@ -594,3 +673,34 @@ CPDefineCheckConstruct Commit_point_define_check() :
                return res;
        }
 }
+
+EntryPointConstruct Entry_point() :
+{}
+{
+
+       <HEAD> 
+               <BEGIN> 
+                       <ENTRY_POINT>
+               <END>
+       <TAIL>
+       {
+               return new EntryPointConstruct();
+       }
+}
+
+InterfaceDefineConstruct Interface_define() :
+{
+       String name;    
+}
+{
+       <HEAD>
+               <BEGIN>
+                       <INTERFACE_DEFINE> (name = <IDENTIFIER>.image)
+               <END>
+       <TAIL>
+       {
+               return new InterfaceDefineConstruct(name);
+       }
+}
+
+