public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
From: John MacFarlane <jgm-TVLZxgkOlNX2fBVCVOL8/A@public.gmane.org>
To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
Subject: Re: simple question about using manyTill
Date: Wed, 24 May 2017 22:26:50 +0200	[thread overview]
Message-ID: <20170524202650.GA20212@Johns-MBP.home> (raw)
In-Reply-To: <d879451a-e34c-495b-b72d-414e752d1e51-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>

+++ Jeff [May 24 17 08:35 ]:
>   I am new to Haskell and I am trying to write a new reader for Pandoc.
>   There is something strange about manyTill and I'm a bit stuck.
>   I have written a minimal code to present the problem. Suppose I want to
>   write a parser that succeeds if the string ends in three equal signs
>   "===", and return the string preceeding the "===". I have written three
>   versions. One with Parsec, and two with Pandoc:
>   import Text.Parsec.String (Parser)
>   import Text.Parsec (parse, ParseError)
>   import Text.Parsec.Combinator (many1, manyTill)
>   import Text.Parsec.Char (anyChar, string, noneOf)
>   import qualified Text.Pandoc.Builder as B (str)
>   import Text.Pandoc.Builder (Inlines)
>   import Text.Parsec.Prim (try)
>   simpleParse :: Parser a -> String -> Either ParseError a
>   simpleParse p = parse p ""
>   header' :: Parser String
>   header' = manyTill anyChar (string "===")
>   header'' :: Parser [Inlines]
>   header'' = manyTill (B.str <$> (many1 anyChar)) (string "===")
>   header''' :: Parser [Inlines]
>   header''' = manyTill (B.str <$> (many1 (noneOf "="))) (string "===")
>   I thought header' and header'' are the same parser except the type is a
>   bit different.

There's an important difference.  header' will parse one
character at a time (anyChar), each time stopping to see
if the end condition (string "===") is met.

header'' will parse CHUNKS of one or more character at
a time (many1 anyChar), only checking after each chunk
to see if the end condition is met.  Does that explain it?


  parent reply	other threads:[~2017-05-24 20:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-24 15:35 Jeff
     [not found] ` <d879451a-e34c-495b-b72d-414e752d1e51-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2017-05-24 20:26   ` John MacFarlane [this message]
     [not found]     ` <20170524202650.GA20212-l/d5Ua9yGnxXsXJlQylH7w@public.gmane.org>
2017-05-25  2:18       ` Jeff

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170524202650.GA20212@Johns-MBP.home \
    --to=jgm-tvlzxgkolnx2fbvcvol8/a@public.gmane.org \
    --cc=pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).