Cope with extraneous whitespace in sexp reader

This commit is contained in:
Tony Garnock-Jones 2011-01-12 12:32:42 -05:00
parent 4df94ffe40
commit 074fd181c9
1 changed files with 31 additions and 25 deletions

View File

@ -81,31 +81,37 @@ public class SexpReader {
}
public Object _read(int c) throws IOException {
switch (c) {
case -1:
return null;
case '(':
return _readList();
case ')':
throw new SexpSyntaxError("Unexpected close-paren");
case '[':
byte[] hint = readSimpleString();
switch (_input.read()) {
case -1:
throw new SexpSyntaxError("End-of-stream between display hint and body");
case ']':
break;
default:
throw new SexpSyntaxError("Unexpected character after display hint");
}
byte[] body = readSimpleString();
return new SexpDisplayHint(hint, body);
default:
if (Character.isDigit(c)) {
return new SexpBytes(_readSimpleString(c - '0'));
} else {
throw new SexpSyntaxError("Unexpected character");
}
while (true) {
switch (c) {
case -1:
return null;
case '(':
return _readList();
case ')':
throw new SexpSyntaxError("Unexpected close-paren");
case '[':
byte[] hint = readSimpleString();
switch (_input.read()) {
case -1:
throw new SexpSyntaxError("End-of-stream between display hint and body");
case ']':
break;
default:
throw new SexpSyntaxError("Unexpected character after display hint");
}
byte[] body = readSimpleString();
return new SexpDisplayHint(hint, body);
default:
if (Character.isDigit(c)) {
return new SexpBytes(_readSimpleString(c - '0'));
} else if (Character.isWhitespace(c)) {
// Skip harmless (?) whitespace
c = _input.read();
continue;
} else {
throw new SexpSyntaxError("Unexpected character: " + c);
}
}
}
}