Parsing method arguments with FParsec

北战南征 提交于 2020-01-13 13:04:06

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/7199589/parsing-method-arguments-with-fparsec

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!