Quantcast

About case classes

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

About case classes

Freewind
I see the example in github:

class JsonParser1 extends Parser {

  /**
   * These case classes form the nodes of the AST.
   */
  sealed abstract class AstNode
  case class ObjectNode(members: List[MemberNode]) extends AstNode
  case class MemberNode(key: String, value: AstNode) extends AstNode
  case class ArrayNode(elements: List[AstNode]) extends AstNode
  case class StringNode(text: String) extends AstNode
  case class NumberNode(value: BigDecimal) extends AstNode

Seems each case class have only one parameter, is it required?

Can I create a node with 2 parameters? e.g.

    case class DoctypeNode(name:String, version: String) extends AstNode

If it's possible, how to create it in rule actions ?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: About case classes

mathias
Administrator
Currently parboiled supports Scala action functions that take up to 7 parameters, which means that you directly create case class instance with up to seven members.
Note that there is no special support for case classes in parboiled!
It's Scalas nice case class features (in this case the implicitly created `apply` methods on the companion object) that make case class usage with parboiled so convenient.

Cheers,
Mathias

---
[hidden email]
http://www.parboiled.org

On 16.07.2011, at 17:27, Freewind [via parboiled users] wrote:

> I see the example in github:
>
> class JsonParser1 extends Parser {
>
>   /**
>    * These case classes form the nodes of the AST.
>    */
>   sealed abstract class AstNode
>   case class ObjectNode(members: List[MemberNode]) extends AstNode
>   case class MemberNode(key: String, value: AstNode) extends AstNode
>   case class ArrayNode(elements: List[AstNode]) extends AstNode
>   case class StringNode(text: String) extends AstNode
>   case class NumberNode(value: BigDecimal) extends AstNode
>
> Seems each case class have only one parameter, is it required?
>
> Can I create a node with 2 parameters? e.g.
>
>     case class DoctypeNode(name:String, version: String) extends AstNode
>
> If it's possible, how to create it in rule actions ?
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/About-case-classes-tp3174803p3174803.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: About case classes

Freewind
Thank you, I know how to use them now
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: About case classes

jqno
In reply to this post by mathias
Hi!

I am writing a small parser for various kinds of CSV files using Parboiled. (Basically, the same semantic data, but from different sources and therefore in different formats.) It turns out that the lines in one of those CSV's each have 9 parameters. So that's two more than the current limit of 7 :). What's the recommended course of action in this case?

I can define a couple of "partial" case classes, match the two halves of each line, and then merge them together. This is a serious option, but it doesn't appeal to me, because semantically, it doesn't really make sense to split up the data. It's a bit arbitrary: I'd be doing it just to work around a limitation of Parboiled.
Or I can submit a feature request to add a Rule8 and a Rule9 class. This also doesn't appeal to me, because it doesn't scale. What if I run into a case where I have 10 parameters in my CSV :)? One has to draw the line somewhere.
Maybe there's a third way I'm overlooking?


By the way, I've never had more fun writing parsers than I do with Parboiled for Scala. Keep up the good work! Thanks!


Regards,
Jan
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: About case classes

mathias
Administrator
Jan,

the current limit of 7 only applies to the number of individual types the rule construction DSL can deal with within a single rule.
So you can't so this:

    def aRule = rule {
        A ~ B ~ C ~ D ~ E ~ F ~ G ~ H ~~> { (a, b, c, d, e, f, g, h) => ... }
    }

(where A through H are Rule1s), since it would require the rule to hold 8 different types.
However, you can use intermediary tuples to work around the limitation:

    def aRule: Rule1[MyType] = rule {
        A ~ B ~ C ~ D ~~> ((_,_,_,_)) ~ E ~ F ~ G ~ H ~~> { case ((a, b, c, d), e, f, g, h) => ... }
    }

With this technique there is essentially no limit to the number of types you can deal with in one rule.
However, there are two disadvantages:
- You have to explicitly list the individual parameters of the final function in the rule.
- Due to a limitation of the type inferencer you need to specify an explicit return type for the rule.

> By the way, I've never had more fun writing parsers than I do with Parboiled for Scala.

That is very nice to hear!
Thanks!

Cheers,
Mathias

---
[hidden email]
http://www.parboiled.org

On 09.12.2011, at 23:18, jqno [via parboiled users] wrote:

> Hi!
>
> I am writing a small parser for various kinds of CSV files using Parboiled. (Basically, the same semantic data, but from different sources and therefore in different formats.) It turns out that the lines in one of those CSV's each have 9 parameters. So that's two more than the current limit of 7 :). What's the recommended course of action in this case?
>
> I can define a couple of "partial" case classes, match the two halves of each line, and then merge them together. This is a serious option, but it doesn't appeal to me, because semantically, it doesn't really make sense to split up the data. It's a bit arbitrary: I'd be doing it just to work around a limitation of Parboiled.
> Or I can submit a feature request to add a Rule8 and a Rule9 class. This also doesn't appeal to me, because it doesn't scale. What if I run into a case where I have 10 parameters in my CSV :)? One has to draw the line somewhere.
> Maybe there's a third way I'm overlooking?
>
>
> By the way, I've never had more fun writing parsers than I do with Parboiled for Scala. Keep up the good work! Thanks!
>
>
> Regards,
> Jan
>
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/About-case-classes-tp3174803p3574258.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.
> NAML

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: About case classes

jqno
Hi Matthias,

Thanks for your quick reply! Your trick indeed works, thanks. It didn't occur to me that I could also use a tuple this way.

Regards,
Jan
Loading...