add more & need C/C++ code recognition
authorPeizhao Ou <peizhaoo@uci.edu>
Thu, 10 Oct 2013 18:29:26 +0000 (11:29 -0700)
committerPeizhao Ou <peizhaoo@uci.edu>
Thu, 10 Oct 2013 18:29:26 +0000 (11:29 -0700)
grammer/spec-compiler.jj
src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java
src/edu/uci/eecs/specCompiler/specExtraction/SpecNotMatchException.java [new file with mode: 0644]

index f7a4248d377c41f64cb7ef3c7b69009e29432d18..110dcb9c4979d84b5c8f4322ffef46e91a171a6f 100644 (file)
@@ -151,15 +151,25 @@ TOKEN :
 |
        <#LETTER: ["a"-"z", "A"-"Z"]>
 |
-       <IDENTIFIER: <LETTER> (<LETTER> | <DIGIT> | "_")>       
+       <IDENTIFIER: <LETTER> (<LETTER> | <DIGIT> | "_")*>
+|
+       <EQUALS: "=">
+|
+       <LEFT_PARAN: "{">
+|
+       <RIGHT_PARAN: "}">
+|
+       <HB_SYMBOL: "->">
+|
+       <COMMA: ",">
 }
 
 void Start() :
 {}
 {
        //Global_construct() <EOF>
-       <EOF>
-       //<IDENTIFIER> <EOF>
+       <IDENTIFIER> <EOF>
+       //<IDENTIFIER> C_CPP_CODE() <EOF>
 }
 
 void Global_construct() :
@@ -167,33 +177,36 @@ void Global_construct() :
 {
        <HEAD>
                <BEGIN> 
-                       //Global_define() (Interface_cluster())? (Happens_before())?
+                       Global_define() (Interface_cluster())? (Happens_before())?
                <END>
        <TAIL>
 }
 
+/*
 void C_CPP_CODE() :
-{String code;}
+{}
 {
        <(~["@"])+>
 }
-
+*/
 void Global_define() :
 {}
 {
-       <GLOBAL_DEFINE> C_CPP_CODE()
+       <GLOBAL_DEFINE> //C_CPP_CODE()
 }
 
 void Conditional_interface() :
 {}
 {
-       <IDENTIFIER> (<"(" <IDENTIFIER> ")"> | "")
+       <IDENTIFIER> (<"(" <IDENTIFIER> ")">)*
 }
 
 void Interface_cluster() :
 {}
 {
-       <IDENTIFIER> "=" "{" Conditional_interface() (",," Conditional_interface())* "}"
+       <IDENTIFIER> <EQUALS> <LEFT_PARAN>
+               Conditional_interface() (<COMMA> Conditional_interface())*
+       <RIGHT_PARAN>
 }
 
 void Interface_clusters() :
@@ -205,7 +218,7 @@ void Interface_clusters() :
 void Happens_before() :
 {}
 {
-       <HAPPENS_BEFORE> (Conditional_interface()  "->" Conditional_interface())+
+       <HAPPENS_BEFORE> (Conditional_interface() <HB_SYMBOL> Conditional_interface())+
 }
 
 void Interface() :
index 2d28fceacb6a5209c54fc035f31598c531272302..0c06badaa2cf54024795c82dccb360f2600dbe1e 100644 (file)
@@ -22,12 +22,11 @@ import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils.Text;
  */
 public class SpecExtractor {
        private ArrayList<SpecConstruct> _constructs;
-       private StringBuilder _potentialConstruct;
-       private int _beginLine, _endLine;
+       private int _beginLineNum, _endLineNum;
+       private String _beginLine;
        
        SpecExtractor() {
                _constructs = new ArrayList<SpecConstruct>();
-               _potentialConstruct = new StringBuilder();
        }
        
        /**
@@ -36,35 +35,37 @@ public class SpecExtractor {
         * to the _constructs list.
         * </p>
         * @param files
+        * @throws SpecNotMatchException 
         */
-       public void extract(File[] files) {
+       public void extract(File[] files) throws SpecNotMatchException {
                for (int i = 0; i < files.length; i++)
                        extract(files[i]);
        }
        
-       public void extract(File file) {
+       public void extract(File file) throws SpecNotMatchException {
                try {
                        LineNumberReader reader = new LineNumberReader(new FileReader(file));
                        String prevLine = "", curLine, trimedLine;
                        StringBuilder specText = new StringBuilder();
-                       boolean _foundHead = false;
+                       boolean foundHead = false;
                        int specIndex = 0;
                        while ((curLine = reader.readLine()) != null) {
                                if (prevLine.endsWith("\\"))
                                        continue;
                                trimedLine = trimSpace(curLine);
-                               if (!_foundHead) {
+                               if (!foundHead) {
                                        if (trimedLine.startsWith("/**")) {
-                                               _beginLine = reader.getLineNumber();
-                                               _foundHead = true;
+                                               _beginLineNum = reader.getLineNumber();
+                                               _beginLine = curLine;
+                                               foundHead = true;
                                                specText.append("\n");
                                                specText.append(curLine);
                                                if (trimedLine.endsWith("*/")) {
-                                                       _endLine = reader.getLineNumber();
-                                                       _foundHead = false;
+                                                       _endLineNum = reader.getLineNumber();
+                                                       foundHead = false;
                                                        
                                                        System.out.println("Spec<" + specIndex + "> Begin: "
-                                                                       + _beginLine + "  End: " + _endLine);
+                                                                       + _beginLine + "  End: " + _endLineNum);
                                                        System.out.println(specText);
                                                        specIndex++;
                                                }
@@ -73,20 +74,24 @@ public class SpecExtractor {
                                        specText.append("\n");
                                        specText.append(curLine);
                                        if (trimedLine.endsWith("*/")) {
-                                               _endLine = reader.getLineNumber();
-                                               _foundHead = false;
+                                               _endLineNum = reader.getLineNumber();
+                                               foundHead = false;
                                                
                                                System.out.println("Spec<" + specIndex + "> Begin: "
-                                                               + _beginLine + "  End: " + _endLine);
+                                                               + _beginLine + "  End: " + _endLineNum);
                                                System.out.println(specText);
                                                specIndex++;
                                                
                                                specText = new StringBuilder();
-                                       } else {
-                                               
                                        }
                                }
                        }
+                       // 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.";
+                               throw new SpecNotMatchException(msg);
+                       }
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (IOException e) {
@@ -116,6 +121,10 @@ public class SpecExtractor {
        public static void main(String[] argvs) {
                SpecExtractor extractor = new SpecExtractor();
                File file = new File("./grammer/spec1.txt");
-               extractor.extract(file);
+               try {
+                       extractor.extract(file);
+               } catch (SpecNotMatchException e) {
+                       e.printStackTrace();
+               }
        }
 }
diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/SpecNotMatchException.java b/src/edu/uci/eecs/specCompiler/specExtraction/SpecNotMatchException.java
new file mode 100644 (file)
index 0000000..cf3ce52
--- /dev/null
@@ -0,0 +1,9 @@
+package edu.uci.eecs.specCompiler.specExtraction;
+
+import java.io.File;
+
+public class SpecNotMatchException extends Exception {
+       public SpecNotMatchException(String msg) {
+               super(msg);
+       }
+}