Scala Parboiled 2 currying up some rules

600 views Asked by At

I'd like to create some helper rules that take one rule and add some features to it. For example enforcing that string literals need to be quoted, or adding token position tracking to the token rules / ADT's.

I tried the following syntax (and quite a few permutations).

  def quoted[T](rl: Rule1[T]) = rule {
    '"' ~ rl ~ '"'
  }

It compiles fine but as soon as I wire it up --e.g.,

  def NodeObjPathEntry: Rule1[CNodeObjPathEntry] = rule {
    WhiteSpace ~ quoted(IdentifierStringUnwrapped) ~ ':' ~ (NodeObjArray | NodeObjObj) ~> CNodeObjPathEntry
  }

With the sub-rules :

def IdentifierStringUnwrapped: Rule1[String] = rule {
    clearSB() ~ IdentifierChars ~ push(sb.toString)   
}

 def IdentifierChars = rule {
    Alpha ~ appendSB() ~ zeroOrMore(AlphaNum ~ appendSB())
  }

I get Illegal rule call: quoted[this.String](this.IdentifierStringUnwrapped)

I could commit to an alternative approach: mix in the primitive token parsers, and then create the variants I need. But I really wanna figure out what is going on.

1

There are 1 answers

0
Alexey Romanov On

This seems to be the issue described under Meta-rules. The solution described would look like

val IdentifierStringUnwrapped: () => Rule1[String] = () => rule { ... }
// same for other rules you want to apply quoted to

def quoted[T](rl: () => Rule1[T]) = rule {
  '"' ~ rl() ~ '"'
}

No change in NodeObjPathEntry.

Unfortunately, this didn't work for my parser, so I may be misunderstanding something.