I have a working Scala analyzer, but the solution is not as clean as I would like. The problem is that some of the productions should consider spaces as part of the token, but βhigher-levelβ production processes should be able to ignore / skip spaces.
If I use a typical Scala parser template to extend lower-level parsers, then skipWhitespace parameters are inherited and things get messy very quickly.
I think it would be better for me not to use the extends approach, but rather have an instance of a low-level analyzer available in the higher-level parser class, but I'm not sure how to do this so that each instance will see only one stream of input characters.
Here is the part of the lowest level analyzer -
class VulgarFractionParser extends RegexParsers { override type Elem = Char override val whiteSpace = "".r
Then I continue as
class NumberParser extends VulgarFractionParser with Positional {
But at this point, NumberParser should explicitly handle spaces just like FractionParser. For NumberParser, it's still pretty manageable, but on the next level, I really want to be able to just define derivatives that use space as a delimiter, just like regular regexParser does.
An example would be something like:
IBM 33.33/ 1200.00 or IBM 33.33/33.50 1200.00
The second value sometimes has two parts separated by a "/" and sometimes has only one part with nothing after a slash (or even not containing a slash).
def bidOrAskPrice = ("$"?) ~> (bidOrAskPrice1 | bidOrAskPrice2 | bidOrAskPrice3) def bidOrAskPrice1 = number ~ ("/".r) ~ number ~ (SPACES) ^^ { case a ~ slash ~ b ~ sp1 => BidOrAsk(a,Some(b)) } def bidOrAskPrice2 = (number ~ "/" ~ (SPACES)) ^^ { case a ~ slash ~ sp => BidOrAsk(a,None) } def bidOrAskPrice3 = (number ~ (SPACES?)) ^^ { case a ~ sp => BidOrAsk(a , None)}