add more & need C/C++ code recognition
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / specExtraction / SpecExtractor.java
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();
+               }
        }
 }