I'm trying to modify an example from DSLs in Action.
Originally, this code was used to parse items followed by account.
lazy val order: Parser[Order] =
items ~ account_spec ^^ {
case i ~ a => Order(i, a)
}
The following text could be parsed with the above parser:
(100 IBM shares to buy at max 45) for account "A1234
------------- item ------------- ------ account ----
But, I'd like to add FOO and an optional not values to the parser:
lazy val order: Parser[Order] =
items <~ "FOO" ~> ("not"?) ~ account_spec ^^ {
case i ~ n ~ a => println(n); Order(i, a)
}
FOO must follow account and, optionally, not will follow.
Example:
(100 IBM shares to buy at max 45) FOO not for account "A1234
---------- item ------------------ --- --- ------ account ----
However, the above code gives me this compile-time error:
[WARNING] ....\OrderDsl.scala:19: error: constructor cannot be instantiated to
expected type;
[WARNING] found : ch8.trading.semantic.dsl.OrderDsl.~[a,b]
[WARNING] required: ch8.trading.semantic.dsl.AST.Items
[WARNING] case i ~ n ~ a => println(n); Order(i, a)
[WARNING] ^
How can I modify the case statement to support parsing an optional "not" value?