diff --git a/java/hop/SexpReader.java b/java/hop/SexpReader.java index 6f73a64..0177fe7 100644 --- a/java/hop/SexpReader.java +++ b/java/hop/SexpReader.java @@ -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); + } + } } }