Case class, Parser Action and optional combinator

69 views
Skip to first unread message

Vladimir Ivanov

unread,
Jul 27, 2015, 4:41:04 PM7/27/15
to parboiled2.org User List
Hi, I have newbie question about parboiled2.

Having following simple case class and rule definitions:

case class Expr(value: Char)

def Expression = rule { optional(ch('+') | ch('*') ~> (Expr(_: Char))) }

I'm getting following error whenever I try to use `optional`, `zeroOrMore`, `oneOrMore` and `times` combinators with Parser Action to create case class (part of AST):  

[error]  The `optional`, `zeroOrMore`, `oneOrMore` and `times` modifiers can only be used on rules of type `Rule0`, `Rule1[T]` and `Rule[I, O <: I]`!
[error]   def Expression = rule  { optional(ch('+') | ch('*') ~> ((c: Char) => Expr(c))) }

I tried to move optional up in the rule tree or explicitly specify type parameters for Rule type with Shapeless HList (like Rule[Char :: HNil, Expr :: HNil]), but got the same result.

Could someone please help with this problem?

Thanks,
Vladimir

Alexander Myltsev

unread,
Jul 28, 2015, 4:57:40 AM7/28/15
to parboiled2.org User List, leste...@gmail.com
Hi,

You're missing `capture`. Try this one: 

def Expression = rule { optional(capture(CharPredicate("+*"))) ~> ((op: Option[String]) => ???) }


On Monday, 27 July 2015 23:41:04 UTC+3, Vladimir Ivanov wrote:
Hi, I have newbie question about parboiled2.

Having following simple case class and rule definitions:

case class Expr(value: Char)

def Expression = rule { optional(capture(ch('+') | ch('*') ~> (Expr(_: Char))) }

Vladimir Ivanov

unread,
Jul 30, 2015, 12:07:20 AM7/30/15
to parboiled2.org User List, alexande...@phystech.edu
Hi Alexander,

Many thanks for you reply and explanation! Works like a charm.

Vladimir

вторник, 28 июля 2015 г., 4:57:40 UTC-4 пользователь Alexander Myltsev написал:
Reply all
Reply to author
Forward
0 new messages