Parsing method arguments with FParsec
Asked Answered
C

1

4

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.

Clockwise answered 26/8, 2011 at 3:0 Comment(0)
I
5

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
Indemnify answered 26/8, 2011 at 3:12 Comment(2)
That is not exactly what I am looking for, if I am not mistaken. sepBy will accept "a,b,c," and that would be considered invalid syntax in my language!Clockwise
You are right. I was looking at the wrong documentation description.Clockwise

© 2022 - 2024 — McMap. All rights reserved.