|
<#LETTER: ["a"-"z", "A"-"Z"]>
|
- <IDENTIFIER: <LETTER> (<LETTER> | <DIGIT> | "_")>
+ <IDENTIFIER: <LETTER> (<LETTER> | <DIGIT> | "_")*>
+|
+ <EQUALS: "=">
+|
+ <LEFT_PARAN: "{">
+|
+ <RIGHT_PARAN: "}">
+|
+ <HB_SYMBOL: "->">
+|
+ <COMMA: ",">
}
void Start() :
{}
{
//Global_construct() <EOF>
- <EOF>
- //<IDENTIFIER> <EOF>
+ <IDENTIFIER> <EOF>
+ //<IDENTIFIER> C_CPP_CODE() <EOF>
}
void Global_construct() :
{
<HEAD>
<BEGIN>
- //Global_define() (Interface_cluster())? (Happens_before())?
+ Global_define() (Interface_cluster())? (Happens_before())?
<END>
<TAIL>
}
+/*
void C_CPP_CODE() :
-{String code;}
+{}
{
<(~["@"])+>
}
-
+*/
void Global_define() :
{}
{
- <GLOBAL_DEFINE> C_CPP_CODE()
+ <GLOBAL_DEFINE> //C_CPP_CODE()
}
void Conditional_interface() :
{}
{
- <IDENTIFIER> (<"(" <IDENTIFIER> ")"> | "")
+ <IDENTIFIER> (<"(" <IDENTIFIER> ")">)*
}
void Interface_cluster() :
{}
{
- <IDENTIFIER> "=" "{" Conditional_interface() (",," Conditional_interface())* "}"
+ <IDENTIFIER> <EQUALS> <LEFT_PARAN>
+ Conditional_interface() (<COMMA> Conditional_interface())*
+ <RIGHT_PARAN>
}
void Interface_clusters() :
void Happens_before() :
{}
{
- <HAPPENS_BEFORE> (Conditional_interface() "->" Conditional_interface())+
+ <HAPPENS_BEFORE> (Conditional_interface() <HB_SYMBOL> Conditional_interface())+
}
void Interface() :
*/
public class SpecExtractor {
private ArrayList<SpecConstruct> _constructs;
- private StringBuilder _potentialConstruct;
- private int _beginLine, _endLine;
+ private int _beginLineNum, _endLineNum;
+ private String _beginLine;
SpecExtractor() {
_constructs = new ArrayList<SpecConstruct>();
- _potentialConstruct = new StringBuilder();
}
/**
* to the _constructs list.
* </p>
* @param files
+ * @throws SpecNotMatchException
*/
- public void extract(File[] files) {
+ public void extract(File[] files) throws SpecNotMatchException {
for (int i = 0; i < files.length; i++)
extract(files[i]);
}
- public void extract(File file) {
+ public void extract(File file) throws SpecNotMatchException {
try {
LineNumberReader reader = new LineNumberReader(new FileReader(file));
String prevLine = "", curLine, trimedLine;
StringBuilder specText = new StringBuilder();
- boolean _foundHead = false;
+ boolean foundHead = false;
int specIndex = 0;
while ((curLine = reader.readLine()) != null) {
if (prevLine.endsWith("\\"))
continue;
trimedLine = trimSpace(curLine);
- if (!_foundHead) {
+ if (!foundHead) {
if (trimedLine.startsWith("/**")) {
- _beginLine = reader.getLineNumber();
- _foundHead = true;
+ _beginLineNum = reader.getLineNumber();
+ _beginLine = curLine;
+ foundHead = true;
specText.append("\n");
specText.append(curLine);
if (trimedLine.endsWith("*/")) {
- _endLine = reader.getLineNumber();
- _foundHead = false;
+ _endLineNum = reader.getLineNumber();
+ foundHead = false;
System.out.println("Spec<" + specIndex + "> Begin: "
- + _beginLine + " End: " + _endLine);
+ + _beginLine + " End: " + _endLineNum);
System.out.println(specText);
specIndex++;
}
specText.append("\n");
specText.append(curLine);
if (trimedLine.endsWith("*/")) {
- _endLine = reader.getLineNumber();
- _foundHead = false;
+ _endLineNum = reader.getLineNumber();
+ foundHead = false;
System.out.println("Spec<" + specIndex + "> Begin: "
- + _beginLine + " End: " + _endLine);
+ + _beginLine + " End: " + _endLineNum);
System.out.println(specText);
specIndex++;
specText = new StringBuilder();
- } else {
-
}
}
}
+ // 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) {
public static void main(String[] argvs) {
SpecExtractor extractor = new SpecExtractor();
File file = new File("./grammer/spec1.txt");
- extractor.extract(file);
+ try {
+ extractor.extract(file);
+ } catch (SpecNotMatchException e) {
+ e.printStackTrace();
+ }
}
}