Skip to content

Commit

Permalink
Move char-range failure formatting to failure pretty-print
Browse files Browse the repository at this point in the history
  • Loading branch information
aengelberg committed Jun 23, 2015
1 parent c9200bf commit 80cd2f4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
17 changes: 10 additions & 7 deletions src/instaparse/failure.clj
Expand Up @@ -38,12 +38,14 @@
[r]
(cond
(:NOT r)
(do (print "NOT ")
(println (:NOT r))),
(do (print "NOT ")
(print (:NOT r))),
(:char-range r)
(print (print/char-range->str r))
(instance? java.util.regex.Pattern r)
(println (print/regexp->str r))
(print (print/regexp->str r))
:else
(prn r)))
(pr r)))

(defn pprint-failure
"Takes an augmented failure object and prints the error message"
Expand All @@ -61,8 +63,9 @@
(= 1 total) (println "Expected:")
:else (println "Expected one of:"))
(doseq [r full-reasons]
(pr r)
(print-reason r)
(println " (followed by end-of-string)"))
(doseq [r partial-reasons]
(print-reason r))))

(print-reason r)
(println))))

18 changes: 6 additions & 12 deletions src/instaparse/gll.clj
Expand Up @@ -615,32 +615,26 @@
(fail tramp [index this] index
{:tag :string :expecting string :full true}))))

(defn char-range-expecting
[lo hi]
(if (= lo hi)
(format "%%x%04x" lo)
(format "%%x%04x-%04x" lo hi)))

(defn char-range-parse
[this index tramp]
(let [lo (:lo this)
hi (:hi this)
^String text (:text tramp)]
(cond
(>= index (count text)) (fail tramp [index this] index
{:tag :char :expecting (char-range-expecting lo hi)})
{:tag :char :expecting {:char-range true :lo lo :hi hi}})
(<= hi 0xFFFF) (let [character (.charAt text index)]
(if (<= lo (int character) hi)
(success tramp [index this] (str character) (inc index))
(fail tramp [index this] index
{:tag :char :expecting (char-range-expecting lo hi)})))
{:tag :char :expecting {:char-range true :lo lo :hi hi}})))
:else (let [code-point (Character/codePointAt text (int index))
char-string (String. (Character/toChars code-point))]
(if (<= lo code-point hi)
(success tramp [index this] char-string
(+ index (count char-string)))
(fail tramp [index this] index
{:tag :char :expecting (char-range-expecting lo hi)}))))))
{:tag :char :expecting {:char-range true :lo lo :hi hi}}))))))

(defn char-range-full-parse
[this index tramp]
Expand All @@ -650,18 +644,18 @@
end (count text)]
(cond
(>= index (count text)) (fail tramp [index this] index
{:tag :char :expecting (char-range-expecting lo hi)})
{:tag :char :expecting {:char-range true :lo lo :hi hi}})
(<= hi 0xFFFF) (let [character (.charAt ^String text index)]
(if (and (= (inc index) end) (<= lo (int character) hi))
(success tramp [index this] (str character) end)
(fail tramp [index this] index
{:tag :char :expecting (char-range-expecting lo hi) :full true})))
{:tag :char :expecting {:char-range true :lo lo :hi hi}})))
:else (let [code-point (Character/codePointAt ^String text (int index))
char-string (String. (Character/toChars code-point))]
(if (and (= (+ index (count char-string)) end) (<= lo code-point hi))
(success tramp [index this] char-string end)
(fail tramp [index this] index
{:tag :char :expecting (char-range-expecting lo hi) :full true}))))))
{:tag :char :expecting {:char-range true :lo lo :hi hi} :full true}))))))

(defn re-match-at-front [regexp text]
(let [^java.util.regex.Matcher matcher (re-matcher regexp text)
Expand Down

0 comments on commit 80cd2f4

Please sign in to comment.