Questions tagged [parsec]

Parsec is an industrial-strength, monadic parser combinator library for Haskell.

Parsec is an industrial strength, monadic parser combinator library for Haskell. Parsec lets you construct parsers by combining higher-order Combinators to create larger expressions. Combinator parsers are written and used within the same programming language as the rest of the program. The parsers are first-class citizens of the language , unlike Happy parsers, which must be generated via a preprocessor.

More information about Parsec, including usage examples, can be found on the Parsec website.

609 questions
8
votes
4 answers

Haskell parsec parsing a string of items

I have a list that I need to parse where the all but the last element needs to be parsed by one parser, and the last element needs to be parsed by another parser. a = "p1 p1b ... p2" or a = "p2" Originally I tried parser = do parse1 <- many…
Chris
  • 483
  • 3
  • 14
8
votes
6 answers

Parser for Quoted string using Parsec

I want to parse input strings like this: "this is \"test \" message \"sample\" text" Now, I wrote a parser for parsing individual text without any quotes: parseString :: Parser String parseString = do char '"' x <- (many $ noneOf "\"") char…
Sibi
  • 47,472
  • 16
  • 95
  • 163
8
votes
2 answers

An easy way to change the type of Parsec user state?

I'm looking for an easy way to combine two parts of ParsecT code that have the same stream and monad, but different user state and outcome. Essentially a function like this would be nice: withUserState :: u -> ParsecT s u m a -> ParsecT s v m a The…
Jakob Runge
  • 2,287
  • 7
  • 36
  • 47
8
votes
1 answer

Haskell - Parsec testing with the help of QuickCheck

I'd like to write a tests for a Parsec parser. Here is the example of parser and data structure: data Event = Event { keyEvent :: String } deriving Show parseKey :: Parser Event parseKey = do char '<' k <- many1…
m0nhawk
  • 22,980
  • 9
  • 45
  • 73
8
votes
1 answer

Is there any way to make parsec report "shift-reduce" conflicts?

I'm playing around with parsec and realized that I had an ambiguous grammar. Obviously that's an error on my part, but I'm sort of used to yacc-style parser generators letting me know I'm being dumb. Parsec just eats characters in the order you give…
Hans
  • 2,230
  • 23
  • 23
7
votes
1 answer

why is parsecs "choice" combinator seemingly stuck on the first choice?

After looking at the CSV sample code in Real World Haskell, I've tried to build a little XML parser. But close tags error out with 'unexpected "/"' errors. Can you tell me why my "closeTag" parser doesn't work (or possibly isn't ever invoked)?…
nont
  • 9,322
  • 7
  • 62
  • 82
7
votes
3 answers

How to negate a parser with Parsec

I have a file with line endings “\r\r\n”, and use the parser eol = string "\r\r\n" :: Parser String to handle them. To get a list of the lines between these separators, I would like to use sepBy along with a parser that returns any text that would…
beardc
  • 20,283
  • 17
  • 76
  • 94
7
votes
1 answer

How do I improve QuickCheck and Parsec debugging?

I am using Haskell and Parsec to parse a file format. My parsing function looks something like: parseInput :: String -> Model parseInput input = ... data Model = Model { mNumV :: Int, mNumF :: Int, ... } In order to test this, I am using…
sdasdadas
  • 23,917
  • 20
  • 63
  • 148
7
votes
2 answers

Performance of uu-parsinglib compared to "try" in Parsec

Question I know Parsec and uu-parsinglib and I've written parsers in both of them. Recently I discovered, that there is a problem in uu-parsinglib, which could significantly affect its performance and I do not see a way to solve it. Lets consider…
Wojciech Danilo
  • 11,573
  • 17
  • 66
  • 132
7
votes
1 answer

Raise ParseError in Haskell/Parsec

What is the prefered way to raise errors (ParseError) in Parsec? I got some code inside a parser that performs a check and if the check fails a ParseError should be returned (i.e. Left ParseError when running parse).
finnsson
  • 4,037
  • 2
  • 35
  • 44
7
votes
3 answers

Difficulty getting a Parsec parser to skip spaces correctly

I'm new to Parsec (and to parsers in general), and I'm having some trouble with this parser I wrote: list = char '(' *> many (spaces *> some letter) <* spaces <* char ')' The idea is to parse lists in this format (I'm working up to…
Neil Forrester
  • 5,101
  • 29
  • 32
7
votes
2 answers

How to read exact N chars with Parsec?

I'm new to Haskell and Parsec. I wish to parse php-serialize format of string 's:numb:"string";' like s:12:"123";6789012"; where number is count of chars. So, function looks like: newtype PhpString = PhpString String pString :: GenParser Char st…
viorior
  • 1,783
  • 11
  • 16
7
votes
1 answer

Using Parsec to write a Read instance

Using Parsec, I'm able to write a function of type String -> Maybe MyType with relative ease. I would now like to create a Read instance for my type based on that; however, I don't understand how readsPrec works or what it is supposed to do. My best…
David
  • 8,275
  • 5
  • 26
  • 36
7
votes
2 answers

Why won't Parsec consider the right-hand side of my <|> alternative?

I’m trying to parse C++ code. Therefore, I need a context-sensitive lexer. In C++, >> is either one or two tokens (>> or > >), depending on the context. To make it even more complex, there is also a token >>= which is always the same regardless of…
user142019
6
votes
2 answers

Is it possible to express chainl1 using applicative?

Is it possible to express the chainl1 combinator from Parsec not using the Monad instance defined by parsec? chainl1 p op = do x <- p rest x where rest x = do f <- op y <- p rest (f x y) <|>…
ase
  • 13,231
  • 4
  • 34
  • 46