*
* @param readFile
* @return
*/
public String cleanNuSMV(String input) throws IOException {
InlineStringReader reader = new InlineStringReader(new StringReader(input)) {
@Override
protected void throwWarning(String message, String buffer) {
throw new RuntimeException("Warning message: " + message);
}
};
InlineStringWriter writer = new InlineStringWriter() {
@Override
protected void throwWarning(String message, String buffer) {
throw new RuntimeException("Warning message: " + message);
}
};
int c;
int prev = -1;
boolean inLTL = false;
while ((c = reader.read()) != -1) {
// skip \rs
if (c == '\r') continue;
if (Character.isWhitespace(c) /* && Character.isWhitespace(prev) */) {
// skip
} else if (c == '-' && reader.readAhead() == '-') {
if (prev != -1 && !Character.isWhitespace(writer.previous())) {
// we need to add whitespace before this comment
writer.write(' ');
}
// a comment
writer.write('-');
writer.enableWordwrap(false); // disable wordwrap for comment
while ((c = reader.read()) != -1) {
if (c == '\r')
continue;
// copy comment directly
writer.write(c);
if (c == '\n')
break;
}
writer.enableWordwrap(true); // re-enable wordwrap for comment
} else if (c == 'p' && !Character.isJavaIdentifierPart(prev) && reader.has(7) && reader.readAhead(6).equals("rocess") && !Character.isJavaIdentifierPart(reader.readAhead(7).charAt(6))) {
// 'process' keyword
writer.write(c);
writer.write(reader.read(6));
writer.write(' ');
prev = ' ';
} else if (c == 'M' && reader.has(6) && reader.readAhead(5).equals("ODULE")) {
// multiple modules will reduce the indent, since MODULE
// does not have an ending statement
if (writer.getIndentSize() != 0)
writer.indentDecrease();
// MODULE
writer.newLine();
writer.write(c);
writer.write(reader.read(5));
writer.write(' ');
} else if (c == 'V' && reader.has(3) && reader.readAhead(2).equals("AR")) {
// VAR
writer.newLine();
writer.newLine();
writer.write(c);
writer.write(reader.read(2));
writer.newLine();
// indent
writer.indentIncrease();
c = ' ';
} else if (c == 'A' && reader.has(6) && reader.readAhead(5).equals("SSIGN")) {
// unindent
writer.indentDecrease();
// ASSIGN
writer.newLine();
writer.write(c);
writer.write(reader.read(5));
writer.newLine();
// indent
writer.indentIncrease();
c = ' ';
} else if (c == 'F' && reader.has(8) && reader.readAhead(7).equals("AIRNESS")) {
// unindent
writer.indentDecrease();
// ASSIGN
writer.newLine();
writer.write(c);
writer.write(reader.read(7));
writer.newLine();
// indent
writer.indentIncrease();
c = ' ';
} else if (c == 'L' && reader.has(7) && reader.readAhead(6).equals("TLSPEC")) {
// unindent
writer.indentDecrease();
// LTLSPEC
writer.newLine();
writer.write(c);
writer.write(reader.read(6));
writer.newLine();
// indent
writer.indentIncrease();
c = ' ';
inLTL = true; // now in LTL space
} else if (c == 'c' && reader.has(4) && reader.readAhead(3).equals("ase") && Character.isWhitespace(reader.readAhead(4).charAt(3))) {
// starting a case statement
writer.write(c);
writer.write(reader.read(3));
writer.newLine();
// indent
writer.indentIncrease();
c = ' ';
} else if (c == 'e' && reader.has(4) && reader.readAhead(3).equals("sac") &&
(Character.isWhitespace(reader.readAhead(4).charAt(3)) || reader.readAhead(4).charAt(3) == ';')) {
// no newline, but decrease indent before writing 'esac;'
writer.indentDecrease();
// ending a case statement
writer.write(c);
writer.write(reader.read(3));
c = 'c';
} else if (needsWhitespaceBefore(c, reader, writer, inLTL) && !Character.isWhitespace(writer.previous())) {
// insert whitespace