ba043430f444364827f36814ce6da03c550c695d
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / specExtraction / SpecExtractor.java
1 package edu.uci.eecs.specCompiler.specExtraction;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileNotFoundException;
6 import java.io.FileReader;
7 import java.io.IOException;
8 import java.io.LineNumberReader;
9 import java.util.ArrayList;
10
11 import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils.Text;
12
13 /**
14  * <p>
15  * This class represents the specification extractor of the specification. The
16  * main function of this class is to read C/C++11 source files and extract the
17  * corresponding specification out, and remember its location, including the
18  * file name and the line number, to help the code generation process.
19  * </p>
20  * @author peizhaoo
21  *
22  */
23 public class SpecExtractor {
24         private ArrayList<SpecConstruct> _constructs;
25         private StringBuilder _potentialConstruct;
26         private int _beginLine, _endLine;
27         
28         SpecExtractor() {
29                 _constructs = new ArrayList<SpecConstruct>();
30                 _potentialConstruct = new StringBuilder();
31         }
32         
33         /**
34          * <p>
35          * Given a list of files, it scans each file and add found SpecConstrcut
36          * to the _constructs list.
37          * </p>
38          * @param files
39          */
40         public void extract(File[] files) {
41                 for (int i = 0; i < files.length; i++)
42                         extract(files[i]);
43         }
44         
45         public void extract(File file) {
46                 try {
47                         LineNumberReader reader = new LineNumberReader(new FileReader(file));
48                         String prevLine = "", curLine, trimedLine;
49                         StringBuilder specText = new StringBuilder();
50                         boolean _foundHead = false;
51                         int specIndex = 0;
52                         while ((curLine = reader.readLine()) != null) {
53                                 if (prevLine.endsWith("\\"))
54                                         continue;
55                                 trimedLine = trimSpace(curLine);
56                                 if (!_foundHead) {
57                                         if (trimedLine.startsWith("/**")) {
58                                                 _beginLine = reader.getLineNumber();
59                                                 _foundHead = true;
60                                                 if (trimedLine.endsWith("*/")) {
61                                                         _endLine = reader.getLineNumber();
62                                                         _foundHead = false;
63                                                         
64                                                         System.out.println("Spec<" + specIndex + "> Begin: "
65                                                                         + _beginLine + "  End: " + _endLine);
66                                                         System.out.println(curLine);
67                                                 }
68                                         }
69                                 } else {
70                                         if (trimedLine.endsWith("*/")) {
71                                                 _endLine = reader.getLineNumber();
72                                                 _foundHead = false;
73                                                 specText.append("\n");
74                                                 specText.append(curLine);
75                                                 
76                                                 System.out.println("Spec<" + specIndex + "> Begin: "
77                                                                 + _beginLine + "  End: " + _endLine);
78                                                 System.out.println(curLine);
79                                                 
80                                                 specText = new StringBuilder();
81                                         } else {
82                                                 
83                                         }
84                                 }
85                         }
86                 } catch (FileNotFoundException e) {
87                         e.printStackTrace();
88                 } catch (IOException e) {
89                         e.printStackTrace();
90                 }
91         }
92         
93         private String trimSpace(String line) {
94                 int i, j;
95                 char ch;
96                 for (i = 0; i < line.length(); i++) {
97                         ch = line.charAt(i);
98                         if (ch == ' ' || ch == '\t')
99                                 i++;
100                         else
101                                 break;
102                 }
103                 for (j = line.length() - 1; j >= 0; j--) {
104                         ch = line.charAt(j);
105                         if (ch == ' ' || ch == '\t')
106                                 j--;
107                         else
108                                 break;
109                 }
110                 if (i > j)
111                         return "";
112                 else
113                         return line.substring(i, j + 1);
114         }
115         
116         public static void main(String[] argvs) {
117                 SpecExtractor extractor = new SpecExtractor();
118                 File file = new File("./grammer/spec1.txt");
119                 extractor.extract(file);
120         }
121 }