Skip to content

Commit

Permalink
Merge pull request #3 from PEZ/wip/cljc
Browse files Browse the repository at this point in the history
Port to cljc
  • Loading branch information
carocad authored Oct 9, 2019
2 parents a6afdcb + 3f3ae57 commit 91f911c
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ pom.xml.asc
.hgignore
.hg/
/.idea/
/nashorn_code_cache
/.cljs_nashorn_repl
9 changes: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
language: clojure
language: generic
jdk:
- openjdk8

jobs:
include:
- stage: Tests
- stage: JvmTests
script:
- lein do clean, compile, check, eastwood
- lein trampoline test

- stage: JsTests
script:
- nvm install 10.10 && nvm use 10.10
- lein trampoline cljsbuild test

- stage: Benchmark
if: branch = master
script:
Expand Down
13 changes: 12 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@
[instaparse/instaparse "1.4.10"]]
:profiles {:dev {:dependencies [[criterium/criterium "0.4.5"] ;; benchmark
[org.clojure/test.check "0.10.0"]]
:plugins [[jonase/eastwood "0.3.5"]]}}
:plugins [[jonase/eastwood "0.3.5"]
[lein-cljsbuild "1.1.7"]]
:cljsbuild {:builds
[{:id "dev"
:source-paths ["src" "test"]
:compiler {:main parcera.test-runner
:output-to "target/out/tests.js"
:target :nodejs
:optimizations :none}}]
:test-commands
{"test" ["node" "target/out/tests.js"]}}}
:provided {:dependencies [[org.clojure/clojurescript "1.10.520"]]}}
:test-selectors {:default (fn [m] (not (some #{:benchmark} (keys m))))
:benchmark :benchmark}
:deploy-repositories [["clojars" {:url "https://clojars.org/repo"
Expand Down
15 changes: 10 additions & 5 deletions src/parcera/core.clj → src/parcera/core.cljc
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
(ns parcera.core
(:require [instaparse.core :as instaparse]))
(:require [instaparse.core :as instaparse])
#?(:cljs (:import goog.string.StringBuffer)))

(def grammar
"code: form*;
"code: form*;
<form>: whitespace ( literal
| symbol
Expand Down Expand Up @@ -139,7 +140,9 @@
| 'tab'
| 'formfeed'
| 'backspace'
| #'\\P{M}\\p{M}*+'; (* https://www.regular-expressions.info/unicode.html *)")
| #'[^\\u0300-\\u036F\\u1DC0-\\u1DFF\\u20D0-\\u20FF][\\u0300-\\u036F\\u1DC0-\\u1DFF\\u20D0-\\u20FF]*';
(* This is supposed to be the JavaScript friendly version of #'\\P{M}\\p{M}*+' mentioned here: https://www.regular-expressions.info/unicode.html
It's cooked by this generator: http://kourge.net/projects/regexp-unicode-block, ticking all 'Combining Diacritical Marks' boxes *)")


(def clojure
Expand All @@ -160,7 +163,8 @@
(defn- code*
"internal function used to imperatively build up the code from the provided
AST as Clojure's str would be too slow"
[ast ^StringBuilder string-builder]
[ast #?(:clj ^StringBuilder string-builder
:cljs ^StringBuffer string-builder)]
(case (first ast)
:code
(doseq [child (rest ast)]
Expand Down Expand Up @@ -280,7 +284,8 @@
In general (= input (parcera/code (parcera/clojure input)))"
[ast]
(let [string-builder (new StringBuilder)]
(let [string-builder #?(:clj (new StringBuilder)
:cljs (new StringBuffer))]
(code* ast string-builder)
(. string-builder (toString))))

Expand Down
6 changes: 6 additions & 0 deletions src/parcera/slurp.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(ns parcera.slurp
"Some glue to help with CLJC things"
#?(:clj (:refer-clojure :exclude [slurp])))

#?(:clj (defmacro slurp [file]
(clojure.core/slurp file)))
17 changes: 14 additions & 3 deletions test/parcera/test/core.clj → test/parcera/test/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
[clojure.test.check.properties :as prop]
[clojure.test.check :as tc]
[parcera.core :as parcera]
[instaparse.core :as instaparse]))
[instaparse.core :as instaparse])
#?(:cljs (:require-macros [parcera.slurp :refer [slurp]])))

(defn- roundtrip
"checks parcera can parse and write back the exact same input code"
Expand Down Expand Up @@ -41,6 +42,16 @@
(prop/for-all [input (gen/fmap pr-str gen/any)]
(= 1 (count (instaparse/parses parcera/clojure input)))))

(deftest simple
(testing "character literals"
(as-> "\\t" input (is (= input (parcera/code (parcera/clojure input)))))
(as-> "\\n" input (is (= input (parcera/code (parcera/clojure input)))))
(as-> "\\r" input (is (= input (parcera/code (parcera/clojure input)))))
(as-> "\\a" input (is (= input (parcera/code (parcera/clojure input)))))
(as-> "\\é" input (is (= input (parcera/code (parcera/clojure input)))))
(as-> "\\ö" input (is (= input (parcera/code (parcera/clojure input)))))
(as-> "\\ï" input (is (= input (parcera/code (parcera/clojure input)))))
(as-> "\\ϕ" input (is (= input (parcera/code (parcera/clojure input)))))))

(deftest data-structures
(testing "grammar definitions"
Expand Down Expand Up @@ -149,11 +160,11 @@
(deftest bootstrap

(testing "parcera should be able to parse itself"
(let [input (slurp "./src/parcera/core.clj")]
(let [input (slurp "./src/parcera/core.cljc")]
(is (and (valid? input) (roundtrip input) (clear input)))))

(testing "parcera should be able to parse its own test suite"
(let [input (slurp "./test/parcera/test/core.clj")]
(let [input (slurp "./test/parcera/test/core.cljc")]
(is (and (valid? input) (roundtrip input) (clear input))))))


Expand Down
11 changes: 11 additions & 0 deletions test/parcera/test_runner.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(ns parcera.test-runner
(:require [cljs.nodejs :as nodejs]
[cljs.test :refer-macros [run-tests]]
[parcera.test.core :as ct]))

(nodejs/enable-util-print!)

(defn -main []
(run-tests 'parcera.test.core))

(set! *main-cli-fn* -main)

0 comments on commit 91f911c

Please sign in to comment.