parsec

How to use Control.Monad.State with Parsec?

血红的双手。 提交于 2019-12-23 07:09:08
问题 I'm surprised that I could not find any info on this. I must be the only person having any trouble with it. So, let's say I have a dash counter. I want it to count the number of dashes in the string, and return the string. Pretend I gave an example that won't work using parsec's state handling. So this should work: dashCounter = do str <- many1 dash count <- get return (count,str) dash = do char '-' modify (+1) And indeed, this compiles. Okay, so I try to use it: :t parse dashCounter "" "----

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

无人久伴 提交于 2019-12-22 07:06:13
问题 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:

How to give a fail message to a given position in parsec

≯℡__Kan透↙ 提交于 2019-12-22 06:57:13
问题 I need to give a failure message to a given position in parsec. I tried by setting the position before giving an unexpected error message, but it didn't work: runParser ( do pos0 <- getPosition id <- many1 alphaNum if (id == reverse id) then return id else setPosition pos0 >> unexpected id eof ) () "" "abccbb" Gives back Left (line 1, column 7): unexpected end of input expecting letter or digit While the correct response is: unexpected abccbb expecting letter or digit It can be produced (with

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

♀尐吖头ヾ 提交于 2019-12-21 17:32:06
问题 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

In Parsec, is there a way to prevent lexeme from consuming newlines?

落爺英雄遲暮 提交于 2019-12-21 03:41:08
问题 All of the parsers in Text.Parsec.Token politely use lexeme to eat whitespace after a token. Unfortunately for me, whitespace includes new lines, which I want to use as expression terminators. Is there a way to convince lexeme to leave a new line? 回答1: No, it is not. Here is the relevant code. From Text.Parsec.Token: lexeme p = do{ x <- p; whiteSpace; return x } --whiteSpace whiteSpace | noLine && noMulti = skipMany (simpleSpace <?> "") | noLine = skipMany (simpleSpace <|> multiLineComment <?

Does F# have 'newtype' of Haskell?

拥有回忆 提交于 2019-12-20 14:45:11
问题 New Library: XParsec This question has lead to a stream-type-independent parsec implementation in F# 3.0 - inspired by FParsec, freed from CharStreams and simplified: http://corsis.github.com/XParsec/ In an FParsec-inspired stream-type-independent simple parsec implementation, I wonder how I could distinguish the following at the type level: parsers that consume a piece of stream parsers that work on the current position without moving ahead in the stream Specifically, how can I restrict in F

Full parser examples with parsec?

烂漫一生 提交于 2019-12-20 08:49:26
问题 I'm trying to make a parser for a simple functional language, a bit like Caml, but I seem to be stuck with the simplest things. So I'd like to know if there are some more complete examples of parsec parsers, something that goes beyond "this is how you parse 2 + 3". Especially function calls in terms and suchlike. And I've read "Write you a Scheme", but the syntax of scheme is quite simple and not really helping for learning. The most problems I have is how to use try , <|> and choice properly

Parsec <|> choice in parser, Error throws but does not go to next parser

穿精又带淫゛_ 提交于 2019-12-20 03:11:25
问题 I am learning haskell with Write yourself a scheme . I am currently trying to implement a char recognition in scheme. A char is #\<character> or #\<character-name> like #\a or #\ or #\space . So i wrote the following code : -- .. some code .. data LispVal = Atom String | List [LispVal] | DottedList [LispVal] LispVal | String String | Number Integer | Bool Bool | Char Char deriving Show -- .... More code ... parseChar :: Parser LispVal parseChar = liftM Char (parseSingleChar <|>

Parsec how to find “matches” within a string

独自空忆成欢 提交于 2019-12-19 16:52:02
问题 How can I use parsec to parse all matched input in a string and discard the rest? Example: I have a simple number parser, and I can find all the numbers if I know what separates them: num :: Parser Int num = read <$> many digit parse (num `sepBy` space) "" "111 4 22" But what if I don't know what is between the numbers? "I will live to be 111 years <b>old</b> if I work out 4 days a week starting at 22." many anyChar doesn't work as a separator, because it consumes everything. So how can I get

Recursive grammars in FParsec

限于喜欢 提交于 2019-12-18 12:24:52
问题 I've decided to check out FParsec, and tried to write a parser for λ expressions. As it turns out, eagerness makes recursive parsing difficult. How can I solve this? Code: open FParsec type λExpr = | Variable of char | Application of λExpr * λExpr | Lambda of char * λExpr let rec FV = function | Variable v -> Set.singleton v | Application (f, x) -> FV f + FV x | Lambda (x, m) -> FV m - Set.singleton x let Λ0 = FV >> (=) Set.empty let apply f p = parse { let! v = p return f v } let λ e = let