Advice on FParsec for handling whitespace
Asked Answered
B

3

5

I have the following subexpression to parse 'quotes' which have the following format

"5.75 @ 5.95"

I therefore have this parsec expression to parse it

let pquote x = (sepBy (pfloat) ((spaces .>> (pchar '/' <|>  pchar '@' )>>. spaces))) x

It works fine.. except when there is a trailing space in my input, as the separator expression starts to consume content.So I wrapped it around an attempt, which works and seems, from what I understand, more or less what this was meant to be.

let pquote x = (sepBy (pfloat) (attempt (spaces .>> (pchar '/' <|>  pchar '@' )>>. spaces))) x

As I dont know fparsec so well, I wonder if there are any better way to write this. it seems a bit heavy (while still being very manageable of course)

Build answered 4/5, 2012 at 15:56 Comment(0)
I
7
let s1 = "5.75         @             5.95              "
let s2 = "5.75/5.95   "
let pquote: Parser<_> =
    pfloat
    .>> spaces .>> skipAnyOf ['@'; '/'] .>> spaces
    .>>. pfloat
    .>> spaces

Notes:

  1. I've made spaces optional everywhere spaces skips any sequence of zero or more whitespaces, so there's no need to use opt - thanks @Daniel;
  2. type Parser<'t> = Parser<'t, UserState> - I define it this way in order to avoid "value restriction" error; you may remove it;
  3. Also, don't forget the following if your program may run on a system with default language settings having decimal comma: System.Threading.Thread.CurrentThread.CurrentCulture <- Globalization.CultureInfo.GetCultureInfo "en-US" this won't work, thanks @Stephan
  4. I would not use sepBy unless I have a value list of unknown size.
  5. If you don't really need the value returned (e.g. '@' characters), it is recommended to use skip* functions instead p* for performance considerations.

UPD added slash as separator

Incurrence answered 4/5, 2012 at 18:18 Comment(3)
your project is crazy. you are crazy. good to see some parsec used here, testimony of quality I guess.Build
spaces parses zero or more whitespaces--no need to use opt.Feverroot
@bytebuster The pfloat parser only accepts decimal points and then parses the number with the System.Globalization.CultureInfo.InvariantCulture, so I don't think you need to set the CurrentThread.CurrentCulture.Horne
F
4

I would probably do something like this, which returns float * float:

let ws = spaces
let quantity = pfloat .>> ws
let price = pfloat .>> ws
let quoteSep = pstring "@" .>> ws
let quote = quantity .>> quoteSep .>>. price //`.>> eof` (if final parser)

It's typical for each parser to consume trailing whitespace. Just make sure your top-level parser includes eof.

Feverroot answered 4/5, 2012 at 16:12 Comment(0)
T
1

Assuming that you could have more than two float in the input and '/' and '@' are delimiters:

let ws = spaces
let str_ws s = pstring s .>> ws
let float_ws = pfloat .>> ws
let pquote = sepBy float_ws (str_ws "/" <|> str_ws "@")

Talking about handling whitespaces, this section in FParsec tutorial is really helpful.

Tucci answered 4/5, 2012 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.