1
Vote

Scanner.Recover Causes IndexOutOfRange (with fix)

description

When trying to recover from an error token when at the end of file, a call to Scanner.Recover will increment the PreviewPosition before first checking if it is at EOF. In some use cases it will cause an error later on.

Instead, the following could be used. I deleted the "src.PreviewPosition++;" that was outside the while loop and moved the one from the bottom of the loop to the top of the loop. I tested this to work at the end of a file and somewhere in the middle.
//Simply skip until whitespace or delimiter character
private bool Recover() {
  var src = Context.Source;
  while (!Context.Source.EOF()) {
    src.PreviewPosition++;
    if(_grammar.IsWhitespaceOrDelimiter(src.PreviewChar)) {
      src.Position = src.PreviewPosition;
      return true;
    }
  }
  return false; 
}

comments

rivantsov wrote Dec 17, 2012 at 4:31 PM

thank you, will look into this
Roman