extracting spec & compiling
[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 StringBuilder _extractedSpecText;
24         
25         private enum State {
26                 Neutral, HeadParsed, BeginParsed, EndParsed
27         }
28         
29         private State _curState;
30         
31         private StringBuilder _potentialConstruct;
32         private int _startLine, _endLine;
33         
34         SpecExtractor() {
35                 _constructs = new ArrayList<SpecConstruct>();
36                 _extractedSpecText = new StringBuilder();
37                 _curState = State.Neutral;
38                 _potentialConstruct = new StringBuilder();
39         }
40         
41         /**
42          * <p>
43          * Given a list of files, it scans each file and add found SpecConstrcut
44          * to the _constructs list.
45          * </p>
46          * @param files
47          */
48         private void extract(File[] files) {
49                 for (int i = 0; i < files.length; i++)
50                         extract(files[i]);
51         }
52         
53         private void extract(File file) {
54                 try {
55                         LineNumberReader reader = new LineNumberReader(new FileReader(file));
56                         String prevLine = "", curLine;
57                         ArrayList<String> text;
58                         while ((curLine = reader.readLine()) != null) {
59                                 
60                         }
61                 } catch (FileNotFoundException e) {
62                         e.printStackTrace();
63                 } catch (IOException e) {
64                         e.printStackTrace();
65                 }
66         }
67         
68         private void parseHead(String prevLine, String line) {
69                 assert (_curState == State.Neutral);
70                 
71                 // "\" is the C/C++ line break. If the previous line ends with "\",
72                 // it may be part of a string literal.
73                 if (prevLine.endsWith("\\"))
74                         return;
75                 String newLine = trimBeginningSpace(line);
76 //              if (newLine.startsWith("/**") && 
77 //                              (newLine.length() == 3 || 
78         }
79         
80         private void parseBegin(String line) {
81                 
82         }
83         
84         private void parseEnd(String line) {
85                 
86         }
87         
88         private void parseTail(String line) {
89                 
90         }
91         
92         private String trimBeginningSpace(String line) {
93                 int i;
94                 for (i = 0; i < line.length(); i++) {
95                         char ch = line.charAt(i);
96                         if (ch == ' ' || ch == '\t')
97                                 i++;
98                         else
99                                 break;
100                 }
101                 return line.substring(i);
102         }
103 }