parser checked
authorPeizhao Ou <peizhaoo@uci.edu>
Tue, 15 Oct 2013 02:01:08 +0000 (19:01 -0700)
committerPeizhao Ou <peizhaoo@uci.edu>
Tue, 15 Oct 2013 02:01:08 +0000 (19:01 -0700)
grammer/spec-compiler.jj
src/edu/uci/eecs/specCompiler/specExtraction/SpecConstruct.java
src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java

index 85aabff176d0ab19da6572b2fac30649eb15f218..2238268a43ac644a62e490703ea226c6d3060f2f 100644 (file)
@@ -71,6 +71,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;
@@ -97,6 +99,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)
 
@@ -198,6 +209,8 @@ TOKEN :
        <STAR: "*">
 |
        <NEGATE: "~">
+|
+       <EXCLAMATION: "!">
 |
        <AND: "&">
 |
@@ -313,7 +326,7 @@ Construct Parse() :
        )
        <EOF>
        {
-               System.out.println(res);
+               //System.out.println(res);
                return res;
        }
 }
@@ -354,7 +367,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> |
@@ -512,8 +525,11 @@ ActionSubConstruct Action() :
 {
        {
                defineVars = new ArrayList<DefineVar>();
+               code = "";
        }
        <ACTION>
+       (
+               (
                (<DEFINEVAR> (defineVarStr = C_CPP_CODE()) 
                {
                        int eqIdx = defineVarStr.indexOf('=');
@@ -523,13 +539,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() :
index 84249e818b9a1f3ee2f3e10726f610426ef30885..95650ccdf0a9420f2cea428831683f638e96d043 100644 (file)
@@ -12,33 +12,45 @@ import java.io.File;
  *
  */
 public class SpecConstruct {
-       public enum ConstructType {
-               GLOBAL, INTERFACE, POTENTIAL_CP, CP_DEFINE, CP_DEFINE_CHECK
-       };
-       public ConstructType type;
        public final String plainText;
        public final File file;
        public final int beginLineNum;
        public final int endLineNum;
        public final String interfaceDeclBody;
+       public final Construct construct;
        
-       public SpecConstruct(ConstructType type, String plainText, File file,
-                       int beginLineNum, int endLineNum) {
-               this.type = type;
+       public SpecConstruct(String plainText, File file,
+                       int beginLineNum, int endLineNum, Construct construct) {
                this.plainText = plainText;
                this.file = file;
                this.beginLineNum = beginLineNum;
                this.endLineNum = endLineNum;
+               this.construct = construct;
                this.interfaceDeclBody = ""; 
        }
        
-       public SpecConstruct(ConstructType type, String plainText, File file,
-                       int beginLineNum, int endLineNum, String interfaceDeclBody) {
-               this.type = type;
+       public SpecConstruct(String plainText, File file,
+                       int beginLineNum, int endLineNum, Construct construct,
+                       String interfaceDeclBody) {
                this.plainText = plainText;
                this.file = file;
                this.beginLineNum = beginLineNum;
                this.endLineNum = endLineNum;
+               this.construct = construct;
                this.interfaceDeclBody = interfaceDeclBody; 
        }
+       
+       public String toString() {
+               StringBuilder sb = new StringBuilder();
+               sb.append("File: " + file.getAbsolutePath() + "\n");
+               sb.append("Begin: "
+                               + beginLineNum + "  End: " + endLineNum + "\n");
+               sb.append(construct);
+               if (construct instanceof InterfaceConstruct) {
+                       sb.append("Function declaration: " + interfaceDeclBody);
+               }
+               boolean a = !false, b = 3 > 0 ? a : !a;
+               return sb.toString();
+               
+       }
 }
index e1fae7ef35081ecd04c4d1744303b315b32bc586..ac6de02b405f06a4db25b1a184009eb6d150c0a9 100644 (file)
@@ -8,6 +8,10 @@ import java.io.IOException;
 import java.io.LineNumberReader;
 import java.util.ArrayList;
 
+import edu.uci.eecs.specCompiler.grammerParser.ParseException;
+import edu.uci.eecs.specCompiler.grammerParser.SpecParser;
+import edu.uci.eecs.specCompiler.grammerParser.TokenMgrError;
+
 /**
  * <p>
  * This class represents the specification extractor of the specification. The
@@ -15,38 +19,40 @@ import java.util.ArrayList;
  * corresponding specification out, and remember its location, including the
  * file name and the line number, to help the code generation process.
  * </p>
+ * 
  * @author peizhaoo
- *
+ * 
  */
 public class SpecExtractor {
        private ArrayList<SpecConstruct> _constructs;
        private int _beginLineNum, _endLineNum;
        private String _beginLine;
-       
+
        SpecExtractor() {
                _constructs = new ArrayList<SpecConstruct>();
        }
-       
+
        /**
         * <p>
-        * Given a list of files, it scans each file and add found SpecConstrcut
-        * to the _constructs list.
+        * Given a list of files, it scans each file and add found SpecConstrcut to
+        * the _constructs list.
         * </p>
+        * 
         * @param files
-        * @throws SpecNotMatchException 
+        * @throws SpecNotMatchException
         */
        public void extract(File[] files) throws SpecNotMatchException {
                for (int i = 0; i < files.length; i++)
                        extract(files[i]);
        }
-       
+
        public void extract(File file) throws SpecNotMatchException {
+               StringBuilder specText = new StringBuilder();
                try {
                        LineNumberReader reader = new LineNumberReader(new FileReader(file));
-                       String prevLine = "", curLine, trimedLine;
-                       StringBuilder specText = new StringBuilder();
+                       String prevLine = "", curLine, trimedLine, funcDecl;
+                       SpecConstruct specConstruct;
                        boolean foundHead = false;
-                       int specIndex = 0;
                        while ((curLine = reader.readLine()) != null) {
                                if (prevLine.endsWith("\\"))
                                        continue;
@@ -61,12 +67,22 @@ public class SpecExtractor {
                                                if (trimedLine.endsWith("*/")) {
                                                        _endLineNum = reader.getLineNumber();
                                                        foundHead = false;
-                                                       //Constrcut inst = SpecParser
-                                                       
-                                                       System.out.println("Spec<" + specIndex + "> Begin: "
-                                                                       + _beginLine + "  End: " + _endLineNum);
-                                                       System.out.println(specText);
-                                                       specIndex++;
+                                                       if (isComment(specText.toString()))
+                                                               continue;
+                                                       Construct inst = SpecParser.parseSpec(specText
+                                                                       .toString());
+                                                       if (inst instanceof InterfaceConstruct) {
+                                                               funcDecl = readFunctionDecl(reader);
+                                                               specConstruct = new SpecConstruct(
+                                                                               specText.toString(), file,
+                                                                               _beginLineNum, _endLineNum, inst, funcDecl);
+                                                       } else {
+                                                               specConstruct = new SpecConstruct(
+                                                                               specText.toString(), file,
+                                                                               _beginLineNum, _endLineNum, inst);
+                                                       }
+                                                       specText = new StringBuilder();
+                                                       System.out.println(specConstruct);
                                                }
                                        }
                                } else {
@@ -75,30 +91,62 @@ public class SpecExtractor {
                                        if (trimedLine.endsWith("*/")) {
                                                _endLineNum = reader.getLineNumber();
                                                foundHead = false;
-                                               
-                                               System.out.println("Spec<" + specIndex + "> Begin: "
-                                                               + _beginLine + "  End: " + _endLineNum);
-                                               System.out.println(specText);
-                                               specIndex++;
-                                               
+                                               if (isComment(specText.toString())) {
+                                                       specText = new StringBuilder();
+                                                       continue;
+                                               }
+                                               Construct inst = SpecParser.parseSpec(specText
+                                                               .toString());
+                                               if (inst instanceof InterfaceConstruct) {
+                                                       funcDecl = readFunctionDecl(reader);
+                                                       specConstruct = new SpecConstruct(
+                                                                       specText.toString(), file,
+                                                                       _beginLineNum, _endLineNum, inst, funcDecl);
+                                               } else {
+                                                       specConstruct = new SpecConstruct(
+                                                                       specText.toString(), file,
+                                                                       _beginLineNum, _endLineNum, inst);
+                                               }
+                                               System.out.println(specConstruct);
                                                specText = new StringBuilder();
                                        }
                                }
-                       } 
+                       }
                        // At the end we can only find the head "/**" but no tail found
                        if (foundHead) {
-                               String msg = "In file \"" + file.getAbsolutePath() + "\", line: "
-                                               + _beginLineNum + "\n" + _beginLine + "\n" + "Can't find matching spec.";
+                               String msg = "In file \"" + file.getAbsolutePath()
+                                               + "\", line: " + _beginLineNum + "\n" + _beginLine
+                                               + "\n" + "Can't find matching spec.";
                                throw new SpecNotMatchException(msg);
                        }
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
+               } catch (ParseException e) {
+                       printSpecInfo(file, specText.toString());
+                       e.printStackTrace();
+               } catch (TokenMgrError e) {
+                       printSpecInfo(file, specText.toString());
+                       e.printStackTrace();
                }
        }
        
-       private String readInterfaceDecl(LineNumberReader reader) throws IOException {
+       private void printSpecInfo(File file, String text) {
+               System.out.println("Error in spec!");
+               System.out.println("File: " + file.getAbsolutePath());
+               System.out.println("Begin: "
+                               + _beginLineNum + "  End: " + _endLineNum);
+               System.out.println(text);
+       }
+       
+       private boolean isComment(String specText) {
+               if (specText.indexOf("@Begin") != -1)
+                       return false;
+               return true;
+       }
+
+       private String readFunctionDecl(LineNumberReader reader) throws IOException {
                String res = "", curLine;
                while ((curLine = reader.readLine()) != null) {
                        int braceIdx = curLine.indexOf(')');
@@ -106,11 +154,13 @@ public class SpecExtractor {
                                res = res + " " + curLine;
                        } else {
                                res = res + curLine.substring(0, braceIdx + 1);
+                               res = trimSpace(res);
+                               break;
                        }
                }
                return res;
        }
-       
+
        private String trimSpace(String line) {
                int i, j;
                char ch;
@@ -129,7 +179,7 @@ public class SpecExtractor {
                else
                        return line.substring(i, j + 1);
        }
-       
+
        public static void main(String[] argvs) {
                SpecExtractor extractor = new SpecExtractor();
                File file = new File("./grammer/spec1.txt");