Optional rules and their results

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

Optional rules and their results

Ragmaanir
Hi,

i want to parse method signatures like:
"method xyz()"
"method abc"

So the parenthesis and parameters are optional. I have defined the following rule:

def MethodDef = rule{ "method" ~+~ ID ~*~ optional(Params) ~*~ TypeDecl ~~> Ast.MethodDefinition }
def Params = rule{ "(" ~* ParamsList ~* ")" }

The '~*~', '~+~' and '~*' are my own methods for inserting whitespace-rules between rules.
The problem now is:
Params returns Ast.Parameters and Ast.MethodDefinition should store that result. But since Params
is optional i dont always have that result. What can i do to make Ast.MethodDefinition store null
when Params is not present in the input?

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

Re: Optional rules and their results

mathias
Administrator
The type signatures of the various methods involved should give you a hint as to your question.
I'm assuming that your rule method "ParamsList" returns a Rule1[Ast.Parameters], which should mean that "Params" returns a Rule1[Ast.Parameters] as well.

If this is the case the compiler will dispatch the call "optional(Params)" to this overload:

def optional[A](sub: Rule1[A]): Rule1[Option[A]] = ...

This means that "optional(Params)" returns a Rule1[Option[Ast.Parameters]], which should be exactly, what you want.
By using "Option[Ast.Parameters]" instead of a simple "Ast.Parameters" you get the idiomatic Scala version of a "nullable" type, without the risk of NPEs...

Does this answer your question?

Cheers,
Mathias

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

On 23.04.2011, at 14:35, Ragmaanir [via parboiled users] wrote:

> Hi,
>
> i want to parse method signatures like:
> "method xyz()"
> "method abc"
>
> So the parenthesis and parameters are optional. I have defined the following rule:
>
> def MethodDef = rule{ "method" ~+~ ID ~*~ optional(Params) ~*~ TypeDecl ~~> Ast.MethodDefinition }
> def Params = rule{ "(" ~* ParamsList ~* ")" }
>
> The '~*~', '~+~' and '~*' are my own methods for inserting whitespace-rules between rules.
> The problem now is:
> Params returns Ast.Parameters and Ast.MethodDefinition should store that result. But since Params
> is optional i dont always have that result. What can i do to make Ast.MethodDefinition store null
> when Params is not present in the input?
>
> Thanks
>
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/Optional-rules-and-their-results-tp2854877p2854877.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: Optional rules and their results

Ragmaanir
Yes, thanks!

Frohe Ostern
Loading...