parsec

How to parse an Integer with parsec

£可爱£侵袭症+ 提交于 2019-12-10 14:14:06
问题 I was expecting to find a function integer :: Stream s m Char => ParsecT s u m Integer or maybe even natural :: Stream s m Char => ParsecT s u m Integer in the standard libraries, but I did not find one. What is the standard way of parsing plain natural numbers directly to an Integer ? 回答1: Here is what I often do is to use the expression read <$> many1 digit which can have type Stream s m Char => ParsecT s u m Integer (or simply Parser Integer ). I don’t like the use of the the partial

How to retrieve value from optional parser in Parsec?

[亡魂溺海] 提交于 2019-12-10 13:14:23
问题 Sorry if it's a novice question - I want to parse something defined by Exp ::= Mandatory_Part Optional_Part0 Optional_Part1 I thought I could do this: proc::Parser String proc = do { ;str<-parserMandatoryPart ;str0<-optional(parserOptionalPart0) --(1) ;str1<-optional(parserOptionalPart1) --(2) ;return str++str0++str1 } I want to get str0/str1 if optional parts are present, otherwise, str0/str1 would be "". But (1) and (2) won't work since optional() doesn't allow extracting result from its

How to use Parsers from Aeson with IO

匆匆过客 提交于 2019-12-10 10:15:16
问题 I have data types with many fields that, if not being manually specified by a JSON configuration file, should be randomly set. I am using Aeson to parse the config file. What is the best way to do this? Currently, I am setting values equal to some impossible value, and then later checking for said value to edit. data Example = Example { a :: Int, b :: Int } default = Example 1 2 instance FromJSON Example where parseJSON = withObject "Example" $ \v -> Example <$> (v .: "a" <|> return (a

Haskell Parsec, adapting oneOf to [String]

做~自己de王妃 提交于 2019-12-10 03:34:38
问题 I'm going through the Write yourself a scheme in 48 hours tutorial. symbol :: Parser Char symbol = oneOf "!#$%&|*+-/:<=>?@^_~" This is great for symbols, but what if I have a list of keywords? (i.e. struct, int) can oneOf be adapted to lists? This is ideally what I want, depicted below. keywords :: Parser String keywords = oneOf ["struct","int",..etc] Or should I import Text.Parsec.Char and try to mapM string over the list of keywords? I'm attempting to tokenize and just wanted to know what

Using Parsec to parse regular expressions

。_饼干妹妹 提交于 2019-12-09 06:12:30
问题 I'm trying to learn Parsec by implementing a small regular expression parser. In BNF, my grammar looks something like: EXP : EXP * | LIT EXP | LIT I've tried to implement this in Haskell as: expr = try star <|> try litE <|> lit litE = do c <- noneOf "*" rest <- expr return (c : rest) lit = do c <- noneOf "*" return [c] star = do content <- expr char '*' return (content ++ "*") There are some infinite loops here though (e.g. expr -> star -> expr without consuming any tokens) which makes the

GHC cannot find libraries on fresh install of Haskell-Platform

旧街凉风 提交于 2019-12-09 04:30:35
I'm trying to learn how to use Haskell, but I've run into a little roadblock. I'm using MacOSX 10.12.6 and installed Haskell Platform using: $ brew cask install haskell-platform with no modifications. When I try to compile a program with parsec imported, Haskell-Platform can't find it: $ ghc -v test.hs ... package parsec-3.1.11-DPgnR92AWEaFOaixmwipet is unusable due to shadowed dependencies: mtl-2.2.1-19EL8AGBsN3DnnOhrC9xY3 text-1.2.2.2-EGUst8sqNAZCw1xLPcmcMH ... test.hs:2:1: error: Could not find module ‘Text.Parsec’ Locations searched: Text/Parsec.hs Text/Parsec.lhs Text/Parsec.hsig Text

Simply using parsec in python

笑着哭i 提交于 2019-12-08 21:23:24
问题 I'm looking at this library, which has little documentation: https://pythonhosted.org/parsec/#examples I understand there are alternatives, but I'd like to use this library. I have the following string I'd like to parse: mystr = """ <kv> key1: "string" key2: 1.00005 key3: [1,2,3] </kv> <csv> date,windspeed,direction 20190805,22,NNW 20190805,23,NW 20190805,20,NE </csv>""" While I'd like to parse the whole thing, I'd settle for just grabbing the <tags> . I have: >>> import parsec >>> tag_start

Parsec.Expr repeated Prefix with different priority

戏子无情 提交于 2019-12-08 20:03:54
问题 The documentation for Parsec.Expr.buildExpressionParser says: Prefix and postfix operators of the same precedence can only occur once (i.e. --2 is not allowed if - is prefix negate). However, I would like to parse such strings. Concretely, consider the following grammar: sentence: | identifier | "~" sentence | sentence & sentence | "!" sentence Where operator precedence is: "~" binds stronger than "&" binds stronger than "!" For example, I would like the sentence ! ~a & b to be parsed as ! (

Parsec: Consume all input

戏子无情 提交于 2019-12-08 16:00:53
问题 One common problem I have with Parsec is that it tends to ignore invalid input if it occurs in the "right" place. As a concrete example, suppose we have integer :: Parser Int , and I write expression = sepBy integer (char '+') (Ignore whitespace issues for a moment.) This correctly parses something like "123+456+789". However, if I feed it "123+456-789", it merrily ignores the illegal "-" character and the trailing part of the expression; I actually wanted an error message telling me about

GHC cannot find libraries on fresh install of Haskell-Platform

我的未来我决定 提交于 2019-12-08 07:24:50
问题 I'm trying to learn how to use Haskell, but I've run into a little roadblock. I'm using MacOSX 10.12.6 and installed Haskell Platform using: $ brew cask install haskell-platform with no modifications. When I try to compile a program with parsec imported, Haskell-Platform can't find it: $ ghc -v test.hs ... package parsec-3.1.11-DPgnR92AWEaFOaixmwipet is unusable due to shadowed dependencies: mtl-2.2.1-19EL8AGBsN3DnnOhrC9xY3 text-1.2.2.2-EGUst8sqNAZCw1xLPcmcMH ... test.hs:2:1: error: Could not