f# - Translate Haskell parsec to FParsec -
how translate haskell code:
import text.parsercombinators.parsec((<|>), unexpected, lookahead, noneof, char) import control.monad(when) data bracketelement = bechar char | bechars string | becoll string | beequiv string | beclass string p_set_elem_char = c <- noneof "]" when (c == '-') $ atend <- (lookahead (char ']') >> return true) <|> (return false) when (not atend) (unexpected "a dash in wrong place in bracket") return (bechar c) to fparsec ? preferable way without monadic syntax provide performance.
thanks in advance, alexander.
sorry little misleading. corrected problem make haskell code compilable:
import text.parsercombinators.parsec((<|>), (<?>), unexpected, lookahead, noneof, char) import control.monad(when) import data.functor.identity import qualified text.parsec.prim pr -- | bracketelement internal module data bracketelement = bechar char | bechars string | becoll string | beequiv string | beclass string deriving show p_set_elem_char :: pr.parsect [char] u identity bracketelement p_set_elem_char = c <- noneof "]" when (c == '-') $ atend <- (lookahead (char ']') >> return true) <|> (return false) when (not atend) (unexpected "a dash in wrong place in bracket") return (bechar c) now possible reproduce *p_set_elem_char* computation.
i sincerely thank of helped me.
i made own approximation, unfortunately not functional be:
type bracketelement = bechar of char | bechars of string | becoll of string | beequiv of string | beclass of string let p_set_elem_char : parser<bracketelement, _> = fun stream -> let statetag = stream.statetag let reply = (noneof "]") stream let chr = reply.result let mutable reply2 = reply(bechar chr) if reply.status = error && statetag = stream.statetag reply2.status <- error reply2.error <- reply.error else if chr = '-' && stream.peek() <> ']' reply2.status <- error reply2.error <- messageerror ("a dash in wrong place in bracket") reply2
using bracketelement type in toyvo's answer, like
let pbechar : parser<_, unit> = let c = pchar '-' .>> followedbyl (pchar ']') "a dash in wrong place in bracket" <|> noneof "-]" c |>> bechar
Comments
Post a Comment