*
*/
public class SpecExtractor {
- private ArrayList<SpecConstruct> _constructs;
+ private ArrayList<Construct> _constructs;
private int _beginLineNum, _endLineNum;
private String _beginLine;
public SpecExtractor() {
- _constructs = new ArrayList<SpecConstruct>();
+ _constructs = new ArrayList<Construct>();
+ }
+
+ ArrayList<Construct> getConstructs() {
+ return this._constructs;
}
/**
* @param files
* @throws SpecNotMatchException
*/
- public void extract(File[] files) throws SpecNotMatchException {
+ public void extract(File[] files) {
for (int i = 0; i < files.length; i++)
extract(files[i]);
}
- public void extract(File file) throws SpecNotMatchException {
+ public void extract(File file) {
StringBuilder specText = new StringBuilder();
- try {
- LineNumberReader reader = new LineNumberReader(new FileReader(file));
- String prevLine = "", curLine, trimedLine, funcDecl;
- SpecConstruct specConstruct;
- boolean foundHead = false;
- while ((curLine = reader.readLine()) != null) {
- if (prevLine.endsWith("\\"))
- continue;
- trimedLine = trimSpace(curLine);
- if (!foundHead) {
- if (trimedLine.startsWith("/**")) {
- _beginLineNum = reader.getLineNumber();
- _beginLine = curLine;
- foundHead = true;
- specText.append("\n");
- specText.append(curLine);
- if (trimedLine.endsWith("*/")) {
- _endLineNum = reader.getLineNumber();
- foundHead = false;
- if (isComment(specText.toString()))
- continue;
- Construct inst = SpecParser.parseSpec(specText
- .toString());
- if (inst instanceof InterfaceConstruct
- || inst instanceof InterfaceDefineConstruct) {
- funcDecl = readFunctionDecl(reader);
- specConstruct = new SpecConstruct(
- specText.toString(), file,
- _beginLineNum, _endLineNum, inst,
- funcDecl);
- } else {
- specConstruct = new SpecConstruct(
- specText.toString(), file,
- _beginLineNum, _endLineNum, inst);
- }
- _constructs.add(specConstruct);
- specText = new StringBuilder();
- // System.out.println(specConstruct);
- }
- }
- } else {
- specText.append("\n");
- specText.append(curLine);
- if (trimedLine.endsWith("*/")) {
- _endLineNum = reader.getLineNumber();
- foundHead = false;
- if (isComment(specText.toString())) {
- specText = new StringBuilder();
- continue;
- }
- Construct inst = SpecParser.parseSpec(specText
- .toString());
- if (inst instanceof InterfaceConstruct
- || inst instanceof InterfaceDefineConstruct) {
- funcDecl = readFunctionDecl(reader);
- specConstruct = new SpecConstruct(
- specText.toString(), file, _beginLineNum,
- _endLineNum, inst, funcDecl);
- } else {
- specConstruct = new SpecConstruct(
- specText.toString(), file, _beginLineNum,
- _endLineNum, inst);
- }
- _constructs.add(specConstruct);
- specText = new StringBuilder();
- // System.out.println(specConstruct);
- }
- }
- }
- // At the end we can only find the head "/**" but no tail found
- if (foundHead) {
- String msg = "In file \"" + file.getAbsolutePath()
- + "\", line: " + _beginLineNum + "\n" + _beginLine
- + "\n" + "Can't find matching spec.";
- throw new SpecNotMatchException(msg);
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ParseException e) {
- printSpecInfo(file, specText.toString());
- e.printStackTrace();
- } catch (TokenMgrError e) {
- printSpecInfo(file, specText.toString());
- e.printStackTrace();
- }
- }
-
- private void printSpecInfo(File file, String text) {
- System.out.println("Error in spec!");
- System.out.println("File: " + file.getAbsolutePath());
- System.out.println("Begin: " + _beginLineNum + " End: " + _endLineNum);
- System.out.println(text);
- }
-
- private boolean isComment(String specText) {
- if (specText.indexOf("@Begin") != -1)
- return false;
- return true;
- }
-
- private String readFunctionDecl(LineNumberReader reader) throws IOException {
- String res = "", curLine;
- while ((curLine = reader.readLine()) != null) {
- int braceIdx = curLine.indexOf(')');
- if (braceIdx == -1) {
- res = res + " " + curLine;
- } else {
- res = res + curLine;
- break;
- }
- }
- return res;
+
}
public static String trimSpace(String line) {
else
return line.substring(i, j + 1);
}
-
- public ArrayList<SpecConstruct> getConstructs() {
- return this._constructs;
- }
-
- public static void main(String[] argvs) {
- SpecExtractor extractor = new SpecExtractor();
- File file = new File("./grammer/spec1.txt");
- try {
- extractor.extract(file);
- } catch (SpecNotMatchException e) {
- e.printStackTrace();
- }
- }
}