dc24b28167d0f2c63adb875d0ec348af7dd30e8e
[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                                                         
65                                                         System.out.println("Spec<" + specIndex + "> Begin: "
66                                                                         + _beginLine + "  End: " + _endLineNum);
67                                                         System.out.println(specText);
68                                                         specIndex++;
69                                                 }
70                                         }
71                                 } else {
72                                         specText.append("\n");
73                                         specText.append(curLine);
74                                         if (trimedLine.endsWith("*/")) {
75                                                 _endLineNum = reader.getLineNumber();
76                                                 foundHead = false;
77                                                 
78                                                 System.out.println("Spec<" + specIndex + "> Begin: "
79                                                                 + _beginLine + "  End: " + _endLineNum);
80                                                 System.out.println(specText);
81                                                 specIndex++;
82                                                 
83                                                 specText = new StringBuilder();
84                                         }
85                                 }
86                         }
87                         // At the end we can only find the head "/**" but no tail found
88                         if (foundHead) {
89                                 String msg = "In file \"" + file.getAbsolutePath() + "\", line: "
90                                                 + _beginLineNum + "\n" + _beginLine + "\n" + "Can't find matching spec.";
91                                 throw new SpecNotMatchException(msg);
92                         }
93                 } catch (FileNotFoundException e) {
94                         e.printStackTrace();
95                 } catch (IOException e) {
96                         e.printStackTrace();
97                 }
98         }
99         
100         private String trimSpace(String line) {
101                 int i, j;
102                 char ch;
103                 for (i = 0; i < line.length(); i++) {
104                         ch = line.charAt(i);
105                         if (ch != ' ' && ch != '\t')
106                                 break;
107                 }
108                 for (j = line.length() - 1; j >= 0; j--) {
109                         ch = line.charAt(j);
110                         if (ch != ' ' && ch != '\t')
111                                 break;
112                 }
113                 if (i > j)
114                         return "";
115                 else
116                         return line.substring(i, j + 1);
117         }
118         
119         public static void main(String[] argvs) {
120                 SpecExtractor extractor = new SpecExtractor();
121                 File file = new File("./grammer/spec1.txt");
122                 try {
123                         extractor.extract(file);
124                 } catch (SpecNotMatchException e) {
125                         e.printStackTrace();
126                 }
127         }
128 }