1 package edu.uci.eecs.specExtraction;
3 import java.util.ArrayList;
5 import edu.uci.eecs.specExtraction.SpecUtils.IntObj;
9 * This class represents a piece of code --- a list of strings (lines). Such a
10 * wrapper makes code extraction and generation easier.
18 // The list that represents all the lines of this code snippet
19 public final ArrayList<String> lines;
22 lines = new ArrayList<String>();
27 * It adds a line, which is very likely to be a simple C/C++ statement,
28 * which is a string lines of statement.
32 * A C/C++ line of statement
34 public void addLine(String line) {
40 * It adds a list of lines, which are very likely to be simple C/C++
45 * A list of C/C++ lines of statement
47 public void addLines(ArrayList<String> code) {
48 for (int i = 0; i < code.size(); i++)
49 lines.add(code.get(i));
54 * It adds a list of lines, which are very likely to be simple C/C++
59 * A Code object --- list of C/C++ lines of statement
61 public void addLines(Code code) {
66 * @return Whether this code snippet is empty
68 public boolean isEmpty() {
69 return lines.size() == 0;
74 * Align the set of code with an initial number of tabs. This basically
75 * tries to make the generated code more readable.
77 * @param initialTabsCnt
78 * The number of tabs that we want to put before the code
81 public void align(int initialTabsCnt) {
82 int tabLevel = initialTabsCnt;
83 IntObj idx = new IntObj(0);
84 alignHelper(idx, tabLevel, false);
89 * This is a helper function to align a list of code. Caller should
90 * initialize an IntObj with intial value "0", and pass with the initial tab
91 * level and pass the value of "false" to the noBraceKeyword.
94 * The IntObj that represents the current index of the code lines
96 * The tab level we are currently at
97 * @param noBraceKeyword
98 * Whether we just encountered a "noBraceKeyword"
100 private void alignHelper(IntObj idx, int tabLevel, boolean noBraceKeyword) {
101 for (; idx.getVal() < lines.size(); idx.inc()) {
102 String curLine = lines.get(idx.getVal());
103 String newLine = null;
104 // Return to the previous recursive level
105 if (closingBrace(curLine)) {
108 if ((noBraceKeyword && !keywordBrace(curLine) && !keywordNoBrace(curLine))) {
109 // Before returning, we just need to first add the line with the
111 newLine = makeTabs(tabLevel) + curLine;
112 lines.set(idx.getVal(), newLine);
116 newLine = makeTabs(tabLevel) + curLine;
117 lines.set(idx.getVal(), newLine);
119 if (keywordBrace(curLine)) {
121 alignHelper(idx, tabLevel + 1, false);
122 // Add the closing line
123 curLine = lines.get(idx.getVal());
124 newLine = makeTabs(tabLevel) + curLine;
125 lines.set(idx.getVal(), newLine);
126 } else if (keywordNoBrace(curLine)) { // No brace
128 alignHelper(idx, tabLevel + 1, true);
137 * Lines that starts with a key word and ends with ";".
143 private boolean closingBrace(String curLine) {
144 return curLine.endsWith("}");
149 * Lines that starts with a key word and ends with "{".
155 private boolean keywordBrace(String curLine) {
156 return (curLine.startsWith("for") || curLine.startsWith("ForEach")
157 || curLine.startsWith("if") || curLine.startsWith("else")
158 || curLine.startsWith("while") || curLine.startsWith("do"))
159 && curLine.endsWith("{");
164 * Lines that starts with a key word and ends with no "{" and no ";".
170 private boolean keywordNoBrace(String curLine) {
171 return (curLine.startsWith("for") || curLine.startsWith("ForEach")
172 || curLine.startsWith("if") || curLine.startsWith("else")
173 || curLine.startsWith("while") || curLine.startsWith("do"))
174 && !curLine.endsWith("{") && !curLine.endsWith(";");
180 * @return Generate a string whose content is a specific number (tabCnt) of
183 private String makeTabs(int tabCnt) {
185 for (int i = 0; i < tabCnt; i++)
190 public String toString() {
191 StringBuilder sb = new StringBuilder();
192 for (int i = 0; i < lines.size(); i++) {
193 sb.append(lines.get(i) + "\n");
195 return sb.toString();