diff --git a/README.md b/README.md index c2105ce..46b0b26 100644 --- a/README.md +++ b/README.md @@ -240,7 +240,7 @@ as follows: - _**<ternary>** ::= "(" expression ")" "?" expression ":" expression._ -- _**<envref>** ::= letter | "_" { letter | digit | "_" }.\_ +- _**<envref>** ::= letter | "_" { letter | digit | "_" | "." }.\_ - _**<var>** ::= envref._ diff --git a/project.clj b/project.clj index ce3a3a0..8a2d500 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject rm-hull/infix "0.4.2" +(defproject rm-hull/infix "0.4.3" :description "A small Clojure library for expressing LISP expressions as infix rather than prefix notation" :url "https://github.com/rm-hull/infix" :license { diff --git a/src/infix/grammar.clj b/src/infix/grammar.clj index 6adc354..a87c971 100644 --- a/src/infix/grammar.clj +++ b/src/infix/grammar.clj @@ -38,7 +38,7 @@ ; ; function ::= envref expression | envref "(" | expression { "," expression } ")". ; ternary ::= expression "?" expression ":" expression. -; envref ::= letter | "_" { letter | digit | "_" }. +; envref ::= letter | "_" { letter | digit | "_" | "." }. ; var ::= envref. ; boolean :: = "true" | "false" ; number ::= integer | decimal | rational | binary | hex @@ -56,7 +56,7 @@ ; TODO: allow unicode/utf8 characters (def letter (from-re #"[a-zA-Z]")) -(def alpha-num (any-of letter digit (match "_"))) +(def alpha-num (any-of letter digit (match "_") (match "."))) (def digits (m/do* @@ -65,7 +65,7 @@ (def envref (m/do* - (fst <- (any-of letter (match "_"))) + (fst <- (any-of letter (match "_") (match "."))) (rst <- (token (many alpha-num))) (m/return (let [kw (keyword (strip-location (cons fst rst)))] (fn [env] diff --git a/test/infix/grammar_test.clj b/test/infix/grammar_test.clj index edc3f62..8aa4484 100644 --- a/test/infix/grammar_test.clj +++ b/test/infix/grammar_test.clj @@ -37,10 +37,11 @@ (<= (Math/abs (- x y)) (* scale epsilon))))) (deftest check-var - (let [env {:x 32 :something_else 19}] + (let [env {:x 32 :something_else 19 :dot.var 608}] (is (thrown? ParseException (parse-all var "54"))) (is (= 32 ((parse-all var "x") env))) (is (= 19 ((parse-all var "something_else") env))) + (is (= 608 ((parse-all var "dot.var") env))) (is (thrown? IllegalStateException ((parse-all var "fred") env))))) (deftest check-integer @@ -110,12 +111,13 @@ (is (thrown? clojure.lang.ArityException ((parse-all function "sqrt(7, 5)") env))))) (deftest check-expression - (let [env (merge base-env {:t 0.324 :x_y_z 3 :_a 4})] + (let [env (merge base-env {:t 0.324 :x_y_z 3 :_a 4 :_a.b 2})] (is (float= 1.4176457261295824 ((parse-all expression "sin(2 * t) + 3 * cos(4 * t)") env))) (is (thrown? IllegalStateException ((parse-all expression "3 + 4") {}))) (is (= 43 ((parse-all expression "3 + 5 * 8") env))) (is (= 64 ((parse-all expression "(3 + 5) * 8") env))) - (is (= 10 ((parse-all expression "x_y_z * 2 + _a") env))))) + (is (= 10 ((parse-all expression "x_y_z * 2 + _a") env))) + (is (= 8 ((parse-all expression "x_y_z * 2 + _a / _a.b") env))))) (deftest check-ternary-op (let [env (merge base-env {:t 150 :x 10 :y 20})]