2d28fceacb6a5209c54fc035f31598c531272302
[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                                                 specText.append("\n");
61                                                 specText.append(curLine);
62                                                 if (trimedLine.endsWith("*/")) {
63                                                         _endLine = reader.getLineNumber();
64                                                         _foundHead = false;
65                                                         
66                                                         System.out.println("Spec<" + specIndex + "> Begin: "
67                                                                         + _beginLine + "  End: " + _endLine);
68                                                         System.out.println(specText);
69                                                         specIndex++;
70                                                 }
71                                         }
72                                 } else {
73                                         specText.append("\n");
74                                         specText.append(curLine);
75                                         if (trimedLine.endsWith("*/")) {
76                                                 _endLine = reader.getLineNumber();
77                                                 _foundHead = false;
78                                                 
79                                                 System.out.println("Spec<" + specIndex + "> Begin: "
80                                                                 + _beginLine + "  End: " + _endLine);
81                                                 System.out.println(specText);
82                                                 specIndex++;
83                                                 
84                                                 specText = new StringBuilder();
85                                         } else {
86                                                 
87                                         }
88                                 }
89                         }
90                 } catch (FileNotFoundException e) {
91                         e.printStackTrace();
92                 } catch (IOException e) {
93                         e.printStackTrace();
94                 }
95         }
96         
97         private String trimSpace(String line) {
98                 int i, j;
99                 char ch;
100                 for (i = 0; i < line.length(); i++) {
101                         ch = line.charAt(i);
102                         if (ch != ' ' && ch != '\t')
103                                 break;
104                 }
105                 for (j = line.length() - 1; j >= 0; j--) {
106                         ch = line.charAt(j);
107                         if (ch != ' ' && ch != '\t')
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 }