Warning: session_start(): open(/var/lib/php/sessions/sess_6v6t7dk37tnhlf4d33o1gdimka, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
package ai.d.ai07;
import drjava.util.F;
import drjava.util.ListUtil;
import drjava.util.StringUtil;
import structcom.sc03.PStringLit;
public class GroupingTest {
public static void main(String[] args) {
String poem =
"for each line in file:\n" +
" compare file \"/bla1/$file\" to file \"/bla2/$file\"";
String groupedPoem =
"[[for] [each] [line] [in] [file]:]\n" +
"[[ ][[compare] [file] [\"/bla1/$file\"] [to] [file] [\"/bla2/$file\"]]]";
group(poem, "lines");
group(poem, "linesWithIndent");
group(poem, "words");
group(poem, "stringLits");
group(poem, "words_skipGrouped");
String grouped = group(poem, "stringLits+words_skipGrouped+linesWithIndent");
System.out.println("Expected =>");
System.out.println(StringUtil.indent(2, groupedPoem));
}
private static String group(String poem, String operator) {
try {
System.out.println(operator + " =>");
String grouped = doGroup(poem, operator);
System.out.println(StringUtil.indent(2, grouped));
return grouped;
} catch (Throwable e) {
System.out.println(operator + " ERROR: " + e);
return "";
}
}
private static String doGroup(String poem, String operator) {
String[] split = operator.split("\\+");
if (split.length > 1) {
for (String op : split) poem = doGroup(poem, op);
return poem;
}
if (operator.equals("lines"))
return groupLines(poem);
if (operator.equals("linesWithIndent"))
return groupLinesWithIndent(poem);
if (operator.equals("words"))
return groupWords(poem);
if (operator.equals("stringLits"))
return groupStringLits(poem);
if (operator.equals("words_skipGrouped"))
return groupWords_skipGrouped(poem);
throw new RuntimeException("Unknown operator '" + operator + "'");
}
private static String groupLines(String poem) {
return StringUtil.join("\n", ListUtil.map(StringUtil.toLines(poem), new F() {
public String _(String s) {
return "[" + s + "]";
}
}));
}
private static String groupLinesWithIndent(String poem) {
return StringUtil.join("\n", ListUtil.map(StringUtil.toLines(poem), new F() {
public String _(String s) {
if (s.startsWith(" "))
return "[[ ][" + s.substring(2) + "]]";
else
return "[" + s + "]";
}
}));
}
private static String groupWords(String poem) {
return groupByPredicate(poem, new F() {
public Boolean _(Character c) {
return Character.isLetterOrDigit(c);
}
});
}
private static String groupWords_skipGrouped(String poem) {
return groupByPredicate_skipGrouped(poem, new F() {
public Boolean _(Character c) {
return Character.isLetterOrDigit(c);
}
});
}
private static String groupByPredicate(String poem, F pred) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < poem.length(); i++) {
char c = poem.charAt(i);
if (!pred._(c))
buf.append(c);
else {
int j = i+1;
while (j < poem.length() && pred._(poem.charAt(j)))
++j;
buf.append("[" + poem.substring(i, j) + "]");
i = j-1;
}
}
return buf.toString();
}
private static String groupByPredicate_skipGrouped(String poem, F pred) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < poem.length(); i++) {
char c = poem.charAt(i);
if (c == '[') {
int j = findEndOfGroup(poem, i); // TODO: This is unreliable
buf.append(poem.substring(i, j));
i = j-1;
} else if (!pred._(c))
buf.append(c);
else {
int j = i+1;
while (j < poem.length() && pred._(poem.charAt(j)))
++j;
buf.append("[" + poem.substring(i, j) + "]");
i = j-1;
}
}
return buf.toString();
}
private static int findEndOfGroup(String poem, int i) {
while (i < poem.length() && poem.charAt(i) != ']')
++i;
return i;
}
private static String groupStringLits(String poem) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < poem.length(); i++) {
char c = poem.charAt(i);
if (c == '"') {
PStringLit parser = new PStringLit();
parser.setText(poem, i);
parser.parse();
int j = parser.getIdx();
buf.append("[" + poem.substring(i, j) + "]");
i = j-1;
} else
buf.append(c);
}
return buf.toString();
}
}