Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7178669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:58:01+00:00 2026-05-28T16:58:01+00:00

I have a simple attoparsec-based pdf parser . It works fine until used with

  • 0

I have a simple attoparsec-based pdf parser. It works fine until used with iteratee.
When size of input exceeds buffer size.

import qualified Data.ByteString as BS
import qualified Data.Iteratee as I
import qualified Data.Attoparsec as P
import qualified Data.Attoparsec.Iteratee as P
import System.Environment (getArgs)
import Control.Monad

import Pdf.Parser.Value

main :: IO ()
main = do
  [i] <- getArgs
  liftM (P.parseOnly parseValue) (BS.readFile i) >>= print  -- works
  I.fileDriverRandomVBuf 2048 (P.parserToIteratee parseValue) i >>= print  -- works
  I.fileDriverRandomVBuf 1024 (P.parserToIteratee parseValue) i >>= print  -- DOES NOT works!!!

Input:

<< /Annots [ 404 0 R 547 0 R ] /ArtBox [ 0.000000 0.000000 612.000000 792.000000 ] /BleedBox [ 0.000000 0.000000 612.000000 792.000000 ] /Contents [ 435 0 R 436 0 R 437 0 R 444 0 R 448 0 R 449 0 R 450 0 R 453 0 R ] /CropBox [ 0.000000 0.000000 612.000000 792.000000 ] /Group 544 0 R /MediaBox [ 0.000000 0.000000 612.000000 792.000000 ] /Parent 239 0 R /Resources << /ColorSpace << /CS0 427 0 R /CS1 427 0 R /CS2 428 0 R >> /ExtGState << /GS0 430 0 R /GS1 431 0 R /GS2 469 0 R /GS3 475 0 R /GS4 439 0 R /GS5 480 0 R /GS6 485 0 R /GS7 491 0 R /GS8 497 0 R >> /Font << /C2_0 447 0 R /T1_0 421 0 R /T1_1 422 0 R /T1_2 423 0 R /T1_3 424 0 R /T1_4 425 0 R /T1_5 426 0 R /T1_6 438 0 R >> /ProcSet [ /PDF /Text /ImageC /ImageI ] /Properties << /MC0 << /Metadata 502 0 R >> >> /XObject << /Fm0 451 0 R /Fm1 504 0 R /Fm2 513 0 R /Fm3 515 0 R /Fm4 517 0 R /Fm5 526 0 R /Fm6 528 0 R /Fm7 537 0 R /Fm8 539 0 R /Im0 540 0 R /Im1 541 0 R /Im2 452 0 R /Im3 542 0 R /Im4 543 0 R >> >> /Rotate 0 /StructParents 1 /TrimBox [ 0.000000 0.000000 612.000000 792.000000 ] /Type /Page >>

