Skip to content

Commit

Permalink
Add float built-in parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnout Roemers committed Apr 7, 2023
1 parent ab7585b commit 159c245
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/crustimoney/built_ins.clj
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,25 @@
(c/with-error :expected-natural-number
(c/regex #"\d+")))

(def float
"Parse a floating point number."
(c/with-error :expected-float
(c/regex #"-?\d+(\.\d+)?")))

(def word
"Parse an alphabetical word."
(c/with-error :expected-word
(c/regex #"[A-Za-z]+")))

(def dquote
"Parse an double quoted string, allowing \\\" escapes."
"Parse an double quoted string, including \\\" escapes."
(c/with-error :expected-double-qoute-string
(c/chain (c/literal "\"")
(c/regex #"(\\\"|[^\"])*")
(c/literal "\""))))

(def squote
"Parse an single quoted string, allowing \\' escapes."
"Parse an single quoted string, including \\' escapes."
(c/with-error :expected-single-qoute-string
(c/chain (c/literal "'")
(c/regex #"(\\'|[^'])*")
Expand Down
9 changes: 9 additions & 0 deletions test/crustimoney/built_ins_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
(is (= #{(r/->error :expected-natural-number 0)}
(core/parse b/natural "-10"))))

(deftest float-test
(is (= (r/->success 0 3) (core/parse b/float "123")))
(is (= (r/->success 0 3) (core/parse b/float "-23")))
(is (= (r/->success 0 1) (core/parse b/float "0")))
(is (= (r/->success 0 4) (core/parse b/float "1.23")))
(is (= (r/->success 0 5) (core/parse b/float "-12.3")))
(is (= #{(r/->error :expected-float 0)}
(core/parse b/float "nan"))))

(deftest word-test
(is (= (r/->success 0 3) (core/parse b/word "Foo")))
(is (= #{(r/->error :expected-word 0)}
Expand Down

0 comments on commit 159c245

Please sign in to comment.