fparsec

Parsing method arguments with FParsec

浪尽此生 提交于 2019-12-05 15:58:23
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

Parsing simple types in FParsec

浪子不回头ぞ 提交于 2019-12-05 01:37:46
问题 I'm trying to parse standard simple types (in the sense of lambda calculus) using FParsec, but I've having difficulty going from a Lex/Yacc style to that used in FParsec, specifically with respect to recursive definitions. Examples of the types I am trying to parse are: o o -> o (o -> o -> o) -> o And here is my attempt: type SType = | Atom | Arrow of SType * SType let ws = spaces let stype, styperef = createParserForwardedToRef() let atom = pchar 'o' .>> ws |>> (fun _ -> Atom) let arrow =

Parsing “x y z” with the precedence of multiply

坚强是说给别人听的谎言 提交于 2019-12-04 07:46:11
I'm trying to write a parser for the Mathematica language in F# using FParsec. I have written one for a MiniML that supports the syntax f x y = (f(x))(y) with high precedence for function application. Now I need to use the same syntax to mean f*x*y and, therefore, have the same precedence as multiply. In particular, x y + 2 = x*y + 2 whereas x y ^ 2 = x * y^2 . How can this be accomplished? As Stephan pointed out in a comment you can split the operator parser into two separate parsers and put your own parser in the middle for space-separated expressions. The following code demonstrates this:

F#, FParsec, and Updating UserState

泄露秘密 提交于 2019-12-04 04:42:10
问题 Okay, since my last question elicited no responses, I'm forging ahead in a different direction. Lol! I can't find any examples beyond the official documentation on managing user state, or accessing the results of a prior parser. N.b. This code does not compile. namespace MultipartMIMEParser open FParsec open System.IO type Header = { name : string ; value : string ; addl : (string * string) list option } type Content = Content of string | Post of Post list and Post = { headers : Header list ;

Parsing simple types in FParsec

匆匆过客 提交于 2019-12-03 15:28:50
I'm trying to parse standard simple types (in the sense of lambda calculus) using FParsec, but I've having difficulty going from a Lex/Yacc style to that used in FParsec, specifically with respect to recursive definitions. Examples of the types I am trying to parse are: o o -> o (o -> o -> o) -> o And here is my attempt: type SType = | Atom | Arrow of SType * SType let ws = spaces let stype, styperef = createParserForwardedToRef() let atom = pchar 'o' .>> ws |>> (fun _ -> Atom) let arrow = pipe2 (stype .>> (pstring "->" .>> ws)) stype (fun t1 t2 -> Arrow (t1,t2)) let arr = parse { let! t1 =

How to convert an FParsec parser to parse whitespace

扶醉桌前 提交于 2019-12-02 14:13:34
问题 I'm implementing a parser that treats comments as white space with FParsec. It seems like it requires a trivial parser conversion, but I don't yet know how to implement that. Here's the code I'm trying to get to type-check - let whitespaceTextChars = " \t\r\n" /// Read whitespace characters. let whitespaceText = many (anyOf whitespaceTextChars) /// Read a line comment. let lineComment = pchar lineCommentChar >>. restOfLine true /// Skip any white space characters. let skipWhitespace =

F#, FParsec, and Calling a Stream Parser Recursively

冷暖自知 提交于 2019-12-02 07:24:00
I'm developing a multi-part MIME parser using F# and FParsec. I'm developing iteratively, and so this is highly unrefined, brittle code--it only solves my first immediate problem. Red, Green, Refactor. I'm required to parse a stream rather than a string, which is really throwing me for a loop. Given that constraint, to the best of my understanding, I need to call a parser recursively. How to do that is beyond my ken, at least with the way I've proceeded thus far. namespace MultipartMIMEParser open FParsec open System.IO type private Post = { contentType : string ; boundary : string ; subtype :

How to convert an FParsec parser to parse whitespace

ぃ、小莉子 提交于 2019-12-02 04:38:50
I'm implementing a parser that treats comments as white space with FParsec. It seems like it requires a trivial parser conversion, but I don't yet know how to implement that. Here's the code I'm trying to get to type-check - let whitespaceTextChars = " \t\r\n" /// Read whitespace characters. let whitespaceText = many (anyOf whitespaceTextChars) /// Read a line comment. let lineComment = pchar lineCommentChar >>. restOfLine true /// Skip any white space characters. let skipWhitespace = skipMany (lineComment <|> whitespaceText) /// Skip at least one white space character. let skipWhitespace1 =

F#, FParsec, and Calling a Stream Parser Recursively, Second Take

泄露秘密 提交于 2019-12-02 03:48:10
Thank you for the replies to my first post and my second post on this project. This question is basically the same question as the first, but with my code updated according to the feedback received on those two questions. How do I call my parser recursively? I'm scratching my head and staring blankly at the code. I've no idea where to go from here. That's when I turn to stackoverflow. I've included in code comments the compile-time errors I'm receiving. One stumbling block may be my discriminated union. I've not worked with discriminated unions much, so I may be using mine incorrectly. The

F#, FParsec, and Updating UserState

点点圈 提交于 2019-12-01 22:10:59
Okay, since my last question elicited no responses, I'm forging ahead in a different direction. Lol! I can't find any examples beyond the official documentation on managing user state, or accessing the results of a prior parser. N.b. This code does not compile. namespace MultipartMIMEParser open FParsec open System.IO type Header = { name : string ; value : string ; addl : (string * string) list option } type Content = Content of string | Post of Post list and Post = { headers : Header list ; content : Content } type private UserState = { Boundary : string } with static member Default = {