scala-parser-combinators
scala-parser-combinators copied to clipboard
RegexParsers: skipWhitespaces when using implicit conversion from a character
(I wanted to lower the priority of this issue, but somehow wasn't allowed to do so in the bug filing form)
A RegexParsers parser should skip whitespaces. It does so by omitting them in advance of each parsing step (so for example the literal("bla") parser at first skips all the whitespaces and then parses the literal "bla").
A string s is implicitly converted to a literal(s) parser. So writing
class MyParser extends RegexParsers { def text = "first"|"second" }
will parse "first" or "second" with arbitrary whitespaces in front of them.
A character c is implicitly converted to accept(c). accept() is defined in Parsers and not in RegexParsers, so the whitespaced aren't skipped here.
class MyParser extends RegexParsers { def text = 'f'|'s' }
will parse "f" or "s" but only if there aren't whitespaces in front of it.
This is somehow inconsistent and it took me a long time to realize it when I tried to write a string parser like
def string = '"' ~ stringcharacters ~ '"'
I think RegexParsers should override the parsers from its parent class and also skip whitespaces in front of them.
Imported From: https://issues.scala-lang.org/browse/SI-6491?orig=1 Reporter: Sebastian Meßmer (smessmer) Affected Versions: 2.9.2
I just tried making a scanner and discovered this issue. I tried to work around it by creating a separate scanner object with different whitespace behavior but haven't been able to get this to work. Is there an intended way to handle whitespace in string/char literals?