parsec

Haskell Parsec combinator 'many' is applied to a parser that accepts an empty string

雨燕双飞 提交于 2019-12-05 11:45:30
import Text.ParserCombinators.Parsec delimiter :: Parser () delimiter = do char '|' return () <?> "delimiter" eol :: Parser () eol = do oneOf "\n\r" return () <?> "end of line" item :: Parser String item = do entry <- manyTill anyChar (try eol <|> try delimiter <|> eof) return entry items :: Parser [String] items = do result <- many item return result When I run parseTest items "a|b|c" with the code above I get the following error: *** Exception: Text.ParserCombinators.Parsec.Prim.many: combinator 'many' is applied to a parser that accepts an empty string. I believe it has something to do with

Cannot compute minimal length of a parser - uu-parsinglib in Haskell

旧巷老猫 提交于 2019-12-05 10:02:47
Lets see the code snippet: pSegmentBegin p i = pIndentExact i *> ((:) <$> p i <*> ((pEOL *> pSegment p i) <|> pure [])) if I change this code in my parser to: pSegmentBegin p i = do pIndentExact i ((:) <$> p i <*> ((pEOL *> pSegment p i) <|> pure [])) I've got an error: canot compute minmal length of a parser due to occurrence of a moadic bind, use addLength to override I thought the above parser should behave the same way. Why this error can occur? EDIT The above example is very simple (to simplify the question) and as noted below it is not necessary to use do notation here, but the real case

Haskell Parsec Parser for Encountering […]

时光怂恿深爱的人放手 提交于 2019-12-05 07:55:44
I'm attempting to write a parser in Haskell using Parsec. Currently I have a program that can parse test x [1,2,3] end The code that does this is given as follows testParser = do { reserved "test"; v <- identifier; symbol "["; l <- sepBy natural commaSep; symbol "]"; p <- pParser; return $ Test v (List l) p } <?> "end" where commaSep is defined as commaSep = skipMany1 (space <|> char ',') Now is there some way for me to parse a similar statement, specifically: test x [1...3] end Being new to Haskell, and Parsec for that matter, I'm sure there's some nice concise way of doing this that I'm just

Custom whiteSpace using Haskell Parsec

坚强是说给别人听的谎言 提交于 2019-12-05 06:20:58
I would like to use Parsec's makeTokenParser to build my parser, but I want to use my own definition of whiteSpace . Doing the following replaces whiteSpace with my definition, but all the lexeme parsers still use the old definition (e.g. P.identifier lexer will use the old whiteSpace). ... lexer :: P.TokenParser () lexer = l { P.whiteSpace = myWhiteSpace } where l = P.makeTokenParser myLanguageDef ... Looking at the code for makeTokenParser I think I understand why it works this way. I want to know if there are any workarounds to avoid completely duplicating the code for makeTokenParser ?

The type signature of Parsec function 'parse' and the class 'Stream'

谁说我不能喝 提交于 2019-12-05 01:29:31
What does the constraint (Stream s Identity t) mean in the following type declaration? parse :: (Stream s Identity t) => Parsec s () a -> SourceName -> s -> Either ParseError a What is Stream in the following class declaration, what does it mean. I'm totally lost. class Monad m => Stream s m t | s -> t where When I use Parsec, I get into a jam with the type-signatures ( xxx :: yyy ) all the time. I always skip the signatures, load the src into ghci, and then copy the type-signature back to my .hs file. It works, but I still don't understand what all these signatures are. EDIT: more about the

Trivial parsec example produces a type error

瘦欲@ 提交于 2019-12-04 23:03:55
I'm trying to get this trivial parsec code to compile import Text.Parsec simple = letter but I keep getting this error No instance for (Stream s0 m0 Char) arising from a use of `letter' Possible fix: add an instance declaration for (Stream s0 m0 Char) In the expression: letter In an equation for `simple': simple = letter fuz I think you have ran against the monomorphism restriction . This restriction means: If a variable is declared with no explicit arguments, its type has to be monomorphic. This forces the typechecker to pick a particular instance of Stream , but it can't decide. There are

User state in Parsec

萝らか妹 提交于 2019-12-04 17:23:19
问题 I'm parsing an expression using Parsec and I want to keep track of variables in these expressions using the user state in Parsec. Unfortunately I don't really get how to do it. Given the following code: import Data.Set as Set inp = "$x = $y + $z" data Var = V String var = do char '$' n <- many1 letter let v = Var n -- I want to modify the set of variables here return v parseAssignment = ... -- parses the above assignment run = case runIdentity $ runParserT parseAssignment Set.empty "" inp of

What is the meaning of Parsec String () (String,String)?

こ雲淡風輕ζ 提交于 2019-12-04 09:51:20
I understand the Parsec modules' parse function, which takes a rule argument, an error message, and an input string: parse rule text = Parsec.parse rule "(source)" text However, I don't understand the meaning of Parsec.Parsec , or how it's different from Parsec.ParsecT . Why do type signatures of custom parsers use this name? For example, in the following code snippet taken from this blogpost , myParser :: Parsec.Parsec String () (String,String) myParser = do letters <- Parsec.many1 Parsec.letter Parsec.spaces digits <- Parsec.many1 Parsec.digit return (letters,digits) what does Parsec.Parsec

Matching bytestrings in Parsec

夙愿已清 提交于 2019-12-04 09:26:56
I am currently trying to use the Full CSV Parser presented in Real World Haskell . In order to I tried to modify the code to use ByteString instead of String , but there is a string combinator which just works with String . Is there a Parsec combinator similar to string that works with ByteString , without having to do conversions back and forth? I've seen there is an alternative parser that handles ByteString : attoparsec , but I would prefer to stick with Parsec, since I'm just learning how to use it. I'm assuming you're starting with something like import Prelude hiding (getContents,

attoparsec or parsec in haskell

一笑奈何 提交于 2019-12-04 07:27:06
问题 I have to parse some files and convert them to some predefined datatypes. Haskell seems to be providing two packages for that: attoparsec parsec What is the difference between the two of them and which one is better suited for parsing a text file according to some rules? 回答1: Parsec Parsec is good for "user-facing" parsers: things where you have a bounded amount of input but error messages matter. It's not terribly fast, but if you have small inputs this shouldn't matter. For example, I would