Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can easily build a custom combinator or parser that validates parsed input.</p> <p>If you only want to use combinators ("Haskell-style"), you could use </p> <pre><code>let pDateString = pstring "1/2/2000" let pDate1 = pDateString &gt;&gt;= fun str -&gt; try preturn (System.DateTime.Parse(str)) with _ -&gt; fail "Date format error" </code></pre> <p>as Guvante just proposed.</p> <p>If you want to avoid construction temporary parsers (see <code>preturn ...</code> and <code>pfail ...</code> above), you can just let the function accept a second parameter and directly return <code>Reply</code> values:</p> <pre><code>let pDate2 = pDateString &gt;&gt;= fun str stream -&gt; try Reply(System.DateTime.Parse(str)) with _ -&gt; Reply(Error, messageError "Date format error") </code></pre> <p>If you want the error location to be at the beginning of the malformed date string, you could replace <code>&gt;&gt;=</code> with <code>&gt;&gt;=?</code>. Note that this also has consequences for error recovery.</p> <p>If you want to have full control, you can write the parser only using the lower level API, starting with a basic version like the following:</p> <pre><code>let pDate3 = fun stream -&gt; let reply = pDateString stream if reply.Status = Ok then try Reply(System.DateTime.Parse(reply.Result)) with _ -&gt; Reply(Error, messageError "Date format error") else Reply(reply.Status, reply.Error) </code></pre> <p>This last version would also allow you to replace the pDateString parser with code that directly accesses the CharStream interface, which could give you some additional flexibility or performance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload