e1fae7ef35081ecd04c4d1744303b315b32bc586
[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 /**
12  * <p>
13  * This class represents the specification extractor of the specification. The
14  * main function of this class is to read C/C++11 source files and extract the
15  * corresponding specification out, and remember its location, including the
16  * file name and the line number, to help the code generation process.
17  * </p>
18  * @author peizhaoo
19  *
20  */
21 public class SpecExtractor {
22         private ArrayList<SpecConstruct> _constructs;
23         private int _beginLineNum, _endLineNum;
24         private String _beginLine;
25         
26         SpecExtractor() {
27                 _constructs = new ArrayList<SpecConstruct>();
28         }
29         
30         /**
31          * <p>
32          * Given a list of files, it scans each file and add found SpecConstrcut
33          * to the _constructs list.
34          * </p>
35          * @param files
36          * @throws SpecNotMatchException 
37          */
38         public void extract(File[] files) throws SpecNotMatchException {
39                 for (int i = 0; i < files.length; i++)
40                         extract(files[i]);
41         }
42         
43         public void extract(File file) throws SpecNotMatchException {
44                 try {
45                         LineNumberReader reader = new LineNumberReader(new FileReader(file));
46                         String prevLine = "", curLine, trimedLine;
47                         StringBuilder specText = new StringBuilder();
48                         boolean foundHead = false;
49                         int specIndex = 0;
50                         while ((curLine = reader.readLine()) != null) {
51                                 if (prevLine.endsWith("\\"))
52                                         continue;
53                                 trimedLine = trimSpace(curLine);
54                                 if (!foundHead) {
55                                         if (trimedLine.startsWith("/**")) {
56                                                 _beginLineNum = reader.getLineNumber();
57                                                 _beginLine = curLine;
58                                                 foundHead = true;
59                                                 specText.append("\n");
60                                                 specText.append(curLine);
61                                                 if (trimedLine.endsWith("*/")) {
62                                                         _endLineNum = reader.getLineNumber();
63                                                         foundHead = false;
64                                                         //Constrcut inst = SpecParser
65                                                         
66                                                         System.out.println("Spec<" + specIndex + "> Begin: "
67                                                                         + _beginLine + "  End: " + _endLineNum);
68                                                         System.out.println(specText);
69                                                         specIndex++;
70                                                 }
71                                         }
72                                 } else {
73                                         specText.append("\n");
74                                         specText.append(curLine);
75                                         if (trimedLine.endsWith("*/")) {
76                                                 _endLineNum = reader.getLineNumber();
77                                                 foundHead = false;
78                                                 
79                                                 System.out.println("Spec<" + specIndex + "> Begin: "
80                                                                 + _beginLine + "  End: " + _endLineNum);
81                                                 System.out.println(specText);
82                                                 specIndex++;
83                                                 
84                                                 specText = new StringBuilder();
85                                         }
86                                 }
87                         } 
88                         // At the end we can only find the head "/**" but no tail found
89                         if (foundHead) {
90                                 String msg = "In file \"" + file.getAbsolutePath() + "\", line: "
91                                                 + _beginLineNum + "\n" + _beginLine + "\n" + "Can't find matching spec.";
92                                 throw new SpecNotMatchException(msg);
93                         }
94                 } catch (FileNotFoundException e) {
95                         e.printStackTrace();
96                 } catch (IOException e) {
97                         e.printStackTrace();
98                 }
99         }
100         
101         private String readInterfaceDecl(LineNumberReader reader) throws IOException {
102                 String res = "", curLine;
103                 while ((curLine = reader.readLine()) != null) {
104                         int braceIdx = curLine.indexOf(')');
105                         if (braceIdx == -1) {
106                                 res = res + " " + curLine;
107                         } else {
108                                 res = res + curLine.substring(0, braceIdx + 1);
109                         }
110                 }
111                 return res;
112         }
113         
114         private String trimSpace(String line) {
115                 int i, j;
116                 char ch;
117                 for (i = 0; i < line.length(); i++) {
118                         ch = line.charAt(i);
119                         if (ch != ' ' && ch != '\t')
120                                 break;
121                 }
122                 for (j = line.length() - 1; j >= 0; j--) {
123                         ch = line.charAt(j);
124                         if (ch != ' ' && ch != '\t')
125                                 break;
126                 }
127                 if (i > j)
128                         return "";
129                 else
130                         return line.substring(i, j + 1);
131         }
132         
133         public static void main(String[] argvs) {
134                 SpecExtractor extractor = new SpecExtractor();
135                 File file = new File("./grammer/spec1.txt");
136                 try {
137                         extractor.extract(file);
138                 } catch (SpecNotMatchException e) {
139                         e.printStackTrace();
140                 }
141         }
142 }