ASN1 parser problem with Rule0

83 views
Skip to first unread message

Simon Ochsenreither

unread,
Apr 23, 2014, 11:36:33 AM4/23/14
to parboil...@googlegroups.com
Hi,

I have been testing parboiled2 by writing an ASN1 parser.

Stuff seems to work fine, with for one issue:

[error] /home/soc/Entwicklung/typeproviders/macros/src/main/scala/de/oxnrtr/typeprovider/ASN1TypeProvider.scala:222: type mismatch;
[error]  found   : org.parboiled2.Rule[shapeless.HNil,shapeless.HList]
[error]  required: org.parboiled2.Rule0
[error]     (which expands to)  org.parboiled2.Rule[shapeless.HNil,shapeless.HNil]
[error]   BuiltinValue |
[error]                ^
[error] one error found


I have basically adding Rule0s whenever the compiler complained that a recursive definition needed a return type. It worked fine except for that case.

Here is the code: https://github.com/soc/typeproviders/blob/master/macros/src/main/scala/de/oxnrtr/typeprovider/ASN1TypeProvider.scala#L221

Any suggestions?

Thanks!

Simon

Mathias Doenitz

unread,
Apr 23, 2014, 11:42:32 AM4/23/14
to parboil...@googlegroups.com
Simon,

from the looks of it you don’t have any parser actions so far, right?

(A side comment, just because I just spotted it:
Instead of

EnumerationItem ~ zeroOrMore("," ~ EnumerationItem)

you can say

oneOrMore(EnumerationItem).separatedBy(‘,')

which is both shorter and more efficient.

Cheers,
Mathias

---
mat...@parboiled.org
http://www.parboiled.org
> --
> You received this message because you are subscribed to the Google Groups "parboiled2.org User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to parboiled-use...@googlegroups.com.
> Visit this group at http://groups.google.com/group/parboiled-user.
> To view this discussion on the web visit https://groups.google.com/d/msgid/parboiled-user/9f6d0e5f-513e-4b3c-9c29-2cb28a3d6271%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Mathias Doenitz

unread,
Apr 23, 2014, 11:48:19 AM4/23/14
to parboil...@googlegroups.com
Simon,

for some reason the compiler is unable to infer `Rule0` for `BuiltinValue` or `ReferencedValue`.
My guess is that the problem is in `BuiltinValue` (because of the large `|`).

I’d attach an explicit `Rule0` type annotations to `BuiltinValue` and see whether you can track down the problem by going down the “rule calling tree" to see, which sub-rule of `BuiltinValue` is the culprit.

Cheers,
Mathias

---
mat...@parboiled.org
http://www.parboiled.org

On 23 Apr 2014, at 17:36, Simon Ochsenreither <simon.och...@gmail.com> wrote:

Mathias Doenitz

unread,
Apr 23, 2014, 2:42:06 PM4/23/14
to parboil...@googlegroups.com
Simon,

one more thing I noticed when looking at your grammar again:
It doesn’t appear to contain any whitespace matching!

Remember that the rule

"COMPONENTS" ~ "OF" ~ Type

is identical to

"COMPONENTSOF" ~ Type

which I don’t think is what you want.
The best way to deal with whitespace in PEGs is usually to match them right after a terminal, e.g. like this

ws(“COMPONENTS") ~ ws(“OF") ~ Type

with

val WSP = CharPredicate(“\n\t")
def ws(s: String) = rule { s ~ zeroOrMore(WSP) }

Currently there is no facility to reduce the required boilerplate, so I just added this ticket:
https://github.com/sirthias/parboiled2/issues/71

Cheers,
Mathias

---
mat...@parboiled.org
http://www.parboiled.org

On 23 Apr 2014, at 17:36, Simon Ochsenreither <simon.och...@gmail.com> wrote:

Simon Ochsenreither

unread,
Apr 23, 2014, 3:28:08 PM4/23/14
to parboil...@googlegroups.com
Hi Mathias,


from the looks of it you don’t have any parser actions so far, right?

correct. I'm still thinking about how to pull off the things I imagine (or if it is possible at all, who knows...),


(A side comment, just because I just spotted it:
Instead of

    EnumerationItem ~ zeroOrMore("," ~ EnumerationItem)

you can say

    oneOrMore(EnumerationItem).separatedBy(‘,')

which is both shorter and more efficient.

Yes, thanks! I planned doing that, but didn't want to change the mostly line-by-line translation before I got a small test suite to verify that I didn't break anything.


It doesn’t appear to contain any whitespace matching!

Oh, right! I always forget this, thanks! :-)

Mathias Doenitz

unread,
Apr 23, 2014, 5:15:58 PM4/23/14
to parboil...@googlegroups.com
> correct. I'm still thinking about how to pull off the things I imagine (or if it is possible at all, who knows…),

Ok.
As long as you think of your rule structure as operating on a typed stack pretty much anything is possible… :)
Let us know, if you think we can help.
> --
> You received this message because you are subscribed to the Google Groups "parboiled2.org User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to parboiled-use...@googlegroups.com.
> Visit this group at http://groups.google.com/group/parboiled-user.
> To view this discussion on the web visit https://groups.google.com/d/msgid/parboiled-user/ce074010-7520-4de0-9002-2b92796c38a1%40googlegroups.com.

Simon Ochsenreither

unread,
Apr 29, 2014, 12:57:02 PM4/29/14
to parboil...@googlegroups.com
Hi Mathias,

thanks for your help!

It looks like the real culprit was this line of code:

  def CharSyms = CharsDefn ~ zeroOrMore(",", CharsDefn)

which should have been

  def CharSyms = CharsDefn ~ zeroOrMore("," ~ CharsDefn)

instead. (I'm wondering if it's possible to make that error impossible to make?)

Thanks and bye!

Mathias Doenitz

unread,
Apr 30, 2014, 6:53:29 AM4/30/14
to parboil...@googlegroups.com
Simon,

ahh, that makes sense.

`zeroOrMore` is defined like this

def zeroOrMore(r: Rule[…]): Rule[…]

and, in a Parser, there are currently these implicits in scope:

/**
* Matches a character and pushes the associated value.
*/
implicit def charAndValue[T](t: (Char, T)): RuleN[...] = ...

/**
* Matches a string and pushes the associated value.
*/
implicit def stringAndValue[T](t: (String, T)): RuleN[...] = ...

which masked the problem in your case.

Looking at your use case I am now inclined to simply remove the `charAndValue` and `stringAndValue` helpers completely.
I am not even sure anymore why I put them in in the first place… :)

With these things gone you would have gotten the proper compiler error.

Thanks for reporting!
> --
> You received this message because you are subscribed to the Google Groups "parboiled2.org User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to parboiled-use...@googlegroups.com.
> Visit this group at http://groups.google.com/group/parboiled-user.
> To view this discussion on the web visit https://groups.google.com/d/msgid/parboiled-user/747595be-00f9-4bd1-9fa5-38f44ae43597%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages