need to fixed SpecExtractor.java
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / specExtraction / SpecExtractor.java
index ddd25bc1273a3d1dc558463b257a77e2bb14de84..ba043430f444364827f36814ce6da03c550c695d 100644 (file)
@@ -8,6 +8,8 @@ import java.io.IOException;
 import java.io.LineNumberReader;
 import java.util.ArrayList;
 
+import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils.Text;
+
 /**
  * <p>
  * This class represents the specification extractor of the specification. The
@@ -20,21 +22,11 @@ import java.util.ArrayList;
  */
 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;
+       private int _beginLine, _endLine;
        
        SpecExtractor() {
                _constructs = new ArrayList<SpecConstruct>();
-               _extractedSpecText = new StringBuilder();
-               _curState = State.Neutral;
                _potentialConstruct = new StringBuilder();
        }
        
@@ -45,18 +37,51 @@ public class SpecExtractor {
         * </p>
         * @param files
         */
-       private void extract(File[] files) {
+       public void extract(File[] files) {
                for (int i = 0; i < files.length; i++)
                        extract(files[i]);
        }
        
-       private void extract(File file) {
+       public void extract(File file) {
                try {
                        LineNumberReader reader = new LineNumberReader(new FileReader(file));
-                       String prevLine = "", curLine;
-                       ArrayList<String> text;
+                       String prevLine = "", curLine, trimedLine;
+                       StringBuilder specText = new StringBuilder();
+                       boolean _foundHead = false;
+                       int specIndex = 0;
                        while ((curLine = reader.readLine()) != null) {
-                               
+                               if (prevLine.endsWith("\\"))
+                                       continue;
+                               trimedLine = trimSpace(curLine);
+                               if (!_foundHead) {
+                                       if (trimedLine.startsWith("/**")) {
+                                               _beginLine = reader.getLineNumber();
+                                               _foundHead = true;
+                                               if (trimedLine.endsWith("*/")) {
+                                                       _endLine = reader.getLineNumber();
+                                                       _foundHead = false;
+                                                       
+                                                       System.out.println("Spec<" + specIndex + "> Begin: "
+                                                                       + _beginLine + "  End: " + _endLine);
+                                                       System.out.println(curLine);
+                                               }
+                                       }
+                               } else {
+                                       if (trimedLine.endsWith("*/")) {
+                                               _endLine = reader.getLineNumber();
+                                               _foundHead = false;
+                                               specText.append("\n");
+                                               specText.append(curLine);
+                                               
+                                               System.out.println("Spec<" + specIndex + "> Begin: "
+                                                               + _beginLine + "  End: " + _endLine);
+                                               System.out.println(curLine);
+                                               
+                                               specText = new StringBuilder();
+                                       } else {
+                                               
+                                       }
+                               }
                        }
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
@@ -65,39 +90,32 @@ public class SpecExtractor {
                }
        }
        
-       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;
+       private String trimSpace(String line) {
+               int i, j;
+               char ch;
                for (i = 0; i < line.length(); i++) {
-                       char ch = line.charAt(i);
+                       ch = line.charAt(i);
                        if (ch == ' ' || ch == '\t')
                                i++;
                        else
                                break;
                }
-               return line.substring(i);
+               for (j = line.length() - 1; j >= 0; j--) {
+                       ch = line.charAt(j);
+                       if (ch == ' ' || ch == '\t')
+                               j--;
+                       else
+                               break;
+               }
+               if (i > j)
+                       return "";
+               else
+                       return line.substring(i, j + 1);
+       }
+       
+       public static void main(String[] argvs) {
+               SpecExtractor extractor = new SpecExtractor();
+               File file = new File("./grammer/spec1.txt");
+               extractor.extract(file);
        }
 }