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;
11 import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils.Text;
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.
23 public class SpecExtractor {
24 private ArrayList<SpecConstruct> _constructs;
25 private StringBuilder _potentialConstruct;
26 private int _beginLine, _endLine;
29 _constructs = new ArrayList<SpecConstruct>();
30 _potentialConstruct = new StringBuilder();
35 * Given a list of files, it scans each file and add found SpecConstrcut
36 * to the _constructs list.
40 public void extract(File[] files) {
41 for (int i = 0; i < files.length; i++)
45 public void extract(File file) {
47 LineNumberReader reader = new LineNumberReader(new FileReader(file));
48 String prevLine = "", curLine, trimedLine;
49 StringBuilder specText = new StringBuilder();
50 boolean _foundHead = false;
52 while ((curLine = reader.readLine()) != null) {
53 if (prevLine.endsWith("\\"))
55 trimedLine = trimSpace(curLine);
57 if (trimedLine.startsWith("/**")) {
58 _beginLine = reader.getLineNumber();
60 specText.append("\n");
61 specText.append(curLine);
62 if (trimedLine.endsWith("*/")) {
63 _endLine = reader.getLineNumber();
66 System.out.println("Spec<" + specIndex + "> Begin: "
67 + _beginLine + " End: " + _endLine);
68 System.out.println(specText);
73 specText.append("\n");
74 specText.append(curLine);
75 if (trimedLine.endsWith("*/")) {
76 _endLine = reader.getLineNumber();
79 System.out.println("Spec<" + specIndex + "> Begin: "
80 + _beginLine + " End: " + _endLine);
81 System.out.println(specText);
84 specText = new StringBuilder();
90 } catch (FileNotFoundException e) {
92 } catch (IOException e) {
97 private String trimSpace(String line) {
100 for (i = 0; i < line.length(); i++) {
102 if (ch != ' ' && ch != '\t')
105 for (j = line.length() - 1; j >= 0; j--) {
107 if (ch != ' ' && ch != '\t')
113 return line.substring(i, j + 1);
116 public static void main(String[] argvs) {
117 SpecExtractor extractor = new SpecExtractor();
118 File file = new File("./grammer/spec1.txt");
119 extractor.extract(file);