From: Peizhao Ou Date: Thu, 10 Oct 2013 18:29:26 +0000 (-0700) Subject: add more & need C/C++ code recognition X-Git-Url: http://plrg.eecs.uci.edu/git/?p=cdsspec-compiler.git;a=commitdiff_plain;h=f8b1679d97465996868b3251c1da6aa412fd098b add more & need C/C++ code recognition --- diff --git a/grammer/spec-compiler.jj b/grammer/spec-compiler.jj index f7a4248..110dcb9 100644 --- a/grammer/spec-compiler.jj +++ b/grammer/spec-compiler.jj @@ -151,15 +151,25 @@ TOKEN : | <#LETTER: ["a"-"z", "A"-"Z"]> | - ( | | "_")> + ( | | "_")*> +| + +| + +| + "> +| + } void Start() : {} { //Global_construct() - - // + + // C_CPP_CODE() } void Global_construct() : @@ -167,33 +177,36 @@ void Global_construct() : { - //Global_define() (Interface_cluster())? (Happens_before())? + Global_define() (Interface_cluster())? (Happens_before())? } +/* void C_CPP_CODE() : -{String code;} +{} { <(~["@"])+> } - +*/ void Global_define() : {} { - C_CPP_CODE() + //C_CPP_CODE() } void Conditional_interface() : {} { - (<"(" ")"> | "") + (<"(" ")">)* } void Interface_cluster() : {} { - "=" "{" Conditional_interface() (",," Conditional_interface())* "}" + + Conditional_interface() ( Conditional_interface())* + } void Interface_clusters() : @@ -205,7 +218,7 @@ void Interface_clusters() : void Happens_before() : {} { - (Conditional_interface() "->" Conditional_interface())+ + (Conditional_interface() Conditional_interface())+ } void Interface() : diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java b/src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java index 2d28fce..0c06bad 100644 --- a/src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java +++ b/src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java @@ -22,12 +22,11 @@ import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils.Text; */ public class SpecExtractor { private ArrayList _constructs; - private StringBuilder _potentialConstruct; - private int _beginLine, _endLine; + private int _beginLineNum, _endLineNum; + private String _beginLine; SpecExtractor() { _constructs = new ArrayList(); - _potentialConstruct = new StringBuilder(); } /** @@ -36,35 +35,37 @@ public class SpecExtractor { * to the _constructs list. *

* @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++; } @@ -73,20 +74,24 @@ public class SpecExtractor { 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) { @@ -116,6 +121,10 @@ public class SpecExtractor { 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(); + } } } diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/SpecNotMatchException.java b/src/edu/uci/eecs/specCompiler/specExtraction/SpecNotMatchException.java new file mode 100644 index 0000000..cf3ce52 --- /dev/null +++ b/src/edu/uci/eecs/specCompiler/specExtraction/SpecNotMatchException.java @@ -0,0 +1,9 @@ +package edu.uci.eecs.specCompiler.specExtraction; + +import java.io.File; + +public class SpecNotMatchException extends Exception { + public SpecNotMatchException(String msg) { + super(msg); + } +}