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 StringBuilder _extractedSpecText;
26 Neutral, HeadParsed, BeginParsed, EndParsed
29 private State _curState;
31 private StringBuilder _potentialConstruct;
32 private int _startLine, _endLine;
35 _constructs = new ArrayList<SpecConstruct>();
36 _extractedSpecText = new StringBuilder();
37 _curState = State.Neutral;
38 _potentialConstruct = new StringBuilder();
43 * Given a list of files, it scans each file and add found SpecConstrcut
44 * to the _constructs list.
48 private void extract(File[] files) {
49 for (int i = 0; i < files.length; i++)
53 private void extract(File file) {
55 LineNumberReader reader = new LineNumberReader(new FileReader(file));
56 String prevLine = "", curLine;
57 ArrayList<String> text;
58 while ((curLine = reader.readLine()) != null) {
61 } catch (FileNotFoundException e) {
63 } catch (IOException e) {
68 private void parseHead(String prevLine, String line) {
69 assert (_curState == State.Neutral);
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("\\"))
75 String newLine = trimBeginningSpace(line);
76 // if (newLine.startsWith("/**") &&
77 // (newLine.length() == 3 ||
80 private void parseBegin(String line) {
84 private void parseEnd(String line) {
88 private void parseTail(String line) {
92 private String trimBeginningSpace(String line) {
94 for (i = 0; i < line.length(); i++) {
95 char ch = line.charAt(i);
96 if (ch == ' ' || ch == '\t')
101 return line.substring(i);