So, the parser works without iteratee, works with big enough chunks, but doesn’t work with smaller chunks. Bug in iteratee? In attoparsec-iteratee? In my code? Is there any workaround? It is a really urgent issue for me.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:58:02+00:00Added an answer on May 28, 2026 at 4:58 pm

    Edit 2: I created a new parser in Pdf/Parser/Value

    dictOrStream :: Parser PdfValue
    dictOrStream = do
      dict <- parseDict
      P.skipSpace
      let s1 = do
                P.string $ fromString "stream"
                content <- P.manyTill P.anyWord8 $ P.endOfLine >> P.string (fromString "endstream")
                return $ PdfValStream (PdfStream dict (BS.pack content))
      s1 <|> return (PdfValDict dict)
    

    then used this parser in parseValue. This works for all your cases. I don’t know why choice fails to backtrack properly, maybe an attoparsec bug?

    Edit: I notice that, if I replace your top-level parseValue with parseDict, it works. It also works if I remove parseStream from the choices in parseValue. I think attoparsec has committed to “parseStream” after the completion of the top-level dictionary, therefore it’s expecting more input (a space, the “stream” token, etc.) leading to this error. At this point there’s an ambiguity between these two parsing options that you’ll need to resolve. I don’t know why it works properly when the entire input is available; I would expect an error to be reported as when your parser is fed chunks.

    As of now, I suspect a bug in either your code, or possibly attoparsec. I ran the following test by manually reading bytestring chunks and feeding it to your attoparsec parser:

    *Main System.IO> h <- openFile "test.pdf" ReadMode
    *Main System.IO Data.ByteString> let hget = hGetSome h 1024
    *Main System.IO Data.ByteString> b <- hget
    *Main System.IO Data.ByteString> let r = P.parse parseValue b
    *Main System.IO Data.ByteString> r
    Partial _
    *Main System.IO Data.ByteString> b <- hget
    *Main System.IO Data.ByteString> let r' = P.feed r b
    *Main System.IO Data.ByteString> r'
    Partial _
    *Main System.IO Data.ByteString> b <- hget
    *Main System.IO Data.ByteString> Data.ByteString.length b
    0
    *Main System.IO Data.ByteString> let r'2 = P.feed r' b
    *Main System.IO Data.ByteString> r'2
    Fail "<< /Annots [ 404 0 R 547 0 R ] /ArtBox [ 0.000000 0.000000 612.000000 792.000000 ] /BleedBox [ 0.000000 0.000000 612.000000 792.000000 ] /Contents [ 435 0 R 436 0 R 437 0 R 444 0 R 448 0 R 449 0 R 450 0 R 453 0 R ] /CropBox [ 0.000000 0.000000 612.000000 792.000000 ] /Group 544 0 R /MediaBox [ 0.000000 0.000000 612.000000 792.000000 ] /Parent 239 0 R /Resources << /ColorSpace << /CS0 427 0 R /CS1 427 0 R /CS2 428 0 R >> /ExtGState << /GS0 430 0 R /GS1 431 0 R /GS2 469 0 R /GS3 475 0 R /GS4 439 0 R /GS5 480 0 R /GS6 485 0 R /GS7 491 0 R /GS8 497 0 R >> /Font << /C2_0 447 0 R /T1_0 421 0 R /T1_1 422 0 R /T1_2 423 0 R /T1_3 424 0 R /T1_4 425 0 R /T1_5 426 0 R /T1_6 438 0 R >> /ProcSet [ /PDF /Text /ImageC /ImageI ] /Properties << /MC0 << /Metadata 502 0 R >> >> /XObject << /Fm0 451 0 R /Fm1 504 0 R /Fm2 513 0 R /Fm3 515 0 R /Fm4 517 0 R /Fm5 526 0 R /Fm6 528 0 R /Fm7 537 0 R /Fm8 539 0 R /Im0 540 0 R /Im1 541 0 R /Im2 452 0 R /Im3 542 0 R /Im4 543 0 R >> >> /Rotate 0 /StructParents 1 /TrimBox [ 0.000000 0.000000" [] "Failed reading: empty"
    

    For some reason, your parser doesn’t seem to like receiving data in chunks, and fails upon receiving the third (empty) chunk without consuming any input. I haven’t yet figured out where your parser is going wrong, but it’s definitely not iteratee or attoparsec-iteratee.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple Perl/CGI scripts based web server which is mainly used to display
I have simple form. <form target=_blank action=somescript.php method=Post id=simpleForm> <input type=hidden name=url value=http://...> <input
I have simple aspx page that has a form and a input field in
i have simple question, let's say i have input like this: <input name=somename type=checkbox
I have simple HTML code: <input type='hidden' name='cat_id' value='123'/> <a href='#' class='edit'>Edit</a> <a href='#'
I have simple WCF Service Application (based on this tutorial : Getting Started ).
I have simple web application based on JSP. Root of application looks like this:
I have simple form set up like the following: <form> <input type=text name=first_number id=first_number
I have simple dialog-based application with a status bar: InitCommonControls(); HWND hStatus = CreateWindowEx(...);
I have simple app in my project. This app works without any problem. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.