Domanda

I am trying to implement a method arguments parser with FParsec.

I was wondering if there is some already implemented feature in FParsec itself that'd aid me on this purpose? I ask this as FParsec provides tooling when dealing with operator precedence, so there might be something for this too.


Parsing the opening and closing braces is pretty straight-forward. The headache lies in dealing with the following 3 cases that can happen:

Method arguments can consist of:

  • no arguments,
  • one argument,
  • several arguments (all comma separated). keep in mind the last argument cannot be preceded by a comma!

I already have some clues on how to implement this by myself in case there is not any built-in feature, namely with the <|> operator and stream copying, but I'd like to stay away from that kind of low level stuff if possible.

È stato utile?

Soluzione

I believe you want to use sepBy.

type AST =
| Arguments of AST list
| Argument of string * string

let parseArguments =
    spaces 
    >>. pchar '(' 
    >>. spaces 
    >>. sepBy parseArgument (pchar ',') 
    .>> spaces 
    .>> pchar ')' 
    |>> Arguments

Edited by devoured_elysium:

The above code is correct although it doesn't compile. I'll post here my compiling version, so that if anyone just wants to try out the code without further ado, they can do it.

type AST =
| Arguments of AST list
| Argument of string

let parseArguments =
    spaces 
    >>. pchar '(' 
    >>. spaces 
    >>. sepBy (many1Satisfy isLetter |>> Argument) (pchar ',')
    .>> spaces 
    .>> pchar ')'
    |>> Arguments

test parseArguments "(a,b,c)" //succeed
test parseArguments "(a,b,c,)" //fail
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top