strojure / parsesso

Parser combinators for Clojure(Script).

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`expecting` adds a message instead of replacing

ferdinand-beyer opened this issue · comments

The docs for expecting read:

(...), it replaces expect error messages with the expect error message msg.
(...)
For example, if the expr parser from the maybe example would fail, the error message is: '...: expecting expression'. Without the expecting combinator, the message would be like '...: expecting "let" or alphabetic character', which is less friendly.

However, testing that with this example:

(def identifier (p/+many char/letter?))
(def let-expr (p/maybe (p/word "let")))
(def expr (-> (p/alt let-expr identifier)
              (p/expecting "expression")))
(p/parse expr "123"))

produces:

Execution error (ExceptionInfo) at strojure.parsesso.impl.reply/value (reply.cljc:71).
error at line 1, column 1:
unexpected "1"
expecting "let", ascii letter or expression

where one would expect:

Execution error (ExceptionInfo) at strojure.parsesso.impl.reply/value (reply.cljc:71).
error at line 1, column 1:
unexpected "1"
expecting expression

As you can see, the message "expression" which is supposed to replace the default "let" or ascii letter is appended instead. Arguably, this is less friendly and potentially misleading, as we don't expect ... OR expression.

I'm currently using my own version of expecting that looks like this:

(defn expecting
  "Variant of [[strojure.parsesso.parser/expecting]] that behaves as advertised.

   As of version 1.1.1-274, there's a bug that _appends_ `msg` instead of _replacing_ it.
   See https://github.com/strojure/parsesso/issues/5"
  [p msg]
  (fn [state context]
    (letfn [(e-err [e] (->> (.-messages e)
                            (remove #(= ::error/expecting (first %)))
                            (cons [::error/expecting msg])
                            (error/->ParseError (.-pos e))
                            (reply/e-err context)))]
      (parser/go p state (reply/assign context {reply/e-err e-err})))))

Let me know if I should open a PR or if you prefer to look into this yourself?

Thank you for report.
The bug have been fixed but clojars is down at the moment, I'll release it asap.

The version 1.1.2-283 has been released.