impulse-org/pdb.values — pull request #28 by pieterhijma (2013-08-29 07:42:26+00:00)
Title: Bugfix regarding reading strings and whitespace.

This bug can be checked in the context of Rascal.

For the following input (in Rascal):

import ValueIO;
readTextValueString(#tuple[str,str], "\< \"f oo\",\"bar\"\>");

The result is:
<"foo","bar"> (instead of <"f oo","bar">)

The following input:
readTextValueString(#tuple[str,str], "\< \"f oo\", \"bar\"\>");

leads to:
|std:///ValueIO.rsc|(1304,199,<43,0>,<45,63>): Java("FactParseError","Unexpected ")
at org.eclipse.imp.pdb.facts.io.StandardTextReader.unexpected(|file:///StandardTextReader.java|(0,0,<795,0>,<795,0>))
at org.eclipse.imp.pdb.facts.io.StandardTextReader.readValue(|file:///StandardTextReader.java|(0,0,<121,0>,<121,0>))
at org.eclipse.imp.pdb.facts.io.StandardTextReader.readFixed(|file:///StandardTextReader.java|(0,0,<740,0>,<740,0>))
at org.eclipse.imp.pdb.facts.io.StandardTextReader.readTuple(|file:///StandardTextReader.java|(0,0,<243,0>,<243,0>))
at org.eclipse.imp.pdb.facts.io.StandardTextReader.readValue(|file:///StandardTextReader.java|(0,0,<109,0>,<109,0>))
at org.eclipse.imp.pdb.facts.io.StandardTextReader.read(|file:///StandardTextReader.java|(0,0,<84,0>,<84,0>))
at org.rascalmpl.library.Prelude.readTextValueString(|file:///Prelude.java|(0,0,<3169,0>,<3169,0>))
at sun.reflect.NativeMethodAccessorImpl.invoke0(|file:///NativeMethodAccessorImpl.java|(0,0,<0,0>,<0,0>))
at readTextValueString(|stdin:///|(37,33,<1,37>,<1,70>))

In StandardTextReader, readValue() is called first, then readTuple() for the tuple. In readFixed() that reads until a '>' character, a stream.read() is done which should remove whitespace. It succeeds in doing this. However, because of the whitespace in front of the ", it will not conclude that it is a string it is trying to read. In line 818, r is not a ", but a whitespace character, resulting in inString being false. In 827, it removes all other whitespace. leaving r at character ", without the boolean inString to true. This results in whitespace being read in the following string.

In the second example, inString is 'flipped', which leads to the situation that whitespace is skipped, when it shouldn't. The result is that the whitespace is not removed in front of \"bar\", which sets the current input to whitespace. The method readValue() cannot handle this case, throwing an unexexpected error.

The fix is straightforward: First read whitespace, then try to determine whether we're in a 'inString' situation.