From 6b2f80a9828fb739f6c95ab504fbbc70fd709946 Mon Sep 17 00:00:00 2001 From: hylisd Date: Mon, 1 Apr 2019 00:33:49 +0800 Subject: [PATCH 1/5] Add implation of basic syntax of silly-j --- project.clj | 3 +- src/hopen/syntax/silly_j.cljc | 52 +++++++++++++++++++++++++++++ test/hopen/runner.cljs | 2 ++ test/hopen/syntax/silly_j_test.cljc | 21 ++++++++++++ 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/hopen/syntax/silly_j.cljc create mode 100644 test/hopen/syntax/silly_j_test.cljc diff --git a/project.clj b/project.clj index b04e9e8..a782880 100644 --- a/project.clj +++ b/project.clj @@ -8,7 +8,8 @@ :target-path "target/%s" :dependencies [[org.clojure/clojure "1.10.0"] - [org.clojure/clojurescript "1.10.520"]] + [org.clojure/clojurescript "1.10.520"] + [instaparse "1.4.10"]] :plugins [[lein-doo "0.1.10"]] :cljsbuild {:builds [{:id "node-test" diff --git a/src/hopen/syntax/silly_j.cljc b/src/hopen/syntax/silly_j.cljc new file mode 100644 index 0000000..6f88a58 --- /dev/null +++ b/src/hopen/syntax/silly_j.cljc @@ -0,0 +1,52 @@ +(ns hopen.syntax.silly-j + (:require [hopen.renderer.xf :as rxf] + [instaparse.core :as insta])) + +;; TODO test case +(defn render + ([template data] + (render template data '())) + ([template data other-env] + (let [new-env (update rxf/default-env :bindings assoc other-env) + renderer (rxf/renderer (parse template) new-env)] + (transduce renderer str data)))) + +(def ebnf + (insta/parser " + S = [STR | SILLYSTR]+ + STR = #'[^{}]+' + SILLYSTR = OBK SILLY CBK + OBK = '{' + CBK = '}' + SILLY = [FCTX | FN] + FCTX = '@' ':' ATTR + ATTR = #'[^}]'+ + FN = FNNAME APPLIES* + APPLIES = SPC | FCTX | APPLY + FNNAME = #'[a-zA-Z0-9\\-]+' + SPC = #'\\ '+ + APPLY = #'^[^@\\ ][^}\\ ]+' + " + )) + +(defn parse [msg] + (->> (ebnf msg) + (insta/transform + {:STR str + ;; :FCTX 'hopen/ctx + :APPLY str + :FNNAME str + :ATTR str + :FCTX (fn [at comma attr] + (list 'hopen/ctx (keyword attr))) + :SPC identity + :FN (fn [fn & applies] + (cons (symbol fn) + (filter #(not= " " %) applies) + )) + :APPLIES identity + :SILLY identity + :SILLYSTR (fn [obrk silly cbrk] + silly) + :S list + }))) diff --git a/test/hopen/runner.cljs b/test/hopen/runner.cljs index 6de7600..4e39f6a 100644 --- a/test/hopen/runner.cljs +++ b/test/hopen/runner.cljs @@ -2,7 +2,9 @@ (:require [cljs.test :as t :include-macros true] [doo.runner :refer-macros [doo-all-tests doo-tests]] [hopen.renderer.xf-test] + [hopen.syntax.silly-j-test] [hopen.util-test])) (doo-tests 'hopen.renderer.xf-test + 'hopen.syntax.silly-j-test 'hopen.util-test) diff --git a/test/hopen/syntax/silly_j_test.cljc b/test/hopen/syntax/silly_j_test.cljc new file mode 100644 index 0000000..b98d70e --- /dev/null +++ b/test/hopen/syntax/silly_j_test.cljc @@ -0,0 +1,21 @@ +(ns hopen.syntax.silly-j-test + (:require #?(:clj [clojure.test :refer [deftest testing is are]] + :cljs [cljs.test :refer [deftest testing is are] + :include-macros true]) + [hopen.syntax.silly-j :refer [parse]])) + +(deftest parse-test + (testing "basic syntax test" + (are [template parsed] + (= (parse template) + parsed) + ;; Split string and contax + "Hello {@:name}, {@:n} * {@:n} = {square @:n}" ["Hello " + '(hopen/ctx :name) + ", " + '(hopen/ctx :n) + " * " + '(hopen/ctx :n) + " = " + '(square (hopen/ctx :n))] + ))) From 1cc77bb30819e77ad37c5829c077b4a34eb199da Mon Sep 17 00:00:00 2001 From: hylisd Date: Mon, 1 Apr 2019 00:51:01 +0800 Subject: [PATCH 2/5] Fix typo --- test/hopen/syntax/silly_j_test.cljc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/hopen/syntax/silly_j_test.cljc b/test/hopen/syntax/silly_j_test.cljc index b98d70e..a1031e0 100644 --- a/test/hopen/syntax/silly_j_test.cljc +++ b/test/hopen/syntax/silly_j_test.cljc @@ -9,7 +9,7 @@ (are [template parsed] (= (parse template) parsed) - ;; Split string and contax + ;; Split string and context "Hello {@:name}, {@:n} * {@:n} = {square @:n}" ["Hello " '(hopen/ctx :name) ", " From 35f3c8760cdb17e67314431c8cde12091d070bb8 Mon Sep 17 00:00:00 2001 From: hylisd Date: Mon, 1 Apr 2019 01:16:25 +0800 Subject: [PATCH 3/5] Fix test --- src/hopen/syntax/silly_j.cljc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/hopen/syntax/silly_j.cljc b/src/hopen/syntax/silly_j.cljc index 6f88a58..4fb3bf5 100644 --- a/src/hopen/syntax/silly_j.cljc +++ b/src/hopen/syntax/silly_j.cljc @@ -2,6 +2,8 @@ (:require [hopen.renderer.xf :as rxf] [instaparse.core :as insta])) +(declare parse) + ;; TODO test case (defn render ([template data] From d7a944ddcfa40a3d01b31938c00c2089a10672e3 Mon Sep 17 00:00:00 2001 From: hylisd Date: Mon, 1 Apr 2019 16:04:16 +0800 Subject: [PATCH 4/5] Rename the attribute name "fn" --- src/hopen/syntax/silly_j.cljc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hopen/syntax/silly_j.cljc b/src/hopen/syntax/silly_j.cljc index 4fb3bf5..6c15d58 100644 --- a/src/hopen/syntax/silly_j.cljc +++ b/src/hopen/syntax/silly_j.cljc @@ -42,8 +42,8 @@ :FCTX (fn [at comma attr] (list 'hopen/ctx (keyword attr))) :SPC identity - :FN (fn [fn & applies] - (cons (symbol fn) + :FN (fn [first-elm & applies] + (cons (symbol first-elm) (filter #(not= " " %) applies) )) :APPLIES identity From 47289afd747b037718422d6add1b8fd3037fd6d5 Mon Sep 17 00:00:00 2001 From: hylisd Date: Mon, 1 Apr 2019 16:06:31 +0800 Subject: [PATCH 5/5] Do some refactors --- src/hopen/syntax/silly_j.cljc | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/hopen/syntax/silly_j.cljc b/src/hopen/syntax/silly_j.cljc index 6c15d58..e788de5 100644 --- a/src/hopen/syntax/silly_j.cljc +++ b/src/hopen/syntax/silly_j.cljc @@ -35,7 +35,6 @@ (->> (ebnf msg) (insta/transform {:STR str - ;; :FCTX 'hopen/ctx :APPLY str :FNNAME str :ATTR str @@ -44,11 +43,9 @@ :SPC identity :FN (fn [first-elm & applies] (cons (symbol first-elm) - (filter #(not= " " %) applies) - )) + (remove #{" "} applies))) :APPLIES identity :SILLY identity - :SILLYSTR (fn [obrk silly cbrk] - silly) + :SILLYSTR (fn [obrk silly cbrk] silly) :S list })))