extracting spec & compiling
authorPeizhao Ou <peizhaoo@uci.edu>
Thu, 10 Oct 2013 01:37:46 +0000 (18:37 -0700)
committerPeizhao Ou <peizhaoo@uci.edu>
Thu, 10 Oct 2013 01:37:46 +0000 (18:37 -0700)
grammer/spec-compiler.jj
src/edu/uci/eecs/specCompiler/specExtraction/SpecConstruct.java [new file with mode: 0644]
src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java [new file with mode: 0644]

index 965e4f5fabc1951becf1361d8b500824f715fb24..6ce449540a67b830787c8a5d8ca810b2d59d841f 100644 (file)
@@ -84,8 +84,11 @@ SKIP :
 |
        "\t"
 |
-       // Comment for the specification
+       // "#" comment for the specification
        <"#" (~["\n", "\r"])* (["\n", "\r"])>
+|
+       // "//" comment for the specification
+       <"//" (~["\n", "\r"])* (["\n", "\r"])>
 }
 
 TOKEN :
@@ -110,14 +113,11 @@ TOKEN :
 |
        <CONDITION: "@Condition:">
 |
-       <NUM_OR_EMPTY: ["1"-"9"](["0"-"9"])+ | "">
-|
-       <ONE_HB_CONDITION: "@HB_condition:" <NUM_OR_EMPTY> (~["@", "#", "$"])+> 
-|
-       <ALL_HB_CONDITIONS: (<ONE_HB_CONDITION>)*>
+       <HB_CONDITIONS: "@HB_condition:">
 |
        <ID: "@ID:">
-| <CHECK: "@Check:">
+|
+       <CHECK: "@Check:">
 |
        <ACTION: "@Action:">
 |
@@ -130,27 +130,34 @@ TOKEN :
        <LABEL: "@Label:">
 |
        <COMMIT_POINT_DEFINE_CHECK: "@Commit_point_define_check:">
-
 |
        <COMMIT_POINT_DEFINE: "@Commit_point_define:">
 |
        <POTENTIAL_COMMIT_POINT_LABEL: "@Potential_commit_point_label:">
 |
-       <IDENTIFIER: (["a"-"z", "A"-"Z", "_"]) (["0"-"9", "a"-"z", "A"-"Z", "_"])*>
+       <#DIGIT: ["0"-"9"]>
+|
+       <#NONZERO_DIGIT: ["1"-"9"]>
+|
+       <#LETTER: ["a"-"z", "A"-"Z"]>
+|
+       <#NUM: <NONZERO_DIGIT> <DIGIT>>
+|
+       <IDENTIFIER: <LETTER> (<LETTER> | <DIGIT> | "_")>       
 }
 
 void Start() :
 {}
 {
-       <EOF>
+       Global_construct() <EOF>
 }
 
 void Global_construct() :
 {}
 {
-       <HEAD> 
+       <HEAD>
                <BEGIN> 
-                       <HAPPENS_BEFORE> <IDENTIFIER> "(" <IDENTIFIER> ")"  "->" <IDENTIFIER>
+                       Global_define() (Interface_cluster())? (Happens_before())?
                <END>
        <TAIL>
 }
@@ -158,7 +165,7 @@ void Global_construct() :
 void C_CPP_CODE() :
 {}
 {
-       <(~["@", "#", "$"])+>
+       <(~["@"])+>
 }
 
 void Global_define() :
@@ -188,11 +195,7 @@ void Interface_clusters() :
 void Happens_before() :
 {}
 {
-       <HEAD> 
-               <BEGIN> 
-                       <HAPPENS_BEFORE> <IDENTIFIER> "(" <IDENTIFIER> ")"  "->" <IDENTIFIER>
-               <END>
-       <TAIL>
+       <HAPPENS_BEFORE> (Conditional_interface()  "->" Conditional_interface())+
 }
 
 void Interface() :
diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/SpecConstruct.java b/src/edu/uci/eecs/specCompiler/specExtraction/SpecConstruct.java
new file mode 100644 (file)
index 0000000..84249e8
--- /dev/null
@@ -0,0 +1,44 @@
+package edu.uci.eecs.specCompiler.specExtraction;
+
+import java.io.File;
+
+/**
+ * <p>
+ * This class represents the plain context for each specification construct.
+ * Besides, it also stores some useful information such as interfaceDeclBody,
+ * which is used in @Interface construct to wrap and rename interface calls.
+ * </p>
+ * @author peizhaoo
+ *
+ */
+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 SpecConstruct(ConstructType type, String plainText, File file,
+                       int beginLineNum, int endLineNum) {
+               this.type = type;
+               this.plainText = plainText;
+               this.file = file;
+               this.beginLineNum = beginLineNum;
+               this.endLineNum = endLineNum;
+               this.interfaceDeclBody = ""; 
+       }
+       
+       public SpecConstruct(ConstructType type, String plainText, File file,
+                       int beginLineNum, int endLineNum, String interfaceDeclBody) {
+               this.type = type;
+               this.plainText = plainText;
+               this.file = file;
+               this.beginLineNum = beginLineNum;
+               this.endLineNum = endLineNum;
+               this.interfaceDeclBody = interfaceDeclBody; 
+       }
+}
diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java b/src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java
new file mode 100644 (file)
index 0000000..ddd25bc
--- /dev/null
@@ -0,0 +1,103 @@
+package edu.uci.eecs.specCompiler.specExtraction;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.LineNumberReader;
+import java.util.ArrayList;
+
+/**
+ * <p>
+ * This class represents the specification extractor of the specification. The
+ * main function of this class is to read C/C++11 source files and extract the
+ * 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 StringBuilder _extractedSpecText;
+       
+       private enum State {
+               Neutral, HeadParsed, BeginParsed, EndParsed
+       }
+       
+       private State _curState;
+       
+       private StringBuilder _potentialConstruct;
+       private int _startLine, _endLine;
+       
+       SpecExtractor() {
+               _constructs = new ArrayList<SpecConstruct>();
+               _extractedSpecText = new StringBuilder();
+               _curState = State.Neutral;
+               _potentialConstruct = new StringBuilder();
+       }
+       
+       /**
+        * <p>
+        * Given a list of files, it scans each file and add found SpecConstrcut
+        * to the _constructs list.
+        * </p>
+        * @param files
+        */
+       private void extract(File[] files) {
+               for (int i = 0; i < files.length; i++)
+                       extract(files[i]);
+       }
+       
+       private void extract(File file) {
+               try {
+                       LineNumberReader reader = new LineNumberReader(new FileReader(file));
+                       String prevLine = "", curLine;
+                       ArrayList<String> text;
+                       while ((curLine = reader.readLine()) != null) {
+                               
+                       }
+               } catch (FileNotFoundException e) {
+                       e.printStackTrace();
+               } catch (IOException e) {
+                       e.printStackTrace();
+               }
+       }
+       
+       private void parseHead(String prevLine, String line) {
+               assert (_curState == State.Neutral);
+               
+               // "\" is the C/C++ line break. If the previous line ends with "\",
+               // it may be part of a string literal.
+               if (prevLine.endsWith("\\"))
+                       return;
+               String newLine = trimBeginningSpace(line);
+//             if (newLine.startsWith("/**") && 
+//                             (newLine.length() == 3 || 
+       }
+       
+       private void parseBegin(String line) {
+               
+       }
+       
+       private void parseEnd(String line) {
+               
+       }
+       
+       private void parseTail(String line) {
+               
+       }
+       
+       private String trimBeginningSpace(String line) {
+               int i;
+               for (i = 0; i < line.length(); i++) {
+                       char ch = line.charAt(i);
+                       if (ch == ' ' || ch == '\t')
+                               i++;
+                       else
+                               break;
+               }
+               return line.substring(i);
+       }
+}