Skip to content
Browse files

-added more char tests

-added "\r\n" handling for char-parsers
  • Loading branch information...
1 parent 1684102 commit 64ee86719d773e694cb4cebdc25d2135aac20287 @Cyrik committed Apr 27, 2012
Showing with 55 additions and 22 deletions.
  1. +3 −3 src/clparsec/char_parsers.clj
  2. +24 −11 src/clparsec/core.clj
  3. +28 −8 test/clparsec/test/char_parsers.clj
View
6 src/clparsec/char_parsers.clj
@@ -48,12 +48,12 @@
(char-return c nil))
(defn any-char [state]
- (if-let [[s c] (read-char state)]
+ (if-let [[s c] (read-char-or-newline state)]
(make-success s c)
(make-failure state (make-parse-error state expected-any-char))))
(defn skip-any-char [state]
- (if-let [[s c] (read-char state)]
+ (if-let [[s c] (read-char-or-newline state)]
(make-success s nil)
(make-failure state (make-parse-error state expected-any-char))))
@@ -140,7 +140,7 @@
(make-failure state (make-parse-error state (expected "end of file")))))
(defn- find-newline-or-eos [s]
- (let [newline-eos (conj *newline-chars* \uFFFF)]
+ (let [newline-eos (conj *newline-chars* \uFFFF)] ;buggy?
(loop [i (dec (count s))]
(if (>= i 0)
(if (newline-eos(nth s i))
View
35 src/clparsec/core.clj
@@ -3,7 +3,7 @@
(:require [clojure [set :as set]]))
-(def ^:dynamic *newline-chars* #{(first "\r") (first "\n")})
+(def ^:dynamic *newline-chars* #{\return \newline "\r\n"})
(def ^:dynamic *whitespace* #{\space \tab \newline \return \formfeed})
(defprotocol AReply
(success? [r]))
@@ -46,7 +46,6 @@
(StandardLocation. line column))
(defn standard-alter-location [character]
- {:pre #{(char? character)}}
(if (*newline-chars* character)
location-inc-line location-inc-column))
(defn location? [obj]
@@ -61,13 +60,13 @@
(next-state [state])
(state-warnings [state])
(location [state])
- (read-char [state])
(peep [state])
(skip-whitespace [state])
(skip-strn [state strn])
(read-chars-or-newlines-while [state pred1 pred normalize-n])
(end? [state])
- (skip-newline [state]))
+ (skip-newline [state])
+ (read-char-or-newline [state]))
(defrecord State
[remainder position location warnings context alter-location]
@@ -80,15 +79,29 @@
(next-state [this]
(when-let [remainder (seq remainder)]
(assoc this
- :remainder (next remainder), :position (inc position),
- :location ((alter-location (first remainder)) location))))
+ :remainder (next remainder), :position (inc position),
+ :location ((alter-location (first remainder)) location))))
(skip [this c] (when (=(first remainder)c)
(next-state this)))
- (skip-newline [this] (when (*newline-chars* (first remainder))
- (next-state this)))
- (read-char [this] (if-let [c (first remainder)]
- (list (next-state this) c)
- nil))
+ (skip-newline [this]
+ (if (= \return (first remainder))
+ (if (= \newline (second remainder))
+ (assoc this
+ :remainder (next (next remainder)), :position (+ position 2)
+ :location ((alter-location "\r\n") location))
+ (next-state this))
+ (if (*newline-chars* (first remainder))
+ (next-state this))))
+ (read-char-or-newline [this]
+ (if-let [c (first remainder)]
+ (if (= \return c)
+ (if (= \newline (second remainder))
+ (list (assoc this
+ :remainder (next (next remainder)), :position (+ position 2)
+ :location ((alter-location "\r\n") location))
+ \newline)
+ (list (next-state this) \newline))
+ (list (next-state this) c))))
(peep [this] (first remainder))
(skip-one [this] ((next-state this) nil))
(skip-whitespace [this] (when (*whitespace* (first remainder))
View
36 test/clparsec/test/char_parsers.clj
@@ -42,28 +42,48 @@
(result-failure-internal parser :fail content n-skipped-chars errors))
-(deftest test-pchar
+(deftest test-basics
(rok (pchar \space) " " 1 \space )
(rok (pchar \tab) "\t\t" 1 \tab )
(rfail (pchar \space) "" 0 #{(expected-string \space)})
- (rfail (pchar \space) "x" 0 #{(expected-string \space)})
-
+ (rfail (pchar \space) "x" 0 #{(expected-string \space)}))
+
+(deftest test-newlines-fail
(rfail (pchar \return) "_\r" 0 #{(expected "newline")})
(rfail pnewline "_\n" 0 #{(expected "newline")})
- (rfail pnewline "" 0 #{(expected "newline")})
-
+ (rfail pnewline "" 0 #{(expected "newline")}))
+
+(deftest test-newlines-ok
(roknl pnewline "\r" 1 \newline)
(roknl (pchar \newline) "\r" 1 \newline)
(roknl (pchar \return) "\r" 1 \return)
+ (roknl (pchar \newline) "\r\n" 2 \newline)
+ (roknl (pchar \return) "\r\n" 2 \return)
(roknl (pchar \newline) "\n" 1 \newline)
- (roknl (pchar \return) "\n" 1 \return)
-
+ (roknl (pchar \return) "\n" 1 \return))
+
+(deftest test-char-return
(rok (skip-char \tab) "\t" 1 nil)
(rok (char-return \tab 0) "\t" 1 0)
(roknl skip-nl "\n" 1 nil)
- (is (thrown? IllegalArgumentException (pchar \uffff)))
+ (roknl (newline-return 0) "\r\n" 2 0)
+ (is (thrown? IllegalArgumentException (pchar \uffff))))
+
+(deftest test-any-char
(rfail any-char "" 0 #{expected-any-char})
(rfail skip-any-char "" 0 #{expected-any-char})
+
+ (rok any-char " " 1 \space)
+ (rok any-char "\ufffe" 1 \ufffe)
+ (rok skip-any-char " " 1 nil)
+ (rok any-char "\t\t" 1 \tab)
+ (rok skip-any-char "\t\t" 1 nil))
+
+(deftest test-any-char-newline
+ (roknl any-char "\r\n" 2 \newline)
+ (roknl skip-any-char "\r\n" 2 nil)
+ (roknl any-char "\n\n" 1 \newline)
+ (roknl skip-any-char "\n\r" 1 nil)
)

0 comments on commit 64ee867

Please sign in to comment.
Something went wrong with that request. Please try again.