0c06badaa2cf54024795c82dccb360f2600dbe1e
[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 int _beginLineNum, _endLineNum;
26         private String _beginLine;
27         
28         SpecExtractor() {
29                 _constructs = new ArrayList<SpecConstruct>();
30         }
31         
32         /**
33          * <p>
34          * Given a list of files, it scans each file and add found SpecConstrcut
35          * to the _constructs list.
36          * </p>
37          * @param files
38          * @throws SpecNotMatchException 
39          */
40         public void extract(File[] files) throws SpecNotMatchException {
41                 for (int i = 0; i < files.length; i++)
42                         extract(files[i]);
43         }
44         
45         public void extract(File file) throws SpecNotMatchException {
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                                                 _beginLineNum = reader.getLineNumber();
59                                                 _beginLine = curLine;
60                                                 foundHead = true;
61                                                 specText.append("\n");
62                                                 specText.append(curLine);
63                                                 if (trimedLine.endsWith("*/")) {
64                                                         _endLineNum = reader.getLineNumber();
65                                                         foundHead = false;
66                                                         
67                                                         System.out.println("Spec<" + specIndex + "> Begin: "
68                                                                         + _beginLine + "  End: " + _endLineNum);
69                                                         System.out.println(specText);
70                                                         specIndex++;
71                                                 }
72                                         }
73                                 } else {
74                                         specText.append("\n");
75                                         specText.append(curLine);
76                                         if (trimedLine.endsWith("*/")) {
77                                                 _endLineNum = reader.getLineNumber();
78                                                 foundHead = false;
79                                                 
80                                                 System.out.println("Spec<" + specIndex + "> Begin: "
81                                                                 + _beginLine + "  End: " + _endLineNum);
82                                                 System.out.println(specText);
83                                                 specIndex++;
84                                                 
85                                                 specText = new StringBuilder();
86                                         }
87                                 }
88                         }
89                         // At the end we can only find the head "/**" but no tail found
90                         if (foundHead) {
91                                 String msg = "In file \"" + file.getAbsolutePath() + "\", line: "
92                                                 + _beginLineNum + "\n" + _beginLine + "\n" + "Can't find matching spec.";
93                                 throw new SpecNotMatchException(msg);
94                         }
95                 } catch (FileNotFoundException e) {
96                         e.printStackTrace();
97                 } catch (IOException e) {
98                         e.printStackTrace();
99                 }
100         }
101         
102         private String trimSpace(String line) {
103                 int i, j;
104                 char ch;
105                 for (i = 0; i < line.length(); i++) {
106                         ch = line.charAt(i);
107                         if (ch != ' ' && ch != '\t')
108                                 break;
109                 }
110                 for (j = line.length() - 1; j >= 0; j--) {
111                         ch = line.charAt(j);
112                         if (ch != ' ' && ch != '\t')
113                                 break;
114                 }
115                 if (i > j)
116                         return "";
117                 else
118                         return line.substring(i, j + 1);
119         }
120         
121         public static void main(String[] argvs) {
122                 SpecExtractor extractor = new SpecExtractor();
123                 File file = new File("./grammer/spec1.txt");
124                 try {
125                         extractor.extract(file);
126                 } catch (SpecNotMatchException e) {
127                         e.printStackTrace();
128                 }
129         }
130 }