hashtable still buggy
[cdsspec-compiler.git] / grammer / spec_compiler.jj
index f914094a1f249af4838c3aeea860edf07897f742..37749aab497b2b2438959e1c2b9b918ef508234e 100644 (file)
@@ -114,6 +114,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;
@@ -141,10 +142,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 +157,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 +182,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());
@@ -206,12 +214,61 @@ import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
                        return sb.toString();
                }
 
+               /**
+               boolean spaceSeparator(Token t) {
+                       switch (t.image) {
+                               case "[":
+                               case "]":
+                               case "=":
+                               case "(":
+                               case ")":
+                               case ",":
+                               case ".":
+                               case "*":
+                               case "~":
+                               case "!":
+                               case "&":
+                               case "|":
+                               case "%":
+                               case "+":
+                               case "-":
+                               case "/":
+                               case "<":
+                               case ">":
+                               case "<=":
+                               case ">=":
+                               case "==":
+                               case "!=":
+                               case "&&":
+                               case "||":
+                               case "^":
+                               case "?":
+                               case ":":
+                               case "::":
+                               case "<<":
+                               case ">>":
+                               case ">>>":
+                               case "+=":
+                               case "-=":
+                               case "*=":
+                               case "/=":
+                               case "%=":
+                               case "^=":
+                               case "&=":
+                               case ";":
+                                       return false;
+                               default:
+                                       return true;
+                       }
+               }
+               */
+
        }
 PARSER_END(SpecParser)
 
 
 
-<*> SKIP :
+<IN_POTENTIAL_SPEC, IN_SPEC> SKIP :
 {
        " "
 |
@@ -240,6 +297,10 @@ SKIP : {
        "/*": IN_COMMENT
 }
 
+<DEFAULT> TOKEN: {
+       <ANY: ~[]>
+}
+
 <*> SKIP : {
        // "//" comment for the specification
        <"//" (~["\n", "\r"])* (["\n", "\r"])>
@@ -317,7 +378,7 @@ SKIP : {
 }
 
 
-<IN_SPEC, DEFAULT> TOKEN :
+<IN_SPEC> TOKEN :
 {
 /*   Specification & C/C++ shared tokens   */
 // Reserved keywords
@@ -334,6 +395,8 @@ SKIP : {
        <INLINE: "inline">
 |
        <STATIC: "static">
+|
+       <FOR: "for">
 |
        <#DIGIT: ["0"-"9"]>
 |
@@ -363,6 +426,8 @@ SKIP : {
 |
 /*   C/C++ only token*/
        <DOT: ".">
+|
+       <DOLLAR: "$">
 |
        <STAR: "*">
 |
@@ -509,7 +574,7 @@ String Type() :
 }
 {
        { type = ""; }
-       ("const"
+       (<CONST>
        { type = "const"; }
        )?
        (((str = <STRUCT>.image | str = <CLASS>.image | str = <UNSIGNED>.image) { type = type + " " + str; })? 
@@ -521,7 +586,7 @@ String Type() :
                        type = name.fullName;
        })
        )
-       ((str = "const".image {
+       ((str = <CONST>.image {
                if (!type.equals(""))
                        type = type + " " + str;
                else
@@ -545,24 +610,6 @@ String Type() :
        }
 }
 
-void Test() :
-{
-       String str;     
-       FunctionHeader func;
-}
-{
-       /*
-       str = Type()
-       {
-               System.out.println(str);
-       }
-       */
-       func = FuncDecl() 
-       {
-               System.out.println(func);
-       }
-       
-}
 
 String ParameterizedName() :
 {
@@ -571,8 +618,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 + ">"; }
        )?
        {
@@ -625,7 +672,7 @@ ArrayList<VariableDeclaration> TemplateParamList() :
                params = new ArrayList<VariableDeclaration>();
        }
        <TEMPLATE>
-       "<"
+       <OPEN_BRACKET>
        (type = <IDENTIFIER>.image 
        name = <IDENTIFIER>.image
        {
@@ -633,13 +680,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;
@@ -655,10 +702,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;
        }
@@ -675,12 +722,16 @@ VariableDeclaration TypeParam() :
        }
 }
 
+
+
 ArrayList<String> C_CPP_CODE(ArrayList<String> headers) :
 {
        String text;
        Token t;
        boolean newLine = false;
+       boolean newSpace = true;
        boolean inTemplate = false;
+       boolean inForLoop = false;
        ArrayList<String> content;
        String header;
 }
@@ -696,6 +747,7 @@ ArrayList<String> C_CPP_CODE(ArrayList<String> headers) :
        t = <CONST> | t = <STRUCT> | t = <CLASS> | t = <UNSIGNED> |
        (t = <TEMPLATE> { inTemplate = true;  })|
        t = <STATIC> | t = <INLINE> |
+       (t = <FOR> { inForLoop = true; })|
        (t = <INCLUDE>
        {
                header = t.image;
@@ -706,11 +758,11 @@ ArrayList<String> C_CPP_CODE(ArrayList<String> headers) :
        })
        | t = <IDENTIFIER> | t = <POUND> |
        (t = <OPEN_BRACE>  { newLine = true; } ) |
-       (t = <CLOSE_BRACE>  { newLine = true; } ) | 
+       (t = <CLOSE_BRACE>  { newLine = true; inForLoop = false;} ) | 
        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; }) |
@@ -730,7 +782,7 @@ ArrayList<String> C_CPP_CODE(ArrayList<String> headers) :
        t = <OR_EQUALS> |
        t = <AND_EQUALS> |
 
-       (t = <SEMI_COLON> { newLine = true; } )
+       (t = <SEMI_COLON> { if (!inForLoop) newLine = true; } )
        | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
        t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL> |
        (t = <DEFINE> { newLine = true; } )
@@ -738,8 +790,17 @@ ArrayList<String> C_CPP_CODE(ArrayList<String> headers) :
        {
                if (text.equals("")) {
                        text = t.image;
+                       newSpace = true;
                } else {
                        text = text + " " + t.image;
+                       /*
+                       if (newSpace && spaceSeparator(t)) {
+                               text = text + " " + t.image;
+                       } else {
+                               text = text + t.image;
+                               if (spaceSeparator(t))
+                                       newSpace = true;
+                       }*/
                }
                if (newLine) {
                        content.add(text);
@@ -762,17 +823,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() :
@@ -853,7 +952,7 @@ SequentialDefineSubConstruct Global_define() :
        <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; } ))?
        (<DEFINE_FUNC> (defineFunc = C_CPP_CODE(null) { defineFuncs.add(defineFunc); }))*