attoparsec

Multi-line *non* match with attoparsec

北战南征 提交于 2019-12-25 04:00:41
问题 I was playing around with parsing (PostgreSQL) logs which can have entries that are multi-line. 2016-01-01 01:01:01 entry1 2016-01-01 01:01:02 entry2a entry2b 2016-01-01 01:01:03 entry3 So - with a Perl or Python script I'd just grab the next line and if it wasn't starting with a timestamp append it to the previous log entry. What is a sensible way to approach this with attoparsec hooked up to io-streams ? I clearly want to do something with lookAhead and failing to match a timestamp but my

Parse recursive data with parsec

偶尔善良 提交于 2019-12-12 12:20:44
问题 import Data.Attoparsec.Text.Lazy import Data.Text.Lazy.Internal (Text) import Data.Text.Lazy (pack) data List a = Nil | Cons a (List a) list :: Text list = pack $ unlines [ "0" , "1" , "2" , "5" ] How can List Int parser coud be implemented to parse Cons 0 (Cons 1 (Cons 2 (Cons 5 Nil))) from list ? ps : pure parser without parsing a [Int] and converting it to List Int is preferable. 回答1: Like this: import Control.Applicative -- rest of imports as in question data List a = Nil | Cons a (List a

How do I make Attoparsec parser succeed without consuming (like parsec lookAhead)

坚强是说给别人听的谎言 提交于 2019-12-09 13:03:08
问题 I wrote a quick attoparsec parser to walk an aspx file and drop all the style attributes, and it's working fine except for one piece of it where I can't figure out how to make it succeed on matching > without consuming it. Here's what I have: anyTill = manyTill anyChar anyBetween start end = start *> anyTill end styleWithQuotes = anyBetween (stringCI "style=\"") (stringCI "\"") styleWithoutQuotes = anyBetween (stringCI "style=") (stringCI " " <|> ">") everythingButStyles = manyTill anyChar

Implementing skipWhile1 in attoparsec

血红的双手。 提交于 2019-12-08 02:47:25
问题 Attoparsec provides the function takeWhile1 that consumes at least one character. However, there is no analog for skipWhile . How can I implement this function skipWhile1 ? Note: This question intentionally shows no research effort as it was answered Q&A-Style. 回答1: Another possible implementation: import Control.Applicative skipWhile1 p = skip p *> skipWhile p This might actually be faster than @Uli's answer because takeWhile builds a result string, whereas skipWhile doesn't. Laziness might

Why do I see Partial results with attoparsec when I expect to see Failure?

回眸只為那壹抹淺笑 提交于 2019-12-06 18:27:58
问题 I'm a little confused by this behaviour of attoparsec. $ ghci > :m Data.Attoparsec.Text > :m + Data.Text > parse (string (pack "module")) (pack "mox") Partial _ > parse (string (pack "module")) (pack "moxxxx") Fail "moxxxx" [] "Failed reading: takeWith" > Why do I need addition characters present to trigger the Fail? Shouldn't it Fail as soon as the first "x" is encountered? 回答1: It's an implementation detail, a string parser doesn't finish before it knows whether there is enough input

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

Haskell: How to use attoparsec in order to read a nested list from a ByteString

为君一笑 提交于 2019-12-01 11:05:15
I have a text file (~ 300 MB large) with a nested list, similar to this one: [[4, 9, 11, 28, 30, 45, 55, 58, 61, 62, 63, 69, 74, 76, 77, 82, 87, 92, 93, 94, 95], [4, 9, 11, 28, 30, 45, 55, 58, 61, 62, 63, 69, 74, 76, 77, 82, 87, 92, 93, 94],[4, 9, 11, 28, 30, 45, 55, 58, 61, 62, 63, 69, 74, 76, 77, 82, 85, 87, 92, 93, 94, 95]] Here is my program to read the file into a haskell Integer list: import qualified Data.ByteString as ByteStr main :: IO () -- HOW to do the same thing but using ByteStr.readFile for file access? main = do fContents <- readFile filePath let numList = readNums fContents

Haskell: How to use attoparsec in order to read a nested list from a ByteString

妖精的绣舞 提交于 2019-12-01 08:37:40
问题 I have a text file (~ 300 MB large) with a nested list, similar to this one: [[4, 9, 11, 28, 30, 45, 55, 58, 61, 62, 63, 69, 74, 76, 77, 82, 87, 92, 93, 94, 95], [4, 9, 11, 28, 30, 45, 55, 58, 61, 62, 63, 69, 74, 76, 77, 82, 87, 92, 93, 94],[4, 9, 11, 28, 30, 45, 55, 58, 61, 62, 63, 69, 74, 76, 77, 82, 85, 87, 92, 93, 94, 95]] Here is my program to read the file into a haskell Integer list: import qualified Data.ByteString as ByteStr main :: IO () -- HOW to do the same thing but using ByteStr

Parsing Karva notation in haskell

旧时模样 提交于 2019-12-01 08:28:59
Karva notation is used in Gene Expression Programming to represent mathematical expressions. See here http://www.gene-expression-programming.com/Tutorial002.asp You create an expression tree by reading the off the gene and filling in nodes from left to right, top to bottom. So for example using the operators ( +, * ) and terminals (1,2,3,4,5,6) in "+*+1+2*3456" would evaluate to 39. How would I do this in haskell using attoparsec (or parsec)? karvaParser :: Parser Int karvaParser = ???????????? Prelude> parse karvaParser "+*+1+2*3456" Done 39 AndrewC (I've proved this is a linear time

Parsing Karva notation in haskell

[亡魂溺海] 提交于 2019-12-01 07:06:06
问题 Karva notation is used in Gene Expression Programming to represent mathematical expressions. See here http://www.gene-expression-programming.com/Tutorial002.asp You create an expression tree by reading the off the gene and filling in nodes from left to right, top to bottom. So for example using the operators ( +, * ) and terminals (1,2,3,4,5,6) in "+*+1+2*3456" would evaluate to 39. How would I do this in haskell using attoparsec (or parsec)? karvaParser :: Parser Int karvaParser = ??????????