save
[cdsspec-compiler.git] / grammer / spec_compiler.jj
index 440007ad07fed8c985a509fc1daf800dba463080..22fada84bf025aaee801daa7da9d01569a3f9729 100644 (file)
@@ -20,6 +20,7 @@
                @InitVar:
                @DefineFunc:
                ...
+               @DefineFunc:
        @Interface_cluster:
                ...
        @Happens-before:
@@ -39,9 +40,6 @@
        @Check: (Optional)
                ...
        @Action: (Optional)
-               # Type here must be a pointer
-               @DefineVar: Type var1 = SomeExpression (Optional)
-               @Code (Optional)
                ...
        @Post_action: (Optional)
        @Post_check: (Optional)
@@ -104,14 +102,15 @@ 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;
 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.EntryPointConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.ClassBeginConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.ClassEndConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
+import edu.uci.eecs.specCompiler.specExtraction.QualifiedName;
+import edu.uci.eecs.specCompiler.specExtraction.SourceFileInfo;
+import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
 
        public class SpecParser {
                private static ArrayList<String> _content;
@@ -122,36 +121,43 @@ import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
                public static void main(String[] argvs)
                throws ParseException, TokenMgrError {
                        try {
-                               String line = "int* A::B<sfd, _sdf>::id(const char * ch_ptr, int a)";
-                               System.out.println(parseFuncHeader(line));
-
                                File f = new File("./grammer/spec.txt");
                                FileInputStream fis = new FileInputStream(f);
                                SpecParser parser = new SpecParser(fis);
-                               /**
+                               
                                ArrayList<String> content = new ArrayList<String>();
                                ArrayList<Construct> constructs = new ArrayList<Construct>();
-                               parser.Parse(f, content, constructs);
+                               ArrayList<String> headers = new ArrayList<String>();
+                               parser.Parse(f, content, constructs, headers);
                                for (int i = 0; i < content.size(); i++) {
                                        System.out.println(content.get(i));
                                }
-                               */
-                               parser.Test();
+                               
+                               for (int i = 0; i < constructs.size(); i++) {
+                                       System.out.println(constructs.get(i));
+                               }
+                               
+                               //parser.Test();
                                System.out.println("Parsing finished!");
                        } catch (FileNotFoundException e) {
                                e.printStackTrace();
                        }
                }
 
-               public static void ParseFile(File f, ArrayList<String> content, ArrayList<Construct> constructs)
+               public static SourceFileInfo ParseFile(File f)
                throws ParseException, TokenMgrError {
                        try {
                                InputStream input = new FileInputStream(f);
                                SpecParser parser = new SpecParser(input);
-                               parser.Parse(f, content, constructs);
+                               ArrayList<String> content = new ArrayList<String>(),
+                                       headers = new ArrayList<String>();
+                               ArrayList<Construct> constructs = new ArrayList<Construct>();
+                               parser.Parse(f, content, constructs, headers);
+                               return new SourceFileInfo(f, content, headers, constructs);
                        } catch (FileNotFoundException e) {
                                e.printStackTrace();
                        }
+                       return null;
                }
 
                public static ArrayList<String> getTemplateArg(String line)
@@ -267,8 +273,6 @@ SKIP : {
        <CHECK: "@Check:">
 |
        <ACTION: "@Action:">
-|
-       <DEFINEVAR: "@DefineVar:">
 |
        <CODE: "@Code:">
 |
@@ -325,7 +329,6 @@ SKIP : {
        <HB_SYMBOL: "->">
 |
        <COMMA: ",">
-
 |
 /*   C/C++ only token*/
        <DOT: ".">
@@ -449,6 +452,7 @@ String Type() :
 {
        String type;
        String str;
+       QualifiedName name;
 }
 {
        { type = ""; }
@@ -457,18 +461,11 @@ String Type() :
        )?
        (((str = <STRUCT>.image | str = <CLASS>.image) { type = type + " " + str; })? 
        (
-       str = QualifiedName() {
+       name = ParseQualifiedName() {
                if (!type.equals(""))
-                       type = type + " " + str;
+                       type = type + " " + name.fullName;
                else
-                       type = str;
-       })
-       (
-       str = ParameterizedName() {
-               if (!type.equals(""))
-                       type = type + " " + str;
-               else
-                       type = str;
+                       type = name.fullName;
        })
        )
        ((str = "const".image {
@@ -496,10 +493,21 @@ String Type() :
 }
 
 void Test() :
-{}
 {
-       Type()
-       //FuncDecl()
+       String str;     
+       FunctionHeader func;
+}
+{
+       /*
+       str = Type()
+       {
+               System.out.println(str);
+       }
+       */
+       func = FuncDecl() 
+       {
+               System.out.println(func);
+       }
 }
 
 String ParameterizedName() :
@@ -520,31 +528,34 @@ String ParameterizedName() :
 
 FunctionHeader FuncDecl() :
 {
-       String ret, qualifiedName, bareName;
-       ArrayList<String> args;
+       String ret;
+       QualifiedName funcName;
+       ArrayList<VariableDeclaration> args;
 }
 {
        ret = Type() 
-       qualifiedName = QualifiedName() 
-       bareName = <IDENTIFIER>.image
+       funcName = ParseQualifiedName() 
        args = FormalParamList() 
        {
-               FunctionHeader res = new FunctionHeader(ret, qualifiedName, bareName, args);
+               FunctionHeader res = new FunctionHeader(ret, funcName, args);
                //System.out.println(res);
                return res;
        }
 }
 
-String QualifiedName() :
+QualifiedName ParseQualifiedName() :
 {
        String qualifiedName, str;
 }
 {
        { qualifiedName = ""; }
-       (LOOKAHEAD(2) (str = ParameterizedName() { qualifiedName = qualifiedName +
-       str + "::"; } <DOUBLECOLON> ))* 
+       (str = ParameterizedName() { qualifiedName = qualifiedName + str; } )
+       ( <DOUBLECOLON> (str = ParameterizedName() { qualifiedName = qualifiedName +
+       "::" + str; }  ))*
        {
-               return qualifiedName;
+               QualifiedName res = new QualifiedName(qualifiedName);
+               //System.out.println(res);
+               return res;
        }
 }
 
@@ -571,41 +582,43 @@ ArrayList<String> TemplateParamList() :
        }
 }
 
-ArrayList<String> FormalParamList() :
+ArrayList<VariableDeclaration > FormalParamList() :
 {
-       ArrayList<String> typeParams;
+       ArrayList<VariableDeclaration > typeParams;
+       VariableDeclaration varDecl;
 }
 {
        {
-               typeParams = new ArrayList<String>();
+               typeParams = new ArrayList<VariableDeclaration >();
        }
        "("
-       (TypeParam(typeParams) (<COMMA> TypeParam(typeParams))*)?
+       ((varDecl = TypeParam() {typeParams.add(varDecl);})
+       ((<COMMA> varDecl = TypeParam() {typeParams.add(varDecl);}))*)?
        ")"
        {
                return typeParams;
        }
 }
 
-void TypeParam(ArrayList<String> typeParams) :
+VariableDeclaration TypeParam() :
 {
        String type, param;
 }
 {
        (type = Type()) (param = <IDENTIFIER>.image)
        {
-               typeParams.add(type);
-               typeParams.add(param);
+               return new VariableDeclaration(type, param);
        }
 }
 
-ArrayList<String> C_CPP_CODE() :
+ArrayList<String> C_CPP_CODE(ArrayList<String> headers) :
 {
        String text;
        Token t;
        boolean newLine = false;
        boolean inTemplate = false;
        ArrayList<String> content;
+       String header;
 }
 {
        {
@@ -618,7 +631,12 @@ ArrayList<String> C_CPP_CODE() :
        (
        t = <CONST> | t = <STRUCT> | t = <CLASS> |
        (t = <TEMPLATE> { inTemplate = true;  })|
-       t = <IDENTIFIER> | t = <POUND> |
+       (header = <INCLUDE>.image {
+               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 = <EQUALS> | t = <OPEN_PAREN> | t = <CLOSE_PAREN> | 
@@ -634,7 +652,6 @@ ArrayList<String> C_CPP_CODE() :
        (t = <SEMI_COLON> { newLine = true; } )
        | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
        t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL> |
-       (t = <INCLUDE> { newLine = true; } ) |
        (t = <DEFINE> { newLine = true; } )
        )
        {
@@ -653,7 +670,7 @@ ArrayList<String> C_CPP_CODE() :
 }
 
 
-void Parse(File f, ArrayList<String> content, ArrayList<Construct> constructs) :
+void Parse(File f, ArrayList<String> content, ArrayList<Construct> constructs, ArrayList<String> headers) :
 {
        Construct inst;
        ArrayList<String> code;
@@ -665,7 +682,7 @@ void Parse(File f, ArrayList<String> content, ArrayList<Construct> constructs) :
                _constructs = constructs;
        }
        ((inst = ParseSpec() { _constructs.add(inst); }) |
-       ((code = C_CPP_CODE()) { _content.addAll(code); })
+       ((code = C_CPP_CODE(headers)) { _content.addAll(code); })
        )* <EOF>
 }
 
@@ -730,21 +747,26 @@ GlobalConstruct Global_construct() :
 
 SequentialDefineSubConstruct Global_define() :
 {
-       String declareVar, initVar, defineFunc;
-       ArrayList<String> code;
+       ArrayList<String> initVar, defineFunc, code;
+       ArrayList<ArrayList<String>> defineFuncs;
+       ArrayList<VariableDeclaration> declareVars;
+       VariableDeclaration declareVar;
+
 }
 {
        {
-               declareVar = "";
-               initVar = "";
-               defineFunc = "";
+               declareVars = null;
+               initVar = null;
+               defineFuncs = new ArrayList<ArrayList<String>>();
        }
        <GLOBAL_DEFINE>
-               (<DECLARE_VAR> (code = C_CPP_CODE() { declareVar = stringArray2String(code); } ))?
-       (<INIT_VAR> (code = C_CPP_CODE() { initVar = stringArray2String(code); } ))?
-       (<DEFINE_FUNC> (code = C_CPP_CODE() { defineFunc = stringArray2String(code); }))?
+               (<DECLARE_VAR> ((declareVar = TypeParam() ";" {
+                       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(declareVar, initVar, defineFunc);
+               SequentialDefineSubConstruct res = new
+                       SequentialDefineSubConstruct(declareVars, initVar, defineFuncs);
                //System.out.println(res);
                return res;
        }
@@ -803,10 +825,10 @@ void Happens_before(GlobalConstruct inst) :
 InterfaceConstruct Interface() :
 {
        InterfaceConstruct res;
-       String interfaceName, condition, idCode, check, postAction,
+       String interfaceName, condition, idCode, check,
                postCheck, commitPoint, hbLabel, hbCondition;
-       ActionSubConstruct action;
        ArrayList<String> commitPointSet;
+       ArrayList<String> action, postAction;
        HashMap<String, String> hbConditions;
        ArrayList<String> content;
 }
@@ -817,10 +839,12 @@ InterfaceConstruct Interface() :
                condition = "";
                idCode = "";
                check = "";
-               postAction = "";
                postCheck = "";
                commitPointSet = new ArrayList<String>();
                hbConditions = new HashMap<String, String>();
+
+               action = null;
+               postAction = null;
        }
                <BEGIN>
                        <INTERFACE> (interfaceName = <IDENTIFIER>.image)
@@ -839,11 +863,11 @@ InterfaceConstruct Interface() :
                                        }
                                )*
 
-                       (<CONDITION> (content = C_CPP_CODE() { condition = stringArray2String(content); }))?
+                       (<CONDITION> (content = C_CPP_CODE(null) { condition = stringArray2String(content); }))?
                        (
                                <HB_CONDITION>
                                (hbLabel = <IDENTIFIER>.image)
-                               (content = C_CPP_CODE() { hbCondition = stringArray2String(content); })
+                               (content = C_CPP_CODE(null) { hbCondition = stringArray2String(content); })
                                {
                                        if (hbConditions.containsKey(hbLabel)) {
                                                throw new ParseException(interfaceName + " has" +
@@ -852,11 +876,11 @@ InterfaceConstruct Interface() :
                                        hbConditions.put(hbLabel, hbCondition);
                                }
                        )*
-                       (<ID> (content = C_CPP_CODE() { idCode = stringArray2String(content); }))?
-                       (<CHECK> (content = C_CPP_CODE() { check = stringArray2String(content); }))?
-                       (action = Action())?
-                       (<POST_ACTION> (content = C_CPP_CODE() { postAction = stringArray2String(content); }))?
-                       (<POST_CHECK> (content = C_CPP_CODE() { postCheck = stringArray2String(content); }))?
+                       (<ID> (content = C_CPP_CODE(null) { idCode = stringArray2String(content); }))?
+                       (<CHECK> (content = C_CPP_CODE(null) { check = stringArray2String(content); }))?
+                       (<ACTION> action = C_CPP_CODE(null))?
+                       (<POST_ACTION> (postAction = C_CPP_CODE(null) ))?
+                       (<POST_CHECK> (content = C_CPP_CODE(null) { postCheck = stringArray2String(content); }))?
                <END>
        {
                res = new InterfaceConstruct(_file, _content.size(), interfaceName, commitPointSet, condition,
@@ -865,37 +889,6 @@ InterfaceConstruct Interface() :
        }
 }
 
-ActionSubConstruct Action() :
-{
-       String type, name, expr, defineVarStr, code;
-       ArrayList<DefineVar> defineVars;
-       ArrayList<String> content;
-}
-{
-       {
-               defineVars = new ArrayList<DefineVar>();
-               code = "";
-       }
-       <ACTION>
-       (
-               (
-               (<DEFINEVAR> (content = C_CPP_CODE() { defineVarStr = stringArray2String(content); }) 
-               {
-                       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> (content = C_CPP_CODE() { code = stringArray2String(content); }))? ) 
-       )
-       
-       {
-               ActionSubConstruct res = new ActionSubConstruct(defineVars, code);
-               return res;
-       }
-}
 
 PotentialCPDefineConstruct Potential_commit_point_define() :
 {
@@ -907,7 +900,7 @@ PotentialCPDefineConstruct Potential_commit_point_define() :
 
        { res = null; }
                <BEGIN>
-                       <POTENTIAL_COMMIT_POINT_DEFINE> (content = C_CPP_CODE() { condition = stringArray2String(content); })
+                       <POTENTIAL_COMMIT_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
                        <LABEL> (label = <IDENTIFIER>.image)
                <END>
        {
@@ -927,7 +920,7 @@ CPDefineConstruct Commit_point_define() :
 
        { res = null; }
                <BEGIN>
-                       <COMMIT_POINT_DEFINE> (content = C_CPP_CODE() { condition = stringArray2String(content); })
+                       <COMMIT_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
                        <POTENTIAL_COMMIT_POINT_LABEL> (potentialCPLabel = <IDENTIFIER>.image)
                        <LABEL> (label = <IDENTIFIER>.image)
                <END>
@@ -948,7 +941,7 @@ CPDefineCheckConstruct Commit_point_define_check() :
 
        { res = null; }
                <BEGIN> 
-                       <COMMIT_POINT_DEFINE_CHECK> (content = C_CPP_CODE() { condition = stringArray2String(content); })
+                       <COMMIT_POINT_DEFINE_CHECK> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
                        <LABEL> (label = <IDENTIFIER>.image)
                <END>
        {