From a54d319679844e59186728d1f71322c3026148bf Mon Sep 17 00:00:00 2001 From: Peizhao Ou Date: Wed, 9 Oct 2013 18:37:46 -0700 Subject: [PATCH] extracting spec & compiling --- grammer/spec-compiler.jj | 39 ++++--- .../specExtraction/SpecConstruct.java | 44 ++++++++ .../specExtraction/SpecExtractor.java | 103 ++++++++++++++++++ 3 files changed, 168 insertions(+), 18 deletions(-) create mode 100644 src/edu/uci/eecs/specCompiler/specExtraction/SpecConstruct.java create mode 100644 src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java diff --git a/grammer/spec-compiler.jj b/grammer/spec-compiler.jj index 965e4f5..6ce4495 100644 --- a/grammer/spec-compiler.jj +++ b/grammer/spec-compiler.jj @@ -84,8 +84,11 @@ SKIP : | "\t" | - // Comment for the specification + // "#" comment for the specification <"#" (~["\n", "\r"])* (["\n", "\r"])> +| + // "//" comment for the specification + <"//" (~["\n", "\r"])* (["\n", "\r"])> } TOKEN : @@ -110,14 +113,11 @@ TOKEN : | | - -| - (~["@", "#", "$"])+> -| - )*> + | -| +| + | | @@ -130,27 +130,34 @@ TOKEN : | - | | | - + <#DIGIT: ["0"-"9"]> +| + <#NONZERO_DIGIT: ["1"-"9"]> +| + <#LETTER: ["a"-"z", "A"-"Z"]> +| + <#NUM: > +| + ( | | "_")> } void Start() : {} { - + Global_construct() } void Global_construct() : {} { - + - "(" ")" "->" + Global_define() (Interface_cluster())? (Happens_before())? } @@ -158,7 +165,7 @@ void Global_construct() : void C_CPP_CODE() : {} { - <(~["@", "#", "$"])+> + <(~["@"])+> } void Global_define() : @@ -188,11 +195,7 @@ void Interface_clusters() : void Happens_before() : {} { - - - "(" ")" "->" - - + (Conditional_interface() "->" Conditional_interface())+ } void Interface() : diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/SpecConstruct.java b/src/edu/uci/eecs/specCompiler/specExtraction/SpecConstruct.java new file mode 100644 index 0000000..84249e8 --- /dev/null +++ b/src/edu/uci/eecs/specCompiler/specExtraction/SpecConstruct.java @@ -0,0 +1,44 @@ +package edu.uci.eecs.specCompiler.specExtraction; + +import java.io.File; + +/** + *

+ * This class represents the plain context for each specification construct. + * Besides, it also stores some useful information such as interfaceDeclBody, + * which is used in @Interface construct to wrap and rename interface calls. + *

+ * @author peizhaoo + * + */ +public class SpecConstruct { + public enum ConstructType { + GLOBAL, INTERFACE, POTENTIAL_CP, CP_DEFINE, CP_DEFINE_CHECK + }; + public ConstructType type; + public final String plainText; + public final File file; + public final int beginLineNum; + public final int endLineNum; + public final String interfaceDeclBody; + + public SpecConstruct(ConstructType type, String plainText, File file, + int beginLineNum, int endLineNum) { + this.type = type; + this.plainText = plainText; + this.file = file; + this.beginLineNum = beginLineNum; + this.endLineNum = endLineNum; + this.interfaceDeclBody = ""; + } + + public SpecConstruct(ConstructType type, String plainText, File file, + int beginLineNum, int endLineNum, String interfaceDeclBody) { + this.type = type; + this.plainText = plainText; + this.file = file; + this.beginLineNum = beginLineNum; + this.endLineNum = endLineNum; + this.interfaceDeclBody = interfaceDeclBody; + } +} diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java b/src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java new file mode 100644 index 0000000..ddd25bc --- /dev/null +++ b/src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java @@ -0,0 +1,103 @@ +package edu.uci.eecs.specCompiler.specExtraction; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.LineNumberReader; +import java.util.ArrayList; + +/** + *

+ * This class represents the specification extractor of the specification. The + * main function of this class is to read C/C++11 source files and extract the + * corresponding specification out, and remember its location, including the + * file name and the line number, to help the code generation process. + *

+ * @author peizhaoo + * + */ +public class SpecExtractor { + private ArrayList _constructs; + private StringBuilder _extractedSpecText; + + private enum State { + Neutral, HeadParsed, BeginParsed, EndParsed + } + + private State _curState; + + private StringBuilder _potentialConstruct; + private int _startLine, _endLine; + + SpecExtractor() { + _constructs = new ArrayList(); + _extractedSpecText = new StringBuilder(); + _curState = State.Neutral; + _potentialConstruct = new StringBuilder(); + } + + /** + *

+ * Given a list of files, it scans each file and add found SpecConstrcut + * to the _constructs list. + *

+ * @param files + */ + private void extract(File[] files) { + for (int i = 0; i < files.length; i++) + extract(files[i]); + } + + private void extract(File file) { + try { + LineNumberReader reader = new LineNumberReader(new FileReader(file)); + String prevLine = "", curLine; + ArrayList text; + while ((curLine = reader.readLine()) != null) { + + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void parseHead(String prevLine, String line) { + assert (_curState == State.Neutral); + + // "\" is the C/C++ line break. If the previous line ends with "\", + // it may be part of a string literal. + if (prevLine.endsWith("\\")) + return; + String newLine = trimBeginningSpace(line); +// if (newLine.startsWith("/**") && +// (newLine.length() == 3 || + } + + private void parseBegin(String line) { + + } + + private void parseEnd(String line) { + + } + + private void parseTail(String line) { + + } + + private String trimBeginningSpace(String line) { + int i; + for (i = 0; i < line.length(); i++) { + char ch = line.charAt(i); + if (ch == ' ' || ch == '\t') + i++; + else + break; + } + return line.substring(i); + } +} -- 2.34.1