1 package edu.uci.eecs.specCompiler.specExtraction;
3 import java.io.BufferedReader;
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;
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.
21 public class SpecExtractor {
22 private ArrayList<SpecConstruct> _constructs;
23 private int _beginLineNum, _endLineNum;
24 private String _beginLine;
27 _constructs = new ArrayList<SpecConstruct>();
32 * Given a list of files, it scans each file and add found SpecConstrcut
33 * to the _constructs list.
36 * @throws SpecNotMatchException
38 public void extract(File[] files) throws SpecNotMatchException {
39 for (int i = 0; i < files.length; i++)
43 public void extract(File file) throws SpecNotMatchException {
45 LineNumberReader reader = new LineNumberReader(new FileReader(file));
46 String prevLine = "", curLine, trimedLine;
47 StringBuilder specText = new StringBuilder();
48 boolean foundHead = false;
50 while ((curLine = reader.readLine()) != null) {
51 if (prevLine.endsWith("\\"))
53 trimedLine = trimSpace(curLine);
55 if (trimedLine.startsWith("/**")) {
56 _beginLineNum = reader.getLineNumber();
59 specText.append("\n");
60 specText.append(curLine);
61 if (trimedLine.endsWith("*/")) {
62 _endLineNum = reader.getLineNumber();
64 //Constrcut inst = SpecParser
66 System.out.println("Spec<" + specIndex + "> Begin: "
67 + _beginLine + " End: " + _endLineNum);
68 System.out.println(specText);
73 specText.append("\n");
74 specText.append(curLine);
75 if (trimedLine.endsWith("*/")) {
76 _endLineNum = reader.getLineNumber();
79 System.out.println("Spec<" + specIndex + "> Begin: "
80 + _beginLine + " End: " + _endLineNum);
81 System.out.println(specText);
84 specText = new StringBuilder();
88 // At the end we can only find the head "/**" but no tail found
90 String msg = "In file \"" + file.getAbsolutePath() + "\", line: "
91 + _beginLineNum + "\n" + _beginLine + "\n" + "Can't find matching spec.";
92 throw new SpecNotMatchException(msg);
94 } catch (FileNotFoundException e) {
96 } catch (IOException e) {
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;
108 res = res + curLine.substring(0, braceIdx + 1);
114 private String trimSpace(String line) {
117 for (i = 0; i < line.length(); i++) {
119 if (ch != ' ' && ch != '\t')
122 for (j = line.length() - 1; j >= 0; j--) {
124 if (ch != ' ' && ch != '\t')
130 return line.substring(i, j + 1);
133 public static void main(String[] argvs) {
134 SpecExtractor extractor = new SpecExtractor();
135 File file = new File("./grammer/spec1.txt");
137 extractor.extract(file);
138 } catch (SpecNotMatchException e) {