Parsing Analysis Methods Using FParsec

I am trying to implement an argument method parser with FParsec.

I was wondering if there is any already implemented function in FParsec itself that will help me with this purpose? I ask about this since FParsec provides tools when working with operator priority, so there might be something for this.


The analysis of open and closing braces is quite straightforward. Headache is to consider the following three cases that may occur:

Method arguments may consist of:

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

I already have some tips on how to implement this myself if there is no built-in function, namely with the operator <| > and streaming copying, but I would like to stay away from this type of material if possible, low level.

+4
source share
1 answer

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 does not compile. I will post my compilation version here, so if someone just wants to try 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 
+4
source

Source: https://habr.com/ru/post/907966/


All Articles