Parsing method arguments with FParsec

浪尽此生 提交于 2019-12-05 15:58:23
ChaosPandion

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